branch: add --dry-run for --delete-merged
"git branch --dry-run --delete-merged ..." prints one line per ref that would be deleted without modifying refs or branch configuration. --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>jch
parent
4ef02bc803
commit
d1d403ccff
|
|
@ -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>)... [<pattern>...]
|
||||
git branch [--dry-run] (--delete-merged <branch>)... [<pattern>...]
|
||||
|
||||
DESCRIPTION
|
||||
-----------
|
||||
|
|
@ -232,6 +232,12 @@ A branch that a surviving branch depends on through a chain of local
|
|||
upstreams is kept, so a branch is never deleted out from under stacked
|
||||
work.
|
||||
|
||||
`--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`::
|
||||
|
|
|
|||
|
|
@ -199,6 +199,7 @@ enum delete_branch_flags {
|
|||
DELETE_BRANCH_QUIET = (1 << 1),
|
||||
DELETE_BRANCH_SKIP_UNMERGED = (1 << 2),
|
||||
DELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),
|
||||
DELETE_BRANCH_DRY_RUN = (1 << 4),
|
||||
};
|
||||
|
||||
static int check_branch_commit(const char *branchname, const char *refname,
|
||||
|
|
@ -340,13 +341,20 @@ static int delete_branches(int argc, const char **argv, int kinds,
|
|||
free(target);
|
||||
}
|
||||
|
||||
if (refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF))
|
||||
if (!(flags & DELETE_BRANCH_DRY_RUN) &&
|
||||
refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF))
|
||||
ret = 1;
|
||||
|
||||
for_each_string_list_item(item, &refs_to_delete) {
|
||||
char *describe_ref = item->util;
|
||||
char *name = item->string;
|
||||
if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {
|
||||
if (flags & DELETE_BRANCH_DRY_RUN) {
|
||||
if (!(flags & DELETE_BRANCH_QUIET))
|
||||
printf(remote_branch
|
||||
? _("Would delete remote-tracking branch %s (was %s).\n")
|
||||
: _("Would delete branch %s (was %s).\n"),
|
||||
name + branch_name_pos, describe_ref);
|
||||
} else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {
|
||||
char *refname = name + branch_name_pos;
|
||||
if (!(flags & DELETE_BRANCH_QUIET))
|
||||
printf(remote_branch
|
||||
|
|
@ -899,6 +907,7 @@ int cmd_branch(int argc,
|
|||
int delete = 0, rename = 0, copy = 0, list = 0,
|
||||
unset_upstream = 0, show_current = 0, edit_description = 0;
|
||||
struct strvec delete_merged = STRVEC_INIT;
|
||||
int dry_run = 0;
|
||||
const char *new_upstream = NULL;
|
||||
int noncreate_actions = 0;
|
||||
/* possible options */
|
||||
|
|
@ -955,6 +964,8 @@ int cmd_branch(int argc,
|
|||
OPT_CALLBACK_F(0, "delete-merged", &delete_merged, N_("branch"),
|
||||
N_("delete merged branches whose upstream matches <branch> (repeatable)"),
|
||||
PARSE_OPT_NONEG, parse_opt_strvec),
|
||||
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")),
|
||||
|
|
@ -1017,6 +1028,9 @@ int cmd_branch(int argc,
|
|||
if (noncreate_actions > 1)
|
||||
usage_with_options(builtin_branch_usage, options);
|
||||
|
||||
if (dry_run && !delete_merged.nr)
|
||||
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"));
|
||||
|
|
@ -1057,7 +1071,8 @@ int cmd_branch(int argc,
|
|||
goto out;
|
||||
} else if (delete_merged.nr) {
|
||||
ret = delete_merged_branches(&delete_merged, argv,
|
||||
quiet ? DELETE_BRANCH_QUIET : 0);
|
||||
(quiet ? DELETE_BRANCH_QUIET : 0) |
|
||||
(dry_run ? DELETE_BRANCH_DRY_RUN : 0));
|
||||
goto out;
|
||||
} else if (show_current) {
|
||||
print_current_branch_name();
|
||||
|
|
|
|||
|
|
@ -1900,6 +1900,19 @@ test_expect_success '--delete-merged deletes only selected merged branches' '
|
|||
git checkout -b tracks-other other/main --track &&
|
||||
sha=$(git rev-parse --short merged) &&
|
||||
|
||||
git branch --dry-run --delete-merged origin/next merged >actual 2>&1 &&
|
||||
echo "Would delete branch merged (was $sha)." >expect &&
|
||||
test_cmp expect actual &&
|
||||
git rev-parse --verify refs/heads/merged &&
|
||||
|
||||
check_branches <<-\EOF &&
|
||||
also-merged
|
||||
main
|
||||
merged
|
||||
tracks-other
|
||||
unmerged
|
||||
EOF
|
||||
|
||||
git branch --delete-merged origin/next merged >actual 2>&1 &&
|
||||
echo "Deleted branch merged (was $sha)." >expect &&
|
||||
test_cmp expect actual &&
|
||||
|
|
@ -1948,9 +1961,12 @@ test_expect_success '--delete-merged keeps the upstream of a surviving branch' '
|
|||
git checkout -b topic feature --track &&
|
||||
git commit --allow-empty -m "topic work" &&
|
||||
|
||||
git branch --delete-merged origin/next 2>err &&
|
||||
git branch --dry-run --delete-merged origin/next >out &&
|
||||
test_grep ! "feature" out &&
|
||||
|
||||
git branch --delete-merged origin/next 2>err &&
|
||||
test_must_be_empty err &&
|
||||
|
||||
check_branches <<-\EOF &&
|
||||
feature
|
||||
main
|
||||
|
|
@ -1978,6 +1994,21 @@ test_expect_success '--delete-merged keeps the upstream chain of a surviving bra
|
|||
git checkout -b tip mid --track &&
|
||||
git commit --allow-empty -m "tip work" &&
|
||||
|
||||
git branch --dry-run --delete-merged origin/next \
|
||||
--delete-merged lower >actual 2>&1 &&
|
||||
test_must_be_empty actual &&
|
||||
|
||||
git config --local --get-regexp "branch\\.(lower|mid|tip)\\.(merge|remote)" >actual &&
|
||||
cat >expect <<-\EOF &&
|
||||
branch.lower.remote origin
|
||||
branch.lower.merge refs/heads/next
|
||||
branch.mid.remote .
|
||||
branch.mid.merge refs/heads/lower
|
||||
branch.tip.remote .
|
||||
branch.tip.merge refs/heads/mid
|
||||
EOF
|
||||
test_cmp expect actual &&
|
||||
|
||||
git branch --delete-merged origin/next \
|
||||
--delete-merged lower >actual 2>&1 &&
|
||||
test_must_be_empty actual &&
|
||||
|
|
@ -2074,4 +2105,9 @@ test_expect_success "branch -d still deletes a deleteMerged=false branch" '
|
|||
)
|
||||
'
|
||||
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue