refs: pass refname when invoking reflog entry callback

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>

Patrick Steinhardt committed Jul 31, 2025 at 16:56 UTC b9fd73a234db1a272f6cbfb528bae0ead9e07bde
17 files changed +63 -45
builtin/fsck.c
+4 -5
@@ -502,13 +502,12 @@ static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
502 }
503 }
504
505 -static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
505 +static int fsck_handle_reflog_ent(const char *refname,
506 + struct object_id *ooid, struct object_id *noid,
507 const char *email UNUSED,
508 timestamp_t timestamp, int tz UNUSED,
508 - const char *message UNUSED, void *cb_data)
509 + const char *message UNUSED, void *cb_data UNUSED)
510 {
510 - const char *refname = cb_data;
511 -
511 if (verbose)
512 fprintf_ln(stderr, _("Checking reflog %s->%s"),
513 oid_to_hex(ooid), oid_to_hex(noid));
@@ -525,7 +524,7 @@ static int fsck_handle_reflog(const char *logname, void *cb_data)
524 strbuf_worktree_ref(cb_data, &refname, logname);
525 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
526 refname.buf, fsck_handle_reflog_ent,
528 - refname.buf);
527 + NULL);
528 strbuf_release(&refname);
529 return 0;
530 }
builtin/gc.c
+2 -1
@@ -312,7 +312,8 @@ struct count_reflog_entries_data {
312 size_t limit;
313 };
314
315 -static int count_reflog_entries(struct object_id *old_oid, struct object_id *new_oid,
315 +static int count_reflog_entries(const char *refname UNUSED,
316 + struct object_id *old_oid, struct object_id *new_oid,
317 const char *committer, timestamp_t timestamp,
318 int tz, const char *msg, void *cb_data)
319 {
builtin/stash.c
+4 -2
@@ -738,7 +738,8 @@ cleanup:
738 return ret;
739 }
740
741 -static int reject_reflog_ent(struct object_id *ooid UNUSED,
741 +static int reject_reflog_ent(const char *refname UNUSED,
742 + struct object_id *ooid UNUSED,
743 struct object_id *noid UNUSED,
744 const char *email UNUSED,
745 timestamp_t timestamp UNUSED,
@@ -2173,7 +2174,8 @@ struct stash_entry_data {
2174 size_t count;
2175 };
2176
2176 -static int collect_stash_entries(struct object_id *old_oid UNUSED,
2177 +static int collect_stash_entries(const char *refname UNUSED,
2178 + struct object_id *old_oid UNUSED,
2179 struct object_id *new_oid,
2180 const char *committer UNUSED,
2181 timestamp_t timestamp UNUSED,
commit.c
+2 -1
@@ -1031,7 +1031,8 @@ static void add_one_commit(struct object_id *oid, struct rev_collect *revs)
1031 commit->object.flags |= TMP_MARK;
1032 }
1033
1034 -static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1034 +static int collect_one_reflog_ent(const char *refname UNUSED,
1035 + struct object_id *ooid, struct object_id *noid,
1036 const char *ident UNUSED,
1037 timestamp_t timestamp UNUSED, int tz UNUSED,
1038 const char *message UNUSED, void *cbdata)
object-name.c
+2 -1
@@ -1516,7 +1516,8 @@ struct grab_nth_branch_switch_cbdata {
1516 struct strbuf *sb;
1517 };
1518
1519 -static int grab_nth_branch_switch(struct object_id *ooid UNUSED,
1519 +static int grab_nth_branch_switch(const char *refname UNUSED,
1520 + struct object_id *ooid UNUSED,
1521 struct object_id *noid UNUSED,
1522 const char *email UNUSED,
1523 timestamp_t timestamp UNUSED,
reflog-walk.c
+4 -3
@@ -22,9 +22,10 @@ struct complete_reflogs {
22 int nr, alloc;
23 };
24
25 -static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
26 - const char *email, timestamp_t timestamp, int tz,
27 - const char *message, void *cb_data)
25 +static int read_one_reflog(const char *refname UNUSED,
26 + struct object_id *ooid, struct object_id *noid,
27 + const char *email, timestamp_t timestamp, int tz,
28 + const char *message, void *cb_data)
29 {
30 struct complete_reflogs *array = cb_data;
31 struct reflog_info *item;
reflog.c
+2 -1
@@ -492,7 +492,8 @@ void reflog_expiry_cleanup(void *cb_data)
492 free_commit_list(cb->mark_list);
493 }
494
495 -int count_reflog_ent(struct object_id *ooid UNUSED,
495 +int count_reflog_ent(const char *refname UNUSED,
496 + struct object_id *ooid UNUSED,
497 struct object_id *noid UNUSED,
498 const char *email UNUSED,
499 timestamp_t timestamp, int tz UNUSED,
reflog.h
+2 -1
@@ -63,7 +63,8 @@ void reflog_expiry_prepare(const char *refname, const struct object_id *oid,
63 int should_expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
64 const char *email, timestamp_t timestamp, int tz,
65 const char *message, void *cb_data);
66 -int count_reflog_ent(struct object_id *ooid, struct object_id *noid,
66 +int count_reflog_ent(const char *refname,
67 + struct object_id *ooid, struct object_id *noid,
68 const char *email, timestamp_t timestamp, int tz,
69 const char *message, void *cb_data);
70 int should_expire_reflog_ent_verbose(struct object_id *ooid,
refs.c
+9 -11
@@ -1022,7 +1022,6 @@ int is_branch(const char *refname)
1022 }
1023
1024 struct read_ref_at_cb {
1025 - const char *refname;
1025 timestamp_t at_time;
1026 int cnt;
1027 int reccnt;
@@ -1052,7 +1051,8 @@ static void set_read_ref_cutoffs(struct read_ref_at_cb *cb,
1051 *cb->cutoff_cnt = cb->reccnt;
1052 }
1053
1055 -static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
1054 +static int read_ref_at_ent(const char *refname,
1055 + struct object_id *ooid, struct object_id *noid,
1056 const char *email UNUSED,
1057 timestamp_t timestamp, int tz,
1058 const char *message, void *cb_data)
@@ -1072,14 +1072,13 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
1072 oidcpy(cb->oid, noid);
1073 if (!oideq(&cb->ooid, noid))
1074 warning(_("log for ref %s has gap after %s"),
1075 - cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
1075 + refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
1076 }
1077 else if (cb->date == cb->at_time)
1078 oidcpy(cb->oid, noid);
1079 else if (!oideq(noid, cb->oid))
1080 warning(_("log for ref %s unexpectedly ended on %s"),
1081 - cb->refname, show_date(cb->date, cb->tz,
1082 - DATE_MODE(RFC2822)));
1081 + refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
1082 cb->reccnt++;
1083 oidcpy(&cb->ooid, ooid);
1084 oidcpy(&cb->noid, noid);
@@ -1094,7 +1093,8 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
1093 return 0;
1094 }
1095
1097 -static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
1096 +static int read_ref_at_ent_oldest(const char *refname UNUSED,
1097 + struct object_id *ooid, struct object_id *noid,
1098 const char *email UNUSED,
1099 timestamp_t timestamp, int tz,
1100 const char *message, void *cb_data)
@@ -1117,7 +1117,6 @@ int read_ref_at(struct ref_store *refs, const char *refname,
1117 struct read_ref_at_cb cb;
1118
1119 memset(&cb, 0, sizeof(cb));
1120 - cb.refname = refname;
1120 cb.at_time = at_time;
1121 cb.cnt = cnt;
1122 cb.msg = msg;
@@ -2976,14 +2975,14 @@ done:
2975
2976 struct reflog_migration_data {
2977 uint64_t index;
2979 - const char *refname;
2978 struct ref_store *old_refs;
2979 struct ref_transaction *transaction;
2980 struct strbuf *errbuf;
2981 struct strbuf *sb, *name, *mail;
2982 };
2983
2986 -static int migrate_one_reflog_entry(struct object_id *old_oid,
2984 +static int migrate_one_reflog_entry(const char *refname,
2985 + struct object_id *old_oid,
2986 struct object_id *new_oid,
2987 const char *committer,
2988 timestamp_t timestamp, int tz,
@@ -3006,7 +3005,7 @@ static int migrate_one_reflog_entry(struct object_id *old_oid,
3005 strbuf_reset(data->sb);
3006 strbuf_addstr(data->sb, fmt_ident(data->name->buf, data->mail->buf, WANT_BLANK_IDENT, date, 0));
3007
3009 - ret = ref_transaction_update_reflog(data->transaction, data->refname,
3008 + ret = ref_transaction_update_reflog(data->transaction, refname,
3009 new_oid, old_oid, data->sb->buf,
3010 msg, data->index++, data->errbuf);
3011 return ret;
@@ -3016,7 +3015,6 @@ static int migrate_one_reflog(const char *refname, void *cb_data)
3015 {
3016 struct migration_data *migration_data = cb_data;
3017 struct reflog_migration_data data = {
3019 - .refname = refname,
3018 .old_refs = migration_data->old_refs,
3019 .transaction = migration_data->transaction,
3020 .errbuf = migration_data->errbuf,
refs.h
+7 -4
@@ -558,10 +558,13 @@ int refs_delete_reflog(struct ref_store *refs, const char *refname);
558 * The cb_data is a caller-supplied pointer given to the iterator
559 * functions.
560 */
561 -typedef int each_reflog_ent_fn(
562 - struct object_id *old_oid, struct object_id *new_oid,
563 - const char *committer, timestamp_t timestamp,
564 - int tz, const char *msg, void *cb_data);
561 +typedef int each_reflog_ent_fn(const char *refname,
562 + struct object_id *old_oid,
563 + struct object_id *new_oid,
564 + const char *committer,
565 + timestamp_t timestamp,
566 + int tz, const char *msg,
567 + void *cb_data);
568
569 /* Iterate over reflog entries in the log for `refname`. */
570
refs/debug.c
+3 -2
@@ -276,7 +276,8 @@ struct debug_reflog {
276 void *cb_data;
277 };
278
279 -static int debug_print_reflog_ent(struct object_id *old_oid,
279 +static int debug_print_reflog_ent(const char *refname,
280 + struct object_id *old_oid,
281 struct object_id *new_oid,
282 const char *committer, timestamp_t timestamp,
283 int tz, const char *msg, void *cb_data)
@@ -291,7 +292,7 @@ static int debug_print_reflog_ent(struct object_id *old_oid,
292 if (new_oid)
293 oid_to_hex_r(n, new_oid);
294
294 - ret = dbg->fn(old_oid, new_oid, committer, timestamp, tz, msg,
295 + ret = dbg->fn(refname, old_oid, new_oid, committer, timestamp, tz, msg,
296 dbg->cb_data);
297 trace_printf_key(&trace_refs,
298 "reflog_ent %s (ret %d): %s -> %s, %s %ld \"%.*s\"\n",
refs/files-backend.c
+9 -6
@@ -2115,7 +2115,9 @@ static int files_delete_reflog(struct ref_store *ref_store,
2115 return ret;
2116 }
2117
2118 -static int show_one_reflog_ent(struct files_ref_store *refs, struct strbuf *sb,
2118 +static int show_one_reflog_ent(struct files_ref_store *refs,
2119 + const char *refname,
2120 + struct strbuf *sb,
2121 each_reflog_ent_fn fn, void *cb_data)
2122 {
2123 struct object_id ooid, noid;
@@ -2142,7 +2144,7 @@ static int show_one_reflog_ent(struct files_ref_store *refs, struct strbuf *sb,
2144 message += 6;
2145 else
2146 message += 7;
2145 - return fn(&ooid, &noid, p, timestamp, tz, message, cb_data);
2147 + return fn(refname, &ooid, &noid, p, timestamp, tz, message, cb_data);
2148 }
2149
2150 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,
2228 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
2229 scanp = bp;
2230 endp = bp + 1;
2229 - ret = show_one_reflog_ent(refs, &sb, fn, cb_data);
2231 + ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
2232 strbuf_reset(&sb);
2233 if (ret)
2234 break;
@@ -2238,7 +2240,7 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
2240 * Process it, and we can end the loop.
2241 */
2242 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2241 - ret = show_one_reflog_ent(refs, &sb, fn, cb_data);
2243 + ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
2244 strbuf_reset(&sb);
2245 break;
2246 }
@@ -2288,7 +2290,7 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store,
2290 return -1;
2291
2292 while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
2291 - ret = show_one_reflog_ent(refs, &sb, fn, cb_data);
2293 + ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
2294 fclose(logfp);
2295 strbuf_release(&sb);
2296 return ret;
@@ -3359,7 +3361,8 @@ struct expire_reflog_cb {
3361 dry_run:1;
3362 };
3363
3362 -static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
3364 +static int expire_reflog_ent(const char *refname UNUSED,
3365 + struct object_id *ooid, struct object_id *noid,
3366 const char *email, timestamp_t timestamp, int tz,
3367 const char *message, void *cb_data)
3368 {
refs/reftable-backend.c
+1 -1
@@ -2148,7 +2148,7 @@ static int yield_log_record(struct reftable_ref_store *refs,
2148
2149 full_committer = fmt_ident(log->value.update.name, log->value.update.email,
2150 WANT_COMMITTER_IDENT, NULL, IDENT_NO_DATE);
2151 - return fn(&old_oid, &new_oid, full_committer,
2151 + return fn(log->refname, &old_oid, &new_oid, full_committer,
2152 log->value.update.time, log->value.update.tz_offset,
2153 log->value.update.message, cb_data);
2154 }
remote.c
+4 -2
@@ -2578,7 +2578,8 @@ struct check_and_collect_until_cb_data {
2578 };
2579
2580 /* Get the timestamp of the latest entry. */
2581 -static int peek_reflog(struct object_id *o_oid UNUSED,
2581 +static int peek_reflog(const char *refname UNUSED,
2582 + struct object_id *o_oid UNUSED,
2583 struct object_id *n_oid UNUSED,
2584 const char *ident UNUSED,
2585 timestamp_t timestamp, int tz UNUSED,
@@ -2589,7 +2590,8 @@ static int peek_reflog(struct object_id *o_oid UNUSED,
2590 return 1;
2591 }
2592
2592 -static int check_and_collect_until(struct object_id *o_oid UNUSED,
2593 +static int check_and_collect_until(const char *refname UNUSED,
2594 + struct object_id *o_oid UNUSED,
2595 struct object_id *n_oid,
2596 const char *ident UNUSED,
2597 timestamp_t timestamp, int tz UNUSED,
revision.c
+2 -1
@@ -1699,7 +1699,8 @@ static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1699 }
1700 }
1701
1702 -static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1702 +static int handle_one_reflog_ent(const char *refname UNUSED,
1703 + struct object_id *ooid, struct object_id *noid,
1704 const char *email UNUSED,
1705 timestamp_t timestamp UNUSED,
1706 int tz UNUSED,
t/helper/test-ref-store.c
+2 -1
@@ -215,7 +215,8 @@ static int cmd_for_each_reflog(struct ref_store *refs,
215 return refs_for_each_reflog(refs, each_reflog, NULL);
216 }
217
218 -static int each_reflog_ent(struct object_id *old_oid, struct object_id *new_oid,
218 +static int each_reflog_ent(const char *refname UNUSED,
219 + struct object_id *old_oid, struct object_id *new_oid,
220 const char *committer, timestamp_t timestamp,
221 int tz, const char *msg, void *cb_data UNUSED)
222 {
wt-status.c
+4 -2
@@ -972,7 +972,8 @@ static void wt_longstatus_print_changed(struct wt_status *s)
972 wt_longstatus_print_trailer(s);
973 }
974
975 -static int stash_count_refs(struct object_id *ooid UNUSED,
975 +static int stash_count_refs(const char *refname UNUSED,
976 + struct object_id *ooid UNUSED,
977 struct object_id *noid UNUSED,
978 const char *email UNUSED,
979 timestamp_t timestamp UNUSED, int tz UNUSED,
@@ -1664,7 +1665,8 @@ struct grab_1st_switch_cbdata {
1665 struct object_id noid;
1666 };
1667
1667 -static int grab_1st_switch(struct object_id *ooid UNUSED,
1668 +static int grab_1st_switch(const char *refname UNUSED,
1669 + struct object_id *ooid UNUSED,
1670 struct object_id *noid,
1671 const char *email UNUSED,
1672 timestamp_t timestamp UNUSED, int tz UNUSED,