]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb/source-inmemory: implement `write_object()` callback
authorPatrick Steinhardt <ps@pks.im>
Fri, 10 Apr 2026 12:12:36 +0000 (14:12 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 14 May 2026 19:50:44 +0000 (04:50 +0900)
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>
odb.c
odb/source-inmemory.c

diff --git a/odb.c b/odb.c
index 6a3912adac2f0e0cf331e9ce3bfff9cbc69762a4..24e929f03cbccfd04171edf791788f4744b4b698 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -733,24 +733,12 @@ int odb_pretend_object(struct object_database *odb,
                       void *buf, unsigned long len, enum object_type type,
                       struct object_id *oid)
 {
-       struct cached_object_entry *co;
-       char *co_buf;
-
        hash_object_file(odb->repo->hash_algo, buf, len, type, oid);
        if (odb_has_object(odb, oid, 0))
                return 0;
 
-       ALLOC_GROW(odb->inmemory_objects->objects,
-                  odb->inmemory_objects->objects_nr + 1,
-                  odb->inmemory_objects->objects_alloc);
-       co = &odb->inmemory_objects->objects[odb->inmemory_objects->objects_nr++];
-       co->value.size = len;
-       co->value.type = type;
-       co_buf = xmalloc(len);
-       memcpy(co_buf, buf, len);
-       co->value.buf = co_buf;
-       oidcpy(&co->oid, oid);
-       return 0;
+       return odb_source_write_object(&odb->inmemory_objects->base,
+                                      buf, len, type, oid, NULL, 0);
 }
 
 void *odb_read_object(struct object_database *odb,
index 39f0e799c745199faf49331eeb8e8485ddbf4894..4848011df5189cff1a2a3b89d0294637b662a177 100644 (file)
@@ -1,4 +1,5 @@
 #include "git-compat-util.h"
+#include "object-file.h"
 #include "odb.h"
 #include "odb/source-inmemory.h"
 #include "odb/streaming.h"
@@ -104,6 +105,29 @@ static int odb_source_inmemory_read_object_stream(struct odb_read_stream **out,
        return 0;
 }
 
+static int odb_source_inmemory_write_object(struct odb_source *source,
+                                           const void *buf, unsigned long len,
+                                           enum object_type type,
+                                           struct object_id *oid,
+                                           struct object_id *compat_oid UNUSED,
+                                           enum odb_write_object_flags flags UNUSED)
+{
+       struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
+       struct cached_object_entry *object;
+
+       hash_object_file(source->odb->repo->hash_algo, buf, len, type, oid);
+
+       ALLOC_GROW(inmemory->objects, inmemory->objects_nr + 1,
+                  inmemory->objects_alloc);
+       object = &inmemory->objects[inmemory->objects_nr++];
+       object->value.size = len;
+       object->value.type = type;
+       object->value.buf = xmemdupz(buf, len);
+       oidcpy(&object->oid, oid);
+
+       return 0;
+}
+
 static void odb_source_inmemory_free(struct odb_source *source)
 {
        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)
        source->base.free = odb_source_inmemory_free;
        source->base.read_object_info = odb_source_inmemory_read_object_info;
        source->base.read_object_stream = odb_source_inmemory_read_object_stream;
+       source->base.write_object = odb_source_inmemory_write_object;
 
        return source;
 }