Skip to content

Commit 511de47

Browse files
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 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 --prune-merged and is rejected otherwise. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
1 parent 8e9a735 commit 511de47

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 (-m|-M) [<old-branch>] <new-branch>
2525
git branch (-c|-C) [<old-branch>] <new-branch>
2626
git branch (-d|-D) [-r] <branch-name>...
2727
git branch --edit-description [<branch-name>]
28-
git branch --prune-merged <branch>...
28+
git branch [--dry-run] --prune-merged <branch>...
2929

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

229+
`--dry-run`::
230+
With `--prune-merged`, print which branches would be
231+
deleted and exit without touching any ref. Useful for
232+
sanity-checking a wide pattern like `'origin/*'` before
233+
committing to the deletion.
234+
229235
`-v`::
230236
`-vv`::
231237
`--verbose`::

builtin/branch.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ static int parse_opt_forked(const struct option *opt, const char *arg, int unset
715715
}
716716

717717
static int prune_merged_branches(int argc, const char **argv,
718-
int quiet)
718+
int quiet, int dry_run)
719719
{
720720
struct ref_store *refs = get_main_ref_store(the_repository);
721721
struct ref_filter filter = REF_FILTER_INIT;
@@ -775,7 +775,8 @@ static int prune_merged_branches(int argc, const char **argv,
775775
FILTER_REFS_BRANCHES,
776776
DELETE_BRANCH_WARN_ONLY |
777777
DELETE_BRANCH_NO_HEAD_FALLBACK |
778-
(quiet ? DELETE_BRANCH_QUIET : 0));
778+
(quiet ? DELETE_BRANCH_QUIET : 0) |
779+
(dry_run ? DELETE_BRANCH_DRY_RUN : 0));
779780

780781
strvec_clear(&deletable);
781782
ref_array_clear(&candidates);
@@ -825,6 +826,7 @@ int cmd_branch(int argc,
825826
int delete = 0, rename = 0, copy = 0, list = 0,
826827
unset_upstream = 0, show_current = 0, edit_description = 0;
827828
int prune_merged = 0;
829+
int dry_run = 0;
828830
const char *new_upstream = NULL;
829831
int noncreate_actions = 0;
830832
/* possible options */
@@ -880,6 +882,8 @@ int cmd_branch(int argc,
880882
N_("edit the description for the branch")),
881883
OPT_BOOL(0, "prune-merged", &prune_merged,
882884
N_("delete local branches whose upstream matches <branch> and is merged")),
885+
OPT_BOOL(0, "dry-run", &dry_run,
886+
N_("with --prune-merged, only print which branches would be deleted")),
883887
OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE),
884888
OPT_MERGED(&filter, N_("print only branches that are merged")),
885889
OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
@@ -942,6 +946,9 @@ int cmd_branch(int argc,
942946
if (noncreate_actions > 1)
943947
usage_with_options(builtin_branch_usage, options);
944948

949+
if (dry_run && !prune_merged)
950+
die(_("--dry-run requires --prune-merged"));
951+
945952
if (recurse_submodules_explicit) {
946953
if (!submodule_propagate_branches)
947954
die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled"));
@@ -981,7 +988,7 @@ int cmd_branch(int argc,
981988
(quiet ? DELETE_BRANCH_QUIET : 0));
982989
goto out;
983990
} else if (prune_merged) {
984-
ret = prune_merged_branches(argc, argv, quiet);
991+
ret = prune_merged_branches(argc, argv, quiet, dry_run);
985992
goto out;
986993
} else if (show_current) {
987994
print_current_branch_name();

t/t3200-branch.sh

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

2043+
test_expect_success '--prune-merged --dry-run lists but does not delete' '
2044+
test_when_finished "rm -rf pm-dry" &&
2045+
git clone pm-upstream pm-dry &&
2046+
git -C pm-dry remote add fork ../pm-fork &&
2047+
test_config -C pm-dry remote.pushDefault fork &&
2048+
test_config -C pm-dry push.default current &&
2049+
git -C pm-dry branch one one-commit &&
2050+
git -C pm-dry branch --set-upstream-to=origin/next one &&
2051+
git -C pm-dry branch two two-commit &&
2052+
git -C pm-dry branch --set-upstream-to=origin/next two &&
2053+
2054+
git -C pm-dry branch --dry-run --prune-merged "origin/*" >actual &&
2055+
test_grep "Would delete branch one " actual &&
2056+
test_grep "Would delete branch two " actual &&
2057+
2058+
git -C pm-dry rev-parse --verify refs/heads/one &&
2059+
git -C pm-dry rev-parse --verify refs/heads/two
2060+
'
2061+
2062+
test_expect_success '--prune-merged --dry-run only lists branches the live run would delete' '
2063+
test_when_finished "rm -rf pm-dry-mixed" &&
2064+
git clone pm-upstream pm-dry-mixed &&
2065+
git -C pm-dry-mixed remote add fork ../pm-fork &&
2066+
test_config -C pm-dry-mixed remote.pushDefault fork &&
2067+
test_config -C pm-dry-mixed push.default current &&
2068+
git -C pm-dry-mixed checkout -b wip origin/next &&
2069+
git -C pm-dry-mixed branch --set-upstream-to=origin/next wip &&
2070+
test_commit -C pm-dry-mixed local-only &&
2071+
git -C pm-dry-mixed checkout - &&
2072+
git -C pm-dry-mixed branch merged one-commit &&
2073+
git -C pm-dry-mixed branch --set-upstream-to=origin/next merged &&
2074+
2075+
git -C pm-dry-mixed branch --dry-run --prune-merged "origin/*" >out &&
2076+
test_grep "Would delete branch merged" out &&
2077+
test_grep ! "Would delete branch wip" out &&
2078+
git -C pm-dry-mixed rev-parse --verify refs/heads/wip &&
2079+
git -C pm-dry-mixed rev-parse --verify refs/heads/merged
2080+
'
2081+
2082+
test_expect_success '--dry-run without --prune-merged is rejected' '
2083+
test_must_fail git -C forked branch --dry-run 2>err &&
2084+
test_grep "requires --prune-merged" err
2085+
'
2086+
20432087
test_done

0 commit comments

Comments
 (0)