3 # Copyright (c) 2007 Christian Couder
5 test_description
='Tests git bisect functionality'
16 if [ -f "$_file" ]; then
17 echo "$_line" >> $_file ||
return $?
18 MSG
="Add <$_line> into <$_file>."
20 echo "$_line" > $_file ||
return $?
21 git add
$_file ||
return $?
22 MSG
="Create file <$_file> with <$_line> inside."
26 git commit
--quiet -m "$MSG" $_file
34 test_expect_success
'set up basic repo with 1 file (hello) and 4 commits' '
35 add_line_into_file "1: Hello World" hello &&
36 HASH1=$(git rev-parse --verify HEAD) &&
37 add_line_into_file "2: A new day for git" hello &&
38 HASH2=$(git rev-parse --verify HEAD) &&
39 add_line_into_file "3: Another new day for git" hello &&
40 HASH3=$(git rev-parse --verify HEAD) &&
41 add_line_into_file "4: Ciao for now" hello &&
42 HASH4=$(git rev-parse --verify HEAD)
45 test_expect_success
'bisect starts with only one bad' '
48 git bisect bad $HASH4 &&
52 test_expect_success
'bisect does not start with only one good' '
55 git bisect good $HASH1 || return 1
59 echo Oops, should have failed.
66 test_expect_success
'bisect start with one bad and good' '
69 git bisect good $HASH1 &&
70 git bisect bad $HASH4 &&
74 test_expect_success
'bisect fails if given any junk instead of revs' '
76 test_must_fail git bisect start foo $HASH1 -- &&
77 test_must_fail git bisect start $HASH4 $HASH1 bar -- &&
78 test -z "$(git for-each-ref "refs/bisect/*")" &&
79 test -z "$(ls .git/BISECT_* 2>/dev/null)" &&
81 test_must_fail git bisect good foo $HASH1 &&
82 test_must_fail git bisect good $HASH1 bar &&
83 test_must_fail git bisect bad frotz &&
84 test_must_fail git bisect bad $HASH3 $HASH4 &&
85 test_must_fail git bisect skip bar $HASH3 &&
86 test_must_fail git bisect skip $HASH1 foo &&
87 test -z "$(git for-each-ref "refs/bisect/*")" &&
88 git bisect good $HASH1 &&
92 test_expect_success
'bisect reset: back in the master branch' '
94 echo "* master" > branch.expect &&
95 git branch > branch.output &&
96 cmp branch.expect branch.output
99 test_expect_success
'bisect reset: back in another branch' '
100 git checkout -b other &&
102 git bisect good $HASH1 &&
103 git bisect bad $HASH3 &&
105 echo " master" > branch.expect &&
106 echo "* other" >> branch.expect &&
107 git branch > branch.output &&
108 cmp branch.expect branch.output
111 test_expect_success
'bisect reset when not bisecting' '
113 git branch > branch.output &&
114 cmp branch.expect branch.output
117 test_expect_success
'bisect reset removes packed refs' '
120 git bisect good $HASH1 &&
121 git bisect bad $HASH3 &&
122 git pack-refs --all --prune &&
125 test -z "$(git for-each-ref "refs/bisect/*")" &&
126 test -z "$(git for-each-ref "refs/heads/bisect")"
129 test_expect_success
'bisect start: back in good branch' '
130 git branch > branch.output &&
131 grep "* other" branch.output > /dev/null &&
132 git bisect start $HASH4 $HASH1 -- &&
134 git bisect start $HASH4 $HASH1 -- &&
137 git branch > branch.output &&
138 grep "* other" branch.output > /dev/null
141 test_expect_success
'bisect start: no ".git/BISECT_START" if junk rev' '
142 git bisect start $HASH4 $HASH1 -- &&
144 test_must_fail git bisect start $HASH4 foo -- &&
145 git branch > branch.output &&
146 grep "* other" branch.output > /dev/null &&
147 test_must_fail test -e .git/BISECT_START
150 test_expect_success
'bisect start: no ".git/BISECT_START" if mistaken rev' '
151 git bisect start $HASH4 $HASH1 -- &&
153 test_must_fail git bisect start $HASH1 $HASH4 -- &&
154 git branch > branch.output &&
155 grep "* other" branch.output > /dev/null &&
156 test_must_fail test -e .git/BISECT_START
159 test_expect_success
'bisect start: no ".git/BISECT_START" if checkout error' '
160 echo "temp stuff" > hello &&
161 test_must_fail git bisect start $HASH4 $HASH1 -- &&
163 git branch > branch.output &&
164 grep "* other" branch.output > /dev/null &&
165 test_must_fail test -e .git/BISECT_START &&
166 test -z "$(git for-each-ref "refs/bisect/*")" &&
167 git checkout HEAD hello
170 # $HASH1 is good, $HASH4 is bad, we skip $HASH3
172 # so we should find $HASH2 as the first bad commit
173 test_expect_success
'bisect skip: successfull result' '
175 git bisect start $HASH4 $HASH1 &&
177 git bisect bad > my_bisect_log.txt &&
178 grep "$HASH2 is first bad commit" my_bisect_log.txt &&
182 # $HASH1 is good, $HASH4 is bad, we skip $HASH3 and $HASH2
183 # so we should not be able to tell the first bad commit
184 # among $HASH2, $HASH3 and $HASH4
185 test_expect_success
'bisect skip: cannot tell between 3 commits' '
186 git bisect start $HASH4 $HASH1 &&
187 git bisect skip || return 1
189 if git bisect skip > my_bisect_log.txt
191 echo Oops, should have failed.
195 grep "first bad commit could be any of" my_bisect_log.txt &&
196 ! grep $HASH1 my_bisect_log.txt &&
197 grep $HASH2 my_bisect_log.txt &&
198 grep $HASH3 my_bisect_log.txt &&
199 grep $HASH4 my_bisect_log.txt &&
204 # $HASH1 is good, $HASH4 is bad, we skip $HASH3
205 # but $HASH2 is good,
206 # so we should not be able to tell the first bad commit
207 # among $HASH3 and $HASH4
208 test_expect_success
'bisect skip: cannot tell between 2 commits' '
209 git bisect start $HASH4 $HASH1 &&
210 git bisect skip || return 1
212 if git bisect good > my_bisect_log.txt
214 echo Oops, should have failed.
218 grep "first bad commit could be any of" my_bisect_log.txt &&
219 ! grep $HASH1 my_bisect_log.txt &&
220 ! grep $HASH2 my_bisect_log.txt &&
221 grep $HASH3 my_bisect_log.txt &&
222 grep $HASH4 my_bisect_log.txt &&
227 # $HASH1 is good, $HASH4 is both skipped and bad, we skip $HASH3
228 # and $HASH2 is good,
229 # so we should not be able to tell the first bad commit
230 # among $HASH3 and $HASH4
231 test_expect_success
'bisect skip: with commit both bad and skipped' '
235 git bisect good $HASH1 &&
237 if git bisect good > my_bisect_log.txt
239 echo Oops, should have failed.
243 grep "first bad commit could be any of" my_bisect_log.txt &&
244 ! grep $HASH1 my_bisect_log.txt &&
245 ! grep $HASH2 my_bisect_log.txt &&
246 grep $HASH3 my_bisect_log.txt &&
247 grep $HASH4 my_bisect_log.txt &&
252 # We want to automatically find the commit that
253 # introduced "Another" into hello.
254 test_expect_success \
255 '"git bisect run" simple case' \
256 'echo "#"\!"/bin/sh" > test_script.sh &&
257 echo "grep Another hello > /dev/null" >> test_script.sh &&
258 echo "test \$? -ne 0" >> test_script.sh &&
259 chmod +x test_script.sh &&
261 git bisect good $HASH1 &&
262 git bisect bad $HASH4 &&
263 git bisect run ./test_script.sh > my_bisect_log.txt &&
264 grep "$HASH3 is first bad commit" my_bisect_log.txt &&
267 # We want to automatically find the commit that
268 # introduced "Ciao" into hello.
269 test_expect_success \
270 '"git bisect run" with more complex "git bisect start"' \
271 'echo "#"\!"/bin/sh" > test_script.sh &&
272 echo "grep Ciao hello > /dev/null" >> test_script.sh &&
273 echo "test \$? -ne 0" >> test_script.sh &&
274 chmod +x test_script.sh &&
275 git bisect start $HASH4 $HASH1 &&
276 git bisect run ./test_script.sh > my_bisect_log.txt &&
277 grep "$HASH4 is first bad commit" my_bisect_log.txt &&
280 # $HASH1 is good, $HASH5 is bad, we skip $HASH3
281 # but $HASH4 is good,
282 # so we should find $HASH5 as the first bad commit
284 test_expect_success
'bisect skip: add line and then a new test' '
285 add_line_into_file "5: Another new line." hello &&
286 HASH5=$(git rev-parse --verify HEAD) &&
287 git bisect start $HASH5 $HASH1 &&
289 git bisect good > my_bisect_log.txt &&
290 grep "$HASH5 is first bad commit" my_bisect_log.txt &&
291 git bisect log > log_to_replay.txt &&
295 test_expect_success
'bisect skip and bisect replay' '
296 git bisect replay log_to_replay.txt > my_bisect_log.txt &&
297 grep "$HASH5 is first bad commit" my_bisect_log.txt &&
302 test_expect_success
'bisect run & skip: cannot tell between 2' '
303 add_line_into_file "6: Yet a line." hello &&
304 HASH6=$(git rev-parse --verify HEAD) &&
305 echo "#"\!"/bin/sh" > test_script.sh &&
306 echo "sed -ne \\\$p hello | grep Ciao > /dev/null && exit 125" >> test_script.sh &&
307 echo "grep line hello > /dev/null" >> test_script.sh &&
308 echo "test \$? -ne 0" >> test_script.sh &&
309 chmod +x test_script.sh &&
310 git bisect start $HASH6 $HASH1 &&
311 if git bisect run ./test_script.sh > my_bisect_log.txt
313 echo Oops, should have failed.
317 grep "first bad commit could be any of" my_bisect_log.txt &&
318 ! grep $HASH3 my_bisect_log.txt &&
319 ! grep $HASH6 my_bisect_log.txt &&
320 grep $HASH4 my_bisect_log.txt &&
321 grep $HASH5 my_bisect_log.txt
326 test_expect_success
'bisect run & skip: find first bad' '
328 add_line_into_file "7: Should be the last line." hello &&
329 HASH7=$(git rev-parse --verify HEAD) &&
330 echo "#"\!"/bin/sh" > test_script.sh &&
331 echo "sed -ne \\\$p hello | grep Ciao > /dev/null && exit 125" >> test_script.sh &&
332 echo "sed -ne \\\$p hello | grep day > /dev/null && exit 125" >> test_script.sh &&
333 echo "grep Yet hello > /dev/null" >> test_script.sh &&
334 echo "test \$? -ne 0" >> test_script.sh &&
335 chmod +x test_script.sh &&
336 git bisect start $HASH7 $HASH1 &&
337 git bisect run ./test_script.sh > my_bisect_log.txt &&
338 grep "$HASH6 is first bad commit" my_bisect_log.txt
341 test_expect_success
'bisect skip only one range' '
343 git bisect start $HASH7 $HASH1 &&
344 git bisect skip $HASH1..$HASH5 &&
345 test "$HASH6" = "$(git rev-parse --verify HEAD)" &&
346 test_must_fail git bisect bad > my_bisect_log.txt &&
347 grep "first bad commit could be any of" my_bisect_log.txt
350 test_expect_success
'bisect skip many ranges' '
351 git bisect start $HASH7 $HASH1 &&
352 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
353 git bisect skip $HASH2 $HASH2.. ..$HASH5 &&
354 test "$HASH6" = "$(git rev-parse --verify HEAD)" &&
355 test_must_fail git bisect bad > my_bisect_log.txt &&
356 grep "first bad commit could be any of" my_bisect_log.txt
359 test_expect_success
'bisect starting with a detached HEAD' '
361 git checkout master^ &&
362 HEAD=$(git rev-parse --verify HEAD) &&
364 test $HEAD = $(cat .git/BISECT_START) &&
366 test $HEAD = $(git rev-parse --verify HEAD)
369 test_expect_success
'bisect errors out if bad and good are mistaken' '
371 test_must_fail git bisect start $HASH2 $HASH4 2> rev_list_error &&
372 grep "mistake good and bad" rev_list_error &&
376 test_expect_success
'bisect does not create a "bisect" branch' '
378 git bisect start $HASH7 $HASH1 &&
380 rev_hash4=$(git rev-parse --verify HEAD) &&
381 test "$rev_hash4" = "$HASH4" &&
382 git branch -D bisect &&
385 rev_hash6=$(git rev-parse --verify HEAD) &&
386 test "$rev_hash6" = "$HASH6" &&
387 git bisect good > my_bisect_log.txt &&
388 grep "$HASH7 is first bad commit" my_bisect_log.txt &&
390 rev_hash6=$(git rev-parse --verify bisect) &&
391 test "$rev_hash6" = "$HASH6" &&
395 # This creates a "side" branch to test "siblings" cases.
397 # H1-H2-H3-H4-H5-H6-H7 <--other
401 test_expect_success
'side branch creation' '
403 git checkout -b side $HASH4 &&
404 add_line_into_file "5(side): first line on a side branch" hello2 &&
405 SIDE_HASH5=$(git rev-parse --verify HEAD) &&
406 add_line_into_file "6(side): second line on a side branch" hello2 &&
407 SIDE_HASH6=$(git rev-parse --verify HEAD) &&
408 add_line_into_file "7(side): third line on a side branch" hello2 &&
409 SIDE_HASH7=$(git rev-parse --verify HEAD)
412 test_expect_success
'good merge base when good and bad are siblings' '
413 git bisect start "$HASH7" "$SIDE_HASH7" > my_bisect_log.txt &&
414 grep "merge base must be tested" my_bisect_log.txt &&
415 grep $HASH4 my_bisect_log.txt &&
416 git bisect good > my_bisect_log.txt &&
417 test_must_fail grep "merge base must be tested" my_bisect_log.txt &&
418 grep $HASH6 my_bisect_log.txt &&
421 test_expect_success
'skipped merge base when good and bad are siblings' '
422 git bisect start "$SIDE_HASH7" "$HASH7" > my_bisect_log.txt &&
423 grep "merge base must be tested" my_bisect_log.txt &&
424 grep $HASH4 my_bisect_log.txt &&
425 git bisect skip > my_bisect_log.txt 2>&1 &&
426 grep "Warning" my_bisect_log.txt &&
427 grep $SIDE_HASH6 my_bisect_log.txt &&
431 test_expect_success
'bad merge base when good and bad are siblings' '
432 git bisect start "$HASH7" HEAD > my_bisect_log.txt &&
433 grep "merge base must be tested" my_bisect_log.txt &&
434 grep $HASH4 my_bisect_log.txt &&
435 test_must_fail git bisect bad > my_bisect_log.txt 2>&1 &&
436 grep "merge base $HASH4 is bad" my_bisect_log.txt &&
437 grep "fixed between $HASH4 and \[$SIDE_HASH7\]" my_bisect_log.txt &&
441 # This creates a few more commits (A and B) to test "siblings" cases
442 # when a good and a bad rev have many merge bases.
444 # We should have the following:
446 # H1-H2-H3-H4-H5-H6-H7
452 # And there A and B have 2 merge bases (S5 and H5) that should be
453 # reported by "git merge-base --all A B".
455 test_expect_success
'many merge bases creation' '
456 git checkout "$SIDE_HASH5" &&
457 git merge -m "merge HASH5 and SIDE_HASH5" "$HASH5" &&
458 A_HASH=$(git rev-parse --verify HEAD) &&
460 git merge -m "merge HASH7 and SIDE_HASH7" "$HASH7" &&
461 B_HASH=$(git rev-parse --verify HEAD) &&
462 git merge-base --all "$A_HASH" "$B_HASH" > merge_bases.txt &&
463 test $(wc -l < merge_bases.txt) = "2" &&
464 grep "$HASH5" merge_bases.txt &&
465 grep "$SIDE_HASH5" merge_bases.txt
468 test_expect_success
'good merge bases when good and bad are siblings' '
469 git bisect start "$B_HASH" "$A_HASH" > my_bisect_log.txt &&
470 grep "merge base must be tested" my_bisect_log.txt &&
471 git bisect good > my_bisect_log2.txt &&
472 grep "merge base must be tested" my_bisect_log2.txt &&
475 grep "$SIDE_HASH5" my_bisect_log.txt &&
476 grep "$HASH5" my_bisect_log2.txt
478 grep "$SIDE_HASH5" my_bisect_log2.txt &&
479 grep "$HASH5" my_bisect_log.txt
486 grep "$1" "$GIT_TRACE" |
grep "\^$2" |
grep "$3" >/dev
/null
489 test_expect_success
'optimized merge base checks' '
490 GIT_TRACE="$(pwd)/trace.log" &&
492 git bisect start "$HASH7" "$SIDE_HASH7" > my_bisect_log.txt &&
493 grep "merge base must be tested" my_bisect_log.txt &&
494 grep "$HASH4" my_bisect_log.txt &&
495 check_trace "rev-list" "$HASH7" "$SIDE_HASH7" &&
496 git bisect good > my_bisect_log2.txt &&
497 test -f ".git/BISECT_ANCESTORS_OK" &&
498 test "$HASH6" = $(git rev-parse --verify HEAD) &&
500 git bisect bad > my_bisect_log3.txt &&
501 test_must_fail check_trace "rev-list" "$HASH6" "$SIDE_HASH7" &&
502 git bisect good "$A_HASH" > my_bisect_log4.txt &&
503 grep "merge base must be tested" my_bisect_log4.txt &&
504 test_must_fail test -f ".git/BISECT_ANCESTORS_OK" &&
505 check_trace "rev-list" "$HASH6" "$A_HASH" &&