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>
main
Patrick Steinhardt 2025-07-31 16:56:49 +02:00 committed by Junio C Hamano
parent cf03815537
commit b9fd73a234
17 changed files with 63 additions and 45 deletions

View 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;
}

View 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)
{

View 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,

View File

@ -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)

View 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,

View File

@ -22,7 +22,8 @@ struct complete_reflogs {
int nr, alloc;
};

static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
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)
{

View File

@ -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,

View File

@ -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,

20
refs.c
View File

@ -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,

11
refs.h
View File

@ -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`. */


View 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",

View 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)
{

View 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);
}

View File

@ -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,

View 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,

View 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)
{

View 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,