[PATCH v2 2/2] builtin/reflog: expire each reflog only once
- From
- Laveesh Bansal <laveeshb@laveeshbansal.com>
- Date
- Nov 10, 2026, 15:26 UTC
- Message-ID
- <0e5d78f552deaf3bd0ba2ec70666cebc2957c9e4.1794324064.git.laveeshb@laveeshbansal.com>
- In-Reply-To
- <cover.1794324064.git.laveeshb@laveeshbansal.com>
"git reflog expire" processes a reflog once for every time it is named, and reports it every time:
$ git reflog expire --dry-run --verbose --expire=now main main
would prune commit (initial): one
would prune commit (initial): oneThe same happens for two names of the same ref, such as "main" and "refs/heads/main", and for a name that --all already covers.
Skip a reflog that this run has already processed.
Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com> ---
Notes:
With --updateref, the first pass can move the ref, so a second pass
over the same reflog could prune entries that the new tip no longer
reaches. It now ends as if the name had been given once.builtin/reflog.c | 8 ++++++++ t/t1410-reflog.sh | 14 ++++++++++++++ 2 files changed, 22 insertions(+)
diff --git a/builtin/reflog.c b/builtin/reflog.c index 42d37a3724..4221136298 100644 --- a/builtin/reflog.c +++ b/builtin/reflog.c @@ -190,6 +190,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix, struct worktree_reflogs collected = { .reflogs = STRING_LIST_INIT_DUP, }; + struct string_list seen = STRING_LIST_INIT_DUP; struct string_list_item *item; const struct option options[] = { OPT_BIT('n', "dry-run", &flags, N_("do not actually prune any entries"), @@ -285,6 +286,12 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix, if (ref) refname = ref; + if (string_list_has_string(&seen, refname)) { + free(ref); + continue; + } + string_list_insert(&seen, refname); + reflog_expire_options_set_refname(&cb.opts, refname); status |= refs_reflog_expire(get_main_ref_store(the_repository), refname, flags, @@ -295,6 +302,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix, free(ref); } string_list_clear(&collected.reflogs, 0); + string_list_clear(&seen, 0); reflog_clear_expire_config(&opts); diff --git a/t/t1410-reflog.sh b/t/t1410-reflog.sh index 998a7e8667..d22216c36d 100755 --- a/t/t1410-reflog.sh +++ b/t/t1410-reflog.sh @@ -274,6 +274,20 @@ test_expect_success 'reflog expire --dry-run --verbose --all' ' test_cmp before after ' +test_expect_success 'reflog expire does not process a reflog twice' ' + git reflog expire --dry-run --verbose \ + --expire=$(($test_tick - 10000)) \ + --expire-unreachable=$(($test_tick - 10000)) \ + --stale-fix \ + --all >once && + git reflog expire --dry-run --verbose \ + --expire=$(($test_tick - 10000)) \ + --expire-unreachable=$(($test_tick - 10000)) \ + --stale-fix \ + --all main refs/heads/main main >twice && + test_cmp once twice +' + test_expect_success 'reflog expire' ' git reflog expire --verbose \
-- 2.54.0