branch: add --dry-run for --delete-merged

With --dry-run, --delete-merged prints the local branches it would
delete, one "Would delete branch <name>" line each, and exits
without touching any ref. The same filtering applies, so the output
is exactly the set that the real run would delete.

--dry-run is only meaningful together with --delete-merged and is
rejected otherwise.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Harald Nordgren
2026-06-15 16:47:22 +00:00
committed by Junio C Hamano
parent 99a37bb7ab
commit 609f13f80d
3 changed files with 61 additions and 4 deletions

View File

@@ -25,7 +25,7 @@ git branch (-m|-M) [<old-branch>] <new-branch>
git branch (-c|-C) [<old-branch>] <new-branch>
git branch (-d|-D) [-r] <branch-name>...
git branch --edit-description [<branch-name>]
git branch --delete-merged <branch>...
git branch [--dry-run] --delete-merged <branch>...
DESCRIPTION
-----------
@@ -226,6 +226,12 @@ A branch whose work has not yet been merged into its upstream is
silently skipped. Delete it with `git branch -D` if you want to
remove it anyway.
`--dry-run`::
With `--delete-merged`, print which branches would be
deleted and exit without touching any ref. Useful for
sanity-checking a wide pattern like `'origin/*'` before
committing to the deletion.
`-v`::
`-vv`::
`--verbose`::

View File

@@ -716,7 +716,7 @@ static int parse_opt_forked(const struct option *opt, const char *arg, int unset
}
static int delete_merged_branches(int argc, const char **argv,
int quiet)
int quiet, int dry_run)
{
struct ref_store *refs = get_main_ref_store(the_repository);
struct ref_filter filter = REF_FILTER_INIT;
@@ -775,7 +775,8 @@ static int delete_merged_branches(int argc, const char **argv,
FILTER_REFS_BRANCHES,
DELETE_BRANCH_SKIP_UNMERGED |
DELETE_BRANCH_NO_HEAD_FALLBACK |
(quiet ? DELETE_BRANCH_QUIET : 0));
(quiet ? DELETE_BRANCH_QUIET : 0) |
(dry_run ? DELETE_BRANCH_DRY_RUN : 0));
strvec_clear(&deletable);
ref_array_clear(&candidates);
@@ -825,6 +826,7 @@ int cmd_branch(int argc,
int delete = 0, rename = 0, copy = 0, list = 0,
unset_upstream = 0, show_current = 0, edit_description = 0;
int delete_merged = 0;
int dry_run = 0;
const char *new_upstream = NULL;
int noncreate_actions = 0;
/* possible options */
@@ -880,6 +882,8 @@ int cmd_branch(int argc,
N_("edit the description for the branch")),
OPT_BOOL(0, "delete-merged", &delete_merged,
N_("delete local branches whose upstream matches <branch> and are merged")),
OPT_BOOL(0, "dry-run", &dry_run,
N_("with --delete-merged, only print which branches would be deleted")),
OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE),
OPT_MERGED(&filter, N_("print only branches that are merged")),
OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
@@ -942,6 +946,9 @@ int cmd_branch(int argc,
if (noncreate_actions > 1)
usage_with_options(builtin_branch_usage, options);
if (dry_run && !delete_merged)
die(_("--dry-run requires --delete-merged"));
if (recurse_submodules_explicit) {
if (!submodule_propagate_branches)
die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled"));
@@ -981,7 +988,7 @@ int cmd_branch(int argc,
(quiet ? DELETE_BRANCH_QUIET : 0));
goto out;
} else if (delete_merged) {
ret = delete_merged_branches(argc, argv, quiet);
ret = delete_merged_branches(argc, argv, quiet, dry_run);
goto out;
} else if (show_current) {
print_current_branch_name();

View File

@@ -2060,4 +2060,48 @@ test_expect_success 'branch -d still deletes a deleteMerged=false branch' '
test_must_fail git -C pm-optout-d rev-parse --verify refs/heads/one
'
test_expect_success '--delete-merged --dry-run lists but does not delete' '
test_when_finished "rm -rf pm-dry" &&
git clone pm-upstream pm-dry &&
git -C pm-dry remote add fork ../pm-fork &&
test_config -C pm-dry remote.pushDefault fork &&
test_config -C pm-dry push.default current &&
git -C pm-dry branch one one-commit &&
git -C pm-dry branch --set-upstream-to=origin/next one &&
git -C pm-dry branch two two-commit &&
git -C pm-dry branch --set-upstream-to=origin/next two &&
git -C pm-dry branch --dry-run --delete-merged "origin/*" >actual &&
test_grep "Would delete branch one " actual &&
test_grep "Would delete branch two " actual &&
git -C pm-dry rev-parse --verify refs/heads/one &&
git -C pm-dry rev-parse --verify refs/heads/two
'
test_expect_success '--delete-merged --dry-run only lists branches the live run would delete' '
test_when_finished "rm -rf pm-dry-mixed" &&
git clone pm-upstream pm-dry-mixed &&
git -C pm-dry-mixed remote add fork ../pm-fork &&
test_config -C pm-dry-mixed remote.pushDefault fork &&
test_config -C pm-dry-mixed push.default current &&
git -C pm-dry-mixed checkout -b wip origin/next &&
git -C pm-dry-mixed branch --set-upstream-to=origin/next wip &&
test_commit -C pm-dry-mixed local-only &&
git -C pm-dry-mixed checkout - &&
git -C pm-dry-mixed branch merged one-commit &&
git -C pm-dry-mixed branch --set-upstream-to=origin/next merged &&
git -C pm-dry-mixed branch --dry-run --delete-merged "origin/*" >out &&
test_grep "Would delete branch merged" out &&
test_grep ! "Would delete branch wip" out &&
git -C pm-dry-mixed rev-parse --verify refs/heads/wip &&
git -C pm-dry-mixed rev-parse --verify refs/heads/merged
'
test_expect_success '--dry-run without --delete-merged is rejected' '
test_must_fail git -C forked branch --dry-run 2>err &&
test_grep "requires --delete-merged" err
'
test_done