Skip to content

Commit 15a171f

Browse files
committed
Merge branch 'mg/branch-d-m-f'
"git branch -d" (delete) and "git branch -m" (move) learned to honor "-f" (force) flag; unlike many other subcommands, the way to force these have been with separate "-D/-M" options, which was inconsistent. * mg/branch-d-m-f: branch: allow -f with -m and -d t3200-branch: test -M
2 parents 00c194a + 356e91f commit 15a171f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

‎builtin/branch.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ static int edit_branch_description(const char *branch_name)
800800

801801
int cmd_branch(int argc, const char **argv, const char *prefix)
802802
{
803-
int delete = 0, rename = 0, force_create = 0, list = 0;
803+
int delete = 0, rename = 0, force = 0, list = 0;
804804
int verbose = 0, abbrev = -1, detached = 0;
805805
int reflog = 0, edit_description = 0;
806806
int quiet = 0, unset_upstream = 0;
@@ -848,7 +848,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
848848
OPT_BOOL('l', "create-reflog", &reflog, N_("create the branch's reflog")),
849849
OPT_BOOL(0, "edit-description", &edit_description,
850850
N_("edit the description for the branch")),
851-
OPT__FORCE(&force_create, N_("force creation (when already exists)")),
851+
OPT__FORCE(&force, N_("force creation, move/rename, deletion")),
852852
{
853853
OPTION_CALLBACK, 0, "no-merged", &merge_filter_ref,
854854
N_("commit"), N_("print only not merged branches"),
@@ -891,7 +891,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
891891
if (with_commit || merge_filter != NO_FILTER)
892892
list = 1;
893893

894-
if (!!delete + !!rename + !!force_create + !!new_upstream +
894+
if (!!delete + !!rename + !!new_upstream +
895895
list + unset_upstream > 1)
896896
usage_with_options(builtin_branch_usage, options);
897897

@@ -904,6 +904,11 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
904904
colopts = 0;
905905
}
906906

907+
if (force) {
908+
delete *= 2;
909+
rename *= 2;
910+
}
911+
907912
if (delete) {
908913
if (!argc)
909914
die(_("branch name required"));
@@ -1020,7 +1025,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
10201025

10211026
branch_existed = ref_exists(branch->refname);
10221027
create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
1023-
force_create, reflog, 0, quiet, track);
1028+
force, reflog, 0, quiet, track);
10241029

10251030
/*
10261031
* We only show the instructions if the user gave us

‎t/t3200-branch.sh‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ test_expect_success 'git branch -m o/o o should fail when o/p exists' '
9797
test_must_fail git branch -m o/o o
9898
'
9999

100+
test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
101+
git branch o/q &&
102+
test_must_fail git branch -m o/q o/p
103+
'
104+
105+
test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
106+
git branch -M o/q o/p
107+
'
108+
109+
test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
110+
git branch o/q &&
111+
git branch -m -f o/q o/p
112+
'
113+
100114
test_expect_success 'git branch -m q r/q should fail when r exists' '
101115
git branch q &&
102116
git branch r &&

0 commit comments

Comments
 (0)