3 # Copyright (c) 2007 Christian Couder
5 test_description
='Tests git bisect functionality'
9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
19 if [ -f "$_file" ]; then
20 echo "$_line" >> $_file ||
return $?
21 MSG
="Add <$_line> into <$_file>."
23 echo "$_line" > $_file ||
return $?
24 git add
$_file ||
return $?
25 MSG
="Create file <$_file> with <$_line> inside."
29 git commit
--quiet -m "$MSG" $_file
37 test_bisect_usage
() {
41 test_expect_code
$code "$@" >out
2>actual
&&
42 test_must_be_empty out
&&
43 test_cmp expect actual
46 test_expect_success
'bisect usage' "
47 test_bisect_usage 1 git bisect reset extra1 extra2 <<-\EOF &&
48 error: 'git bisect reset' requires either no argument or a commit
50 test_bisect_usage 1 git bisect terms extra1 extra2 <<-\EOF &&
51 error: 'git bisect terms' requires 0 or 1 argument
53 test_bisect_usage 1 git bisect next extra1 <<-\EOF &&
54 error: 'git bisect next' requires 0 arguments
56 test_bisect_usage 1 git bisect log extra1 <<-\EOF &&
57 error: We are not bisecting.
59 test_bisect_usage 1 git bisect replay <<-\EOF &&
60 error: no logfile given
62 test_bisect_usage 1 git bisect run <<-\EOF
63 error: 'git bisect run' failed: no command provided.
67 test_expect_success
'set up basic repo with 1 file (hello) and 4 commits' '
68 add_line_into_file "1: Hello World" hello &&
69 HASH1=$(git rev-parse --verify HEAD) &&
70 add_line_into_file "2: A new day for git" hello &&
71 HASH2=$(git rev-parse --verify HEAD) &&
72 add_line_into_file "3: Another new day for git" hello &&
73 HASH3=$(git rev-parse --verify HEAD) &&
74 add_line_into_file "4: Ciao for now" hello &&
75 HASH4=$(git rev-parse --verify HEAD)
78 test_expect_success
'bisect starts with only one bad' '
81 git bisect bad $HASH4 &&
85 test_expect_success
'bisect does not start with only one good' '
88 git bisect good $HASH1 &&
89 test_must_fail git bisect next
92 test_expect_success
'bisect start with one bad and good' '
95 git bisect good $HASH1 &&
96 git bisect bad $HASH4 &&
100 test_expect_success
'bisect fails if given any junk instead of revs' '
102 test_must_fail git bisect start foo $HASH1 -- &&
103 test_must_fail git bisect start $HASH4 $HASH1 bar -- &&
104 test -z "$(git for-each-ref "refs/bisect/*")" &&
105 test -z "$(ls .git/BISECT_* 2>/dev/null)" &&
107 test_must_fail git bisect good foo $HASH1 &&
108 test_must_fail git bisect good $HASH1 bar &&
109 test_must_fail git bisect bad frotz &&
110 test_must_fail git bisect bad $HASH3 $HASH4 &&
111 test_must_fail git bisect skip bar $HASH3 &&
112 test_must_fail git bisect skip $HASH1 foo &&
113 test -z "$(git for-each-ref "refs/bisect/*")" &&
114 git bisect good $HASH1 &&
115 git bisect bad $HASH4
118 test_expect_success
'bisect start without -- takes unknown arg as pathspec' '
120 git bisect start foo bar &&
121 grep foo ".git/BISECT_NAMES" &&
122 grep bar ".git/BISECT_NAMES"
125 test_expect_success
'bisect reset: back in a branch checked out also elsewhere' '
126 echo "shared" > branch.expect &&
127 test_bisect_reset() {
128 git -C $1 bisect start &&
129 git -C $1 bisect good $HASH1 &&
130 git -C $1 bisect bad $HASH3 &&
131 git -C $1 bisect reset &&
132 git -C $1 branch --show-current > branch.output &&
133 cmp branch.expect branch.output
136 git worktree remove wt1 &&
137 git worktree remove wt2 &&
140 git worktree add wt1 -b shared &&
141 git worktree add wt2 -f shared &&
142 # we test in both worktrees to ensure that works
143 # as expected with "first" and "next" worktrees
144 test_bisect_reset wt1 &&
145 test_bisect_reset wt2
148 test_expect_success
'bisect reset: back in the main branch' '
150 echo "* main" > branch.expect &&
151 git branch > branch.output &&
152 cmp branch.expect branch.output
155 test_expect_success
'bisect reset: back in another branch' '
156 git checkout -b other &&
158 git bisect good $HASH1 &&
159 git bisect bad $HASH3 &&
161 echo " main" > branch.expect &&
162 echo "* other" >> branch.expect &&
163 git branch > branch.output &&
164 cmp branch.expect branch.output
167 test_expect_success
'bisect reset when not bisecting' '
169 git branch > branch.output &&
170 cmp branch.expect branch.output
173 test_expect_success
'bisect reset cleans up even when not bisecting' '
174 echo garbage >.git/BISECT_LOG &&
176 test_path_is_missing .git/BISECT_LOG
179 test_expect_success
'bisect reset removes packed refs' '
182 git bisect good $HASH1 &&
183 git bisect bad $HASH3 &&
184 git pack-refs --all --prune &&
187 test -z "$(git for-each-ref "refs/bisect/*")" &&
188 test -z "$(git for-each-ref "refs/heads/bisect")"
191 test_expect_success
'bisect reset removes bisect state after --no-checkout' '
193 git bisect start --no-checkout &&
194 git bisect good $HASH1 &&
195 git bisect bad $HASH3 &&
198 test -z "$(git for-each-ref "refs/bisect/*")" &&
199 test -z "$(git for-each-ref "refs/heads/bisect")" &&
200 test -z "$(git for-each-ref "BISECT_HEAD")"
203 test_expect_success
'bisect start: back in good branch' '
204 git branch > branch.output &&
205 grep "* other" branch.output > /dev/null &&
206 git bisect start $HASH4 $HASH1 -- &&
208 git bisect start $HASH4 $HASH1 -- &&
211 git branch > branch.output &&
212 grep "* other" branch.output > /dev/null
215 test_expect_success
'bisect start: no ".git/BISECT_START" created if junk rev' '
217 test_must_fail git bisect start $HASH4 foo -- &&
218 git branch > branch.output &&
219 grep "* other" branch.output > /dev/null &&
220 test_path_is_missing .git/BISECT_START
223 test_expect_success
'bisect start: existing ".git/BISECT_START" not modified if junk rev' '
224 git bisect start $HASH4 $HASH1 -- &&
226 cp .git/BISECT_START saved &&
227 test_must_fail git bisect start $HASH4 foo -- &&
228 git branch > branch.output &&
229 test_grep "* (no branch, bisect started on other)" branch.output > /dev/null &&
230 test_cmp saved .git/BISECT_START
232 test_expect_success
'bisect start: no ".git/BISECT_START" if mistaken rev' '
233 git bisect start $HASH4 $HASH1 -- &&
235 test_must_fail git bisect start $HASH1 $HASH4 -- &&
236 git branch > branch.output &&
237 grep "* other" branch.output > /dev/null &&
238 test_path_is_missing .git/BISECT_START
241 test_expect_success
'bisect start: no ".git/BISECT_START" if checkout error' '
242 echo "temp stuff" > hello &&
243 test_must_fail git bisect start $HASH4 $HASH1 -- &&
245 git branch > branch.output &&
246 grep "* other" branch.output > /dev/null &&
247 test_path_is_missing .git/BISECT_START &&
248 test -z "$(git for-each-ref "refs/bisect/*")" &&
249 git checkout HEAD hello
252 # $HASH1 is good, $HASH4 is bad, we skip $HASH3
254 # so we should find $HASH2 as the first bad commit
255 test_expect_success
'bisect skip: successful result' '
256 test_when_finished git bisect reset &&
258 git bisect start $HASH4 $HASH1 &&
260 git bisect bad > my_bisect_log.txt &&
261 grep "$HASH2 is the first bad commit" my_bisect_log.txt
264 # $HASH1 is good, $HASH4 is bad, we skip $HASH3 and $HASH2
265 # so we should not be able to tell the first bad commit
266 # among $HASH2, $HASH3 and $HASH4
267 test_expect_success
'bisect skip: cannot tell between 3 commits' '
268 test_when_finished git bisect reset &&
269 git bisect start $HASH4 $HASH1 &&
271 test_expect_code 2 git bisect skip >my_bisect_log.txt &&
272 grep "first bad commit could be any of" my_bisect_log.txt &&
273 ! grep $HASH1 my_bisect_log.txt &&
274 grep $HASH2 my_bisect_log.txt &&
275 grep $HASH3 my_bisect_log.txt &&
276 grep $HASH4 my_bisect_log.txt
279 # $HASH1 is good, $HASH4 is bad, we skip $HASH3
280 # but $HASH2 is good,
281 # so we should not be able to tell the first bad commit
282 # among $HASH3 and $HASH4
283 test_expect_success
'bisect skip: cannot tell between 2 commits' '
284 test_when_finished git bisect reset &&
285 git bisect start $HASH4 $HASH1 &&
287 test_expect_code 2 git bisect good >my_bisect_log.txt &&
288 grep "first bad commit could be any of" my_bisect_log.txt &&
289 ! grep $HASH1 my_bisect_log.txt &&
290 ! grep $HASH2 my_bisect_log.txt &&
291 grep $HASH3 my_bisect_log.txt &&
292 grep $HASH4 my_bisect_log.txt
295 # $HASH1 is good, $HASH4 is both skipped and bad, we skip $HASH3
296 # and $HASH2 is good,
297 # so we should not be able to tell the first bad commit
298 # among $HASH3 and $HASH4
299 test_expect_success
'bisect skip: with commit both bad and skipped' '
300 test_when_finished git bisect reset &&
304 git bisect good $HASH1 &&
306 test_expect_code 2 git bisect good >my_bisect_log.txt &&
307 grep "first bad commit could be any of" my_bisect_log.txt &&
308 ! grep $HASH1 my_bisect_log.txt &&
309 ! grep $HASH2 my_bisect_log.txt &&
310 grep $HASH3 my_bisect_log.txt &&
311 grep $HASH4 my_bisect_log.txt
314 test_bisect_run_args
() {
315 test_when_finished
"rm -f run.sh actual" &&
318 cat <&6 >expect.out
&&
319 cat <&7 >expect.err
&&
320 write_script run.sh
<<-\EOF &&
328 test_when_finished "git bisect reset" &&
330 git bisect good $HASH1 &&
331 git bisect bad $HASH4 &&
332 git bisect run ./run.sh $@ >actual.out.raw 2>actual.err &&
333 # Prune just the log output
341 <actual.out.raw >actual.out &&
342 test_cmp expect.out actual.out &&
343 test_cmp expect.err actual.err &&
344 test_cmp expect.args actual.args
347 test_expect_success 'git bisect run: args, stdout and stderr with no arguments' "
348 test_bisect_run_args <<-'EOF_ARGS' 6<<-EOF_OUT 7<<-'EOF_ERR'
351 $HASH4 is the first bad commit
352 bisect found first bad commit
357 test_expect_success 'git bisect run: args, stdout and stderr: "--" argument' "
358 test_bisect_run_args -- <<-'EOF_ARGS' 6<<-EOF_OUT 7<<-'EOF_ERR'
361 running './run.sh' '--'
362 $HASH4 is the first bad commit
363 bisect found first bad commit
368 test_expect_success 'git bisect run: args, stdout and stderr: "--log foo --no-log bar" arguments' "
369 test_bisect_run_args --log foo --no-log bar <<-'EOF_ARGS' 6<<-EOF_OUT 7<<-'EOF_ERR'
375 running './run.sh' '--log' 'foo' '--no-log' 'bar'
376 $HASH4 is the first bad commit
377 bisect found first bad commit
382 test_expect_success 'git bisect run: args, stdout and stderr: "--bisect-start" argument' "
383 test_bisect_run_args --bisect-start <<-'EOF_ARGS' 6<<-EOF_OUT 7<<-'EOF_ERR'
386 running './run.sh' '--bisect-start'
387 $HASH4 is the first bad commit
388 bisect found first bad commit
393 test_expect_success 'git bisect run: negative exit code' "
394 write_script fail.sh <<-'EOF' &&
397 cat <<-'EOF' >expect &&
398 bisect run failed: exit code -1 from './fail.sh' is < 0 or >= 128
400 test_when_finished 'git bisect reset' &&
402 git bisect good $HASH1 &&
403 git bisect bad $HASH4 &&
404 ! git bisect run ./fail.sh 2>err &&
405 sed -En 's/.*(bisect.*code) (-?
[0-9]+) (from.
*)/\
1 -1 \
3/p
' err >actual &&
406 test_cmp expect actual
409 test_expect_success 'git bisect run
: unable to verify on good
' "
410 write_script fail.sh <<-'EOF' &&
411 head=\$(git rev-parse --verify HEAD)
412 good=\$(git rev-parse --verify $HASH1)
413 if test "\$head" = "\$good"
420 cat <<-'EOF' >expect &&
421 unable to verify '.
/fail.sh
' on good revision
423 test_when_finished 'git bisect
reset' &&
425 git bisect good $HASH1 &&
426 git bisect bad $HASH4 &&
427 ! git bisect run ./fail.sh 2>err &&
428 sed -n 's
/.
*\
(unable to verify.
*\
)/\
1/p
' err >actual &&
429 test_cmp expect actual
432 # We want to automatically find the commit that
433 # added "Another" into hello.
434 test_expect_success '"git bisect run" simple
case' '
435 write_script test_script.sh
<<-\EOF &&
436 ! grep Another hello >/dev/null
439 git bisect good $HASH1 &&
440 git bisect bad $HASH4 &&
441 git bisect run ./test_script.sh >my_bisect_log.txt &&
442 grep "$HASH3 is the first bad commit" my_bisect_log.txt &&
446 # We want to make sure no arguments has been eaten
447 test_expect_success '"git bisect run" simple case' '
449 git bisect good $HASH1 &&
450 git bisect bad $HASH4 &&
451 git bisect run printf "%s %s\n" reset --bisect-skip >my_bisect_log.txt &&
452 grep -e "reset --bisect-skip" my_bisect_log.txt &&
456 # We want to automatically find the commit that
457 # added "Ciao" into hello.
458 test_expect_success '"git bisect run" with more complex "git bisect start"' '
459 write_script test_script.sh <<-\
EOF &&
460 ! grep Ciao hello >/dev/null
462 git bisect start $HASH4 $HASH1 &&
463 git bisect run ./test_script.sh >my_bisect_log.txt &&
464 grep "$HASH4 is the first bad commit" my_bisect_log.txt &&
468 test_expect_success 'bisect run accepts exit code 126 as bad' '
469 test_when_finished "git bisect reset" &&
470 write_script test_script.sh <<-\EOF &&
471 ! grep Another hello || exit 126 >/dev/null
474 git bisect good $HASH1 &&
475 git bisect bad $HASH4 &&
476 git bisect run ./test_script.sh >my_bisect_log.txt &&
477 grep "$HASH3 is the first bad commit" my_bisect_log.txt
480 test_expect_success POSIXPERM 'bisect run fails with non-executable test script' '
481 test_when_finished "git bisect reset" &&
482 >not-executable.sh &&
483 chmod -x not-executable.sh &&
485 git bisect good $HASH1 &&
486 git bisect bad $HASH4 &&
487 test_must_fail git bisect run ./not-executable.sh >my_bisect_log.txt &&
488 ! grep "is the first bad commit" my_bisect_log.txt
491 test_expect_success 'bisect run accepts exit code 127 as bad' '
492 test_when_finished "git bisect reset" &&
493 write_script test_script.sh <<-\EOF &&
494 ! grep Another hello || exit 127 >/dev/null
497 git bisect good $HASH1 &&
498 git bisect bad $HASH4 &&
499 git bisect run ./test_script.sh >my_bisect_log.txt &&
500 grep "$HASH3 is the first bad commit" my_bisect_log.txt
503 test_expect_success 'bisect run fails with missing test script' '
504 test_when_finished "git bisect reset" &&
505 rm -f does-not-exist.sh &&
507 git bisect good $HASH1 &&
508 git bisect bad $HASH4 &&
509 test_must_fail git bisect run ./does-not-exist.sh >my_bisect_log.txt &&
510 ! grep "is the first bad commit" my_bisect_log.txt
513 # $HASH1 is good, $HASH5 is bad, we skip $HASH3
514 # but $HASH4 is good,
515 # so we should find $HASH5 as the first bad commit
517 test_expect_success 'bisect skip: add line and then a new test' '
518 add_line_into_file "5: Another new line." hello &&
519 HASH5=$(git rev-parse --verify HEAD) &&
520 git bisect start $HASH5 $HASH1 &&
522 git bisect good > my_bisect_log.txt &&
523 grep "$HASH5 is the first bad commit" my_bisect_log.txt &&
524 git bisect log > log_to_replay.txt &&
528 test_expect_success 'bisect skip and bisect replay' '
529 git bisect replay log_to_replay.txt > my_bisect_log.txt &&
530 grep "$HASH5 is the first bad commit" my_bisect_log.txt &&
535 test_expect_success 'bisect run & skip: cannot tell between 2' '
536 add_line_into_file "6: Yet a line." hello &&
537 HASH6=$(git rev-parse --verify HEAD) &&
538 write_script test_script.sh <<-\EOF &&
539 sed -ne \$p hello | grep Ciao >/dev/null && exit 125
540 ! grep line hello >/dev/null
542 git bisect start $HASH6 $HASH1 &&
543 test_expect_code 2 git bisect run ./test_script.sh >my_bisect_log.txt &&
544 grep "first bad commit could be any of" my_bisect_log.txt &&
545 ! grep $HASH3 my_bisect_log.txt &&
546 ! grep $HASH6 my_bisect_log.txt &&
547 grep $HASH4 my_bisect_log.txt &&
548 grep $HASH5 my_bisect_log.txt
552 test_expect_success 'bisect run & skip: find first bad' '
554 add_line_into_file "7: Should be the last line." hello &&
555 HASH7=$(git rev-parse --verify HEAD) &&
556 write_script test_script.sh <<-\EOF &&
557 sed -ne \$p hello | grep Ciao >/dev/null && exit 125
558 sed -ne \$p hello | grep day >/dev/null && exit 125
559 ! grep Yet hello >/dev/null
561 git bisect start $HASH7 $HASH1 &&
562 git bisect run ./test_script.sh >my_bisect_log.txt &&
563 grep "$HASH6 is the first bad commit" my_bisect_log.txt
566 test_expect_success 'bisect skip only one range' '
568 git bisect start $HASH7 $HASH1 &&
569 git bisect skip $HASH1..$HASH5 &&
570 test "$HASH6" = "$(git rev-parse --verify HEAD)" &&
571 test_must_fail git bisect bad > my_bisect_log.txt &&
572 grep "first bad commit could be any of" my_bisect_log.txt
575 test_expect_success 'bisect skip many ranges' '
576 git bisect start $HASH7 $HASH1 &&
577 test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
578 git bisect skip $HASH2 $HASH2.. ..$HASH5 &&
579 test "$HASH6" = "$(git rev-parse --verify HEAD)" &&
580 test_must_fail git bisect bad > my_bisect_log.txt &&
581 grep "first bad commit could be any of" my_bisect_log.txt
584 test_expect_success 'bisect starting with a detached HEAD' '
586 git checkout main^ &&
587 HEAD=$(git rev-parse --verify HEAD) &&
589 test $HEAD = $(cat .git/BISECT_START) &&
591 test $HEAD = $(git rev-parse --verify HEAD)
594 test_expect_success 'bisect errors out if bad and good are mistaken' '
596 test_must_fail git bisect start $HASH2 $HASH4 2> rev_list_error &&
597 test_grep "mistook good and bad" rev_list_error &&
601 test_expect_success 'bisect does not create a "bisect" branch' '
603 git bisect start $HASH7 $HASH1 &&
605 rev_hash4=$(git rev-parse --verify HEAD) &&
606 test "$rev_hash4" = "$HASH4" &&
607 git branch -D bisect &&
610 rev_hash6=$(git rev-parse --verify HEAD) &&
611 test "$rev_hash6" = "$HASH6" &&
612 git bisect good > my_bisect_log.txt &&
613 grep "$HASH7 is the first bad commit" my_bisect_log.txt &&
615 rev_hash6=$(git rev-parse --verify bisect) &&
616 test "$rev_hash6" = "$HASH6" &&
620 # This creates a "side" branch to test "siblings" cases.
622 # H1-H2-H3-H4-H5-H6-H7 <--other
626 test_expect_success 'side branch creation' '
628 git checkout -b side $HASH4 &&
629 add_line_into_file "5(side): first line on a side branch" hello2 &&
630 SIDE_HASH5=$(git rev-parse --verify HEAD) &&
631 add_line_into_file "6(side): second line on a side branch" hello2 &&
632 SIDE_HASH6=$(git rev-parse --verify HEAD) &&
633 add_line_into_file "7(side): third line on a side branch" hello2 &&
634 SIDE_HASH7=$(git rev-parse --verify HEAD)
637 test_expect_success 'good merge base when good and bad are siblings' '
638 git bisect start "$HASH7" "$SIDE_HASH7" > my_bisect_log.txt &&
639 test_grep "merge base must be tested" my_bisect_log.txt &&
640 grep $HASH4 my_bisect_log.txt &&
641 git bisect good > my_bisect_log.txt &&
642 ! grep "merge base must be tested" my_bisect_log.txt &&
643 grep $HASH6 my_bisect_log.txt &&
646 test_expect_success 'skipped merge base when good and bad are siblings' '
647 git bisect start "$SIDE_HASH7" "$HASH7" > my_bisect_log.txt &&
648 test_grep "merge base must be tested" my_bisect_log.txt &&
649 grep $HASH4 my_bisect_log.txt &&
650 git bisect skip > my_bisect_log.txt 2>&1 &&
651 grep "warning" my_bisect_log.txt &&
652 grep $SIDE_HASH6 my_bisect_log.txt &&
656 test_expect_success 'bad merge base when good and bad are siblings' '
657 git bisect start "$HASH7" HEAD > my_bisect_log.txt &&
658 test_grep "merge base must be tested" my_bisect_log.txt &&
659 grep $HASH4 my_bisect_log.txt &&
660 test_must_fail git bisect bad > my_bisect_log.txt 2>&1 &&
661 test_grep "merge base $HASH4 is bad" my_bisect_log.txt &&
662 test_grep "fixed between $HASH4 and \[$SIDE_HASH7\]" my_bisect_log.txt &&
666 # This creates a few more commits (A and B) to test "siblings" cases
667 # when a good and a bad rev have many merge bases.
669 # We should have the following:
671 # H1-H2-H3-H4-H5-H6-H7
677 # And there A and B have 2 merge bases (S5 and H5) that should be
678 # reported by "git merge-base --all A B".
680 test_expect_success 'many merge bases creation' '
681 git checkout "$SIDE_HASH5" &&
682 git merge -m "merge HASH5 and SIDE_HASH5" "$HASH5" &&
683 A_HASH=$(git rev-parse --verify HEAD) &&
685 git merge -m "merge HASH7 and SIDE_HASH7" "$HASH7" &&
686 B_HASH=$(git rev-parse --verify HEAD) &&
687 git merge-base --all "$A_HASH" "$B_HASH" > merge_bases.txt &&
688 test_line_count = 2 merge_bases.txt &&
689 grep "$HASH5" merge_bases.txt &&
690 grep "$SIDE_HASH5" merge_bases.txt
693 # We want to automatically find the merge that
694 # added "line" into hello.
695 test_expect_success '"git bisect run --first-parent" simple case' '
696 git rev-list --first-parent $B_HASH ^$HASH4 >first_parent_chain.txt &&
697 write_script test_script.sh <<-\EOF &&
698 grep $(git rev-parse HEAD) first_parent_chain.txt || exit -1
699 ! grep line hello >/dev/null
701 git bisect start --first-parent &&
702 test_path_is_file ".git/BISECT_FIRST_PARENT" &&
703 git bisect good $HASH4 &&
704 git bisect bad $B_HASH &&
705 git bisect run ./test_script.sh >my_bisect_log.txt &&
706 grep "$B_HASH is the first bad commit" my_bisect_log.txt &&
708 test_path_is_missing .git/BISECT_FIRST_PARENT
711 test_expect_success 'good merge bases when good and bad are siblings' '
712 git bisect start "$B_HASH" "$A_HASH" > my_bisect_log.txt &&
713 test_grep "merge base must be tested" my_bisect_log.txt &&
714 git bisect good > my_bisect_log2.txt &&
715 test_grep "merge base must be tested" my_bisect_log2.txt &&
718 grep "$SIDE_HASH5" my_bisect_log.txt &&
719 grep "$HASH5" my_bisect_log2.txt
721 grep "$SIDE_HASH5" my_bisect_log2.txt &&
722 grep "$HASH5" my_bisect_log.txt
728 test_expect_success 'optimized merge base checks' '
729 git bisect start "$HASH7" "$SIDE_HASH7" > my_bisect_log.txt &&
730 test_grep "merge base must be tested" my_bisect_log.txt &&
731 grep "$HASH4" my_bisect_log.txt &&
732 git bisect good > my_bisect_log2.txt &&
733 test -f ".git/BISECT_ANCESTORS_OK" &&
734 test "$HASH6" = $(git rev-parse --verify HEAD) &&
736 git bisect good "$A_HASH" > my_bisect_log4.txt &&
737 test_grep "merge base must be tested" my_bisect_log4.txt &&
738 test_path_is_missing ".git/BISECT_ANCESTORS_OK"
741 # This creates another side branch called "parallel" with some files
742 # in some directories, to test bisecting with paths.
744 # We should have the following:
746 # P1-P2-P3-P4-P5-P6-P7
748 # H1-H2-H3-H4-H5-H6-H7
754 test_expect_success '"parallel" side branch creation' '
756 git checkout -b parallel $HASH1 &&
758 add_line_into_file "1(para): line 1 on parallel branch" dir1/file1 &&
759 PARA_HASH1=$(git rev-parse --verify HEAD) &&
760 add_line_into_file "2(para): line 2 on parallel branch" dir2/file2 &&
761 PARA_HASH2=$(git rev-parse --verify HEAD) &&
762 add_line_into_file "3(para): line 3 on parallel branch" dir2/file3 &&
763 PARA_HASH3=$(git rev-parse --verify HEAD) &&
764 git merge -m "merge HASH4 and PARA_HASH3" "$HASH4" &&
765 PARA_HASH4=$(git rev-parse --verify HEAD) &&
766 add_line_into_file "5(para): add line on parallel branch" dir1/file1 &&
767 PARA_HASH5=$(git rev-parse --verify HEAD) &&
768 add_line_into_file "6(para): add line on parallel branch" dir2/file2 &&
769 PARA_HASH6=$(git rev-parse --verify HEAD) &&
770 git merge -m "merge HASH7 and PARA_HASH6" "$HASH7" &&
771 PARA_HASH7=$(git rev-parse --verify HEAD)
774 test_expect_success 'restricting bisection on one dir' '
776 git bisect start HEAD $HASH1 -- dir1 &&
777 para1=$(git rev-parse --verify HEAD) &&
778 test "$para1" = "$PARA_HASH1" &&
779 git bisect bad > my_bisect_log.txt &&
780 grep "$PARA_HASH1 is the first bad commit" my_bisect_log.txt
783 test_expect_success 'restricting bisection on one dir and a file' '
785 git bisect start HEAD $HASH1 -- dir1 hello &&
786 para4=$(git rev-parse --verify HEAD) &&
787 test "$para4" = "$PARA_HASH4" &&
789 hash3=$(git rev-parse --verify HEAD) &&
790 test "$hash3" = "$HASH3" &&
792 hash4=$(git rev-parse --verify HEAD) &&
793 test "$hash4" = "$HASH4" &&
795 para1=$(git rev-parse --verify HEAD) &&
796 test "$para1" = "$PARA_HASH1" &&
797 git bisect good > my_bisect_log.txt &&
798 grep "$PARA_HASH4 is the first bad commit" my_bisect_log.txt
801 test_expect_success 'skipping away from skipped commit' '
802 git bisect start $PARA_HASH7 $HASH1 &&
803 para4=$(git rev-parse --verify HEAD) &&
804 test "$para4" = "$PARA_HASH4" &&
806 hash7=$(git rev-parse --verify HEAD) &&
807 test "$hash7" = "$HASH7" &&
809 para3=$(git rev-parse --verify HEAD) &&
810 test "$para3" = "$PARA_HASH3"
813 test_expect_success 'erroring out when using bad path arguments' '
814 test_must_fail git bisect start $PARA_HASH7 $HASH1 -- foobar 2> error.txt &&
815 test_grep "bad path arguments" error.txt
818 test_expect_success 'test bisection on bare repo - --no-checkout specified
' '
819 git clone
--bare . bare.nocheckout
&&
821 cd bare.nocheckout
&&
822 git bisect start
--no-checkout &&
823 git bisect good
$HASH1 &&
824 git bisect bad
$HASH4 &&
825 git bisect run
eval \
826 "test \$(git rev-list BISECT_HEAD ^$HASH2 --max-count=1 | wc -l) = 0" \
829 grep "$HASH3 is the first bad commit" nocheckout.log
833 test_expect_success 'test bisection on bare repo
- --no-checkout defaulted
' '
834 git clone
--bare . bare.defaulted
&&
838 git bisect good
$HASH1 &&
839 git bisect bad
$HASH4 &&
840 git bisect run
eval \
841 "test \$(git rev-list BISECT_HEAD ^$HASH2 --max-count=1 | wc -l) = 0" \
844 grep "$HASH3 is the first bad commit" defaulted.log
848 # This creates a broken branch which cannot be checked out because
849 # the tree created has been deleted.
851 # H1-H2-H3-H4-H5-H6-H7 <--other
853 # S5-S6'-S7'-S8'-S9 <--broken
855 # Commits marked with ' have a missing tree.
857 test_expect_success
'broken branch creation' '
859 git checkout -b broken $HASH4 &&
860 git tag BROKEN_HASH4 $HASH4 &&
861 add_line_into_file "5(broken): first line on a broken branch" hello2 &&
862 git tag BROKEN_HASH5 &&
864 :> missing/MISSING &&
865 git add missing/MISSING &&
866 git commit -m "6(broken): Added file that will be deleted" &&
867 git tag BROKEN_HASH6 &&
868 deleted=$(git rev-parse --verify HEAD:missing) &&
869 add_line_into_file "7(broken): second line on a broken branch" hello2 &&
870 git tag BROKEN_HASH7 &&
871 add_line_into_file "8(broken): third line on a broken branch" hello2 &&
872 git tag BROKEN_HASH8 &&
873 git rm missing/MISSING &&
874 git commit -m "9(broken): Remove missing file" &&
875 git tag BROKEN_HASH9 &&
876 rm .git/objects/$(test_oid_to_path $deleted)
879 echo "" > expected.ok
880 cat > expected.missing-tree.default
<<EOF
881 fatal: unable to read tree ($deleted)
884 test_expect_success
'bisect fails if tree is broken on start commit' '
886 test_must_fail git bisect start BROKEN_HASH7 BROKEN_HASH4 2>error.txt &&
887 test_cmp expected.missing-tree.default error.txt
890 test_expect_success
'bisect fails if tree is broken on trial commit' '
892 test_must_fail git bisect start BROKEN_HASH9 BROKEN_HASH4 2>error.txt &&
893 git reset --hard broken &&
894 git checkout broken &&
895 test_cmp expected.missing-tree.default error.txt
900 echo "Checking $1 is the same as $2" &&
901 test_cmp_rev
"$1" "$2"
904 test_expect_success
'bisect: --no-checkout - start commit bad' '
906 git bisect start BROKEN_HASH7 BROKEN_HASH4 --no-checkout &&
907 check_same BROKEN_HASH6 BISECT_HEAD &&
911 test_expect_success
'bisect: --no-checkout - trial commit bad' '
913 git bisect start broken BROKEN_HASH4 --no-checkout &&
914 check_same BROKEN_HASH6 BISECT_HEAD &&
918 test_expect_success
'bisect: --no-checkout - target before breakage' '
920 git bisect start broken BROKEN_HASH4 --no-checkout &&
921 check_same BROKEN_HASH6 BISECT_HEAD &&
922 git bisect bad BISECT_HEAD &&
923 check_same BROKEN_HASH5 BISECT_HEAD &&
924 git bisect bad BISECT_HEAD &&
925 check_same BROKEN_HASH5 bisect/bad &&
929 test_expect_success
'bisect: --no-checkout - target in breakage' '
931 git bisect start broken BROKEN_HASH4 --no-checkout &&
932 check_same BROKEN_HASH6 BISECT_HEAD &&
933 git bisect bad BISECT_HEAD &&
934 check_same BROKEN_HASH5 BISECT_HEAD &&
935 test_must_fail git bisect good BISECT_HEAD &&
936 check_same BROKEN_HASH6 bisect/bad &&
940 test_expect_success
'bisect: --no-checkout - target after breakage' '
942 git bisect start broken BROKEN_HASH4 --no-checkout &&
943 check_same BROKEN_HASH6 BISECT_HEAD &&
944 git bisect good BISECT_HEAD &&
945 check_same BROKEN_HASH8 BISECT_HEAD &&
946 test_must_fail git bisect good BISECT_HEAD &&
947 check_same BROKEN_HASH9 bisect/bad &&
951 test_expect_success
'bisect: demonstrate identification of damage boundary' "
953 git checkout broken &&
954 git bisect start broken main --no-checkout &&
955 test_must_fail git bisect run \"\$SHELL_PATH\" -c '
956 GOOD=\$(git for-each-ref \"--format=%(objectname)\" refs/bisect/good-*) &&
957 git rev-list --objects BISECT_HEAD --not \$GOOD >tmp.\$\$ &&
958 git pack-objects --stdout >/dev/null < tmp.\$\$
962 check_same BROKEN_HASH6 bisect/bad &&
966 cat > expected.bisect-log
<<EOF
967 # bad: [$HASH4] Add <4: Ciao for now> into <hello>.
968 # good: [$HASH2] Add <2: A new day for git> into <hello>.
969 git bisect start '$HASH4' '$HASH2'
970 # good: [$HASH3] Add <3: Another new day for git> into <hello>.
971 git bisect good $HASH3
972 # first bad commit: [$HASH4] Add <4: Ciao for now> into <hello>.
975 test_expect_success
'bisect log: successful result' '
977 git bisect start $HASH4 $HASH2 &&
979 git bisect log >bisect-log.txt &&
980 test_cmp expected.bisect-log bisect-log.txt &&
984 cat > expected.bisect-skip-log
<<EOF
985 # bad: [$HASH4] Add <4: Ciao for now> into <hello>.
986 # good: [$HASH2] Add <2: A new day for git> into <hello>.
987 git bisect start '$HASH4' '$HASH2'
988 # skip: [$HASH3] Add <3: Another new day for git> into <hello>.
989 git bisect skip $HASH3
990 # only skipped commits left to test
991 # possible first bad commit: [$HASH4] Add <4: Ciao for now> into <hello>.
992 # possible first bad commit: [$HASH3] Add <3: Another new day for git> into <hello>.
995 test_expect_success
'bisect log: only skip commits left' '
997 git bisect start $HASH4 $HASH2 &&
998 test_must_fail git bisect skip &&
999 git bisect log >bisect-skip-log.txt &&
1000 test_cmp expected.bisect-skip-log bisect-skip-log.txt &&
1004 test_expect_success
'"git bisect bad HEAD" behaves as "git bisect bad"' '
1005 git checkout parallel &&
1006 git bisect start HEAD $HASH1 &&
1007 git bisect good HEAD &&
1008 git bisect bad HEAD &&
1009 test "$HASH6" = $(git rev-parse --verify HEAD) &&
1013 test_expect_success
'bisect starts with only one new' '
1016 git bisect new $HASH4 &&
1020 test_expect_success
'bisect does not start with only one old' '
1023 git bisect old $HASH1 &&
1024 test_must_fail git bisect next
1027 test_expect_success
'bisect start with one new and old' '
1030 git bisect old $HASH1 &&
1031 git bisect new $HASH4 &&
1033 git bisect new >bisect_result &&
1034 grep "$HASH2 is the first new commit" bisect_result &&
1035 git bisect log >log_to_replay.txt &&
1039 test_expect_success
'bisect replay with old and new' '
1040 git bisect replay log_to_replay.txt >bisect_result &&
1041 grep "$HASH2 is the first new commit" bisect_result &&
1045 test_expect_success
'bisect replay with CRLF log' '
1046 append_cr <log_to_replay.txt >log_to_replay_crlf.txt &&
1047 git bisect replay log_to_replay_crlf.txt >bisect_result_crlf &&
1048 grep "$HASH2 is the first new commit" bisect_result_crlf &&
1052 test_expect_success
'bisect cannot mix old/new and good/bad' '
1054 git bisect bad $HASH4 &&
1055 test_must_fail git bisect old $HASH1
1058 test_expect_success
'bisect terms needs 0 or 1 argument' '
1060 test_must_fail git bisect terms only-one &&
1061 test_must_fail git bisect terms 1 2 &&
1062 test_must_fail git bisect terms 2>actual &&
1063 echo "error: no terms defined" >expected &&
1064 test_cmp expected actual
1067 test_expect_success
'bisect terms shows good/bad after start' '
1069 git bisect start HEAD $HASH1 &&
1070 git bisect terms --term-good >actual &&
1071 echo good >expected &&
1072 test_cmp expected actual &&
1073 git bisect terms --term-bad >actual &&
1074 echo bad >expected &&
1075 test_cmp expected actual
1078 test_expect_success
'bisect start with one term1 and term2' '
1080 git bisect start --term-old term2 --term-new term1 &&
1081 git bisect term2 $HASH1 &&
1082 git bisect term1 $HASH4 &&
1084 git bisect term1 >bisect_result &&
1085 grep "$HASH2 is the first term1 commit" bisect_result &&
1086 git bisect log >log_to_replay.txt &&
1090 test_expect_success
'bogus command does not start bisect' '
1092 test_must_fail git bisect --bisect-terms 1 2 2>out &&
1093 ! grep "You need to start" out &&
1094 test_must_fail git bisect --bisect-terms 2>out &&
1095 ! grep "You need to start" out &&
1096 grep "git bisect.*visualize" out &&
1100 test_expect_success
'bisect replay with term1 and term2' '
1101 git bisect replay log_to_replay.txt >bisect_result &&
1102 grep "$HASH2 is the first term1 commit" bisect_result &&
1106 test_expect_success
'bisect start term1 term2' '
1108 git bisect start --term-new term1 --term-old term2 $HASH4 $HASH1 &&
1110 git bisect term1 >bisect_result &&
1111 grep "$HASH2 is the first term1 commit" bisect_result &&
1112 git bisect log >log_to_replay.txt &&
1116 test_expect_success
'bisect cannot mix terms' '
1118 git bisect start --term-good term1 --term-bad term2 $HASH4 $HASH1 &&
1119 test_must_fail git bisect a &&
1120 test_must_fail git bisect b &&
1121 test_must_fail git bisect bad &&
1122 test_must_fail git bisect good &&
1123 test_must_fail git bisect new &&
1124 test_must_fail git bisect old
1127 test_expect_success
'bisect terms rejects invalid terms' '
1129 test_must_fail git bisect start --term-good &&
1130 test_must_fail git bisect start --term-good invalid..term &&
1131 test_must_fail git bisect start --term-bad &&
1132 test_must_fail git bisect terms --term-bad invalid..term &&
1133 test_must_fail git bisect terms --term-good bad &&
1134 test_must_fail git bisect terms --term-good old &&
1135 test_must_fail git bisect terms --term-good skip &&
1136 test_must_fail git bisect terms --term-good reset &&
1137 test_path_is_missing .git/BISECT_TERMS
1140 test_expect_success
'bisect start --term-* does store terms' '
1142 git bisect start --term-bad=one --term-good=two &&
1143 git bisect terms >actual &&
1144 cat <<-EOF >expected &&
1145 Your current terms are two for the old state
1146 and one for the new state.
1148 test_cmp expected actual &&
1149 git bisect terms --term-bad >actual &&
1150 echo one >expected &&
1151 test_cmp expected actual &&
1152 git bisect terms --term-good >actual &&
1153 echo two >expected &&
1154 test_cmp expected actual
1157 test_expect_success
'bisect start takes options and revs in any order' '
1159 git bisect start --term-good one $HASH4 \
1160 --term-good two --term-bad bad-term \
1161 $HASH1 --term-good three -- &&
1162 (git bisect terms --term-bad && git bisect terms --term-good) >actual &&
1163 printf "%s\n%s\n" bad-term three >expected &&
1164 test_cmp expected actual
1167 # Bisect is started with --term-new and --term-old arguments,
1168 # then skip. The HEAD should be changed.
1169 test_expect_success
'bisect skip works with --term*' '
1171 git bisect start --term-new=fixed --term-old=unfixed HEAD $HASH1 &&
1172 hash_skipped_from=$(git rev-parse --verify HEAD) &&
1174 hash_skipped_to=$(git rev-parse --verify HEAD) &&
1175 test "$hash_skipped_from" != "$hash_skipped_to"
1178 test_expect_success
'git bisect reset cleans bisection state properly' '
1181 git bisect good $HASH1 &&
1182 git bisect bad $HASH4 &&
1184 test -z "$(git for-each-ref "refs/bisect/*")" &&
1185 test_ref_missing BISECT_EXPECTED_REV &&
1186 test_path_is_missing ".git/BISECT_ANCESTORS_OK" &&
1187 test_path_is_missing ".git/BISECT_LOG" &&
1188 test_path_is_missing ".git/BISECT_RUN" &&
1189 test_path_is_missing ".git/BISECT_TERMS" &&
1190 test_path_is_missing ".git/BISECT_HEAD" &&
1191 test_path_is_missing ".git/BISECT_START"
1194 test_expect_success
'bisect handles annotated tags' '
1195 test_commit commit-one &&
1196 git tag -m foo tag-one &&
1197 test_commit commit-two &&
1198 git tag -m foo tag-two &&
1200 git bisect good tag-one &&
1201 git bisect bad tag-two >output &&
1202 bad=$(git rev-parse --verify tag-two^{commit}) &&
1203 grep "$bad is the first bad commit" output
1206 test_expect_success
'bisect run fails with exit code equals or greater than 128' '
1207 write_script test_script.sh <<-\EOF &&
1210 test_must_fail git bisect run ./test_script.sh &&
1211 write_script test_script.sh <<-\EOF &&
1214 test_must_fail git bisect run ./test_script.sh
1217 test_expect_success
'bisect visualize with a filename with dash and space' '
1218 echo "My test line" >>"./-hello 2" &&
1219 git add -- "./-hello 2" &&
1220 git commit --quiet -m "Add test line" -- "./-hello 2" &&
1221 git bisect visualize -p -- "-hello 2"
1224 test_expect_success
'bisect state output with multiple good commits' '
1226 git bisect start >output &&
1227 grep "waiting for both good and bad commits" output &&
1228 git bisect log >output &&
1229 grep "waiting for both good and bad commits" output &&
1230 git bisect good "$HASH1" >output &&
1231 grep "waiting for bad commit, 1 good commit known" output &&
1232 git bisect log >output &&
1233 grep "waiting for bad commit, 1 good commit known" output &&
1234 git bisect good "$HASH2" >output &&
1235 grep "waiting for bad commit, 2 good commits known" output &&
1236 git bisect log >output &&
1237 grep "waiting for bad commit, 2 good commits known" output
1240 test_expect_success
'bisect state output with bad commit' '
1242 git bisect start >output &&
1243 grep "waiting for both good and bad commits" output &&
1244 git bisect log >output &&
1245 grep "waiting for both good and bad commits" output &&
1246 git bisect bad "$HASH4" >output &&
1247 grep -F "waiting for good commit(s), bad commit known" output &&
1248 git bisect log >output &&
1249 grep -F "waiting for good commit(s), bad commit known" output
1252 test_expect_success
'verify correct error message' '
1254 git bisect start $HASH4 $HASH1 &&
1255 write_script test_script.sh <<-\EOF &&
1258 test_must_fail git bisect run ./test_script.sh 2>error &&
1259 grep "git bisect good.*exited with error code" error