oidtree: extend iteration to allow for arbitrary return codes

The interface `cb_each()` iterates through a crit-bit tree and calls a
specific callback function for each of the contained items. The callback
function is expected to return either:

  - `CB_CONTINUE` in case iteration shall continue.

  - `CB_BREAK` to abort iteration.

This is needlessly restrictive though, as callers may want to return
arbitrary values and have them be bubbled up to the `cb_each()` call
site. In fact, this is a rather common pattern we have: whenever such a
callback function returns a non-zero error code, we abort iteration and
bubble up the code as-is.

Refactor both the crit-bit tree and oidtree subsystems to behave
accordingly.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2026-03-20 08:07:28 +01:00
committed by Junio C Hamano
parent 1382e54a9c
commit fe446b01ae
6 changed files with 43 additions and 33 deletions

View File

@@ -103,12 +103,12 @@ static void update_candidates(struct disambiguate_state *ds, const struct object
static int match_hash(unsigned, const unsigned char *, const unsigned char *);
static enum cb_next match_prefix(const struct object_id *oid, void *arg)
static int match_prefix(const struct object_id *oid, void *arg)
{
struct disambiguate_state *ds = arg;
/* no need to call match_hash, oidtree_each did prefix match */
update_candidates(ds, oid);
return ds->ambiguous ? CB_BREAK : CB_CONTINUE;
return ds->ambiguous;
}
static void find_short_object_filename(struct disambiguate_state *ds)