object-name: merge `update_candidates()` and `match_prefix()`

There's only a single callsite for `match_prefix()`, and that function is a rather trivial wrapper of `update_candidates()`. Merge these two functions into a single `update_disambiguate_state()` function. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 20, 2026 at 08:07 UTC e9b7caa1b14bc1fe825b216941a0655d6afdffe5
1 file changed +18 -16
object-name.c
+18 -16
@@ -51,27 +51,31 @@ struct disambiguate_state {
51 unsigned always_call_fn:1;
52 };
53
54 -static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
54 +static int update_disambiguate_state(const struct object_id *current,
55 + struct object_info *oi UNUSED,
56 + void *cb_data)
57 {
58 + struct disambiguate_state *ds = cb_data;
59 +
60 /* 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;
59 - return;
63 + return ds->ambiguous;
64 }
65 if (!ds->candidate_exists) {
66 /* this is the first candidate */
67 oidcpy(&ds->candidate, current);
68 ds->candidate_exists = 1;
65 - return;
69 + return 0;
70 } else if (oideq(&ds->candidate, current)) {
71 /* the same as what we already have seen */
68 - return;
72 + return 0;
73 }
74
75 if (!ds->fn) {
76 /* cannot disambiguate between ds->candidate and current */
77 ds->ambiguous = 1;
74 - return;
78 + return ds->ambiguous;
79 }
80
81 if (!ds->candidate_checked) {
@@ -84,7 +88,7 @@ static void update_candidates(struct disambiguate_state *ds, const struct object
88 /* discard the candidate; we know it does not satisfy fn */
89 oidcpy(&ds->candidate, current);
90 ds->candidate_checked = 0;
87 - return;
91 + return 0;
92 }
93
94 /* if we reach this point, we know ds->candidate satisfies fn */
@@ -95,17 +99,12 @@ static void update_candidates(struct disambiguate_state *ds, const struct object
99 */
100 ds->candidate_ok = 0;
101 ds->ambiguous = 1;
102 + return ds->ambiguous;
103 }
104
105 /* otherwise, current can be discarded and candidate is still good */
101 -}
106
103 -static int match_prefix(const struct object_id *oid, struct object_info *oi UNUSED, void *arg)
104 -{
105 - struct disambiguate_state *ds = arg;
106 - /* no need to call match_hash, oidtree_each did prefix match */
107 - update_candidates(ds, oid);
108 - return ds->ambiguous;
107 + return 0;
108 }
109
110 static void find_short_object_filename(struct disambiguate_state *ds)
@@ -117,7 +116,8 @@ static void find_short_object_filename(struct disambiguate_state *ds)
116 struct odb_source *source;
117
118 for (source = ds->repo->objects->sources; source && !ds->ambiguous; source = source->next)
120 - odb_source_loose_for_each_object(source, NULL, match_prefix, ds, &opts);
119 + odb_source_loose_for_each_object(source, NULL, update_disambiguate_state,
120 + ds, &opts);
121 }
122
123 static int finish_object_disambiguation(struct disambiguate_state *ds,
@@ -508,7 +508,8 @@ static enum get_oid_result get_short_oid(struct repository *r,
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);
511 + odb_for_each_object_ext(r->objects, NULL, update_disambiguate_state,
512 + &ds, &opts);
513 status = finish_object_disambiguation(&ds, oid);
514
515 /*
@@ -518,7 +519,8 @@ static enum get_oid_result get_short_oid(struct repository *r,
519 */
520 if (status == MISSING_OBJECT) {
521 odb_reprepare(r->objects);
521 - odb_for_each_object_ext(r->objects, NULL, match_prefix, &ds, &opts);
522 + odb_for_each_object_ext(r->objects, NULL, update_disambiguate_state,
523 + &ds, &opts);
524 status = finish_object_disambiguation(&ds, oid);
525 }
526