Skip to content

Commit 46fbe41

Browse files
avargitster
authored andcommitted
reflog expire: narrow scope of "cb" in cmd_reflog_expire()
As with the preceding change for "reflog delete", change the "cb_data" we pass to callbacks to be &cb.cmd itself, instead of passing &cb and having the callback lookup cb->cmd. This makes it clear that the "cb" itself is the same memzero'd structure on each iteration of the for-loops that use &cb, except for the "cmd" member. The "struct expire_reflog_policy_cb" we pass to reflog_expire() will have the members that aren't "cmd" modified by the callbacks, but before we invoke them everything except "cmd" is zero'd out. This included the "tip_commit", "mark_list" and "tips". It might have looked as though we were re-using those between iterations, but the first thing we did in reflog_expiry_prepare() was to either NULL them, or clobber them with another value. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4a0339b commit 46fbe41

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

builtin/reflog.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ static void reflog_expiry_prepare(const char *refname,
357357
struct expire_reflog_policy_cb *cb = cb_data;
358358

359359
if (!cb->cmd.expire_unreachable || is_head(refname)) {
360-
cb->tip_commit = NULL;
361360
cb->unreachable_expire_kind = UE_HEAD;
362361
} else {
363362
cb->tip_commit = lookup_commit_reference_gently(the_repository,
@@ -371,8 +370,6 @@ static void reflog_expiry_prepare(const char *refname,
371370
if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
372371
cb->unreachable_expire_kind = UE_ALWAYS;
373372

374-
cb->mark_list = NULL;
375-
cb->tips = NULL;
376373
if (cb->unreachable_expire_kind != UE_ALWAYS) {
377374
if (cb->unreachable_expire_kind == UE_HEAD) {
378375
struct commit_list *elem;
@@ -541,7 +538,7 @@ static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, c
541538

542539
static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
543540
{
544-
struct expire_reflog_policy_cb cb;
541+
struct cmd_reflog_expire_cb cmd = { 0 };
545542
timestamp_t now = time(NULL);
546543
int i, status, do_all, all_worktrees = 1;
547544
int explicit_expiry = 0;
@@ -553,28 +550,27 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
553550

554551
save_commit_buffer = 0;
555552
do_all = status = 0;
556-
memset(&cb, 0, sizeof(cb));
557553

558-
cb.cmd.expire_total = default_reflog_expire;
559-
cb.cmd.expire_unreachable = default_reflog_expire_unreachable;
554+
cmd.expire_total = default_reflog_expire;
555+
cmd.expire_unreachable = default_reflog_expire_unreachable;
560556

561557
for (i = 1; i < argc; i++) {
562558
const char *arg = argv[i];
563559

564560
if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
565561
flags |= EXPIRE_REFLOGS_DRY_RUN;
566562
else if (skip_prefix(arg, "--expire=", &arg)) {
567-
if (parse_expiry_date(arg, &cb.cmd.expire_total))
563+
if (parse_expiry_date(arg, &cmd.expire_total))
568564
die(_("'%s' is not a valid timestamp"), arg);
569565
explicit_expiry |= EXPIRE_TOTAL;
570566
}
571567
else if (skip_prefix(arg, "--expire-unreachable=", &arg)) {
572-
if (parse_expiry_date(arg, &cb.cmd.expire_unreachable))
568+
if (parse_expiry_date(arg, &cmd.expire_unreachable))
573569
die(_("'%s' is not a valid timestamp"), arg);
574570
explicit_expiry |= EXPIRE_UNREACH;
575571
}
576572
else if (!strcmp(arg, "--stale-fix"))
577-
cb.cmd.stalefix = 1;
573+
cmd.stalefix = 1;
578574
else if (!strcmp(arg, "--rewrite"))
579575
flags |= EXPIRE_REFLOGS_REWRITE;
580576
else if (!strcmp(arg, "--updateref"))
@@ -600,14 +596,14 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
600596
* even in older repository. We cannot trust what's reachable
601597
* from reflog if the repository was pruned with older git.
602598
*/
603-
if (cb.cmd.stalefix) {
604-
repo_init_revisions(the_repository, &cb.cmd.revs, prefix);
605-
cb.cmd.revs.do_not_die_on_missing_tree = 1;
606-
cb.cmd.revs.ignore_missing = 1;
607-
cb.cmd.revs.ignore_missing_links = 1;
599+
if (cmd.stalefix) {
600+
repo_init_revisions(the_repository, &cmd.revs, prefix);
601+
cmd.revs.do_not_die_on_missing_tree = 1;
602+
cmd.revs.ignore_missing = 1;
603+
cmd.revs.ignore_missing_links = 1;
608604
if (flags & EXPIRE_REFLOGS_VERBOSE)
609605
printf(_("Marking reachable objects..."));
610-
mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL);
606+
mark_reachable_objects(&cmd.revs, 0, 0, NULL);
611607
if (flags & EXPIRE_REFLOGS_VERBOSE)
612608
putchar('\n');
613609
}
@@ -629,6 +625,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
629625
free_worktrees(worktrees);
630626
for (i = 0; i < collected.nr; i++) {
631627
struct collected_reflog *e = collected.e[i];
628+
struct expire_reflog_policy_cb cb = { .cmd = cmd };
632629

633630
set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
634631
status |= reflog_expire(e->reflog, flags,
@@ -643,6 +640,8 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
643640

644641
for (; i < argc; i++) {
645642
char *ref;
643+
struct expire_reflog_policy_cb cb = { .cmd = cmd };
644+
646645
if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) {
647646
status |= error(_("%s points nowhere!"), argv[i]);
648647
continue;

0 commit comments

Comments
 (0)