builtin/reflog: say "would prune" for named refs with --dry-run

With --dry-run --verbose, "git reflog expire --all" reports the entries
it would remove as "would prune", but when refs are named on the command
line it reports them as "prune", which looks as if they were removed:

    $ git reflog expire --dry-run --verbose --expire=now HEAD
    prune commit (initial): first

Nothing is removed in either case, only the label is wrong. This was
reported at [1].

fcd2c3d9d8 (reflog + refs-backend: move "verbose" out of the backend,
2021-12-22) added dry_run to struct expire_reflog_policy_cb and set it
in the --all loop, but not in the loop for named refs. Set it there
too.

Add tests for named refs and --all. No test checked the keep/prune
wording before.

[1] https://lore.kernel.org/git/b46cbce8-d2bb-46d2-bdd3-545422813d6b@tngtech.com/

Reported-by: Sven Weiland <sven.weiland@tngtech.com>
Assisted-by: Claude Sonnet 5.5
Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Laveesh Bansal authored and Junio C Hamano committed 2026-10-24 10:39:31 -07:00
1 parent 7e34d8174d
commit d1536ac3e2
2 files changed
+38 -1

No files matched your search

+4 -1
View File
@@ -279,7 +279,10 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
for (i = 0; i < argc; i++) {
char *ref;
struct expire_reflog_policy_cb cb = { .opts = opts };
struct expire_reflog_policy_cb cb = {
.opts = opts,
.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
};
if (!repo_dwim_log(the_repository, argv[i], strlen(argv[i]), NULL, &ref)) {
status |= error(_("reflog could not be found: '%s'"), argv[i]);
+34
View File
@@ -240,6 +240,40 @@ test_expect_success 'reflog expire --dry-run should not touch reflog' '
check_fsck "missing blob $F"
'
# A dry run only ever says "keep" or "would prune".
check_dry_run_output () {
test_grep "^would prune " "$1" &&
sed -e "/^keep /d" -e "/^would prune /d" \
-e "/^Marking reachable objects/d" "$1" >unexpected &&
test_must_be_empty unexpected
}
test_expect_success 'reflog expire --dry-run --verbose with a named ref' '
git reflog refs/heads/main >before &&
git reflog expire --dry-run --verbose \
--expire=$(($test_tick - 10000)) \
--expire-unreachable=$(($test_tick - 10000)) \
--stale-fix \
refs/heads/main >output &&
check_dry_run_output output &&
git reflog refs/heads/main >after &&
test_cmp before after
'
test_expect_success 'reflog expire --dry-run --verbose --all' '
git reflog refs/heads/main >before &&
git reflog expire --dry-run --verbose \
--expire=$(($test_tick - 10000)) \
--expire-unreachable=$(($test_tick - 10000)) \
--stale-fix \
--all >output &&
check_dry_run_output output &&
git reflog refs/heads/main >after &&
test_cmp before after
'
test_expect_success 'reflog expire' '
git reflog expire --verbose \