Skip to content

Commit eac58de

Browse files
pks-tgitster
authored andcommitted
object-name: backend-generic get_short_oid()
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>
1 parent d2612fe commit eac58de

1 file changed

Lines changed: 6 additions & 26 deletions

File tree

object-name.c

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,6 @@ static void find_short_object_filename(struct disambiguate_state *ds)
120120
odb_source_loose_for_each_object(source, NULL, match_prefix, ds, &opts);
121121
}
122122

123-
static void find_short_packed_object(struct disambiguate_state *ds)
124-
{
125-
struct odb_for_each_object_options opts = {
126-
.prefix = &ds->bin_pfx,
127-
.prefix_hex_len = ds->len,
128-
};
129-
struct odb_source *source;
130-
131-
/* Skip, unless oids from the storage hash algorithm are wanted */
132-
if (ds->bin_pfx.algo && (&hash_algos[ds->bin_pfx.algo] != ds->repo->hash_algo))
133-
return;
134-
135-
odb_prepare_alternates(ds->repo->objects);
136-
for (source = ds->repo->objects->sources; source; source = source->next) {
137-
struct odb_source_files *files = odb_source_files_downcast(source);
138-
139-
packfile_store_for_each_object(files->packed, NULL, match_prefix, ds, &opts);
140-
if (ds->ambiguous)
141-
break;
142-
}
143-
}
144-
145123
static int finish_object_disambiguation(struct disambiguate_state *ds,
146124
struct object_id *oid)
147125
{
@@ -499,6 +477,7 @@ static enum get_oid_result get_short_oid(struct repository *r,
499477
struct object_id *oid,
500478
unsigned flags)
501479
{
480+
struct odb_for_each_object_options opts = { 0 };
502481
int status;
503482
struct disambiguate_state ds;
504483
int quietly = !!(flags & GET_OID_QUIETLY);
@@ -526,8 +505,10 @@ static enum get_oid_result get_short_oid(struct repository *r,
526505
else
527506
ds.fn = default_disambiguate_hint;
528507

529-
find_short_object_filename(&ds);
530-
find_short_packed_object(&ds);
508+
opts.prefix = &ds.bin_pfx;
509+
opts.prefix_hex_len = ds.len;
510+
511+
odb_for_each_object_ext(r->objects, NULL, match_prefix, &ds, &opts);
531512
status = finish_object_disambiguation(&ds, oid);
532513

533514
/*
@@ -537,8 +518,7 @@ static enum get_oid_result get_short_oid(struct repository *r,
537518
*/
538519
if (status == MISSING_OBJECT) {
539520
odb_reprepare(r->objects);
540-
find_short_object_filename(&ds);
541-
find_short_packed_object(&ds);
521+
odb_for_each_object_ext(r->objects, NULL, match_prefix, &ds, &opts);
542522
status = finish_object_disambiguation(&ds, oid);
543523
}
544524

0 commit comments

Comments
 (0)