refs: drop unused params from the reflog iterator callback
The ref and reflog iterators share much of the same underlying code to
iterate over the corresponding entries. This results in some weird code
because the reflog iterator also exposes an object ID as well as a flag
to the callback function. Neither of these fields do refer to the reflog
though -- they refer to the corresponding ref with the same name. This
is quite misleading. In practice at least the object ID cannot really be
implemented in any other way as a reflog does not have a specific object
ID in the first place. This is further stressed by the fact that none of
the callbacks except for our test helper make use of these fields.
Split up the infrastucture so that ref and reflog iterators use separate
callback signatures. This allows us to drop the nonsensical fields from
the reflog iterator.
Note that internally, the backends still use the same shared infra to
iterate over both types. As the backends should never end up being
called directly anyway, this is not much of a problem and thus kept
as-is for simplicity's sake.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committedFeb 21, 2024 at 13:37 UTC31f898397bb2f44692b8bcc4fd64fffaf3b59c48
11 files changed+65-54
builtin/fsck.c
+1-3
index a7cf94f67e..f892487c9b 100644--- a/builtin/fsck.c+++ b/builtin/fsck.c@@ -509,9 +509,7 @@ static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid return 0; }-static int fsck_handle_reflog(const char *logname,- const struct object_id *oid UNUSED,- int flag UNUSED, void *cb_data)+static int fsck_handle_reflog(const char *logname, void *cb_data) { struct strbuf refname = STRBUF_INIT;
index 303c5fac4d..895579aeb7 100644--- a/refs.h+++ b/refs.h@@ -534,12 +534,19 @@ int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_dat /* youngest entry first */ int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data);+/*+ * The signature for the callback function for the {refs_,}for_each_reflog()+ * functions below. The memory pointed to by the refname argument is only+ * guaranteed to be valid for the duration of a single callback invocation.+ */+typedef int each_reflog_fn(const char *refname, void *cb_data);+ /* * Calls the specified function for each reflog file until it returns nonzero, * and returns the value. Reflog file order is unspecified. */-int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data);-int for_each_reflog(each_ref_fn fn, void *cb_data);+int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data);+int for_each_reflog(each_reflog_fn fn, void *cb_data); #define REFNAME_ALLOW_ONELEVEL 1 #define REFNAME_REFSPEC_PATTERN 2
refs/files-backend.c
+1-7
index 05bb0c875c..c7aff6b331 100644--- a/refs/files-backend.c+++ b/refs/files-backend.c@@ -2115,10 +2115,8 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store, struct files_reflog_iterator { struct ref_iterator base;- struct ref_store *ref_store; struct dir_iterator *dir_iterator;- struct object_id oid; }; static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)@@ -2129,8 +2127,6 @@ static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator) int ok; while ((ok = dir_iterator_advance(diter)) == ITER_OK) {- int flags;- if (!S_ISREG(diter->st.st_mode)) continue; if (diter->basename[0] == '.')@@ -2140,14 +2136,12 @@ static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator) if (!refs_resolve_ref_unsafe(iter->ref_store, diter->relative_path, 0,- &iter->oid, &flags)) {+ NULL, NULL)) { error("bad ref for %s", diter->path.buf); continue; } iter->base.refname = diter->relative_path;- iter->base.oid = &iter->oid;- iter->base.flags = flags; return ITER_OK; }
refs/reftable-backend.c
+1-7
index 39e9a9d4e2..4998b676c2 100644--- a/refs/reftable-backend.c+++ b/refs/reftable-backend.c@@ -1594,7 +1594,6 @@ struct reftable_reflog_iterator { struct reftable_ref_store *refs; struct reftable_iterator iter; struct reftable_log_record log;- struct object_id oid; char *last_name; int err; };@@ -1605,8 +1604,6 @@ static int reftable_reflog_iterator_advance(struct ref_iterator *ref_iterator) (struct reftable_reflog_iterator *)ref_iterator; while (!iter->err) {- int flags;- iter->err = reftable_iterator_next_log(&iter->iter, &iter->log); if (iter->err) break;@@ -1620,7 +1617,7 @@ static int reftable_reflog_iterator_advance(struct ref_iterator *ref_iterator) continue; if (!refs_resolve_ref_unsafe(&iter->refs->base, iter->log.refname,- 0, &iter->oid, &flags)) {+ 0, NULL, NULL)) { error(_("bad ref for %s"), iter->log.refname); continue; }@@ -1628,8 +1625,6 @@ static int reftable_reflog_iterator_advance(struct ref_iterator *ref_iterator) free(iter->last_name); iter->last_name = xstrdup(iter->log.refname); iter->base.refname = iter->log.refname;- iter->base.oid = &iter->oid;- iter->base.flags = flags; break; }@@ -1682,7 +1677,6 @@ static struct reftable_reflog_iterator *reflog_iterator_for_stack(struct reftabl iter = xcalloc(1, sizeof(*iter)); base_ref_iterator_init(&iter->base, &reftable_reflog_iterator_vtable); iter->refs = refs;- iter->base.oid = &iter->oid; ret = refs->err; if (ret)
revision.c
+1-3
index 2424c9bd67..ac45c6d8f2 100644--- a/revision.c+++ b/revision.c@@ -1686,9 +1686,7 @@ static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid, return 0; }-static int handle_one_reflog(const char *refname_in_wt,- const struct object_id *oid UNUSED,- int flag UNUSED, void *cb_data)+static int handle_one_reflog(const char *refname_in_wt, void *cb_data) { struct all_refs_cb *cb = cb_data; struct strbuf refname = STRBUF_INIT;