Skip to content

Commit 16c839f

Browse files
HaraldNordgrengitster
authored andcommitted
branch: add --dry-run for --prune-merged
With --dry-run, --prune-merged prints the local branches it would delete -- one "Would delete branch <name>" line per candidate -- and exits without touching any ref. This is the natural sanity check before letting a broad pattern like 'origin/*' run for real: the @{push}-vs-@{upstream} and unmerged filtering still applies, so the dry-run output is exactly the set that the live run would delete. --dry-run is only meaningful in combination with --prune-merged and is rejected otherwise. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent c6b3502 commit 16c839f

3 files changed

Lines changed: 61 additions & 4 deletions

File tree

Documentation/git-branch.adoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ git branch (-c|-C) [<old-branch>] <new-branch>
2525
git branch (-d|-D) [-r] <branch-name>...
2626
git branch --edit-description [<branch-name>]
2727
git branch --forked <branch>...
28-
git branch --prune-merged <branch>...
28+
git branch --prune-merged [--dry-run] <branch>...
2929

3030
DESCRIPTION
3131
-----------
@@ -235,6 +235,12 @@ Branches refused by the "fully merged" safety check are listed as
235235
warnings and skipped; pass them to `git branch -D` explicitly if
236236
you want them gone.
237237

238+
`--dry-run`::
239+
With `--prune-merged`, print which branches would be
240+
deleted and exit without touching any ref. Useful for
241+
sanity-checking a wide pattern like `'origin/*'` before
242+
committing to the deletion.
243+
238244
`-v`::
239245
`-vv`::
240246
`--verbose`::

builtin/branch.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,8 @@ static int list_forked_branches(int argc, const char **argv)
857857
return 0;
858858
}
859859

860-
static int prune_merged_branches(int argc, const char **argv, int quiet)
860+
static int prune_merged_branches(int argc, const char **argv, int quiet,
861+
int dry_run)
861862
{
862863
struct ref_store *refs = get_main_ref_store(the_repository);
863864
struct string_list candidates = STRING_LIST_INIT_DUP;
@@ -914,7 +915,7 @@ static int prune_merged_branches(int argc, const char **argv, int quiet)
914915
quiet,
915916
1, /* warn_only */
916917
1, /* no_head_fallback */
917-
0 /* dry_run */);
918+
dry_run);
918919

919920
strvec_clear(&deletable);
920921
string_list_clear(&candidates, 0);
@@ -964,6 +965,7 @@ int cmd_branch(int argc,
964965
unset_upstream = 0, show_current = 0, edit_description = 0;
965966
int forked = 0;
966967
int prune_merged = 0;
968+
int dry_run = 0;
967969
const char *new_upstream = NULL;
968970
int noncreate_actions = 0;
969971
/* possible options */
@@ -1021,6 +1023,8 @@ int cmd_branch(int argc,
10211023
N_("list local branches whose upstream matches the given <branch>...")),
10221024
OPT_BOOL(0, "prune-merged", &prune_merged,
10231025
N_("delete local branches whose upstream matches the given <branch>... and is merged")),
1026+
OPT_BOOL(0, "dry-run", &dry_run,
1027+
N_("with --prune-merged, only print which branches would be deleted")),
10241028
OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE),
10251029
OPT_MERGED(&filter, N_("print only branches that are merged")),
10261030
OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
@@ -1079,6 +1083,9 @@ int cmd_branch(int argc,
10791083
if (noncreate_actions > 1)
10801084
usage_with_options(builtin_branch_usage, options);
10811085

1086+
if (dry_run && !prune_merged)
1087+
die(_("--dry-run requires --prune-merged"));
1088+
10821089
if (recurse_submodules_explicit) {
10831090
if (!submodule_propagate_branches)
10841091
die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled"));
@@ -1120,7 +1127,7 @@ int cmd_branch(int argc,
11201127
ret = list_forked_branches(argc, argv);
11211128
goto out;
11221129
} else if (prune_merged) {
1123-
ret = prune_merged_branches(argc, argv, quiet);
1130+
ret = prune_merged_branches(argc, argv, quiet, dry_run);
11241131
goto out;
11251132
} else if (show_current) {
11261133
print_current_branch_name();

t/t3200-branch.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,4 +2020,48 @@ test_expect_success 'branch -d still deletes a pruneMerged=false branch' '
20202020
test_must_fail git -C pm-optout-d rev-parse --verify refs/heads/one
20212021
'
20222022

2023+
test_expect_success '--prune-merged --dry-run lists but does not delete' '
2024+
test_when_finished "rm -rf pm-dry" &&
2025+
git clone pm-upstream pm-dry &&
2026+
git -C pm-dry remote add fork ../pm-fork &&
2027+
test_config -C pm-dry remote.pushDefault fork &&
2028+
test_config -C pm-dry push.default current &&
2029+
git -C pm-dry branch one one-commit &&
2030+
git -C pm-dry branch --set-upstream-to=origin/next one &&
2031+
git -C pm-dry branch two two-commit &&
2032+
git -C pm-dry branch --set-upstream-to=origin/next two &&
2033+
2034+
git -C pm-dry branch --prune-merged --dry-run "origin/*" >actual &&
2035+
test_grep "Would delete branch one " actual &&
2036+
test_grep "Would delete branch two " actual &&
2037+
2038+
git -C pm-dry rev-parse --verify refs/heads/one &&
2039+
git -C pm-dry rev-parse --verify refs/heads/two
2040+
'
2041+
2042+
test_expect_success '--prune-merged --dry-run only lists branches the live run would delete' '
2043+
test_when_finished "rm -rf pm-dry-mixed" &&
2044+
git clone pm-upstream pm-dry-mixed &&
2045+
git -C pm-dry-mixed remote add fork ../pm-fork &&
2046+
test_config -C pm-dry-mixed remote.pushDefault fork &&
2047+
test_config -C pm-dry-mixed push.default current &&
2048+
git -C pm-dry-mixed checkout -b wip origin/next &&
2049+
git -C pm-dry-mixed branch --set-upstream-to=origin/next wip &&
2050+
test_commit -C pm-dry-mixed local-only &&
2051+
git -C pm-dry-mixed checkout - &&
2052+
git -C pm-dry-mixed branch merged one-commit &&
2053+
git -C pm-dry-mixed branch --set-upstream-to=origin/next merged &&
2054+
2055+
git -C pm-dry-mixed branch --prune-merged --dry-run "origin/*" >out &&
2056+
test_grep "Would delete branch merged" out &&
2057+
test_grep ! "Would delete branch wip" out &&
2058+
git -C pm-dry-mixed rev-parse --verify refs/heads/wip &&
2059+
git -C pm-dry-mixed rev-parse --verify refs/heads/merged
2060+
'
2061+
2062+
test_expect_success '--dry-run without --prune-merged is rejected' '
2063+
test_must_fail git -C forked branch --dry-run 2>err &&
2064+
test_grep "requires --prune-merged" err
2065+
'
2066+
20232067
test_done

0 commit comments

Comments
 (0)