Skip to content

Commit 67f47ea

Browse files
pks-tgitster
authored andcommitted
object-name: abbreviate loose object names without disambiguate_state
The function `find_short_object_filename()` takes an object ID and computes the minimum required object name length to make it unique. This is done by reusing the object disambiguation infrastructure, where we iterate through every loose object and then update the disambiguate state one by one. Ultimately, we don't care about the disambiguate state though. It is used because this infrastructure knows how to enumerate only those objects that match a given prefix. But now that we have extended the `odb_for_each_object()` function to do this for us we have an easier way to do this. Consequently, we really only use the disambiguate state now to propagate `struct min_abbrev_data`. Refactor the code and drop this indirection so that we use `struct min_abbrev_data` directly. This also allows us to drop some now-unused logic from the disambiguate infrastructure. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e9b7caa commit 67f47ea

1 file changed

Lines changed: 20 additions & 34 deletions

File tree

object-name.c

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ struct disambiguate_state {
4848
unsigned candidate_ok:1;
4949
unsigned disambiguate_fn_used:1;
5050
unsigned ambiguous:1;
51-
unsigned always_call_fn:1;
5251
};
5352

5453
static int update_disambiguate_state(const struct object_id *current,
@@ -58,10 +57,6 @@ static int update_disambiguate_state(const struct object_id *current,
5857
struct disambiguate_state *ds = cb_data;
5958

6059
/* The hash algorithm of current has already been filtered */
61-
if (ds->always_call_fn) {
62-
ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0;
63-
return ds->ambiguous;
64-
}
6560
if (!ds->candidate_exists) {
6661
/* this is the first candidate */
6762
oidcpy(&ds->candidate, current);
@@ -107,19 +102,6 @@ static int update_disambiguate_state(const struct object_id *current,
107102
return 0;
108103
}
109104

110-
static void find_short_object_filename(struct disambiguate_state *ds)
111-
{
112-
struct odb_for_each_object_options opts = {
113-
.prefix = &ds->bin_pfx,
114-
.prefix_hex_len = ds->len,
115-
};
116-
struct odb_source *source;
117-
118-
for (source = ds->repo->objects->sources; source && !ds->ambiguous; source = source->next)
119-
odb_source_loose_for_each_object(source, NULL, update_disambiguate_state,
120-
ds, &opts);
121-
}
122-
123105
static int finish_object_disambiguation(struct disambiguate_state *ds,
124106
struct object_id *oid)
125107
{
@@ -632,11 +614,26 @@ static int extend_abbrev_len(const struct object_id *oid,
632614
return 0;
633615
}
634616

635-
static int repo_extend_abbrev_len(struct repository *r UNUSED,
636-
const struct object_id *oid,
637-
void *cb_data)
617+
static int extend_abbrev_len_loose(const struct object_id *oid,
618+
struct object_info *oi UNUSED,
619+
void *cb_data)
638620
{
639-
return extend_abbrev_len(oid, cb_data);
621+
struct min_abbrev_data *data = cb_data;
622+
extend_abbrev_len(oid, data);
623+
return 0;
624+
}
625+
626+
static void find_abbrev_len_loose(struct min_abbrev_data *mad)
627+
{
628+
struct odb_for_each_object_options opts = {
629+
.prefix = mad->oid,
630+
.prefix_hex_len = mad->cur_len,
631+
};
632+
struct odb_source *source;
633+
634+
for (source = mad->repo->objects->sources; source; source = source->next)
635+
odb_source_loose_for_each_object(source, NULL, extend_abbrev_len_loose,
636+
mad, &opts);
640637
}
641638

642639
static void find_abbrev_len_for_midx(struct multi_pack_index *m,
@@ -752,9 +749,7 @@ int repo_find_unique_abbrev_r(struct repository *r, char *hex,
752749
{
753750
const struct git_hash_algo *algo =
754751
oid->algo ? &hash_algos[oid->algo] : r->hash_algo;
755-
struct disambiguate_state ds;
756752
struct min_abbrev_data mad;
757-
struct object_id oid_ret;
758753
const unsigned hexsz = algo->hexsz;
759754

760755
if (len < 0) {
@@ -794,16 +789,7 @@ int repo_find_unique_abbrev_r(struct repository *r, char *hex,
794789
mad.oid = oid;
795790

796791
find_abbrev_len_packed(&mad);
797-
798-
if (init_object_disambiguation(r, hex, mad.cur_len, algo, &ds) < 0)
799-
return -1;
800-
801-
ds.fn = repo_extend_abbrev_len;
802-
ds.always_call_fn = 1;
803-
ds.cb_data = (void *)&mad;
804-
805-
find_short_object_filename(&ds);
806-
(void)finish_object_disambiguation(&ds, &oid_ret);
792+
find_abbrev_len_loose(&mad);
807793

808794
hex[mad.cur_len] = 0;
809795
return mad.cur_len;

0 commit comments

Comments
 (0)