Skip to content

Commit

Permalink
refs: always treat iterators as ordered
Browse files Browse the repository at this point in the history
In the preceding commit we have converted the reflog iterator of the
"files" backend to be ordered, which was the only remaining ref iterator
that wasn't ordered. Refactor the ref iterator infrastructure so that we
always assume iterators to be ordered, thus simplifying the code.

Signed-off-by: Patrick Steinhardt <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
pks-t authored and gitster committed Feb 21, 2024
1 parent 6f22780 commit 5e01d83
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 50 deletions.
4 changes: 0 additions & 4 deletions refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1594,10 +1594,6 @@ struct ref_iterator *refs_ref_iterator_begin(
if (trim)
iter = prefix_ref_iterator_begin(iter, "", trim);

/* Sanity check for subclasses: */
if (!iter->ordered)
BUG("reference iterator is not ordered");

return iter;
}

Expand Down
3 changes: 1 addition & 2 deletions refs/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ static int debug_ref_iterator_advance(struct ref_iterator *ref_iterator)
trace_printf_key(&trace_refs, "iterator_advance: %s (0)\n",
diter->iter->refname);

diter->base.ordered = diter->iter->ordered;
diter->base.refname = diter->iter->refname;
diter->base.oid = diter->iter->oid;
diter->base.flags = diter->iter->flags;
Expand Down Expand Up @@ -222,7 +221,7 @@ debug_ref_iterator_begin(struct ref_store *ref_store, const char *prefix,
drefs->refs->be->iterator_begin(drefs->refs, prefix,
exclude_patterns, flags);
struct debug_ref_iterator *diter = xcalloc(1, sizeof(*diter));
base_ref_iterator_init(&diter->base, &debug_ref_iterator_vtable, 1);
base_ref_iterator_init(&diter->base, &debug_ref_iterator_vtable);
diter->iter = res;
trace_printf_key(&trace_refs, "ref_iterator_begin: \"%s\" (0x%x)\n",
prefix, flags);
Expand Down
7 changes: 3 additions & 4 deletions refs/files-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,7 @@ static struct ref_iterator *files_ref_iterator_begin(

CALLOC_ARRAY(iter, 1);
ref_iterator = &iter->base;
base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable,
overlay_iter->ordered);
base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
iter->iter0 = overlay_iter;
iter->repo = ref_store->repo;
iter->flags = flags;
Expand Down Expand Up @@ -2202,7 +2201,7 @@ static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
CALLOC_ARRAY(iter, 1);
ref_iterator = &iter->base;

base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 1);
base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
iter->dir_iterator = diter;
iter->ref_store = ref_store;
strbuf_release(&sb);
Expand All @@ -2220,7 +2219,7 @@ static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_st
return reflog_iterator_begin(ref_store, refs->gitcommondir);
} else {
return merge_ref_iterator_begin(
1, reflog_iterator_begin(ref_store, refs->base.gitdir),
reflog_iterator_begin(ref_store, refs->base.gitdir),
reflog_iterator_begin(ref_store, refs->gitcommondir),
ref_iterator_select, refs);
}
Expand Down
26 changes: 8 additions & 18 deletions refs/iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ int ref_iterator_abort(struct ref_iterator *ref_iterator)
}

void base_ref_iterator_init(struct ref_iterator *iter,
struct ref_iterator_vtable *vtable,
int ordered)
struct ref_iterator_vtable *vtable)
{
iter->vtable = vtable;
iter->ordered = !!ordered;
iter->refname = NULL;
iter->oid = NULL;
iter->flags = 0;
Expand Down Expand Up @@ -74,7 +72,7 @@ struct ref_iterator *empty_ref_iterator_begin(void)
struct empty_ref_iterator *iter = xcalloc(1, sizeof(*iter));
struct ref_iterator *ref_iterator = &iter->base;

base_ref_iterator_init(ref_iterator, &empty_ref_iterator_vtable, 1);
base_ref_iterator_init(ref_iterator, &empty_ref_iterator_vtable);
return ref_iterator;
}

Expand Down Expand Up @@ -250,7 +248,6 @@ static struct ref_iterator_vtable merge_ref_iterator_vtable = {
};

struct ref_iterator *merge_ref_iterator_begin(
int ordered,
struct ref_iterator *iter0, struct ref_iterator *iter1,
ref_iterator_select_fn *select, void *cb_data)
{
Expand All @@ -265,7 +262,7 @@ struct ref_iterator *merge_ref_iterator_begin(
* references through only if they exist in both iterators.
*/

base_ref_iterator_init(ref_iterator, &merge_ref_iterator_vtable, ordered);
base_ref_iterator_init(ref_iterator, &merge_ref_iterator_vtable);
iter->iter0 = iter0;
iter->iter1 = iter1;
iter->select = select;
Expand Down Expand Up @@ -314,12 +311,9 @@ struct ref_iterator *overlay_ref_iterator_begin(
} else if (is_empty_ref_iterator(back)) {
ref_iterator_abort(back);
return front;
} else if (!front->ordered || !back->ordered) {
BUG("overlay_ref_iterator requires ordered inputs");
}

return merge_ref_iterator_begin(1, front, back,
overlay_iterator_select, NULL);
return merge_ref_iterator_begin(front, back, overlay_iterator_select, NULL);
}

struct prefix_ref_iterator {
Expand Down Expand Up @@ -358,16 +352,12 @@ static int prefix_ref_iterator_advance(struct ref_iterator *ref_iterator)

if (cmp > 0) {
/*
* If the source iterator is ordered, then we
* As the source iterator is ordered, we
* can stop the iteration as soon as we see a
* refname that comes after the prefix:
*/
if (iter->iter0->ordered) {
ok = ref_iterator_abort(iter->iter0);
break;
} else {
continue;
}
ok = ref_iterator_abort(iter->iter0);
break;
}

if (iter->trim) {
Expand Down Expand Up @@ -439,7 +429,7 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
CALLOC_ARRAY(iter, 1);
ref_iterator = &iter->base;

base_ref_iterator_init(ref_iterator, &prefix_ref_iterator_vtable, iter0->ordered);
base_ref_iterator_init(ref_iterator, &prefix_ref_iterator_vtable);

iter->iter0 = iter0;
iter->prefix = xstrdup(prefix);
Expand Down
2 changes: 1 addition & 1 deletion refs/packed-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ static struct ref_iterator *packed_ref_iterator_begin(

CALLOC_ARRAY(iter, 1);
ref_iterator = &iter->base;
base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable, 1);
base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable);

if (exclude_patterns)
populate_excluded_jump_list(iter, snapshot, exclude_patterns);
Expand Down
2 changes: 1 addition & 1 deletion refs/ref-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ struct ref_iterator *cache_ref_iterator_begin(struct ref_cache *cache,

CALLOC_ARRAY(iter, 1);
ref_iterator = &iter->base;
base_ref_iterator_init(ref_iterator, &cache_ref_iterator_vtable, 1);
base_ref_iterator_init(ref_iterator, &cache_ref_iterator_vtable);
ALLOC_GROW(iter->levels, 10, iter->levels_alloc);

iter->levels_nr = 1;
Expand Down
18 changes: 2 additions & 16 deletions refs/refs-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,6 @@ enum do_for_each_ref_flags {
*/
struct ref_iterator {
struct ref_iterator_vtable *vtable;

/*
* Does this `ref_iterator` iterate over references in order
* by refname?
*/
unsigned int ordered : 1;

const char *refname;
const struct object_id *oid;
unsigned int flags;
Expand Down Expand Up @@ -399,11 +392,9 @@ enum iterator_selection ref_iterator_select(struct ref_iterator *iter_worktree,
* Iterate over the entries from iter0 and iter1, with the values
* interleaved as directed by the select function. The iterator takes
* ownership of iter0 and iter1 and frees them when the iteration is
* over. A derived class should set `ordered` to 1 or 0 based on
* whether it generates its output in order by reference name.
* over.
*/
struct ref_iterator *merge_ref_iterator_begin(
int ordered,
struct ref_iterator *iter0, struct ref_iterator *iter1,
ref_iterator_select_fn *select, void *cb_data);

Expand Down Expand Up @@ -432,8 +423,6 @@ struct ref_iterator *overlay_ref_iterator_begin(
* As an convenience to callers, if prefix is the empty string and
* trim is zero, this function returns iter0 directly, without
* wrapping it.
*
* The resulting ref_iterator is ordered if iter0 is.
*/
struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
const char *prefix,
Expand All @@ -444,14 +433,11 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
/*
* Base class constructor for ref_iterators. Initialize the
* ref_iterator part of iter, setting its vtable pointer as specified.
* `ordered` should be set to 1 if the iterator will iterate over
* references in order by refname; otherwise it should be set to 0.
* This is meant to be called only by the initializers of derived
* classes.
*/
void base_ref_iterator_init(struct ref_iterator *iter,
struct ref_iterator_vtable *vtable,
int ordered);
struct ref_iterator_vtable *vtable);

/*
* Base class destructor for ref_iterators. Destroy the ref_iterator
Expand Down
8 changes: 4 additions & 4 deletions refs/reftable-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ static struct reftable_ref_iterator *ref_iterator_for_stack(struct reftable_ref_
int ret;

iter = xcalloc(1, sizeof(*iter));
base_ref_iterator_init(&iter->base, &reftable_ref_iterator_vtable, 1);
base_ref_iterator_init(&iter->base, &reftable_ref_iterator_vtable);
iter->prefix = prefix;
iter->base.oid = &iter->oid;
iter->flags = flags;
Expand Down Expand Up @@ -532,7 +532,7 @@ static struct ref_iterator *reftable_be_iterator_begin(struct ref_store *ref_sto
* single iterator.
*/
worktree_iter = ref_iterator_for_stack(refs, refs->worktree_stack, prefix, flags);
return merge_ref_iterator_begin(1, &worktree_iter->base, &main_iter->base,
return merge_ref_iterator_begin(&worktree_iter->base, &main_iter->base,
ref_iterator_select, NULL);
}

Expand Down Expand Up @@ -1680,7 +1680,7 @@ static struct reftable_reflog_iterator *reflog_iterator_for_stack(struct reftabl
int ret;

iter = xcalloc(1, sizeof(*iter));
base_ref_iterator_init(&iter->base, &reftable_reflog_iterator_vtable, 1);
base_ref_iterator_init(&iter->base, &reftable_reflog_iterator_vtable);
iter->refs = refs;
iter->base.oid = &iter->oid;

Expand Down Expand Up @@ -1715,7 +1715,7 @@ static struct ref_iterator *reftable_be_reflog_iterator_begin(struct ref_store *

worktree_iter = reflog_iterator_for_stack(refs, refs->worktree_stack);

return merge_ref_iterator_begin(1, &worktree_iter->base, &main_iter->base,
return merge_ref_iterator_begin(&worktree_iter->base, &main_iter->base,
ref_iterator_select, NULL);
}

Expand Down

0 comments on commit 5e01d83

Please sign in to comment.