odb/source-inmemory: implement `read_object_info()` callback

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>

Patrick Steinhardt committed Apr 10, 2026 at 14:12 UTC ec45c1e8bf8958bdcca2b324573d02ac934c51ea
2 files changed +54 -38
odb.c
+1 -38
@@ -32,25 +32,6 @@
32 KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
33 struct odb_source *, 1, fspathhash, fspatheq)
34
35 -static const struct cached_object *find_cached_object(struct object_database *object_store,
36 - const struct object_id *oid)
37 -{
38 - static const struct cached_object empty_tree = {
39 - .type = OBJ_TREE,
40 - .buf = "",
41 - };
42 - const struct cached_object_entry *co = object_store->inmemory_objects->objects;
43 -
44 - for (size_t i = 0; i < object_store->inmemory_objects->objects_nr; i++, co++)
45 - if (oideq(&co->oid, oid))
46 - return &co->value;
47 -
48 - if (oid->algo && oideq(oid, hash_algos[oid->algo].empty_tree))
49 - return &empty_tree;
50 -
51 - return NULL;
52 -}
53 -
35 int odb_mkstemp(struct object_database *odb,
36 struct strbuf *temp_filename, const char *pattern)
37 {
@@ -570,7 +551,6 @@ static int do_oid_object_info_extended(struct object_database *odb,
551 const struct object_id *oid,
552 struct object_info *oi, unsigned flags)
553 {
573 - const struct cached_object *co;
554 const struct object_id *real = oid;
555 int already_retried = 0;
556
@@ -580,25 +560,8 @@ static int do_oid_object_info_extended(struct object_database *odb,
560 if (is_null_oid(real))
561 return -1;
562
583 - co = find_cached_object(odb, real);
584 - if (co) {
585 - if (oi) {
586 - if (oi->typep)
587 - *(oi->typep) = co->type;
588 - if (oi->sizep)
589 - *(oi->sizep) = co->size;
590 - if (oi->disk_sizep)
591 - *(oi->disk_sizep) = 0;
592 - if (oi->delta_base_oid)
593 - oidclr(oi->delta_base_oid, odb->repo->hash_algo);
594 - if (oi->contentp)
595 - *oi->contentp = xmemdupz(co->buf, co->size);
596 - if (oi->mtimep)
597 - *oi->mtimep = 0;
598 - oi->whence = OI_CACHED;
599 - }
563 + if (!odb_source_read_object_info(&odb->inmemory_objects->base, oid, oi, flags))
564 return 0;
601 - }
565
566 odb_prepare_alternates(odb);
567
odb/source-inmemory.c
+53
@@ -1,5 +1,57 @@
1 #include "git-compat-util.h"
2 +#include "odb.h"
3 #include "odb/source-inmemory.h"
4 +#include "repository.h"
5 +
6 +static const struct cached_object *find_cached_object(struct odb_source_inmemory *source,
7 + const struct object_id *oid)
8 +{
9 + static const struct cached_object empty_tree = {
10 + .type = OBJ_TREE,
11 + .buf = "",
12 + };
13 + const struct cached_object_entry *co = source->objects;
14 +
15 + for (size_t i = 0; i < source->objects_nr; i++, co++)
16 + if (oideq(&co->oid, oid))
17 + return &co->value;
18 +
19 + if (oid->algo && oideq(oid, hash_algos[oid->algo].empty_tree))
20 + return &empty_tree;
21 +
22 + return NULL;
23 +}
24 +
25 +static int odb_source_inmemory_read_object_info(struct odb_source *source,
26 + const struct object_id *oid,
27 + struct object_info *oi,
28 + enum object_info_flags flags UNUSED)
29 +{
30 + struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
31 + const struct cached_object *object;
32 +
33 + object = find_cached_object(inmemory, oid);
34 + if (!object)
35 + return -1;
36 +
37 + if (oi) {
38 + if (oi->typep)
39 + *(oi->typep) = object->type;
40 + if (oi->sizep)
41 + *(oi->sizep) = object->size;
42 + if (oi->disk_sizep)
43 + *(oi->disk_sizep) = 0;
44 + if (oi->delta_base_oid)
45 + oidclr(oi->delta_base_oid, source->odb->repo->hash_algo);
46 + if (oi->contentp)
47 + *oi->contentp = xmemdupz(object->buf, object->size);
48 + if (oi->mtimep)
49 + *oi->mtimep = 0;
50 + oi->whence = OI_CACHED;
51 + }
52 +
53 + return 0;
54 +}
55
56 static void odb_source_inmemory_free(struct odb_source *source)
57 {
@@ -19,6 +71,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
71 odb_source_init(&source->base, odb, ODB_SOURCE_INMEMORY, "source", false);
72
73 source->base.free = odb_source_inmemory_free;
74 + source->base.read_object_info = odb_source_inmemory_read_object_info;
75
76 return source;
77 }