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

index f60eecbdbbdfff2981db7dd8ef6882aee9022243..44d9bbedeca6f2e5b770c1b7201ba95b40c43165 100644 (file)
@@ -169,6 +169,44 @@ static int odb_source_inmemory_for_each_object(struct odb_source *source,
                            odb_source_inmemory_for_each_object_cb, &payload);
 }
 
+struct find_abbrev_len_data {
+       const struct object_id *oid;
+       unsigned len;
+};
+
+static int find_abbrev_len_cb(const struct object_id *oid,
+                             struct object_info *oi UNUSED,
+                             void *cb_data)
+{
+       struct find_abbrev_len_data *data = cb_data;
+       unsigned len = oid_common_prefix_hexlen(oid, data->oid);
+       if (len != hash_algos[oid->algo].hexsz && len >= data->len)
+               data->len = len + 1;
+       return 0;
+}
+
+static int odb_source_inmemory_find_abbrev_len(struct odb_source *source,
+                                              const struct object_id *oid,
+                                              unsigned min_len,
+                                              unsigned *out)
+{
+       struct odb_for_each_object_options opts = {
+               .prefix = oid,
+               .prefix_hex_len = min_len,
+       };
+       struct find_abbrev_len_data data = {
+               .oid = oid,
+               .len = min_len,
+       };
+       int ret;
+
+       ret = odb_source_inmemory_for_each_object(source, NULL, find_abbrev_len_cb,
+                                                 &data, &opts);
+       *out = data.len;
+
+       return ret;
+}
+
 static int odb_source_inmemory_write_object(struct odb_source *source,
                                            const void *buf, unsigned long len,
                                            enum object_type type,
@@ -275,6 +313,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
        source->base.read_object_info = odb_source_inmemory_read_object_info;
        source->base.read_object_stream = odb_source_inmemory_read_object_stream;
        source->base.for_each_object = odb_source_inmemory_for_each_object;
+       source->base.find_abbrev_len = odb_source_inmemory_find_abbrev_len;
        source->base.write_object = odb_source_inmemory_write_object;
        source->base.write_object_stream = odb_source_inmemory_write_object_stream;