]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb/source-loose: wire up `find_abbrev_len()` callback
authorPatrick Steinhardt <ps@pks.im>
Thu, 21 May 2026 08:22:29 +0000 (10:22 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 21 May 2026 13:35:19 +0000 (22:35 +0900)
Move `odb_source_loose_find_abbrev_len()` and its associated helpers
from "object-file.c" into "odb/source-loose.c" and wire it up as the
`find_abbrev_len` callback of the loose source.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
object-file.c
object-file.h
odb/source-files.c
odb/source-loose.c

index 157ecad3ea204a98d4c10f93b4138f3758d5414f..11957aa44f44fdc631e9c469e99c1d4870e2ac29 100644 (file)
@@ -1662,45 +1662,6 @@ out:
        return ret;
 }
 
-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;
-}
-
-int odb_source_loose_find_abbrev_len(struct odb_source *source,
-                                    const struct object_id *oid,
-                                    unsigned min_len,
-                                    unsigned *out)
-{
-       struct odb_source_files *files = odb_source_files_downcast(source);
-       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_for_each_object(&files->loose->base, NULL, find_abbrev_len_cb,
-                                        &data, &opts);
-       *out = data.len;
-
-       return ret;
-}
-
 static int check_stream_oid(git_zstream *stream,
                            const char *hdr,
                            unsigned long size,
index 9ee5649220931b72947148ded4f34c2056d53f45..96760db0e1cb2bfca7938a1d1c80595af46c7021 100644 (file)
@@ -110,18 +110,6 @@ int odb_source_loose_count_objects(struct odb_source *source,
                                   enum odb_count_objects_flags flags,
                                   unsigned long *out);
 
-/*
- * Find the shortest unique prefix for the given object ID, where `min_len` is
- * the minimum length that the prefix should have.
- *
- * Returns 0 on success, in which case the computed length will be written to
- * `out`. Otherwise, a negative error code is returned.
- */
-int odb_source_loose_find_abbrev_len(struct odb_source *source,
-                                    const struct object_id *oid,
-                                    unsigned min_len,
-                                    unsigned *out);
-
 /**
  * format_object_header() is a thin wrapper around s xsnprintf() that
  * writes the initial "<type> <obj-len>" part of the loose object
index 676a641739bcbfb412e1db8715d438bbda66bd84..4a54b10e4af11df6e998d7b45bd647a06b434718 100644 (file)
@@ -136,7 +136,7 @@ static int odb_source_files_find_abbrev_len(struct odb_source *source,
        if (ret < 0)
                goto out;
 
-       ret = odb_source_loose_find_abbrev_len(source, oid, len, &len);
+       ret = odb_source_find_abbrev_len(&files->loose->base, oid, len, &len);
        if (ret < 0)
                goto out;
 
index 4e8b923498b5b2a8829ff6d541f75de789bf195e..4b8d10bc870374fa8174ed419eaf43611f299e30 100644 (file)
@@ -481,6 +481,45 @@ static int odb_source_loose_for_each_object(struct odb_source *source,
                                             NULL, NULL, &data);
 }
 
+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_loose_find_abbrev_len(struct odb_source *source,
+                                           const struct object_id *oid,
+                                           unsigned min_len,
+                                           unsigned *out)
+{
+       struct odb_source_loose *loose = odb_source_loose_downcast(source);
+       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_for_each_object(&loose->base, NULL, find_abbrev_len_cb,
+                                        &data, &opts);
+       *out = data.len;
+
+       return ret;
+}
+
 static void odb_source_loose_clear_cache(struct odb_source_loose *loose)
 {
        oidtree_clear(loose->cache);
@@ -537,6 +576,7 @@ struct odb_source_loose *odb_source_loose_new(struct odb_source_files *files)
        loose->base.read_object_info = odb_source_loose_read_object_info;
        loose->base.read_object_stream = odb_source_loose_read_object_stream;
        loose->base.for_each_object = odb_source_loose_for_each_object;
+       loose->base.find_abbrev_len = odb_source_loose_find_abbrev_len;
 
        if (!is_absolute_path(loose->base.path))
                chdir_notify_register(NULL, odb_source_loose_reparent, loose);