# [PATCH] git-bisect.sh: don't accidentally override existing branch "bisect"

11 messages from 2008-04-30 to 2008-05-06. Participants: Gerrit Pape, Christian Couder, Richard Quirk, Karl Hasselström, Junio C Hamano, Johannes Schindelin.
Thread: https://gitlist.dev/t/13335

## Gerrit Pape, 2008-04-30 16:46

Subject: [PATCH] git-bisect.sh: don't accidentally override existing branch "bisect"
Message-ID: <20080430164613.28314.qmail@b31db398e1accc.315fe32.mid.smarden.org>
URL: https://gitlist.dev/e/20080430164613.28314.qmail%40b31db398e1accc.315fe32.mid.smarden.org

```
If a branch named "bisect" or "new-bisect" already was created in the
repo by other means than git bisect, doing a git bisect used to override
the branch without a warning.  Now if the branch "bisect" or
"new-bisect" already exists, and it was not created by git bisect itself,
git bisect start fails with an appropriate error message.  Additionally,
if checking out a new bisect state fails due to a merge problem, git
bisect cleans up the temporary branch "new-bisect".

The accidental override has been noticed by Andres Salomon, reported
through
 http://bugs.debian.org/478647

Signed-off-by: Gerrit Pape <pape@smarden.org>
---
 Documentation/git-bisect.txt |    2 +-
 git-bisect.sh                |   20 ++++++++++++++------
 t/t6030-bisect-porcelain.sh  |   18 ++++++++++++++++++
 3 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/Documentation/git-bisect.txt b/Documentation/git-bisect.txt
index 698ffde..1c7e38d 100644
--- a/Documentation/git-bisect.txt
+++ b/Documentation/git-bisect.txt
@@ -85,7 +85,7 @@ Oh, and then after you want to reset to the original head, do a
 $ git bisect reset
 ------------------------------------------------
 
-to get back to the master branch, instead of being in one of the
+to get back to the original branch, instead of being in one of the
 bisection branches ("git bisect start" will do that for you too,
 actually: it will reset the bisection state, and before it does that
 it checks that you're not using some old bisection branch).
diff --git a/git-bisect.sh b/git-bisect.sh
index d8d9bfd..48d81d5 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -69,14 +69,19 @@ bisect_start() {
 	head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
 	head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
 	die "Bad HEAD - I need a HEAD"
+	#
+	# Check that we either already have BISECT_START, or that the
+	# branches bisect, new-bisect don't exist, to not override them.
+	#
+	test -s "$GIT_DIR/BISECT_START" ||
+		if git show-ref bisect > /dev/null ||
+		    git show-ref new-bisect > /dev/null; then
+			die 'The branches "bisect" and "new-bisect" must not exist.'
+		fi
 	start_head=''
 	case "$head" in
 	refs/heads/bisect)
-		if [ -s "$GIT_DIR/BISECT_START" ]; then
-		    branch=`cat "$GIT_DIR/BISECT_START"`
-		else
-		    branch=master
-		fi
+		branch=`cat "$GIT_DIR/BISECT_START"`
 		git checkout $branch || exit
 		;;
 	refs/heads/*|$_x40)
@@ -329,7 +334,10 @@ bisect_next() {
 
 	echo "Bisecting: $bisect_nr revisions left to test after this"
 	git branch -f new-bisect "$bisect_rev"
-	git checkout -q new-bisect || exit
+	git checkout -q new-bisect || {
+		git branch -d new-bisect
+		exit
+	}
 	git branch -M new-bisect bisect
 	git show-branch "$bisect_rev"
 }
diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
index 5e3e544..05f1e15 100755
--- a/t/t6030-bisect-porcelain.sh
+++ b/t/t6030-bisect-porcelain.sh
@@ -284,6 +284,24 @@ test_expect_success 'bisect starting with a detached HEAD' '
 
 '
 
+test_expect_success 'bisect refuses to start if branch bisect exists' '
+	git bisect reset &&
+	git branch bisect &&
+	test_must_fail git bisect start &&
+	git branch -d bisect &&
+	git checkout -b bisect &&
+	test_must_fail git bisect start &&
+	git checkout master &&
+	git branch -d bisect
+'
+
+test_expect_success 'bisect refuses to start if branch new-bisect exists' '
+	git bisect reset &&
+	git branch new-bisect &&
+	test_must_fail git bisect start &&
+	git branch -d new-bisect
+'
+
 #
 #
 test_done
-- 
1.5.5.1

```

## Christian Couder, 2008-04-30 21:30

Subject: Re: [PATCH] git-bisect.sh: don't accidentally override existing branch "bisect"
Message-ID: <200804302330.18354.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/200804302330.18354.chriscool%40tuxfamily.org
In-Reply-To: <20080430164613.28314.qmail@b31db398e1accc.315fe32.mid.smarden.org>

```
Le mercredi 30 avril 2008, Gerrit Pape a écrit :
> If a branch named "bisect" or "new-bisect" already was created in the
> repo by other means than git bisect, doing a git bisect used to override
> the branch without a warning.  Now if the branch "bisect" or
> "new-bisect" already exists, and it was not created by git bisect itself,
> git bisect start fails with an appropriate error message.  Additionally,
> if checking out a new bisect state fails due to a merge problem, git
> bisect cleans up the temporary branch "new-bisect".
>
> The accidental override has been noticed by Andres Salomon, reported
> through
>  http://bugs.debian.org/478647
>
> Signed-off-by: Gerrit Pape <pape@smarden.org>
> ---
>  Documentation/git-bisect.txt |    2 +-
>  git-bisect.sh                |   20 ++++++++++++++------
>  t/t6030-bisect-porcelain.sh  |   18 ++++++++++++++++++
>  3 files changed, 33 insertions(+), 7 deletions(-)
>
> diff --git a/Documentation/git-bisect.txt b/Documentation/git-bisect.txt
> index 698ffde..1c7e38d 100644
> --- a/Documentation/git-bisect.txt
> +++ b/Documentation/git-bisect.txt
> @@ -85,7 +85,7 @@ Oh, and then after you want to reset to the original
> head, do a $ git bisect reset
>  ------------------------------------------------
>
> -to get back to the master branch, instead of being in one of the
> +to get back to the original branch, instead of being in one of the
>  bisection branches ("git bisect start" will do that for you too,
>  actually: it will reset the bisection state, and before it does that
>  it checks that you're not using some old bisection branch).
> diff --git a/git-bisect.sh b/git-bisect.sh
> index d8d9bfd..48d81d5 100755
> --- a/git-bisect.sh
> +++ b/git-bisect.sh
> @@ -69,14 +69,19 @@ bisect_start() {
>  	head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
>  	head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
>  	die "Bad HEAD - I need a HEAD"
> +	#
> +	# Check that we either already have BISECT_START, or that the
> +	# branches bisect, new-bisect don't exist, to not override them.
> +	#
> +	test -s "$GIT_DIR/BISECT_START" ||
> +		if git show-ref bisect > /dev/null ||
> +		    git show-ref new-bisect > /dev/null; then
> +			die 'The branches "bisect" and "new-bisect" must not exist.'
> +		fi

Minor nitpick: you may use:

git show-ref -q {new-,}bisect

instead of:

git show-ref bisect > /dev/null ||
	git show-ref new-bisect > /dev/null

That would give something like:

	test -s "$GIT_DIR/BISECT_START" ||
		git show-ref -q {new-,}bisect &&
			die 'The branches "bisect" and "new-bisect" must not exist.'

>  	start_head=''
>  	case "$head" in
>  	refs/heads/bisect)
> -		if [ -s "$GIT_DIR/BISECT_START" ]; then
> -		    branch=`cat "$GIT_DIR/BISECT_START"`
> -		else
> -		    branch=master
> -		fi
> +		branch=`cat "$GIT_DIR/BISECT_START"`
>  		git checkout $branch || exit
>  		;;
>  	refs/heads/*|$_x40)
> @@ -329,7 +334,10 @@ bisect_next() {
>
>  	echo "Bisecting: $bisect_nr revisions left to test after this"
>  	git branch -f new-bisect "$bisect_rev"
> -	git checkout -q new-bisect || exit
> +	git checkout -q new-bisect || {
> +		git branch -d new-bisect
> +		exit

Here we "exit 0" if "git branch -d new-bisect" succeeds.
That seems wrong.

> +	}
>  	git branch -M new-bisect bisect
>  	git show-branch "$bisect_rev"
>  }
> diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
> index 5e3e544..05f1e15 100755
> --- a/t/t6030-bisect-porcelain.sh
> +++ b/t/t6030-bisect-porcelain.sh
> @@ -284,6 +284,24 @@ test_expect_success 'bisect starting with a detached
> HEAD' '
>
>  '
>
> +test_expect_success 'bisect refuses to start if branch bisect exists' '
> +	git bisect reset &&
> +	git branch bisect &&
> +	test_must_fail git bisect start &&
> +	git branch -d bisect &&
> +	git checkout -b bisect &&
> +	test_must_fail git bisect start &&
> +	git checkout master &&
> +	git branch -d bisect
> +'
> +
> +test_expect_success 'bisect refuses to start if branch new-bisect
> exists' ' +	git bisect reset &&
> +	git branch new-bisect &&
> +	test_must_fail git bisect start &&
> +	git branch -d new-bisect
> +'
> +
>  #
>  #
>  test_done

Otherwise the patch looks good.

Thanks,
Christian.

```

## Richard Quirk, 2008-05-01 12:15

Subject: Re: [PATCH] git-bisect.sh: don't accidentally override existing branch "bisect"
Message-ID: <cac9e4380805010515h783dcf74h39fcc522c78885d3@mail.gmail.com>
URL: https://gitlist.dev/e/cac9e4380805010515h783dcf74h39fcc522c78885d3%40mail.gmail.com
In-Reply-To: <200804302330.18354.chriscool@tuxfamily.org>

```
On Wed, Apr 30, 2008 at 11:30 PM, Christian Couder
<chriscool@tuxfamily.org> wrote:

>  Minor nitpick: you may use:
>
>  git show-ref -q {new-,}bisect
>
>  instead of:
>
>
>  git show-ref bisect > /dev/null ||
>         git show-ref new-bisect > /dev/null

Careful with that - it's a bashism and would fail if /bin/sh is dash.
ie it would say that a branch called literally "{new-,}bisect" does
not exist, even if new-bisect and bisect do.

(this time reply-all instead of just to Christian!)

```

## Christian Couder, 2008-05-01 12:27

Subject: Re: [PATCH] git-bisect.sh: don't accidentally override existing branch "bisect"
Message-ID: <200805011427.22901.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/200805011427.22901.chriscool%40tuxfamily.org
In-Reply-To: <cac9e4380805010515h783dcf74h39fcc522c78885d3@mail.gmail.com>

```
Le jeudi 1 mai 2008, Richard Quirk a écrit :
> On Wed, Apr 30, 2008 at 11:30 PM, Christian Couder
>
> <chriscool@tuxfamily.org> wrote:
> >  Minor nitpick: you may use:
> >
> >  git show-ref -q {new-,}bisect
> >
> >  instead of:
> >
> >
> >  git show-ref bisect > /dev/null ||
> >         git show-ref new-bisect > /dev/null
>
> Careful with that - it's a bashism and would fail if /bin/sh is dash.
> ie it would say that a branch called literally "{new-,}bisect" does
> not exist, even if new-bisect and bisect do.

You are right. Thanks.
So what about a plain:

git show-ref -q bisect new-bisect

Regards,
Christian.

```

## Karl Hasselström, 2008-05-02 08:22

Subject: Re: [PATCH] git-bisect.sh: don't accidentally override existing branch "bisect"
Message-ID: <20080502082232.GA20020@diana.vm.bytemark.co.uk>
URL: https://gitlist.dev/e/20080502082232.GA20020%40diana.vm.bytemark.co.uk
In-Reply-To: <20080430164613.28314.qmail@b31db398e1accc.315fe32.mid.smarden.org>

```
On 2008-04-30 16:46:13 +0000, Gerrit Pape wrote:

> If a branch named "bisect" or "new-bisect" already was created in
> the repo by other means than git bisect, doing a git bisect used to
> override the branch without a warning. Now if the branch "bisect" or
> "new-bisect" already exists, and it was not created by git bisect
> itself, git bisect start fails with an appropriate error message.
> Additionally, if checking out a new bisect state fails due to a
> merge problem, git bisect cleans up the temporary branch
> "new-bisect".

Makes me wonder why bisect has to use a branch at all, and not just a
detached HEAD ... I seem to recall this having been discussed before,
but I can't find it now.

-- 
Karl Hasselström, kha@treskal.com
      www.treskal.com/kalle

```

## Gerrit Pape, 2008-05-02 08:56

Subject: [PATCH amend] git-bisect.sh: don't accidentally override existing branch "bisect"
Message-ID: <20080502085620.3361.qmail@17e811992e6d42.315fe32.mid.smarden.org>
URL: https://gitlist.dev/e/20080502085620.3361.qmail%4017e811992e6d42.315fe32.mid.smarden.org
In-Reply-To: <200804302330.18354.chriscool@tuxfamily.org>

```
If a branch named "bisect" or "new-bisect" already was created in the
repo by other means than git bisect, doing a git bisect used to override
the branch without a warning.  Now if the branch "bisect" or
"new-bisect" already exists, and it was not created by git bisect itself,
git bisect start fails with an appropriate error message.  Additionally,
if checking out a new bisect state fails due to a merge problem, git
bisect cleans up the temporary branch "new-bisect".

The accidental override has been noticed by Andres Salomon, reported
through
 http://bugs.debian.org/478647

Signed-off-by: Gerrit Pape <pape@smarden.org>
---

On Wed, Apr 30, 2008 at 11:30:18PM +0200, Christian Couder wrote:
> >     echo "Bisecting: $bisect_nr revisions left to test after this"
> >     git branch -f new-bisect "$bisect_rev"
> > -   git checkout -q new-bisect || exit
> > +   git checkout -q new-bisect || {
> > +           git branch -d new-bisect
> > +           exit
>
> Here we "exit 0" if "git branch -d new-bisect" succeeds.
> That seems wrong.

Thanks, I changed it to first delete the new-bisect branch, and then use
git checkout to create the branch and do the checkout.  So we have the
exit code from git branch if it fails.

On Thu, May 01, 2008 at 02:27:22PM +0200, Christian Couder wrote:
> Le jeudi 1 mai 2008, Richard Quirk a écrit :
> > Careful with that - it's a bashism and would fail if /bin/sh is
> > dash.
> > ie it would say that a branch called literally "{new-,}bisect" does
> > not exist, even if new-bisect and bisect do.
>
> You are right. Thanks.
> So what about a plain:
>
> git show-ref -q bisect new-bisect

I'm not sure we can rely on this, the exit code of that command if one
of the branches exists, but not the other, isn't documented.  The patch
now uses --verify -q on each branch, which is documented and should be
reliable.


 Documentation/git-bisect.txt |    2 +-
 git-bisect.sh                |   19 ++++++++++++-------
 t/t6030-bisect-porcelain.sh  |   18 ++++++++++++++++++
 3 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/Documentation/git-bisect.txt b/Documentation/git-bisect.txt
index 698ffde..1c7e38d 100644
--- a/Documentation/git-bisect.txt
+++ b/Documentation/git-bisect.txt
@@ -85,7 +85,7 @@ Oh, and then after you want to reset to the original head, do a
 $ git bisect reset
 ------------------------------------------------
 
-to get back to the master branch, instead of being in one of the
+to get back to the original branch, instead of being in one of the
 bisection branches ("git bisect start" will do that for you too,
 actually: it will reset the bisection state, and before it does that
 it checks that you're not using some old bisection branch).
diff --git a/git-bisect.sh b/git-bisect.sh
index d8d9bfd..f8c411a 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -69,14 +69,19 @@ bisect_start() {
 	head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
 	head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
 	die "Bad HEAD - I need a HEAD"
+	#
+	# Check that we either already have BISECT_START, or that the
+	# branches bisect, new-bisect don't exist, to not override them.
+	#
+	test -s "$GIT_DIR/BISECT_START" ||
+		if git show-ref --verify -q refs/heads/bisect ||
+		    git show-ref --verify -q refs/heads/new-bisect; then
+			die 'The branches "bisect" and "new-bisect" must not exist.'
+		fi
 	start_head=''
 	case "$head" in
 	refs/heads/bisect)
-		if [ -s "$GIT_DIR/BISECT_START" ]; then
-		    branch=`cat "$GIT_DIR/BISECT_START"`
-		else
-		    branch=master
-		fi
+		branch=`cat "$GIT_DIR/BISECT_START"`
 		git checkout $branch || exit
 		;;
 	refs/heads/*|$_x40)
@@ -328,8 +333,8 @@ bisect_next() {
 	exit_if_skipped_commits "$bisect_rev"
 
 	echo "Bisecting: $bisect_nr revisions left to test after this"
-	git branch -f new-bisect "$bisect_rev"
-	git checkout -q new-bisect || exit
+	git branch -D new-bisect
+	git checkout -q -b new-bisect "$bisect_rev" || exit
 	git branch -M new-bisect bisect
 	git show-branch "$bisect_rev"
 }
diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
index 5e3e544..05f1e15 100755
--- a/t/t6030-bisect-porcelain.sh
+++ b/t/t6030-bisect-porcelain.sh
@@ -284,6 +284,24 @@ test_expect_success 'bisect starting with a detached HEAD' '
 
 '
 
+test_expect_success 'bisect refuses to start if branch bisect exists' '
+	git bisect reset &&
+	git branch bisect &&
+	test_must_fail git bisect start &&
+	git branch -d bisect &&
+	git checkout -b bisect &&
+	test_must_fail git bisect start &&
+	git checkout master &&
+	git branch -d bisect
+'
+
+test_expect_success 'bisect refuses to start if branch new-bisect exists' '
+	git bisect reset &&
+	git branch new-bisect &&
+	test_must_fail git bisect start &&
+	git branch -d new-bisect
+'
+
 #
 #
 test_done
-- 
1.5.5.1

```

## Junio C Hamano, 2008-05-02 17:38

Subject: Re: [PATCH] git-bisect.sh: don't accidentally override existing branch "bisect"
Message-ID: <7v8wysy5bz.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v8wysy5bz.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <20080502082232.GA20020@diana.vm.bytemark.co.uk>

```
Karl Hasselström <kha@treskal.com> writes:

> On 2008-04-30 16:46:13 +0000, Gerrit Pape wrote:
>
>> If a branch named "bisect" or "new-bisect" already was created in
>> the repo by other means than git bisect, doing a git bisect used to
>> override the branch without a warning. Now if the branch "bisect" or
>> "new-bisect" already exists, and it was not created by git bisect
>> itself, git bisect start fails with an appropriate error message.
>> Additionally, if checking out a new bisect state fails due to a
>> merge problem, git bisect cleans up the temporary branch
>> "new-bisect".
>
> Makes me wonder why bisect has to use a branch at all, and not just a
> detached HEAD ... I seem to recall this having been discussed before,
> but I can't find it now.

Only because the mechanism predates detached HEAD and no other reason.
Whoever wants to update it to use detached HEAD needs to design what
should happen when the bisection was started while the HEAD is detached
(should we come back to the same HEAD?  how? ...), but other than that I
do not offhand see fundamental difficulties.

```

## Christian Couder, 2008-05-03 08:42

Subject: Re: [PATCH amend] git-bisect.sh: don't accidentally override existing branch "bisect"
Message-ID: <200805031042.32190.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/200805031042.32190.chriscool%40tuxfamily.org
In-Reply-To: <20080502085620.3361.qmail@17e811992e6d42.315fe32.mid.smarden.org>

```
Le vendredi 2 mai 2008, Gerrit Pape a écrit :

[...]
> @@ -328,8 +333,8 @@ bisect_next() {
>  	exit_if_skipped_commits "$bisect_rev"
>
>  	echo "Bisecting: $bisect_nr revisions left to test after this"
> -	git branch -f new-bisect "$bisect_rev"
> -	git checkout -q new-bisect || exit
> +	git branch -D new-bisect

Doesn't this output an error if the branch "new-bisect" does not exists ?

$ git branch -D new-bisect
error: branch 'new-bisect' not found.

> +	git checkout -q -b new-bisect "$bisect_rev" || exit
>  	git branch -M new-bisect bisect
>  	git show-branch "$bisect_rev"
>  }

Thanks,
Christian.

```

## Johannes Schindelin, 2008-05-03 12:48

Subject: Re: [PATCH] git-bisect.sh: don't accidentally override existing branch "bisect"
Message-ID: <alpine.DEB.1.00.0805031347460.30431@racer>
URL: https://gitlist.dev/e/alpine.DEB.1.00.0805031347460.30431%40racer
In-Reply-To: <7v8wysy5bz.fsf@gitster.siamese.dyndns.org>

```
Hi,

On Fri, 2 May 2008, Junio C Hamano wrote:

> Karl Hasselström <kha@treskal.com> writes:
> 
> > On 2008-04-30 16:46:13 +0000, Gerrit Pape wrote:
> >
> >> If a branch named "bisect" or "new-bisect" already was created in the 
> >> repo by other means than git bisect, doing a git bisect used to 
> >> override the branch without a warning. Now if the branch "bisect" or 
> >> "new-bisect" already exists, and it was not created by git bisect 
> >> itself, git bisect start fails with an appropriate error message. 
> >> Additionally, if checking out a new bisect state fails due to a merge 
> >> problem, git bisect cleans up the temporary branch "new-bisect".
> >
> > Makes me wonder why bisect has to use a branch at all, and not just a 
> > detached HEAD ... I seem to recall this having been discussed before, 
> > but I can't find it now.
> 
> Only because the mechanism predates detached HEAD and no other reason. 
> Whoever wants to update it to use detached HEAD needs to design what 
> should happen when the bisection was started while the HEAD is detached 
> (should we come back to the same HEAD?  how? ...), but other than that I 
> do not offhand see fundamental difficulties.

IMO it should behave as the rebase machinery does: record $(git rev-parse 
HEAD) in the case of a detached HEAD, and go back to that.  It is dead 
easy.

Ciao,
Dscho

```

## Gerrit Pape, 2008-05-05 07:43

Subject: [PATCH amend] git-bisect.sh: don't accidentally override existing branch "bisect"
Message-ID: <20080505074300.5555.qmail@11d6801606c1fe.315fe32.mid.smarden.org>
URL: https://gitlist.dev/e/20080505074300.5555.qmail%4011d6801606c1fe.315fe32.mid.smarden.org
In-Reply-To: <200805031042.32190.chriscool@tuxfamily.org>

```
If a branch named "bisect" or "new-bisect" already was created in the
repo by other means than git bisect, doing a git bisect used to override
the branch without a warning.  Now if the branch "bisect" or
"new-bisect" already exists, and it was not created by git bisect itself,
git bisect start fails with an appropriate error message.  Additionally,
if checking out a new bisect state fails due to a merge problem, git
bisect cleans up the temporary branch "new-bisect".

The accidental override has been noticed by Andres Salomon, reported
through
 http://bugs.debian.org/478647

Signed-off-by: Gerrit Pape <pape@smarden.org>
---

On Sat, May 03, 2008 at 10:42:31AM +0200, Christian Couder wrote:
> Le vendredi 2 mai 2008, Gerrit Pape a ?crit :
> > -   git branch -f new-bisect "$bisect_rev"
> > -   git checkout -q new-bisect || exit
> > +   git branch -D new-bisect
> 
> Doesn't this output an error if the branch "new-bisect" does not
> exists ?

It does, thanks.  Another amend, direct stderr to /dev/null.


 Documentation/git-bisect.txt |    2 +-
 git-bisect.sh                |   19 ++++++++++++-------
 t/t6030-bisect-porcelain.sh  |   18 ++++++++++++++++++
 3 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/Documentation/git-bisect.txt b/Documentation/git-bisect.txt
index 698ffde..1c7e38d 100644
--- a/Documentation/git-bisect.txt
+++ b/Documentation/git-bisect.txt
@@ -85,7 +85,7 @@ Oh, and then after you want to reset to the original head, do a
 $ git bisect reset
 ------------------------------------------------
 
-to get back to the master branch, instead of being in one of the
+to get back to the original branch, instead of being in one of the
 bisection branches ("git bisect start" will do that for you too,
 actually: it will reset the bisection state, and before it does that
 it checks that you're not using some old bisection branch).
diff --git a/git-bisect.sh b/git-bisect.sh
index d8d9bfd..b5171c9 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -69,14 +69,19 @@ bisect_start() {
 	head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
 	head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
 	die "Bad HEAD - I need a HEAD"
+	#
+	# Check that we either already have BISECT_START, or that the
+	# branches bisect, new-bisect don't exist, to not override them.
+	#
+	test -s "$GIT_DIR/BISECT_START" ||
+		if git show-ref --verify -q refs/heads/bisect ||
+		    git show-ref --verify -q refs/heads/new-bisect; then
+			die 'The branches "bisect" and "new-bisect" must not exist.'
+		fi
 	start_head=''
 	case "$head" in
 	refs/heads/bisect)
-		if [ -s "$GIT_DIR/BISECT_START" ]; then
-		    branch=`cat "$GIT_DIR/BISECT_START"`
-		else
-		    branch=master
-		fi
+		branch=`cat "$GIT_DIR/BISECT_START"`
 		git checkout $branch || exit
 		;;
 	refs/heads/*|$_x40)
@@ -328,8 +333,8 @@ bisect_next() {
 	exit_if_skipped_commits "$bisect_rev"
 
 	echo "Bisecting: $bisect_nr revisions left to test after this"
-	git branch -f new-bisect "$bisect_rev"
-	git checkout -q new-bisect || exit
+	git branch -D new-bisect 2> /dev/null
+	git checkout -q -b new-bisect "$bisect_rev" || exit
 	git branch -M new-bisect bisect
 	git show-branch "$bisect_rev"
 }
diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
index 5e3e544..05f1e15 100755
--- a/t/t6030-bisect-porcelain.sh
+++ b/t/t6030-bisect-porcelain.sh
@@ -284,6 +284,24 @@ test_expect_success 'bisect starting with a detached HEAD' '
 
 '
 
+test_expect_success 'bisect refuses to start if branch bisect exists' '
+	git bisect reset &&
+	git branch bisect &&
+	test_must_fail git bisect start &&
+	git branch -d bisect &&
+	git checkout -b bisect &&
+	test_must_fail git bisect start &&
+	git checkout master &&
+	git branch -d bisect
+'
+
+test_expect_success 'bisect refuses to start if branch new-bisect exists' '
+	git bisect reset &&
+	git branch new-bisect &&
+	test_must_fail git bisect start &&
+	git branch -d new-bisect
+'
+
 #
 #
 test_done
-- 
1.5.5.1

```

## Christian Couder, 2008-05-06 06:20

Subject: Re: [PATCH amend] git-bisect.sh: don't accidentally override existing branch "bisect"
Message-ID: <200805060820.59138.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/200805060820.59138.chriscool%40tuxfamily.org
In-Reply-To: <20080505074300.5555.qmail@11d6801606c1fe.315fe32.mid.smarden.org>

```
Le lundi 5 mai 2008, Gerrit Pape a écrit :
> If a branch named "bisect" or "new-bisect" already was created in the
> repo by other means than git bisect, doing a git bisect used to override
> the branch without a warning.  Now if the branch "bisect" or
> "new-bisect" already exists, and it was not created by git bisect itself,
> git bisect start fails with an appropriate error message.  Additionally,
> if checking out a new bisect state fails due to a merge problem, git
> bisect cleans up the temporary branch "new-bisect".
>
> The accidental override has been noticed by Andres Salomon, reported
> through
>  http://bugs.debian.org/478647
>
> Signed-off-by: Gerrit Pape <pape@smarden.org>

Tested-by: Christian Couder <chriscool@tuxfamily.org>

This one looks good to me.

Thanks,
Christian.

> ---
>
> On Sat, May 03, 2008 at 10:42:31AM +0200, Christian Couder wrote:
> > Le vendredi 2 mai 2008, Gerrit Pape a ?crit :
> > > -   git branch -f new-bisect "$bisect_rev"
> > > -   git checkout -q new-bisect || exit
> > > +   git branch -D new-bisect
> >
> > Doesn't this output an error if the branch "new-bisect" does not
> > exists ?
>
> It does, thanks.  Another amend, direct stderr to /dev/null.
>
>
>  Documentation/git-bisect.txt |    2 +-
>  git-bisect.sh                |   19 ++++++++++++-------
>  t/t6030-bisect-porcelain.sh  |   18 ++++++++++++++++++
>  3 files changed, 31 insertions(+), 8 deletions(-)
>
> diff --git a/Documentation/git-bisect.txt b/Documentation/git-bisect.txt
> index 698ffde..1c7e38d 100644
> --- a/Documentation/git-bisect.txt
> +++ b/Documentation/git-bisect.txt
> @@ -85,7 +85,7 @@ Oh, and then after you want to reset to the original
> head, do a $ git bisect reset
>  ------------------------------------------------
>
> -to get back to the master branch, instead of being in one of the
> +to get back to the original branch, instead of being in one of the
>  bisection branches ("git bisect start" will do that for you too,
>  actually: it will reset the bisection state, and before it does that
>  it checks that you're not using some old bisection branch).
> diff --git a/git-bisect.sh b/git-bisect.sh
> index d8d9bfd..b5171c9 100755
> --- a/git-bisect.sh
> +++ b/git-bisect.sh
> @@ -69,14 +69,19 @@ bisect_start() {
>  	head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
>  	head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
>  	die "Bad HEAD - I need a HEAD"
> +	#
> +	# Check that we either already have BISECT_START, or that the
> +	# branches bisect, new-bisect don't exist, to not override them.
> +	#
> +	test -s "$GIT_DIR/BISECT_START" ||
> +		if git show-ref --verify -q refs/heads/bisect ||
> +		    git show-ref --verify -q refs/heads/new-bisect; then
> +			die 'The branches "bisect" and "new-bisect" must not exist.'
> +		fi
>  	start_head=''
>  	case "$head" in
>  	refs/heads/bisect)
> -		if [ -s "$GIT_DIR/BISECT_START" ]; then
> -		    branch=`cat "$GIT_DIR/BISECT_START"`
> -		else
> -		    branch=master
> -		fi
> +		branch=`cat "$GIT_DIR/BISECT_START"`
>  		git checkout $branch || exit
>  		;;
>  	refs/heads/*|$_x40)
> @@ -328,8 +333,8 @@ bisect_next() {
>  	exit_if_skipped_commits "$bisect_rev"
>
>  	echo "Bisecting: $bisect_nr revisions left to test after this"
> -	git branch -f new-bisect "$bisect_rev"
> -	git checkout -q new-bisect || exit
> +	git branch -D new-bisect 2> /dev/null
> +	git checkout -q -b new-bisect "$bisect_rev" || exit
>  	git branch -M new-bisect bisect
>  	git show-branch "$bisect_rev"
>  }
> diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
> index 5e3e544..05f1e15 100755
> --- a/t/t6030-bisect-porcelain.sh
> +++ b/t/t6030-bisect-porcelain.sh
> @@ -284,6 +284,24 @@ test_expect_success 'bisect starting with a detached
> HEAD' '
>
>  '
>
> +test_expect_success 'bisect refuses to start if branch bisect exists' '
> +	git bisect reset &&
> +	git branch bisect &&
> +	test_must_fail git bisect start &&
> +	git branch -d bisect &&
> +	git checkout -b bisect &&
> +	test_must_fail git bisect start &&
> +	git checkout master &&
> +	git branch -d bisect
> +'
> +
> +test_expect_success 'bisect refuses to start if branch new-bisect
> exists' ' +	git bisect reset &&
> +	git branch new-bisect &&
> +	test_must_fail git bisect start &&
> +	git branch -d new-bisect
> +'
> +
>  #
>  #
>  test_done

```
