]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb/source-inmemory: implement `read_object_info()` callback
authorPatrick Steinhardt <ps@pks.im>
Fri, 10 Apr 2026 12:12:34 +0000 (14:12 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 14 May 2026 19:50:44 +0000 (04:50 +0900)
Implement the `read_object_info()` 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 ea3fcf5e118d7261b4f6e61063d52d0f18599105..6a3912adac2f0e0cf331e9ce3bfff9cbc69762a4 100644 (file)
--- a/odb.c
+++ b/odb.c
 KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
        struct odb_source *, 1, fspathhash, fspatheq)
 
-static const struct cached_object *find_cached_object(struct object_database *object_store,
-                                                     const struct object_id *oid)
-{
-       static const struct cached_object empty_tree = {
-               .type = OBJ_TREE,
-               .buf = "",
-       };
-       const struct cached_object_entry *co = object_store->inmemory_objects->objects;
-
-       for (size_t i = 0; i < object_store->inmemory_objects->objects_nr; i++, co++)
-               if (oideq(&co->oid, oid))
-                       return &co->value;
-
-       if (oid->algo && oideq(oid, hash_algos[oid->algo].empty_tree))
-               return &empty_tree;
-
-       return NULL;
-}
-
 int odb_mkstemp(struct object_database *odb,
                struct strbuf *temp_filename, const char *pattern)
 {
@@ -570,7 +551,6 @@ static int do_oid_object_info_extended(struct object_database *odb,
                                       const struct object_id *oid,
                                       struct object_info *oi, unsigned flags)
 {
-       const struct cached_object *co;
        const struct object_id *real = oid;
        int already_retried = 0;
 
@@ -580,25 +560,8 @@ static int do_oid_object_info_extended(struct object_database *odb,
        if (is_null_oid(real))
                return -1;
 
-       co = find_cached_object(odb, real);
-       if (co) {
-               if (oi) {
-                       if (oi->typep)
-                               *(oi->typep) = co->type;
-                       if (oi->sizep)
-                               *(oi->sizep) = co->size;
-                       if (oi->disk_sizep)
-                               *(oi->disk_sizep) = 0;
-                       if (oi->delta_base_oid)
-                               oidclr(oi->delta_base_oid, odb->repo->hash_algo);
-                       if (oi->contentp)
-                               *oi->contentp = xmemdupz(co->buf, co->size);
-                       if (oi->mtimep)
-                               *oi->mtimep = 0;
-                       oi->whence = OI_CACHED;
-               }
+       if (!odb_source_read_object_info(&odb->inmemory_objects->base, oid, oi, flags))
                return 0;
-       }
 
        odb_prepare_alternates(odb);
 
index ccbb622eaef031611f572c0ca6b589940a76311b..12c80f9b34a58adcff605f3317f3a40a1c70d3ec 100644 (file)
@@ -1,5 +1,57 @@
 #include "git-compat-util.h"
+#include "odb.h"
 #include "odb/source-inmemory.h"
+#include "repository.h"
+
+static const struct cached_object *find_cached_object(struct odb_source_inmemory *source,
+                                                     const struct object_id *oid)
+{
+       static const struct cached_object empty_tree = {
+               .type = OBJ_TREE,
+               .buf = "",
+       };
+       const struct cached_object_entry *co = source->objects;
+
+       for (size_t i = 0; i < source->objects_nr; i++, co++)
+               if (oideq(&co->oid, oid))
+                       return &co->value;
+
+       if (oid->algo && oideq(oid, hash_algos[oid->algo].empty_tree))
+               return &empty_tree;
+
+       return NULL;
+}
+
+static int odb_source_inmemory_read_object_info(struct odb_source *source,
+                                               const struct object_id *oid,
+                                               struct object_info *oi,
+                                               enum object_info_flags flags UNUSED)
+{
+       struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
+       const struct cached_object *object;
+
+       object = find_cached_object(inmemory, oid);
+       if (!object)
+               return -1;
+
+       if (oi) {
+               if (oi->typep)
+                       *(oi->typep) = object->type;
+               if (oi->sizep)
+                       *(oi->sizep) = object->size;
+               if (oi->disk_sizep)
+                       *(oi->disk_sizep) = 0;
+               if (oi->delta_base_oid)
+                       oidclr(oi->delta_base_oid, source->odb->repo->hash_algo);
+               if (oi->contentp)
+                       *oi->contentp = xmemdupz(object->buf, object->size);
+               if (oi->mtimep)
+                       *oi->mtimep = 0;
+               oi->whence = OI_CACHED;
+       }
+
+       return 0;
+}
 
 static void odb_source_inmemory_free(struct odb_source *source)
 {
@@ -19,6 +71,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
        odb_source_init(&source->base, odb, ODB_SOURCE_INMEMORY, "source", false);
 
        source->base.free = odb_source_inmemory_free;
+       source->base.read_object_info = odb_source_inmemory_read_object_info;
 
        return source;
 }