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 <pape@smarden.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Gerrit Pape committed
May 5, 2008 at 07:43 UTC
ee831f7ddfc40e9144208c93e24d20ff0ad69194
3 files changed
+31
-8
Documentation/git-bisect.txt
+1
-1
@@ -78,7 +78,7 @@ Oh, and then after you want to reset to the original head, do a
78
$ git bisect reset
79
------------------------------------------------
80
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
82
bisection branches ("git bisect start" will do that for you too,
83
actually: it will reset the bisection state, and before it does that
84
it checks that you're not using some old bisection branch).
git-bisect.sh
+12
-7
@@ -65,14 +65,19 @@ bisect_start() {
65
head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
66
head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
67
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
77
start_head=''
78
case "$head" in
79
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"`
81
git checkout $branch || exit
82
;;
83
refs/heads/*|$_x40)
@@ -324,8 +329,8 @@ bisect_next() {
329
exit_if_skipped_commits "$bisect_rev"
330
331
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
334
git branch -M new-bisect bisect
335
git show-branch "$bisect_rev"
336
}
t/t6030-bisect-porcelain.sh
+18
@@ -284,6 +284,24 @@ test_expect_success 'bisect starting with a detached HEAD' '
284
285
'
286
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
+
305
#
306
#
307
test_done