]> git.ipfire.org Git - thirdparty/git.git/commitdiff
branch: add --dry-run for --prune-merged
authorHarald Nordgren <haraldnordgren@gmail.com>
Wed, 3 Jun 2026 09:04:39 +0000 (09:04 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 3 Jun 2026 10:10:05 +0000 (19:10 +0900)
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.

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>
Documentation/git-branch.adoc
builtin/branch.c
t/t3200-branch.sh

index 69878549fc4ec862015fe86638f2250c0531c022..c579df4fe04e060edab84a57c93d114bf2849a4d 100644 (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 (--prune-merged <branch>)...
+git branch [--dry-run] (--prune-merged <branch>)...
 
 DESCRIPTION
 -----------
@@ -230,6 +230,12 @@ Branches refused by the "fully merged" safety check are listed as
 warnings and skipped; pass them to `git branch -D` explicitly if
 you want them gone.
 
+`--dry-run`::
+       With `--prune-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`::
index e03805a8a706790191a606effe994df1f94d980a..1811511b9ebc79af44c51200eb032dc0fb143323 100644 (file)
@@ -860,7 +860,7 @@ static void collect_forked_set(const struct string_list *upstreams,
 }
 
 static int prune_merged_branches(const struct string_list *upstreams,
-                                int quiet)
+                                int quiet, int dry_run)
 {
        struct ref_store *refs = get_main_ref_store(the_repository);
        struct string_list candidates = STRING_LIST_INIT_DUP;
@@ -917,7 +917,7 @@ static int prune_merged_branches(const struct string_list *upstreams,
                                      quiet,
                                      1, /* warn_only */
                                      1, /* no_head_fallback */
-                                     0  /* dry_run */);
+                                     dry_run);
 
        strvec_clear(&deletable);
        string_list_clear(&candidates, 0);
@@ -967,6 +967,7 @@ int cmd_branch(int argc,
            unset_upstream = 0, show_current = 0, edit_description = 0;
        struct string_list forked_upstreams = STRING_LIST_INIT_DUP;
        struct string_list prune_merged_upstreams = STRING_LIST_INIT_DUP;
+       int dry_run = 0;
        const char *new_upstream = NULL;
        int noncreate_actions = 0;
        /* possible options */
@@ -1024,6 +1025,8 @@ int cmd_branch(int argc,
                        N_("list local branches whose upstream matches <branch> (repeatable)")),
                OPT_STRING_LIST(0, "prune-merged", &prune_merged_upstreams, N_("branch"),
                        N_("delete local branches whose upstream matches <branch> and is merged (repeatable)")),
+               OPT_BOOL(0, "dry-run", &dry_run,
+                       N_("with --prune-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")),
@@ -1083,6 +1086,9 @@ int cmd_branch(int argc,
        if (noncreate_actions > 1)
                usage_with_options(builtin_branch_usage, options);
 
+       if (dry_run && !prune_merged_upstreams.nr)
+               die(_("--dry-run requires --prune-merged"));
+
        if (recurse_submodules_explicit) {
                if (!submodule_propagate_branches)
                        die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled"));
@@ -1124,7 +1130,7 @@ int cmd_branch(int argc,
                if (argc)
                        die(_("--prune-merged does not take positional arguments; "
                              "repeat --prune-merged for each <branch>"));
-               ret = prune_merged_branches(&prune_merged_upstreams, quiet);
+               ret = prune_merged_branches(&prune_merged_upstreams, quiet, dry_run);
                goto out;
        } else if (show_current) {
                print_current_branch_name();
index 9e331795901d102d0cc64fc70d9e52150f757923..29bfd0e109121897bb4ce08411e741667ca33465 100755 (executable)
@@ -2027,4 +2027,48 @@ test_expect_success 'branch -d still deletes a pruneMerged=false branch' '
        test_must_fail git -C pm-optout-d rev-parse --verify refs/heads/one
 '
 
+test_expect_success '--prune-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 --prune-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 '--prune-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 --prune-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 --prune-merged is rejected' '
+       test_must_fail git -C forked branch --dry-run 2>err &&
+       test_grep "requires --prune-merged" err
+'
+
 test_done