]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: pass refname when invoking reflog entry callback
authorPatrick Steinhardt <ps@pks.im>
Thu, 31 Jul 2025 14:56:49 +0000 (16:56 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 6 Aug 2025 21:19:30 +0000 (14:19 -0700)
With `refs_for_each_reflog_ent()` callers can iterate through all the
reflog entries for a given reference. The callback that is being invoked
for each such entry does not receive the name of the reference that we
are currently iterating through. This isn't really a limiting factor, as
callers can simply pass the name via the callback data.

But this layout sometimes does make for a bit of an awkward calling
pattern. One example: when iterating through all reflogs, and for each
reflog we iterate through all refnames, we have to do some extra book
keeping to track which reference name we are currently yielding reflog
entries for.

Change the signature of the callback function so that the reference name
of the reflog gets passed through to it. Adapt callers accordingly and
start using the new parameter in trivial cases. The next commit will
refactor the reference migration logic to make use of this parameter so
that we can simplify its logic a bit.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
17 files changed:
builtin/fsck.c
builtin/gc.c
builtin/stash.c
commit.c
object-name.c
reflog-walk.c
reflog.c
reflog.h
refs.c
refs.h
refs/debug.c
refs/files-backend.c
refs/reftable-backend.c
remote.c
revision.c
t/helper/test-ref-store.c
wt-status.c

index 0084cf7400bd46ba7d8b72a908a67c3536abc907..67eb5e4fa0fe3525d8efcbdea5c3c92d64d735bb 100644 (file)
@@ -502,13 +502,12 @@ static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
        }
 }
 
-static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
+static int fsck_handle_reflog_ent(const char *refname,
+                                 struct object_id *ooid, struct object_id *noid,
                                  const char *email UNUSED,
                                  timestamp_t timestamp, int tz UNUSED,
-                                 const char *message UNUSED, void *cb_data)
+                                 const char *message UNUSED, void *cb_data UNUSED)
 {
-       const char *refname = cb_data;
-
        if (verbose)
                fprintf_ln(stderr, _("Checking reflog %s->%s"),
                           oid_to_hex(ooid), oid_to_hex(noid));
@@ -525,7 +524,7 @@ static int fsck_handle_reflog(const char *logname, void *cb_data)
        strbuf_worktree_ref(cb_data, &refname, logname);
        refs_for_each_reflog_ent(get_main_ref_store(the_repository),
                                 refname.buf, fsck_handle_reflog_ent,
-                                refname.buf);
+                                NULL);
        strbuf_release(&refname);
        return 0;
 }
index fab8f4dd4f7b6cc9b7c5c96d8b5647ece14b718a..9ae87065d35c8f2fd15fd714340376b159e9df33 100644 (file)
@@ -312,7 +312,8 @@ struct count_reflog_entries_data {
        size_t limit;
 };
 
-static int count_reflog_entries(struct object_id *old_oid, struct object_id *new_oid,
+static int count_reflog_entries(const char *refname UNUSED,
+                               struct object_id *old_oid, struct object_id *new_oid,
                                const char *committer, timestamp_t timestamp,
                                int tz, const char *msg, void *cb_data)
 {
index e2f95cc2ebc219894ead5deea0807c0288cc4eec..a1ed67661e3e7f6b3178d895690f06001e1cb964 100644 (file)
@@ -738,7 +738,8 @@ cleanup:
        return ret;
 }
 
-static int reject_reflog_ent(struct object_id *ooid UNUSED,
+static int reject_reflog_ent(const char *refname UNUSED,
+                            struct object_id *ooid UNUSED,
                             struct object_id *noid UNUSED,
                             const char *email UNUSED,
                             timestamp_t timestamp UNUSED,
@@ -2173,7 +2174,8 @@ struct stash_entry_data {
        size_t count;
 };
 
-static int collect_stash_entries(struct object_id *old_oid UNUSED,
+static int collect_stash_entries(const char *refname UNUSED,
+                                struct object_id *old_oid UNUSED,
                                 struct object_id *new_oid,
                                 const char *committer UNUSED,
                                 timestamp_t timestamp UNUSED,
index 15115125c3612c045b3be2f68a94aae4bad1fc4a..7ebd05f3527b13291d709b1eede1c35139fe22b9 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -1031,7 +1031,8 @@ static void add_one_commit(struct object_id *oid, struct rev_collect *revs)
        commit->object.flags |= TMP_MARK;
 }
 
-static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
+static int collect_one_reflog_ent(const char *refname UNUSED,
+                                 struct object_id *ooid, struct object_id *noid,
                                  const char *ident UNUSED,
                                  timestamp_t timestamp UNUSED, int tz UNUSED,
                                  const char *message UNUSED, void *cbdata)
index ddafe7f9b13a9642e30eece89578b37ac15b5872..9ec192c37318a854b17c7fd046e7b022c43e809b 100644 (file)
@@ -1516,7 +1516,8 @@ struct grab_nth_branch_switch_cbdata {
        struct strbuf *sb;
 };
 
-static int grab_nth_branch_switch(struct object_id *ooid UNUSED,
+static int grab_nth_branch_switch(const char *refname UNUSED,
+                                 struct object_id *ooid UNUSED,
                                  struct object_id *noid UNUSED,
                                  const char *email UNUSED,
                                  timestamp_t timestamp UNUSED,
index c7070b13b004b7c207a3f99740999f582c15c1b0..4f1ce047498116fb229306845cb90401baf4c247 100644 (file)
@@ -22,9 +22,10 @@ struct complete_reflogs {
        int nr, alloc;
 };
 
-static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
-               const char *email, timestamp_t timestamp, int tz,
-               const char *message, void *cb_data)
+static int read_one_reflog(const char *refname UNUSED,
+                          struct object_id *ooid, struct object_id *noid,
+                          const char *email, timestamp_t timestamp, int tz,
+                          const char *message, void *cb_data)
 {
        struct complete_reflogs *array = cb_data;
        struct reflog_info *item;
index 39c205fd26e77b2a71ffe8c22812479f87b8d8bd..2264b3bd605f3a28e0bcaa5487d4ce7bedef6010 100644 (file)
--- a/reflog.c
+++ b/reflog.c
@@ -492,7 +492,8 @@ void reflog_expiry_cleanup(void *cb_data)
        free_commit_list(cb->mark_list);
 }
 
-int count_reflog_ent(struct object_id *ooid UNUSED,
+int count_reflog_ent(const char *refname UNUSED,
+                    struct object_id *ooid UNUSED,
                     struct object_id *noid UNUSED,
                     const char *email UNUSED,
                     timestamp_t timestamp, int tz UNUSED,
index 63bb56280f4ed6b69448a2366e24c592dc7ba4b0..44b306c08ae391dc14e4dbaadd58902ca82ccc95 100644 (file)
--- a/reflog.h
+++ b/reflog.h
@@ -63,7 +63,8 @@ void reflog_expiry_prepare(const char *refname, const struct object_id *oid,
 int should_expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
                             const char *email, timestamp_t timestamp, int tz,
                             const char *message, void *cb_data);
-int count_reflog_ent(struct object_id *ooid, struct object_id *noid,
+int count_reflog_ent(const char *refname,
+                    struct object_id *ooid, struct object_id *noid,
                     const char *email, timestamp_t timestamp, int tz,
                     const char *message, void *cb_data);
 int should_expire_reflog_ent_verbose(struct object_id *ooid,
diff --git a/refs.c b/refs.c
index 4bd80287054e9b010a70cb48cc26e4bee379347e..6ed0cd6ddca00d8d734c9e9457d518aa9d8d7150 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -1022,7 +1022,6 @@ int is_branch(const char *refname)
 }
 
 struct read_ref_at_cb {
-       const char *refname;
        timestamp_t at_time;
        int cnt;
        int reccnt;
@@ -1052,7 +1051,8 @@ static void set_read_ref_cutoffs(struct read_ref_at_cb *cb,
                *cb->cutoff_cnt = cb->reccnt;
 }
 
-static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
+static int read_ref_at_ent(const char *refname,
+                          struct object_id *ooid, struct object_id *noid,
                           const char *email UNUSED,
                           timestamp_t timestamp, int tz,
                           const char *message, void *cb_data)
@@ -1072,14 +1072,13 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
                        oidcpy(cb->oid, noid);
                        if (!oideq(&cb->ooid, noid))
                                warning(_("log for ref %s has gap after %s"),
-                                       cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
+                                       refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
                }
                else if (cb->date == cb->at_time)
                        oidcpy(cb->oid, noid);
                else if (!oideq(noid, cb->oid))
                        warning(_("log for ref %s unexpectedly ended on %s"),
-                               cb->refname, show_date(cb->date, cb->tz,
-                                                      DATE_MODE(RFC2822)));
+                               refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
                cb->reccnt++;
                oidcpy(&cb->ooid, ooid);
                oidcpy(&cb->noid, noid);
@@ -1094,7 +1093,8 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
        return 0;
 }
 
-static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
+static int read_ref_at_ent_oldest(const char *refname UNUSED,
+                                 struct object_id *ooid, struct object_id *noid,
                                  const char *email UNUSED,
                                  timestamp_t timestamp, int tz,
                                  const char *message, void *cb_data)
@@ -1117,7 +1117,6 @@ int read_ref_at(struct ref_store *refs, const char *refname,
        struct read_ref_at_cb cb;
 
        memset(&cb, 0, sizeof(cb));
-       cb.refname = refname;
        cb.at_time = at_time;
        cb.cnt = cnt;
        cb.msg = msg;
@@ -2976,14 +2975,14 @@ done:
 
 struct reflog_migration_data {
        uint64_t index;
-       const char *refname;
        struct ref_store *old_refs;
        struct ref_transaction *transaction;
        struct strbuf *errbuf;
        struct strbuf *sb, *name, *mail;
 };
 
-static int migrate_one_reflog_entry(struct object_id *old_oid,
+static int migrate_one_reflog_entry(const char *refname,
+                                   struct object_id *old_oid,
                                    struct object_id *new_oid,
                                    const char *committer,
                                    timestamp_t timestamp, int tz,
@@ -3006,7 +3005,7 @@ static int migrate_one_reflog_entry(struct object_id *old_oid,
        strbuf_reset(data->sb);
        strbuf_addstr(data->sb, fmt_ident(data->name->buf, data->mail->buf, WANT_BLANK_IDENT, date, 0));
 
-       ret = ref_transaction_update_reflog(data->transaction, data->refname,
+       ret = ref_transaction_update_reflog(data->transaction, refname,
                                            new_oid, old_oid, data->sb->buf,
                                            msg, data->index++, data->errbuf);
        return ret;
@@ -3016,7 +3015,6 @@ static int migrate_one_reflog(const char *refname, void *cb_data)
 {
        struct migration_data *migration_data = cb_data;
        struct reflog_migration_data data = {
-               .refname = refname,
                .old_refs = migration_data->old_refs,
                .transaction = migration_data->transaction,
                .errbuf = migration_data->errbuf,
diff --git a/refs.h b/refs.h
index 99b58d0b73c9db903428b6d0edcda7a117854cca..0bf50ce25cc06756c17b4474e8ac1b2600f4c8a5 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -558,10 +558,13 @@ int refs_delete_reflog(struct ref_store *refs, const char *refname);
  * The cb_data is a caller-supplied pointer given to the iterator
  * functions.
  */
-typedef int each_reflog_ent_fn(
-               struct object_id *old_oid, struct object_id *new_oid,
-               const char *committer, timestamp_t timestamp,
-               int tz, const char *msg, void *cb_data);
+typedef int each_reflog_ent_fn(const char *refname,
+                              struct object_id *old_oid,
+                              struct object_id *new_oid,
+                              const char *committer,
+                              timestamp_t timestamp,
+                              int tz, const char *msg,
+                              void *cb_data);
 
 /* Iterate over reflog entries in the log for `refname`. */
 
index 485e3079d7a3a7b70899b3d88ffa765dc8740498..5e113db307a2e56eddf21ba20f83ec8265904ebd 100644 (file)
@@ -276,7 +276,8 @@ struct debug_reflog {
        void *cb_data;
 };
 
-static int debug_print_reflog_ent(struct object_id *old_oid,
+static int debug_print_reflog_ent(const char *refname,
+                                 struct object_id *old_oid,
                                  struct object_id *new_oid,
                                  const char *committer, timestamp_t timestamp,
                                  int tz, const char *msg, void *cb_data)
@@ -291,7 +292,7 @@ static int debug_print_reflog_ent(struct object_id *old_oid,
        if (new_oid)
                oid_to_hex_r(n, new_oid);
 
-       ret = dbg->fn(old_oid, new_oid, committer, timestamp, tz, msg,
+       ret = dbg->fn(refname, old_oid, new_oid, committer, timestamp, tz, msg,
                      dbg->cb_data);
        trace_printf_key(&trace_refs,
                         "reflog_ent %s (ret %d): %s -> %s, %s %ld \"%.*s\"\n",
index f53895cf4bd9fa605266fc02882855174e9221b9..dff52a583a9cc5b75d854cfccb314e228963af6d 100644 (file)
@@ -2115,7 +2115,9 @@ static int files_delete_reflog(struct ref_store *ref_store,
        return ret;
 }
 
-static int show_one_reflog_ent(struct files_ref_store *refs, struct strbuf *sb,
+static int show_one_reflog_ent(struct files_ref_store *refs,
+                              const char *refname,
+                              struct strbuf *sb,
                               each_reflog_ent_fn fn, void *cb_data)
 {
        struct object_id ooid, noid;
@@ -2142,7 +2144,7 @@ static int show_one_reflog_ent(struct files_ref_store *refs, struct strbuf *sb,
                message += 6;
        else
                message += 7;
-       return fn(&ooid, &noid, p, timestamp, tz, message, cb_data);
+       return fn(refname, &ooid, &noid, p, timestamp, tz, message, cb_data);
 }
 
 static char *find_beginning_of_line(char *bob, char *scan)
@@ -2226,7 +2228,7 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
                                strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
                                scanp = bp;
                                endp = bp + 1;
-                               ret = show_one_reflog_ent(refs, &sb, fn, cb_data);
+                               ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
                                strbuf_reset(&sb);
                                if (ret)
                                        break;
@@ -2238,7 +2240,7 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
                                 * Process it, and we can end the loop.
                                 */
                                strbuf_splice(&sb, 0, 0, buf, endp - buf);
-                               ret = show_one_reflog_ent(refs, &sb, fn, cb_data);
+                               ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
                                strbuf_reset(&sb);
                                break;
                        }
@@ -2288,7 +2290,7 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store,
                return -1;
 
        while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
-               ret = show_one_reflog_ent(refs, &sb, fn, cb_data);
+               ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
        fclose(logfp);
        strbuf_release(&sb);
        return ret;
@@ -3359,7 +3361,8 @@ struct expire_reflog_cb {
                     dry_run:1;
 };
 
-static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
+static int expire_reflog_ent(const char *refname UNUSED,
+                            struct object_id *ooid, struct object_id *noid,
                             const char *email, timestamp_t timestamp, int tz,
                             const char *message, void *cb_data)
 {
index 99fafd75ebe8ff001d30b6d38885c76bbea45219..25a1d516184e43cbfc6e57bc5ae66ec3b9ed1214 100644 (file)
@@ -2148,7 +2148,7 @@ static int yield_log_record(struct reftable_ref_store *refs,
 
        full_committer = fmt_ident(log->value.update.name, log->value.update.email,
                                   WANT_COMMITTER_IDENT, NULL, IDENT_NO_DATE);
-       return fn(&old_oid, &new_oid, full_committer,
+       return fn(log->refname, &old_oid, &new_oid, full_committer,
                  log->value.update.time, log->value.update.tz_offset,
                  log->value.update.message, cb_data);
 }
index e965f022f12b78784964d4940615b1e9cf35a0e4..db9eea4fa456d027bf2b57bb13b831fceac8afa2 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -2578,7 +2578,8 @@ struct check_and_collect_until_cb_data {
 };
 
 /* Get the timestamp of the latest entry. */
-static int peek_reflog(struct object_id *o_oid UNUSED,
+static int peek_reflog(const char *refname UNUSED,
+                      struct object_id *o_oid UNUSED,
                       struct object_id *n_oid UNUSED,
                       const char *ident UNUSED,
                       timestamp_t timestamp, int tz UNUSED,
@@ -2589,7 +2590,8 @@ static int peek_reflog(struct object_id *o_oid UNUSED,
        return 1;
 }
 
-static int check_and_collect_until(struct object_id *o_oid UNUSED,
+static int check_and_collect_until(const char *refname UNUSED,
+                                  struct object_id *o_oid UNUSED,
                                   struct object_id *n_oid,
                                   const char *ident UNUSED,
                                   timestamp_t timestamp, int tz UNUSED,
index 212ca0de2768b85ea0c217ea862ae2d6c06857b3..0fc1a167a1089627202efdf29de7ff4fd2d09918 100644 (file)
@@ -1699,7 +1699,8 @@ static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
        }
 }
 
-static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
+static int handle_one_reflog_ent(const char *refname UNUSED,
+                                struct object_id *ooid, struct object_id *noid,
                                 const char *email UNUSED,
                                 timestamp_t timestamp UNUSED,
                                 int tz UNUSED,
index 8d9a271845c4b60fb568a77020e21340e0a759c5..b2380d57ba30659bed220a86ae98b98cd92e8cfb 100644 (file)
@@ -215,7 +215,8 @@ static int cmd_for_each_reflog(struct ref_store *refs,
        return refs_for_each_reflog(refs, each_reflog, NULL);
 }
 
-static int each_reflog_ent(struct object_id *old_oid, struct object_id *new_oid,
+static int each_reflog_ent(const char *refname UNUSED,
+                          struct object_id *old_oid, struct object_id *new_oid,
                           const char *committer, timestamp_t timestamp,
                           int tz, const char *msg, void *cb_data UNUSED)
 {
index 454601afa15a951adb6b70ae0687485a4448ed68..71bd17b610a89fc91db06bf7ecba1d028ec42dbb 100644 (file)
@@ -972,7 +972,8 @@ static void wt_longstatus_print_changed(struct wt_status *s)
        wt_longstatus_print_trailer(s);
 }
 
-static int stash_count_refs(struct object_id *ooid UNUSED,
+static int stash_count_refs(const char *refname UNUSED,
+                           struct object_id *ooid UNUSED,
                            struct object_id *noid UNUSED,
                            const char *email UNUSED,
                            timestamp_t timestamp UNUSED, int tz UNUSED,
@@ -1664,7 +1665,8 @@ struct grab_1st_switch_cbdata {
        struct object_id noid;
 };
 
-static int grab_1st_switch(struct object_id *ooid UNUSED,
+static int grab_1st_switch(const char *refname UNUSED,
+                          struct object_id *ooid UNUSED,
                           struct object_id *noid,
                           const char *email UNUSED,
                           timestamp_t timestamp UNUSED, int tz UNUSED,