Skip to content

Commit ee831f7

Browse files
g-papegitster
authored andcommitted
git-bisect.sh: don't accidentally override existing branch "bisect"
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6233a52 commit ee831f7

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

Documentation/git-bisect.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Oh, and then after you want to reset to the original head, do a
7878
$ git bisect reset
7979
------------------------------------------------
8080

81-
to get back to the master branch, instead of being in one of the
81+
to get back to the original branch, instead of being in one of the
8282
bisection branches ("git bisect start" will do that for you too,
8383
actually: it will reset the bisection state, and before it does that
8484
it checks that you're not using some old bisection branch).

git-bisect.sh

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,19 @@ bisect_start() {
6565
head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
6666
head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
6767
die "Bad HEAD - I need a HEAD"
68+
#
69+
# Check that we either already have BISECT_START, or that the
70+
# branches bisect, new-bisect don't exist, to not override them.
71+
#
72+
test -s "$GIT_DIR/BISECT_START" ||
73+
if git show-ref --verify -q refs/heads/bisect ||
74+
git show-ref --verify -q refs/heads/new-bisect; then
75+
die 'The branches "bisect" and "new-bisect" must not exist.'
76+
fi
6877
start_head=''
6978
case "$head" in
7079
refs/heads/bisect)
71-
if [ -s "$GIT_DIR/BISECT_START" ]; then
72-
branch=`cat "$GIT_DIR/BISECT_START"`
73-
else
74-
branch=master
75-
fi
80+
branch=`cat "$GIT_DIR/BISECT_START"`
7681
git checkout $branch || exit
7782
;;
7883
refs/heads/*|$_x40)
@@ -324,8 +329,8 @@ bisect_next() {
324329
exit_if_skipped_commits "$bisect_rev"
325330

326331
echo "Bisecting: $bisect_nr revisions left to test after this"
327-
git branch -f new-bisect "$bisect_rev"
328-
git checkout -q new-bisect || exit
332+
git branch -D new-bisect 2> /dev/null
333+
git checkout -q -b new-bisect "$bisect_rev" || exit
329334
git branch -M new-bisect bisect
330335
git show-branch "$bisect_rev"
331336
}

t/t6030-bisect-porcelain.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,24 @@ test_expect_success 'bisect starting with a detached HEAD' '
284284
285285
'
286286

287+
test_expect_success 'bisect refuses to start if branch bisect exists' '
288+
git bisect reset &&
289+
git branch bisect &&
290+
test_must_fail git bisect start &&
291+
git branch -d bisect &&
292+
git checkout -b bisect &&
293+
test_must_fail git bisect start &&
294+
git checkout master &&
295+
git branch -d bisect
296+
'
297+
298+
test_expect_success 'bisect refuses to start if branch new-bisect exists' '
299+
git bisect reset &&
300+
git branch new-bisect &&
301+
test_must_fail git bisect start &&
302+
git branch -d new-bisect
303+
'
304+
287305
#
288306
#
289307
test_done

0 commit comments

Comments
 (0)