reflog delete: narrow scope of "cmd" passed to count_reflog_ent()

Change the "cb_data" we pass to the count_reflog_ent() to be the &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-loop that uses &cb, except for the "cmd" member. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Dec 22, 2021 at 05:06 UTC 4a0339b36fa48c2bd8dd3becbe87bd59a958b306
1 file changed +11 -11
builtin/reflog.c
+11 -11
@@ -662,20 +662,18 @@ static int count_reflog_ent(struct object_id *ooid, struct object_id *noid,
662 const char *email, timestamp_t timestamp, int tz,
663 const char *message, void *cb_data)
664 {
665 - struct expire_reflog_policy_cb *cb = cb_data;
666 - if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total)
667 - cb->cmd.recno++;
665 + struct cmd_reflog_expire_cb *cb = cb_data;
666 + if (!cb->expire_total || timestamp < cb->expire_total)
667 + cb->recno++;
668 return 0;
669 }
670
671 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
672 {
673 - struct expire_reflog_policy_cb cb;
673 + struct cmd_reflog_expire_cb cmd = { 0 };
674 int i, status = 0;
675 unsigned int flags = 0;
676
677 - memset(&cb, 0, sizeof(cb));
678 -
677 for (i = 1; i < argc; i++) {
678 const char *arg = argv[i];
679 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
@@ -703,6 +701,7 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
701 const char *spec = strstr(argv[i], "@{");
702 char *ep, *ref;
703 int recno;
704 + struct expire_reflog_policy_cb cb = { 0 };
705
706 if (!spec) {
707 status |= error(_("not a reflog: %s"), argv[i]);
@@ -716,14 +715,15 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
715
716 recno = strtoul(spec + 2, &ep, 10);
717 if (*ep == '}') {
719 - cb.cmd.recno = -recno;
720 - for_each_reflog_ent(ref, count_reflog_ent, &cb);
718 + cmd.recno = -recno;
719 + for_each_reflog_ent(ref, count_reflog_ent, &cmd);
720 } else {
722 - cb.cmd.expire_total = approxidate(spec + 2);
723 - for_each_reflog_ent(ref, count_reflog_ent, &cb);
724 - cb.cmd.expire_total = 0;
721 + cmd.expire_total = approxidate(spec + 2);
722 + for_each_reflog_ent(ref, count_reflog_ent, &cmd);
723 + cmd.expire_total = 0;
724 }
725
726 + cb.cmd = cmd;
727 status |= reflog_expire(ref, flags,
728 reflog_expiry_prepare,
729 should_expire_reflog_ent,