]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-name: backend-generic `get_short_oid()`
authorPatrick Steinhardt <ps@pks.im>
Fri, 20 Mar 2026 07:07:34 +0000 (08:07 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 20 Mar 2026 20:16:42 +0000 (13:16 -0700)
The function `get_short_oid()` takes as input an abbreviated object ID
and tries to turn that object ID into the full object ID. This is done
by iterating through all objects that have the user-provided prefix. If
that yields exactly one object we know that the abbreviated object ID is
unambiguous, otherwise it is ambiguous and we print the list of objects
that match the prefix.

We iterate through all objects with the given prefix by calling both
`find_short_packed_object()` and `find_short_object_filename()`, which
is of course specific to the "files" backend. But we now have a generic
way to iterate through objects with a specific prefix.

Refactor the code to use `odb_for_each_object()` instead so that it
works with object backends different than the "files" backend.

Remove the now-unused `find_short_packed_object()` function.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
object-name.c

index 4c3ace150ecdc2d5d8df900c98c16544cb56d69d..7a224ab4afc0c611ea990ec259e0f09bcca483bd 100644 (file)
@@ -120,28 +120,6 @@ static void find_short_object_filename(struct disambiguate_state *ds)
                odb_source_loose_for_each_object(source, NULL, match_prefix, ds, &opts);
 }
 
-static void find_short_packed_object(struct disambiguate_state *ds)
-{
-       struct odb_for_each_object_options opts = {
-               .prefix = &ds->bin_pfx,
-               .prefix_hex_len = ds->len,
-       };
-       struct odb_source *source;
-
-       /* Skip, unless oids from the storage hash algorithm are wanted */
-       if (ds->bin_pfx.algo && (&hash_algos[ds->bin_pfx.algo] != ds->repo->hash_algo))
-               return;
-
-       odb_prepare_alternates(ds->repo->objects);
-       for (source = ds->repo->objects->sources; source; source = source->next) {
-               struct odb_source_files *files = odb_source_files_downcast(source);
-
-               packfile_store_for_each_object(files->packed, NULL, match_prefix, ds, &opts);
-               if (ds->ambiguous)
-                       break;
-       }
-}
-
 static int finish_object_disambiguation(struct disambiguate_state *ds,
                                        struct object_id *oid)
 {
@@ -499,6 +477,7 @@ static enum get_oid_result get_short_oid(struct repository *r,
                                         struct object_id *oid,
                                         unsigned flags)
 {
+       struct odb_for_each_object_options opts = { 0 };
        int status;
        struct disambiguate_state ds;
        int quietly = !!(flags & GET_OID_QUIETLY);
@@ -526,8 +505,10 @@ static enum get_oid_result get_short_oid(struct repository *r,
        else
                ds.fn = default_disambiguate_hint;
 
-       find_short_object_filename(&ds);
-       find_short_packed_object(&ds);
+       opts.prefix = &ds.bin_pfx;
+       opts.prefix_hex_len = ds.len;
+
+       odb_for_each_object_ext(r->objects, NULL, match_prefix, &ds, &opts);
        status = finish_object_disambiguation(&ds, oid);
 
        /*
@@ -537,8 +518,7 @@ static enum get_oid_result get_short_oid(struct repository *r,
         */
        if (status == MISSING_OBJECT) {
                odb_reprepare(r->objects);
-               find_short_object_filename(&ds);
-               find_short_packed_object(&ds);
+               odb_for_each_object_ext(r->objects, NULL, match_prefix, &ds, &opts);
                status = finish_object_disambiguation(&ds, oid);
        }