# [RFC/PATCH 0/2] Make git branch -f forceful

9 messages from 2014-12-04 to 2014-12-08. Participants: Michael J Gruber, Junio C Hamano.
Thread: https://gitlist.dev/t/38109

## Michael J Gruber, 2014-12-04 13:26

Subject: [RFC/PATCH 0/2] Make git branch -f forceful
Message-ID: <cover.1417699299.git.git@drmicha.warpmail.net>
URL: https://gitlist.dev/e/cover.1417699299.git.git%40drmicha.warpmail.net

```
For many git commands, '-f/--force' is a way to force actions which
would otherwise error out. Way more than once, I've been trying this
with 'git branch -d' and 'git branch -m'...

I've had these two patches sitting in my tree for 3 years now it seems.
Here's a rebase.

Before applying these for food I should probably rename force_create to force.

Michael J Gruber (2):
  t3200-branch: test -M
  branch: allow -f with -m and -d

 builtin/branch.c  |  9 +++++++--
 t/t3200-branch.sh | 14 ++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

-- 
2.2.0.rc3.286.g888a711

```

## Michael J Gruber, 2014-12-04 13:26

Subject: [RFC/PATCH 1/2] t3200-branch: test -M
Message-ID: <4522a9a6606467e130c2430d717286c92c777b6f.1417699299.git.git@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4522a9a6606467e130c2430d717286c92c777b6f.1417699299.git.git%40drmicha.warpmail.net
In-Reply-To: <cover.1417699299.git.git@drmicha.warpmail.net>

```
Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
 t/t3200-branch.sh | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 432921b..0b3b8f5 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -97,6 +97,15 @@ test_expect_success 'git branch -m o/o o should fail when o/p exists' '
 	test_must_fail git branch -m o/o o
 '
 
+test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
+	git branch o/q &&
+	test_must_fail git branch -m o/q o/p
+'
+
+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 q r/q should fail when r exists' '
 	git branch q &&
 	git branch r &&
-- 
2.2.0.rc3.286.g888a711

```

## Michael J Gruber, 2014-12-04 13:26

Subject: [RFC/PATCH 2/2] branch: allow -f with -m and -d
Message-ID: <7b1ae15228841d3eaed739e89295ec10a2be45fa.1417699299.git.git@drmicha.warpmail.net>
URL: https://gitlist.dev/e/7b1ae15228841d3eaed739e89295ec10a2be45fa.1417699299.git.git%40drmicha.warpmail.net
In-Reply-To: <cover.1417699299.git.git@drmicha.warpmail.net>

```
-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.

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
 builtin/branch.c  | 9 +++++++--
 t/t3200-branch.sh | 5 +++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 3b79c50..8ea04d7 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -848,7 +848,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_create, N_("force creation, move/rename, deletion")),
 		{
 			OPTION_CALLBACK, 0, "no-merged", &merge_filter_ref,
 			N_("commit"), N_("print only not merged branches"),
@@ -891,7 +891,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);
 
@@ -904,6 +904,11 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		colopts = 0;
 	}
 
+	if (force_create) {
+		delete *= 2;
+		rename *= 2;
+	}
+
 	if (delete) {
 		if (!argc)
 			die(_("branch name required"));
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 0b3b8f5..ddea498 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -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 &&
-- 
2.2.0.rc3.286.g888a711

```

## Junio C Hamano, 2014-12-04 19:13

Subject: Re: [RFC/PATCH 2/2] branch: allow -f with -m and -d
Message-ID: <xmqqh9xbtehw.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqh9xbtehw.fsf%40gitster.dls.corp.google.com
In-Reply-To: <7b1ae15228841d3eaed739e89295ec10a2be45fa.1417699299.git.git@drmicha.warpmail.net>

```
Michael J Gruber <git@drmicha.warpmail.net> writes:

> -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.

I like that goal.  And I agree with your s/force_create/force/g
remark on the cover, too.



> Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
> ---
>  builtin/branch.c  | 9 +++++++--
>  t/t3200-branch.sh | 5 +++++
>  2 files changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/branch.c b/builtin/branch.c
> index 3b79c50..8ea04d7 100644
> --- a/builtin/branch.c
> +++ b/builtin/branch.c
> @@ -848,7 +848,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_create, N_("force creation, move/rename, deletion")),
>  		{
>  			OPTION_CALLBACK, 0, "no-merged", &merge_filter_ref,
>  			N_("commit"), N_("print only not merged branches"),
> @@ -891,7 +891,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 +

This puzzled me but earlier -f implied creation and no other mode
(hence it was an error to give it together with delete and other
modes), but now -f is merely a "do forcibly whatever mode of
operation other option determines" that does not conflict.

What should "-f -u" and "-f -l" do, then, though?

>  	    list + unset_upstream > 1)
>  		usage_with_options(builtin_branch_usage, options);
>  
> @@ -904,6 +904,11 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
>  		colopts = 0;
>  	}
>  
> +	if (force_create) {
> +		delete *= 2;
> +		rename *= 2;
> +	}
> +
>  	if (delete) {
>  		if (!argc)
>  			die(_("branch name required"));
> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
> index 0b3b8f5..ddea498 100755
> --- a/t/t3200-branch.sh
> +++ b/t/t3200-branch.sh
> @@ -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 &&

```

## Michael J Gruber, 2014-12-05 10:57

Subject: Re: [RFC/PATCH 2/2] branch: allow -f with -m and -d
Message-ID: <54818FA7.7040102@drmicha.warpmail.net>
URL: https://gitlist.dev/e/54818FA7.7040102%40drmicha.warpmail.net
In-Reply-To: <xmqqh9xbtehw.fsf@gitster.dls.corp.google.com>

```
Junio C Hamano schrieb am 04.12.2014 um 20:13:
> Michael J Gruber <git@drmicha.warpmail.net> writes:
> 
>> -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.
> 
> I like that goal.  And I agree with your s/force_create/force/g
> remark on the cover, too.
> 
> 
> 
>> Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
>> ---
>>  builtin/branch.c  | 9 +++++++--
>>  t/t3200-branch.sh | 5 +++++
>>  2 files changed, 12 insertions(+), 2 deletions(-)
>>
>> diff --git a/builtin/branch.c b/builtin/branch.c
>> index 3b79c50..8ea04d7 100644
>> --- a/builtin/branch.c
>> +++ b/builtin/branch.c
>> @@ -848,7 +848,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_create, N_("force creation, move/rename, deletion")),
>>  		{
>>  			OPTION_CALLBACK, 0, "no-merged", &merge_filter_ref,
>>  			N_("commit"), N_("print only not merged branches"),
>> @@ -891,7 +891,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 +
> 
> This puzzled me but earlier -f implied creation and no other mode
> (hence it was an error to give it together with delete and other
> modes), but now -f is merely a "do forcibly whatever mode of
> operation other option determines" that does not conflict.
> 
> What should "-f -u" and "-f -l" do, then, though?
> 
>>  	    list + unset_upstream > 1)
>>  		usage_with_options(builtin_branch_usage, options);
>>  

I would say there is nothing to force, so we ignore -f there.
Alternatively, we could warn about that. While I do consider forcing
something that doesn't need force a mistake in other contexts, I would
not apply that thinking to the -f option.

Michael

```

## Junio C Hamano, 2014-12-05 18:02

Subject: Re: [RFC/PATCH 2/2] branch: allow -f with -m and -d
Message-ID: <xmqqbnniq8k8.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqbnniq8k8.fsf%40gitster.dls.corp.google.com
In-Reply-To: <54818FA7.7040102@drmicha.warpmail.net>

```
Michael J Gruber <git@drmicha.warpmail.net> writes:

>> What should "-f -u" and "-f -l" do, then, though?
>> 
>>>  	    list + unset_upstream > 1)
>>>  		usage_with_options(builtin_branch_usage, options);
>>>  
>
> I would say there is nothing to force, so we ignore -f there.

OK, and that is what the updated code does.

```

## Michael J Gruber, 2014-12-08 16:28

Subject: [PATCHv2 0/2] Make git branch -f forceful
Message-ID: <cover.1418055912.git.git@drmicha.warpmail.net>
URL: https://gitlist.dev/e/cover.1418055912.git.git%40drmicha.warpmail.net
In-Reply-To: <xmqqbnniq8k8.fsf@gitster.dls.corp.google.com>

```
For many git commands, '-f/--force' is a way to force actions which
would otherwise error out. Way more than once, I've been trying this
with 'git branch -d' and 'git branch -m'...

I've had these two patches sitting in my tree for 3 years now it seems.
Here's a rebase.

In v2 I rename force_create to force and spell out the "-f" behaviour
for other options in the commit message.

Michael J Gruber (2):
  t3200-branch: test -M
  branch: allow -f with -m and -d

 builtin/branch.c  | 13 +++++++++----
 t/t3200-branch.sh | 14 ++++++++++++++
 2 files changed, 23 insertions(+), 4 deletions(-)

-- 
2.2.0.345.g7041aac

```

## Michael J Gruber, 2014-12-08 16:28

Subject: [PATCHv2 1/2] t3200-branch: test -M
Message-ID: <4f734d9f44bd6aa1b54ca3f81aa0e3822286f9d9.1418055912.git.git@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4f734d9f44bd6aa1b54ca3f81aa0e3822286f9d9.1418055912.git.git%40drmicha.warpmail.net
In-Reply-To: <xmqqbnniq8k8.fsf@gitster.dls.corp.google.com>

```
Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t3200-branch.sh | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 432921b..0b3b8f5 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -97,6 +97,15 @@ test_expect_success 'git branch -m o/o o should fail when o/p exists' '
 	test_must_fail git branch -m o/o o
 '
 
+test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
+	git branch o/q &&
+	test_must_fail git branch -m o/q o/p
+'
+
+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 q r/q should fail when r exists' '
 	git branch q &&
 	git branch r &&
-- 
2.2.0.345.g7041aac

```

## Michael J Gruber, 2014-12-08 16:28

Subject: [PATCHv2 2/2] branch: allow -f with -m and -d
Message-ID: <80b7cc5312666af6136d641a24e42aed78636724.1418055912.git.git@drmicha.warpmail.net>
URL: https://gitlist.dev/e/80b7cc5312666af6136d641a24e42aed78636724.1418055912.git.git%40drmicha.warpmail.net
In-Reply-To: <xmqqbnniq8k8.fsf@gitster.dls.corp.google.com>

```
-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>
---
 builtin/branch.c  | 13 +++++++++----
 t/t3200-branch.sh |  5 +++++
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 3b79c50..dc6f0b2 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -800,7 +800,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;
@@ -848,7 +848,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"),
@@ -891,7 +891,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);
 
@@ -904,6 +904,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"));
@@ -1020,7 +1025,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
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 0b3b8f5..ddea498 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -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 &&
-- 
2.2.0.345.g7041aac

```
