Skip to content

Commit

Permalink
branch: allow -f with -m and -d
Browse files Browse the repository at this point in the history
-f/--force is the standard way to force an action, and is used by branch
for the recreation of existing branches, but not for deleting unmerged
branches nor for renaming to an existing branch.

Make "-m -f" equivalent to "-M" and "-d -f" equivalent to" -D", i.e.
allow -f/--force to be used with -m/-d also.

For the list modes, "-f" is simply ignored.

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Michael J Gruber authored and Junio C Hamano committed Dec 10, 2014
1 parent ff7aa81 commit 356e91f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 9 additions & 4 deletions builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ static int edit_branch_description(const char *branch_name)

int cmd_branch(int argc, const char **argv, const char *prefix)
{
int delete = 0, rename = 0, force_create = 0, list = 0;
int delete = 0, rename = 0, force = 0, list = 0;
int verbose = 0, abbrev = -1, detached = 0;
int reflog = 0, edit_description = 0;
int quiet = 0, unset_upstream = 0;
Expand Down Expand Up @@ -838,7 +838,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
OPT_BOOL('l', "create-reflog", &reflog, N_("create the branch's reflog")),
OPT_BOOL(0, "edit-description", &edit_description,
N_("edit the description for the branch")),
OPT__FORCE(&force_create, N_("force creation (when already exists)")),
OPT__FORCE(&force, N_("force creation, move/rename, deletion")),
{
OPTION_CALLBACK, 0, "no-merged", &merge_filter_ref,
N_("commit"), N_("print only not merged branches"),
Expand Down Expand Up @@ -884,7 +884,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
if (with_commit || merge_filter != NO_FILTER)
list = 1;

if (!!delete + !!rename + !!force_create + !!new_upstream +
if (!!delete + !!rename + !!new_upstream +
list + unset_upstream > 1)
usage_with_options(builtin_branch_usage, options);

Expand All @@ -897,6 +897,11 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
colopts = 0;
}

if (force) {
delete *= 2;
rename *= 2;
}

if (delete) {
if (!argc)
die(_("branch name required"));
Expand Down Expand Up @@ -1013,7 +1018,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)

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

/*
* We only show the instructions if the user gave us
Expand Down
5 changes: 5 additions & 0 deletions t/t3200-branch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
git branch -M o/q o/p
'

test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
git branch o/q &&
git branch -m -f o/q o/p
'

test_expect_success 'git branch -m q r/q should fail when r exists' '
git branch q &&
git branch r &&
Expand Down

0 comments on commit 356e91f

Please sign in to comment.