aboutsummaryrefslogtreecommitdiff
path: root/cbtree.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2026-03-20 08:07:28 +0100
committerJunio C Hamano <gitster@pobox.com>2026-03-20 13:16:22 -0700
commitfe446b01aeaab307adcbfb39d4aaa72c37afbcda (patch)
tree5f28e0581528e50f32f44120a8890a328b2e136f /cbtree.c
parent1382e54a9c9e5f98271a943af9c10299c6ba934b (diff)
downloadgit-fe446b01aeaab307adcbfb39d4aaa72c37afbcda.tar.xz
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>
Diffstat (limited to 'cbtree.c')
-rw-r--r--cbtree.c21
1 files changed, 12 insertions, 9 deletions
diff --git a/cbtree.c b/cbtree.c
index cf8cf75b89..4ab794bddc 100644
--- a/cbtree.c
+++ b/cbtree.c
@@ -96,26 +96,28 @@ struct cb_node *cb_lookup(struct cb_tree *t, const uint8_t *k, size_t klen)
return p && !memcmp(p->k, k, klen) ? p : NULL;
}
-static enum cb_next cb_descend(struct cb_node *p, cb_iter fn, void *arg)
+static int cb_descend(struct cb_node *p, cb_iter fn, void *arg)
{
if (1 & (uintptr_t)p) {
struct cb_node *q = cb_node_of(p);
- enum cb_next n = cb_descend(q->child[0], fn, arg);
-
- return n == CB_BREAK ? n : cb_descend(q->child[1], fn, arg);
+ int ret = cb_descend(q->child[0], fn, arg);
+ if (ret)
+ return ret;
+ return cb_descend(q->child[1], fn, arg);
} else {
return fn(p, arg);
}
}
-void cb_each(struct cb_tree *t, const uint8_t *kpfx, size_t klen,
- cb_iter fn, void *arg)
+int cb_each(struct cb_tree *t, const uint8_t *kpfx, size_t klen,
+ cb_iter fn, void *arg)
{
struct cb_node *p = t->root;
struct cb_node *top = p;
size_t i = 0;
- if (!p) return; /* empty tree */
+ if (!p)
+ return 0; /* empty tree */
/* Walk tree, maintaining top pointer */
while (1 & (uintptr_t)p) {
@@ -130,7 +132,8 @@ void cb_each(struct cb_tree *t, const uint8_t *kpfx, size_t klen,
for (i = 0; i < klen; i++) {
if (p->k[i] != kpfx[i])
- return; /* "best" match failed */
+ return 0; /* "best" match failed */
}
- cb_descend(top, fn, arg);
+
+ return cb_descend(top, fn, arg);
}