odb/source-inmemory: implement `write_object()` callback
Implement the `write_object()` callback function for the in-memory source. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Apr 10, 2026 at 14:12 UTC
f611f4ba41de07a89649c74c01477cf55b20bc31
2 files changed
+27
-14
odb.c
+2
-14
@@ -733,24 +733,12 @@ int odb_pretend_object(struct object_database *odb,
733
void *buf, unsigned long len, enum object_type type,
734
struct object_id *oid)
735
{
736
- struct cached_object_entry *co;
737
- char *co_buf;
738
-
736
hash_object_file(odb->repo->hash_algo, buf, len, type, oid);
737
if (odb_has_object(odb, oid, 0))
738
return 0;
739
743
- ALLOC_GROW(odb->inmemory_objects->objects,
744
- odb->inmemory_objects->objects_nr + 1,
745
- odb->inmemory_objects->objects_alloc);
746
- co = &odb->inmemory_objects->objects[odb->inmemory_objects->objects_nr++];
747
- co->value.size = len;
748
- co->value.type = type;
749
- co_buf = xmalloc(len);
750
- memcpy(co_buf, buf, len);
751
- co->value.buf = co_buf;
752
- oidcpy(&co->oid, oid);
753
- return 0;
740
+ return odb_source_write_object(&odb->inmemory_objects->base,
741
+ buf, len, type, oid, NULL, 0);
742
}
743
744
void *odb_read_object(struct object_database *odb,
odb/source-inmemory.c
+25
@@ -1,4 +1,5 @@
1
#include "git-compat-util.h"
2
+#include "object-file.h"
3
#include "odb.h"
4
#include "odb/source-inmemory.h"
5
#include "odb/streaming.h"
@@ -104,6 +105,29 @@ static int odb_source_inmemory_read_object_stream(struct odb_read_stream **out,
105
return 0;
106
}
107
108
+static int odb_source_inmemory_write_object(struct odb_source *source,
109
+ const void *buf, unsigned long len,
110
+ enum object_type type,
111
+ struct object_id *oid,
112
+ struct object_id *compat_oid UNUSED,
113
+ enum odb_write_object_flags flags UNUSED)
114
+{
115
+ struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
116
+ struct cached_object_entry *object;
117
+
118
+ hash_object_file(source->odb->repo->hash_algo, buf, len, type, oid);
119
+
120
+ ALLOC_GROW(inmemory->objects, inmemory->objects_nr + 1,
121
+ inmemory->objects_alloc);
122
+ object = &inmemory->objects[inmemory->objects_nr++];
123
+ object->value.size = len;
124
+ object->value.type = type;
125
+ object->value.buf = xmemdupz(buf, len);
126
+ oidcpy(&object->oid, oid);
127
+
128
+ return 0;
129
+}
130
+
131
static void odb_source_inmemory_free(struct odb_source *source)
132
{
133
struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
@@ -124,6 +148,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
148
source->base.free = odb_source_inmemory_free;
149
source->base.read_object_info = odb_source_inmemory_read_object_info;
150
source->base.read_object_stream = odb_source_inmemory_read_object_stream;
151
+ source->base.write_object = odb_source_inmemory_write_object;
152
153
return source;
154
}