Merge branch 'dd/diff-customize-index-line-abbrev'
[git] / t / t6030-bisect-porcelain.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Christian Couder
4 #
5 test_description='Tests git bisect functionality'
6
7 exec </dev/null
8
9 . ./test-lib.sh
10
11 add_line_into_file()
12 {
13     _line=$1
14     _file=$2
15
16     if [ -f "$_file" ]; then
17         echo "$_line" >> $_file || return $?
18         MSG="Add <$_line> into <$_file>."
19     else
20         echo "$_line" > $_file || return $?
21         git add $_file || return $?
22         MSG="Create file <$_file> with <$_line> inside."
23     fi
24
25     test_tick
26     git commit --quiet -m "$MSG" $_file
27 }
28
29 HASH1=
30 HASH2=
31 HASH3=
32 HASH4=
33
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)
43 '
44
45 test_expect_success 'bisect starts with only one bad' '
46         git bisect reset &&
47         git bisect start &&
48         git bisect bad $HASH4 &&
49         git bisect next
50 '
51
52 test_expect_success 'bisect does not start with only one good' '
53         git bisect reset &&
54         git bisect start &&
55         git bisect good $HASH1 &&
56         test_must_fail git bisect next
57 '
58
59 test_expect_success 'bisect start with one bad and good' '
60         git bisect reset &&
61         git bisect start &&
62         git bisect good $HASH1 &&
63         git bisect bad $HASH4 &&
64         git bisect next
65 '
66
67 test_expect_success 'bisect fails if given any junk instead of revs' '
68         git bisect reset &&
69         test_must_fail git bisect start foo $HASH1 -- &&
70         test_must_fail git bisect start $HASH4 $HASH1 bar -- &&
71         test -z "$(git for-each-ref "refs/bisect/*")" &&
72         test -z "$(ls .git/BISECT_* 2>/dev/null)" &&
73         git bisect start &&
74         test_must_fail git bisect good foo $HASH1 &&
75         test_must_fail git bisect good $HASH1 bar &&
76         test_must_fail git bisect bad frotz &&
77         test_must_fail git bisect bad $HASH3 $HASH4 &&
78         test_must_fail git bisect skip bar $HASH3 &&
79         test_must_fail git bisect skip $HASH1 foo &&
80         test -z "$(git for-each-ref "refs/bisect/*")" &&
81         git bisect good $HASH1 &&
82         git bisect bad $HASH4
83 '
84
85 test_expect_success 'bisect reset: back in the master branch' '
86         git bisect reset &&
87         echo "* master" > branch.expect &&
88         git branch > branch.output &&
89         cmp branch.expect branch.output
90 '
91
92 test_expect_success 'bisect reset: back in another branch' '
93         git checkout -b other &&
94         git bisect start &&
95         git bisect good $HASH1 &&
96         git bisect bad $HASH3 &&
97         git bisect reset &&
98         echo "  master" > branch.expect &&
99         echo "* other" >> branch.expect &&
100         git branch > branch.output &&
101         cmp branch.expect branch.output
102 '
103
104 test_expect_success 'bisect reset when not bisecting' '
105         git bisect reset &&
106         git branch > branch.output &&
107         cmp branch.expect branch.output
108 '
109
110 test_expect_success 'bisect reset removes packed refs' '
111         git bisect reset &&
112         git bisect start &&
113         git bisect good $HASH1 &&
114         git bisect bad $HASH3 &&
115         git pack-refs --all --prune &&
116         git bisect next &&
117         git bisect reset &&
118         test -z "$(git for-each-ref "refs/bisect/*")" &&
119         test -z "$(git for-each-ref "refs/heads/bisect")"
120 '
121
122 test_expect_success 'bisect reset removes bisect state after --no-checkout' '
123         git bisect reset &&
124         git bisect start --no-checkout &&
125         git bisect good $HASH1 &&
126         git bisect bad $HASH3 &&
127         git bisect next &&
128         git bisect reset &&
129         test -z "$(git for-each-ref "refs/bisect/*")" &&
130         test -z "$(git for-each-ref "refs/heads/bisect")" &&
131         test -z "$(git for-each-ref "BISECT_HEAD")"
132 '
133
134 test_expect_success 'bisect start: back in good branch' '
135         git branch > branch.output &&
136         grep "* other" branch.output > /dev/null &&
137         git bisect start $HASH4 $HASH1 -- &&
138         git bisect good &&
139         git bisect start $HASH4 $HASH1 -- &&
140         git bisect bad &&
141         git bisect reset &&
142         git branch > branch.output &&
143         grep "* other" branch.output > /dev/null
144 '
145
146 test_expect_success 'bisect start: no ".git/BISECT_START" created if junk rev' '
147         git bisect reset &&
148         test_must_fail git bisect start $HASH4 foo -- &&
149         git branch > branch.output &&
150         grep "* other" branch.output > /dev/null &&
151         test_path_is_missing .git/BISECT_START
152 '
153
154 test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if junk rev' '
155         git bisect start $HASH4 $HASH1 -- &&
156         git bisect good &&
157         cp .git/BISECT_START saved &&
158         test_must_fail git bisect start $HASH4 foo -- &&
159         git branch > branch.output &&
160         test_i18ngrep "* (no branch, bisect started on other)" branch.output > /dev/null &&
161         test_cmp saved .git/BISECT_START
162 '
163 test_expect_success 'bisect start: no ".git/BISECT_START" if mistaken rev' '
164         git bisect start $HASH4 $HASH1 -- &&
165         git bisect good &&
166         test_must_fail git bisect start $HASH1 $HASH4 -- &&
167         git branch > branch.output &&
168         grep "* other" branch.output > /dev/null &&
169         test_path_is_missing .git/BISECT_START
170 '
171
172 test_expect_success 'bisect start: no ".git/BISECT_START" if checkout error' '
173         echo "temp stuff" > hello &&
174         test_must_fail git bisect start $HASH4 $HASH1 -- &&
175         git branch &&
176         git branch > branch.output &&
177         grep "* other" branch.output > /dev/null &&
178         test_path_is_missing .git/BISECT_START &&
179         test -z "$(git for-each-ref "refs/bisect/*")" &&
180         git checkout HEAD hello
181 '
182
183 # $HASH1 is good, $HASH4 is bad, we skip $HASH3
184 # but $HASH2 is bad,
185 # so we should find $HASH2 as the first bad commit
186 test_expect_success 'bisect skip: successful result' '
187         test_when_finished git bisect reset &&
188         git bisect reset &&
189         git bisect start $HASH4 $HASH1 &&
190         git bisect skip &&
191         git bisect bad > my_bisect_log.txt &&
192         grep "$HASH2 is the first bad commit" my_bisect_log.txt
193 '
194
195 # $HASH1 is good, $HASH4 is bad, we skip $HASH3 and $HASH2
196 # so we should not be able to tell the first bad commit
197 # among $HASH2, $HASH3 and $HASH4
198 test_expect_success 'bisect skip: cannot tell between 3 commits' '
199         test_when_finished git bisect reset &&
200         git bisect start $HASH4 $HASH1 &&
201         git bisect skip &&
202         test_expect_code 2 git bisect skip >my_bisect_log.txt &&
203         grep "first bad commit could be any of" my_bisect_log.txt &&
204         ! grep $HASH1 my_bisect_log.txt &&
205         grep $HASH2 my_bisect_log.txt &&
206         grep $HASH3 my_bisect_log.txt &&
207         grep $HASH4 my_bisect_log.txt
208 '
209
210 # $HASH1 is good, $HASH4 is bad, we skip $HASH3
211 # but $HASH2 is good,
212 # so we should not be able to tell the first bad commit
213 # among $HASH3 and $HASH4
214 test_expect_success 'bisect skip: cannot tell between 2 commits' '
215         test_when_finished git bisect reset &&
216         git bisect start $HASH4 $HASH1 &&
217         git bisect skip &&
218         test_expect_code 2 git bisect good >my_bisect_log.txt &&
219         grep "first bad commit could be any of" my_bisect_log.txt &&
220         ! grep $HASH1 my_bisect_log.txt &&
221         ! grep $HASH2 my_bisect_log.txt &&
222         grep $HASH3 my_bisect_log.txt &&
223         grep $HASH4 my_bisect_log.txt
224 '
225
226 # $HASH1 is good, $HASH4 is both skipped and bad, we skip $HASH3
227 # and $HASH2 is good,
228 # so we should not be able to tell the first bad commit
229 # among $HASH3 and $HASH4
230 test_expect_success 'bisect skip: with commit both bad and skipped' '
231         test_when_finished git bisect reset &&
232         git bisect start &&
233         git bisect skip &&
234         git bisect bad &&
235         git bisect good $HASH1 &&
236         git bisect skip &&
237         test_expect_code 2 git bisect good >my_bisect_log.txt &&
238         grep "first bad commit could be any of" my_bisect_log.txt &&
239         ! grep $HASH1 my_bisect_log.txt &&
240         ! grep $HASH2 my_bisect_log.txt &&
241         grep $HASH3 my_bisect_log.txt &&
242         grep $HASH4 my_bisect_log.txt
243 '
244
245 # We want to automatically find the commit that
246 # added "Another" into hello.
247 test_expect_success '"git bisect run" simple case' '
248         write_script test_script.sh <<-\EOF &&
249         ! grep Another hello >/dev/null
250         EOF
251         git bisect start &&
252         git bisect good $HASH1 &&
253         git bisect bad $HASH4 &&
254         git bisect run ./test_script.sh >my_bisect_log.txt &&
255         grep "$HASH3 is the first bad commit" my_bisect_log.txt &&
256         git bisect reset
257 '
258
259 # We want to automatically find the commit that
260 # added "Ciao" into hello.
261 test_expect_success '"git bisect run" with more complex "git bisect start"' '
262         write_script test_script.sh <<-\EOF &&
263         ! grep Ciao hello >/dev/null
264         EOF
265         git bisect start $HASH4 $HASH1 &&
266         git bisect run ./test_script.sh >my_bisect_log.txt &&
267         grep "$HASH4 is the first bad commit" my_bisect_log.txt &&
268         git bisect reset
269 '
270
271 # $HASH1 is good, $HASH5 is bad, we skip $HASH3
272 # but $HASH4 is good,
273 # so we should find $HASH5 as the first bad commit
274 HASH5=
275 test_expect_success 'bisect skip: add line and then a new test' '
276         add_line_into_file "5: Another new line." hello &&
277         HASH5=$(git rev-parse --verify HEAD) &&
278         git bisect start $HASH5 $HASH1 &&
279         git bisect skip &&
280         git bisect good > my_bisect_log.txt &&
281         grep "$HASH5 is the first bad commit" my_bisect_log.txt &&
282         git bisect log > log_to_replay.txt &&
283         git bisect reset
284 '
285
286 test_expect_success 'bisect skip and bisect replay' '
287         git bisect replay log_to_replay.txt > my_bisect_log.txt &&
288         grep "$HASH5 is the first bad commit" my_bisect_log.txt &&
289         git bisect reset
290 '
291
292 HASH6=
293 test_expect_success 'bisect run & skip: cannot tell between 2' '
294         add_line_into_file "6: Yet a line." hello &&
295         HASH6=$(git rev-parse --verify HEAD) &&
296         write_script test_script.sh <<-\EOF &&
297         sed -ne \$p hello | grep Ciao >/dev/null && exit 125
298         ! grep line hello >/dev/null
299         EOF
300         git bisect start $HASH6 $HASH1 &&
301         test_expect_code 2 git bisect run ./test_script.sh >my_bisect_log.txt &&
302         grep "first bad commit could be any of" my_bisect_log.txt &&
303         ! grep $HASH3 my_bisect_log.txt &&
304         ! grep $HASH6 my_bisect_log.txt &&
305         grep $HASH4 my_bisect_log.txt &&
306         grep $HASH5 my_bisect_log.txt
307 '
308
309 HASH7=
310 test_expect_success 'bisect run & skip: find first bad' '
311         git bisect reset &&
312         add_line_into_file "7: Should be the last line." hello &&
313         HASH7=$(git rev-parse --verify HEAD) &&
314         write_script test_script.sh <<-\EOF &&
315         sed -ne \$p hello | grep Ciao >/dev/null && exit 125
316         sed -ne \$p hello | grep day >/dev/null && exit 125
317         ! grep Yet hello >/dev/null
318         EOF
319         git bisect start $HASH7 $HASH1 &&
320         git bisect run ./test_script.sh >my_bisect_log.txt &&
321         grep "$HASH6 is the first bad commit" my_bisect_log.txt
322 '
323
324 test_expect_success 'bisect skip only one range' '
325         git bisect reset &&
326         git bisect start $HASH7 $HASH1 &&
327         git bisect skip $HASH1..$HASH5 &&
328         test "$HASH6" = "$(git rev-parse --verify HEAD)" &&
329         test_must_fail git bisect bad > my_bisect_log.txt &&
330         grep "first bad commit could be any of" my_bisect_log.txt
331 '
332
333 test_expect_success 'bisect skip many ranges' '
334         git bisect start $HASH7 $HASH1 &&
335         test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
336         git bisect skip $HASH2 $HASH2.. ..$HASH5 &&
337         test "$HASH6" = "$(git rev-parse --verify HEAD)" &&
338         test_must_fail git bisect bad > my_bisect_log.txt &&
339         grep "first bad commit could be any of" my_bisect_log.txt
340 '
341
342 test_expect_success 'bisect starting with a detached HEAD' '
343         git bisect reset &&
344         git checkout master^ &&
345         HEAD=$(git rev-parse --verify HEAD) &&
346         git bisect start &&
347         test $HEAD = $(cat .git/BISECT_START) &&
348         git bisect reset &&
349         test $HEAD = $(git rev-parse --verify HEAD)
350 '
351
352 test_expect_success 'bisect errors out if bad and good are mistaken' '
353         git bisect reset &&
354         test_must_fail git bisect start $HASH2 $HASH4 2> rev_list_error &&
355         test_i18ngrep "mistook good and bad" rev_list_error &&
356         git bisect reset
357 '
358
359 test_expect_success 'bisect does not create a "bisect" branch' '
360         git bisect reset &&
361         git bisect start $HASH7 $HASH1 &&
362         git branch bisect &&
363         rev_hash4=$(git rev-parse --verify HEAD) &&
364         test "$rev_hash4" = "$HASH4" &&
365         git branch -D bisect &&
366         git bisect good &&
367         git branch bisect &&
368         rev_hash6=$(git rev-parse --verify HEAD) &&
369         test "$rev_hash6" = "$HASH6" &&
370         git bisect good > my_bisect_log.txt &&
371         grep "$HASH7 is the first bad commit" my_bisect_log.txt &&
372         git bisect reset &&
373         rev_hash6=$(git rev-parse --verify bisect) &&
374         test "$rev_hash6" = "$HASH6" &&
375         git branch -D bisect
376 '
377
378 # This creates a "side" branch to test "siblings" cases.
379 #
380 # H1-H2-H3-H4-H5-H6-H7  <--other
381 #            \
382 #             S5-S6-S7  <--side
383 #
384 test_expect_success 'side branch creation' '
385         git bisect reset &&
386         git checkout -b side $HASH4 &&
387         add_line_into_file "5(side): first line on a side branch" hello2 &&
388         SIDE_HASH5=$(git rev-parse --verify HEAD) &&
389         add_line_into_file "6(side): second line on a side branch" hello2 &&
390         SIDE_HASH6=$(git rev-parse --verify HEAD) &&
391         add_line_into_file "7(side): third line on a side branch" hello2 &&
392         SIDE_HASH7=$(git rev-parse --verify HEAD)
393 '
394
395 test_expect_success 'good merge base when good and bad are siblings' '
396         git bisect start "$HASH7" "$SIDE_HASH7" > my_bisect_log.txt &&
397         test_i18ngrep "merge base must be tested" my_bisect_log.txt &&
398         grep $HASH4 my_bisect_log.txt &&
399         git bisect good > my_bisect_log.txt &&
400         ! grep "merge base must be tested" my_bisect_log.txt &&
401         grep $HASH6 my_bisect_log.txt &&
402         git bisect reset
403 '
404 test_expect_success 'skipped merge base when good and bad are siblings' '
405         git bisect start "$SIDE_HASH7" "$HASH7" > my_bisect_log.txt &&
406         test_i18ngrep "merge base must be tested" my_bisect_log.txt &&
407         grep $HASH4 my_bisect_log.txt &&
408         git bisect skip > my_bisect_log.txt 2>&1 &&
409         grep "warning" my_bisect_log.txt &&
410         grep $SIDE_HASH6 my_bisect_log.txt &&
411         git bisect reset
412 '
413
414 test_expect_success 'bad merge base when good and bad are siblings' '
415         git bisect start "$HASH7" HEAD > my_bisect_log.txt &&
416         test_i18ngrep "merge base must be tested" my_bisect_log.txt &&
417         grep $HASH4 my_bisect_log.txt &&
418         test_must_fail git bisect bad > my_bisect_log.txt 2>&1 &&
419         test_i18ngrep "merge base $HASH4 is bad" my_bisect_log.txt &&
420         test_i18ngrep "fixed between $HASH4 and \[$SIDE_HASH7\]" my_bisect_log.txt &&
421         git bisect reset
422 '
423
424 # This creates a few more commits (A and B) to test "siblings" cases
425 # when a good and a bad rev have many merge bases.
426 #
427 # We should have the following:
428 #
429 # H1-H2-H3-H4-H5-H6-H7
430 #            \  \     \
431 #             S5-A     \
432 #              \        \
433 #               S6-S7----B
434 #
435 # And there A and B have 2 merge bases (S5 and H5) that should be
436 # reported by "git merge-base --all A B".
437 #
438 test_expect_success 'many merge bases creation' '
439         git checkout "$SIDE_HASH5" &&
440         git merge -m "merge HASH5 and SIDE_HASH5" "$HASH5" &&
441         A_HASH=$(git rev-parse --verify HEAD) &&
442         git checkout side &&
443         git merge -m "merge HASH7 and SIDE_HASH7" "$HASH7" &&
444         B_HASH=$(git rev-parse --verify HEAD) &&
445         git merge-base --all "$A_HASH" "$B_HASH" > merge_bases.txt &&
446         test_line_count = 2 merge_bases.txt &&
447         grep "$HASH5" merge_bases.txt &&
448         grep "$SIDE_HASH5" merge_bases.txt
449 '
450
451 # We want to automatically find the merge that
452 # added "line" into hello.
453 test_expect_success '"git bisect run --first-parent" simple case' '
454         git rev-list --first-parent $B_HASH ^$HASH4 >first_parent_chain.txt &&
455         write_script test_script.sh <<-\EOF &&
456         grep $(git rev-parse HEAD) first_parent_chain.txt || exit -1
457         ! grep line hello >/dev/null
458         EOF
459         git bisect start --first-parent &&
460         test_path_is_file ".git/BISECT_FIRST_PARENT" &&
461         git bisect good $HASH4 &&
462         git bisect bad $B_HASH &&
463         git bisect run ./test_script.sh >my_bisect_log.txt &&
464         grep "$B_HASH is the first bad commit" my_bisect_log.txt &&
465         git bisect reset &&
466         test_path_is_missing .git/BISECT_FIRST_PARENT
467 '
468
469 test_expect_success 'good merge bases when good and bad are siblings' '
470         git bisect start "$B_HASH" "$A_HASH" > my_bisect_log.txt &&
471         test_i18ngrep "merge base must be tested" my_bisect_log.txt &&
472         git bisect good > my_bisect_log2.txt &&
473         test_i18ngrep "merge base must be tested" my_bisect_log2.txt &&
474         {
475                 {
476                         grep "$SIDE_HASH5" my_bisect_log.txt &&
477                         grep "$HASH5" my_bisect_log2.txt
478                 } || {
479                         grep "$SIDE_HASH5" my_bisect_log2.txt &&
480                         grep "$HASH5" my_bisect_log.txt
481                 }
482         } &&
483         git bisect reset
484 '
485
486 test_expect_success 'optimized merge base checks' '
487         git bisect start "$HASH7" "$SIDE_HASH7" > my_bisect_log.txt &&
488         test_i18ngrep "merge base must be tested" my_bisect_log.txt &&
489         grep "$HASH4" my_bisect_log.txt &&
490         git bisect good > my_bisect_log2.txt &&
491         test -f ".git/BISECT_ANCESTORS_OK" &&
492         test "$HASH6" = $(git rev-parse --verify HEAD) &&
493         git bisect bad &&
494         git bisect good "$A_HASH" > my_bisect_log4.txt &&
495         test_i18ngrep "merge base must be tested" my_bisect_log4.txt &&
496         test_path_is_missing ".git/BISECT_ANCESTORS_OK"
497 '
498
499 # This creates another side branch called "parallel" with some files
500 # in some directories, to test bisecting with paths.
501 #
502 # We should have the following:
503 #
504 #    P1-P2-P3-P4-P5-P6-P7
505 #   /        /        /
506 # H1-H2-H3-H4-H5-H6-H7
507 #            \  \     \
508 #             S5-A     \
509 #              \        \
510 #               S6-S7----B
511 #
512 test_expect_success '"parallel" side branch creation' '
513         git bisect reset &&
514         git checkout -b parallel $HASH1 &&
515         mkdir dir1 dir2 &&
516         add_line_into_file "1(para): line 1 on parallel branch" dir1/file1 &&
517         PARA_HASH1=$(git rev-parse --verify HEAD) &&
518         add_line_into_file "2(para): line 2 on parallel branch" dir2/file2 &&
519         PARA_HASH2=$(git rev-parse --verify HEAD) &&
520         add_line_into_file "3(para): line 3 on parallel branch" dir2/file3 &&
521         PARA_HASH3=$(git rev-parse --verify HEAD) &&
522         git merge -m "merge HASH4 and PARA_HASH3" "$HASH4" &&
523         PARA_HASH4=$(git rev-parse --verify HEAD) &&
524         add_line_into_file "5(para): add line on parallel branch" dir1/file1 &&
525         PARA_HASH5=$(git rev-parse --verify HEAD) &&
526         add_line_into_file "6(para): add line on parallel branch" dir2/file2 &&
527         PARA_HASH6=$(git rev-parse --verify HEAD) &&
528         git merge -m "merge HASH7 and PARA_HASH6" "$HASH7" &&
529         PARA_HASH7=$(git rev-parse --verify HEAD)
530 '
531
532 test_expect_success 'restricting bisection on one dir' '
533         git bisect reset &&
534         git bisect start HEAD $HASH1 -- dir1 &&
535         para1=$(git rev-parse --verify HEAD) &&
536         test "$para1" = "$PARA_HASH1" &&
537         git bisect bad > my_bisect_log.txt &&
538         grep "$PARA_HASH1 is the first bad commit" my_bisect_log.txt
539 '
540
541 test_expect_success 'restricting bisection on one dir and a file' '
542         git bisect reset &&
543         git bisect start HEAD $HASH1 -- dir1 hello &&
544         para4=$(git rev-parse --verify HEAD) &&
545         test "$para4" = "$PARA_HASH4" &&
546         git bisect bad &&
547         hash3=$(git rev-parse --verify HEAD) &&
548         test "$hash3" = "$HASH3" &&
549         git bisect good &&
550         hash4=$(git rev-parse --verify HEAD) &&
551         test "$hash4" = "$HASH4" &&
552         git bisect good &&
553         para1=$(git rev-parse --verify HEAD) &&
554         test "$para1" = "$PARA_HASH1" &&
555         git bisect good > my_bisect_log.txt &&
556         grep "$PARA_HASH4 is the first bad commit" my_bisect_log.txt
557 '
558
559 test_expect_success 'skipping away from skipped commit' '
560         git bisect start $PARA_HASH7 $HASH1 &&
561         para4=$(git rev-parse --verify HEAD) &&
562         test "$para4" = "$PARA_HASH4" &&
563         git bisect skip &&
564         hash7=$(git rev-parse --verify HEAD) &&
565         test "$hash7" = "$HASH7" &&
566         git bisect skip &&
567         para3=$(git rev-parse --verify HEAD) &&
568         test "$para3" = "$PARA_HASH3"
569 '
570
571 test_expect_success 'erroring out when using bad path parameters' '
572         test_must_fail git bisect start $PARA_HASH7 $HASH1 -- foobar 2> error.txt &&
573         test_i18ngrep "bad path parameters" error.txt
574 '
575
576 test_expect_success 'test bisection on bare repo - --no-checkout specified' '
577         git clone --bare . bare.nocheckout &&
578         (
579                 cd bare.nocheckout &&
580                 git bisect start --no-checkout &&
581                 git bisect good $HASH1 &&
582                 git bisect bad $HASH4 &&
583                 git bisect run eval \
584                         "test \$(git rev-list BISECT_HEAD ^$HASH2 --max-count=1 | wc -l) = 0" \
585                         >../nocheckout.log
586         ) &&
587         grep "$HASH3 is the first bad commit" nocheckout.log
588 '
589
590
591 test_expect_success 'test bisection on bare repo - --no-checkout defaulted' '
592         git clone --bare . bare.defaulted &&
593         (
594                 cd bare.defaulted &&
595                 git bisect start &&
596                 git bisect good $HASH1 &&
597                 git bisect bad $HASH4 &&
598                 git bisect run eval \
599                         "test \$(git rev-list BISECT_HEAD ^$HASH2 --max-count=1 | wc -l) = 0" \
600                         >../defaulted.log
601         ) &&
602         grep "$HASH3 is the first bad commit" defaulted.log
603 '
604
605 #
606 # This creates a broken branch which cannot be checked out because
607 # the tree created has been deleted.
608 #
609 # H1-H2-H3-H4-H5-H6-H7  <--other
610 #            \
611 #             S5-S6'-S7'-S8'-S9  <--broken
612 #
613 # Commits marked with ' have a missing tree.
614 #
615 test_expect_success 'broken branch creation' '
616         git bisect reset &&
617         git checkout -b broken $HASH4 &&
618         git tag BROKEN_HASH4 $HASH4 &&
619         add_line_into_file "5(broken): first line on a broken branch" hello2 &&
620         git tag BROKEN_HASH5 &&
621         mkdir missing &&
622         :> missing/MISSING &&
623         git add missing/MISSING &&
624         git commit -m "6(broken): Added file that will be deleted" &&
625         git tag BROKEN_HASH6 &&
626         deleted=$(git rev-parse --verify HEAD:missing) &&
627         add_line_into_file "7(broken): second line on a broken branch" hello2 &&
628         git tag BROKEN_HASH7 &&
629         add_line_into_file "8(broken): third line on a broken branch" hello2 &&
630         git tag BROKEN_HASH8 &&
631         git rm missing/MISSING &&
632         git commit -m "9(broken): Remove missing file" &&
633         git tag BROKEN_HASH9 &&
634         rm .git/objects/$(test_oid_to_path $deleted)
635 '
636
637 echo "" > expected.ok
638 cat > expected.missing-tree.default <<EOF
639 fatal: unable to read tree $deleted
640 EOF
641
642 test_expect_success 'bisect fails if tree is broken on start commit' '
643         git bisect reset &&
644         test_must_fail git bisect start BROKEN_HASH7 BROKEN_HASH4 2>error.txt &&
645         test_cmp expected.missing-tree.default error.txt
646 '
647
648 test_expect_success 'bisect fails if tree is broken on trial commit' '
649         git bisect reset &&
650         test_must_fail git bisect start BROKEN_HASH9 BROKEN_HASH4 2>error.txt &&
651         git reset --hard broken &&
652         git checkout broken &&
653         test_cmp expected.missing-tree.default error.txt
654 '
655
656 check_same()
657 {
658         echo "Checking $1 is the same as $2" &&
659         test_cmp_rev "$1" "$2"
660 }
661
662 test_expect_success 'bisect: --no-checkout - start commit bad' '
663         git bisect reset &&
664         git bisect start BROKEN_HASH7 BROKEN_HASH4 --no-checkout &&
665         check_same BROKEN_HASH6 BISECT_HEAD &&
666         git bisect reset
667 '
668
669 test_expect_success 'bisect: --no-checkout - trial commit bad' '
670         git bisect reset &&
671         git bisect start broken BROKEN_HASH4 --no-checkout &&
672         check_same BROKEN_HASH6 BISECT_HEAD &&
673         git bisect reset
674 '
675
676 test_expect_success 'bisect: --no-checkout - target before breakage' '
677         git bisect reset &&
678         git bisect start broken BROKEN_HASH4 --no-checkout &&
679         check_same BROKEN_HASH6 BISECT_HEAD &&
680         git bisect bad BISECT_HEAD &&
681         check_same BROKEN_HASH5 BISECT_HEAD &&
682         git bisect bad BISECT_HEAD &&
683         check_same BROKEN_HASH5 bisect/bad &&
684         git bisect reset
685 '
686
687 test_expect_success 'bisect: --no-checkout - target in breakage' '
688         git bisect reset &&
689         git bisect start broken BROKEN_HASH4 --no-checkout &&
690         check_same BROKEN_HASH6 BISECT_HEAD &&
691         git bisect bad BISECT_HEAD &&
692         check_same BROKEN_HASH5 BISECT_HEAD &&
693         test_must_fail git bisect good BISECT_HEAD &&
694         check_same BROKEN_HASH6 bisect/bad &&
695         git bisect reset
696 '
697
698 test_expect_success 'bisect: --no-checkout - target after breakage' '
699         git bisect reset &&
700         git bisect start broken BROKEN_HASH4 --no-checkout &&
701         check_same BROKEN_HASH6 BISECT_HEAD &&
702         git bisect good BISECT_HEAD &&
703         check_same BROKEN_HASH8 BISECT_HEAD &&
704         test_must_fail git bisect good BISECT_HEAD &&
705         check_same BROKEN_HASH9 bisect/bad &&
706         git bisect reset
707 '
708
709 test_expect_success 'bisect: demonstrate identification of damage boundary' "
710         git bisect reset &&
711         git checkout broken &&
712         git bisect start broken master --no-checkout &&
713         test_must_fail git bisect run \"\$SHELL_PATH\" -c '
714                 GOOD=\$(git for-each-ref \"--format=%(objectname)\" refs/bisect/good-*) &&
715                 git rev-list --objects BISECT_HEAD --not \$GOOD >tmp.\$\$ &&
716                 git pack-objects --stdout >/dev/null < tmp.\$\$
717                 rc=\$?
718                 rm -f tmp.\$\$
719                 test \$rc = 0' &&
720         check_same BROKEN_HASH6 bisect/bad &&
721         git bisect reset
722 "
723
724 cat > expected.bisect-log <<EOF
725 # bad: [$HASH4] Add <4: Ciao for now> into <hello>.
726 # good: [$HASH2] Add <2: A new day for git> into <hello>.
727 git bisect start '$HASH4' '$HASH2'
728 # good: [$HASH3] Add <3: Another new day for git> into <hello>.
729 git bisect good $HASH3
730 # first bad commit: [$HASH4] Add <4: Ciao for now> into <hello>.
731 EOF
732
733 test_expect_success 'bisect log: successful result' '
734         git bisect reset &&
735         git bisect start $HASH4 $HASH2 &&
736         git bisect good &&
737         git bisect log >bisect-log.txt &&
738         test_cmp expected.bisect-log bisect-log.txt &&
739         git bisect reset
740 '
741
742 cat > expected.bisect-skip-log <<EOF
743 # bad: [$HASH4] Add <4: Ciao for now> into <hello>.
744 # good: [$HASH2] Add <2: A new day for git> into <hello>.
745 git bisect start '$HASH4' '$HASH2'
746 # skip: [$HASH3] Add <3: Another new day for git> into <hello>.
747 git bisect skip $HASH3
748 # only skipped commits left to test
749 # possible first bad commit: [$HASH4] Add <4: Ciao for now> into <hello>.
750 # possible first bad commit: [$HASH3] Add <3: Another new day for git> into <hello>.
751 EOF
752
753 test_expect_success 'bisect log: only skip commits left' '
754         git bisect reset &&
755         git bisect start $HASH4 $HASH2 &&
756         test_must_fail git bisect skip &&
757         git bisect log >bisect-skip-log.txt &&
758         test_cmp expected.bisect-skip-log bisect-skip-log.txt &&
759         git bisect reset
760 '
761
762 test_expect_success '"git bisect bad HEAD" behaves as "git bisect bad"' '
763         git checkout parallel &&
764         git bisect start HEAD $HASH1 &&
765         git bisect good HEAD &&
766         git bisect bad HEAD &&
767         test "$HASH6" = $(git rev-parse --verify HEAD) &&
768         git bisect reset
769 '
770
771 test_expect_success 'bisect starts with only one new' '
772         git bisect reset &&
773         git bisect start &&
774         git bisect new $HASH4 &&
775         git bisect next
776 '
777
778 test_expect_success 'bisect does not start with only one old' '
779         git bisect reset &&
780         git bisect start &&
781         git bisect old $HASH1 &&
782         test_must_fail git bisect next
783 '
784
785 test_expect_success 'bisect start with one new and old' '
786         git bisect reset &&
787         git bisect start &&
788         git bisect old $HASH1 &&
789         git bisect new $HASH4 &&
790         git bisect new &&
791         git bisect new >bisect_result &&
792         grep "$HASH2 is the first new commit" bisect_result &&
793         git bisect log >log_to_replay.txt &&
794         git bisect reset
795 '
796
797 test_expect_success 'bisect replay with old and new' '
798         git bisect replay log_to_replay.txt >bisect_result &&
799         grep "$HASH2 is the first new commit" bisect_result &&
800         git bisect reset
801 '
802
803 test_expect_success 'bisect replay with CRLF log' '
804         append_cr <log_to_replay.txt >log_to_replay_crlf.txt &&
805         git bisect replay log_to_replay_crlf.txt >bisect_result_crlf &&
806         grep "$HASH2 is the first new commit" bisect_result_crlf &&
807         git bisect reset
808 '
809
810 test_expect_success 'bisect cannot mix old/new and good/bad' '
811         git bisect start &&
812         git bisect bad $HASH4 &&
813         test_must_fail git bisect old $HASH1
814 '
815
816 test_expect_success 'bisect terms needs 0 or 1 argument' '
817         git bisect reset &&
818         test_must_fail git bisect terms only-one &&
819         test_must_fail git bisect terms 1 2 &&
820         test_must_fail git bisect terms 2>actual &&
821         echo "error: no terms defined" >expected &&
822         test_i18ncmp expected actual
823 '
824
825 test_expect_success 'bisect terms shows good/bad after start' '
826         git bisect reset &&
827         git bisect start HEAD $HASH1 &&
828         git bisect terms --term-good >actual &&
829         echo good >expected &&
830         test_cmp expected actual &&
831         git bisect terms --term-bad >actual &&
832         echo bad >expected &&
833         test_cmp expected actual
834 '
835
836 test_expect_success 'bisect start with one term1 and term2' '
837         git bisect reset &&
838         git bisect start --term-old term2 --term-new term1 &&
839         git bisect term2 $HASH1 &&
840         git bisect term1 $HASH4 &&
841         git bisect term1 &&
842         git bisect term1 >bisect_result &&
843         grep "$HASH2 is the first term1 commit" bisect_result &&
844         git bisect log >log_to_replay.txt &&
845         git bisect reset
846 '
847
848 test_expect_success 'bisect replay with term1 and term2' '
849         git bisect replay log_to_replay.txt >bisect_result &&
850         grep "$HASH2 is the first term1 commit" bisect_result &&
851         git bisect reset
852 '
853
854 test_expect_success 'bisect start term1 term2' '
855         git bisect reset &&
856         git bisect start --term-new term1 --term-old term2 $HASH4 $HASH1 &&
857         git bisect term1 &&
858         git bisect term1 >bisect_result &&
859         grep "$HASH2 is the first term1 commit" bisect_result &&
860         git bisect log >log_to_replay.txt &&
861         git bisect reset
862 '
863
864 test_expect_success 'bisect cannot mix terms' '
865         git bisect reset &&
866         git bisect start --term-good term1 --term-bad term2 $HASH4 $HASH1 &&
867         test_must_fail git bisect a &&
868         test_must_fail git bisect b &&
869         test_must_fail git bisect bad &&
870         test_must_fail git bisect good &&
871         test_must_fail git bisect new &&
872         test_must_fail git bisect old
873 '
874
875 test_expect_success 'bisect terms rejects invalid terms' '
876         git bisect reset &&
877         test_must_fail git bisect start --term-good &&
878         test_must_fail git bisect start --term-good invalid..term &&
879         test_must_fail git bisect start --term-bad &&
880         test_must_fail git bisect terms --term-bad invalid..term &&
881         test_must_fail git bisect terms --term-good bad &&
882         test_must_fail git bisect terms --term-good old &&
883         test_must_fail git bisect terms --term-good skip &&
884         test_must_fail git bisect terms --term-good reset &&
885         test_path_is_missing .git/BISECT_TERMS
886 '
887
888 test_expect_success 'bisect start --term-* does store terms' '
889         git bisect reset &&
890         git bisect start --term-bad=one --term-good=two &&
891         git bisect terms >actual &&
892         cat <<-EOF >expected &&
893         Your current terms are two for the old state
894         and one for the new state.
895         EOF
896         test_i18ncmp expected actual &&
897         git bisect terms --term-bad >actual &&
898         echo one >expected &&
899         test_cmp expected actual &&
900         git bisect terms --term-good >actual &&
901         echo two >expected &&
902         test_cmp expected actual
903 '
904
905 test_expect_success 'bisect start takes options and revs in any order' '
906         git bisect reset &&
907         git bisect start --term-good one $HASH4 \
908                 --term-good two --term-bad bad-term \
909                 $HASH1 --term-good three -- &&
910         (git bisect terms --term-bad && git bisect terms --term-good) >actual &&
911         printf "%s\n%s\n" bad-term three >expected &&
912         test_cmp expected actual
913 '
914
915 test_expect_success 'git bisect reset cleans bisection state properly' '
916         git bisect reset &&
917         git bisect start &&
918         git bisect good $HASH1 &&
919         git bisect bad $HASH4 &&
920         git bisect reset &&
921         test -z "$(git for-each-ref "refs/bisect/*")" &&
922         test_path_is_missing "$GIT_DIR/BISECT_EXPECTED_REV" &&
923         test_path_is_missing "$GIT_DIR/BISECT_ANCESTORS_OK" &&
924         test_path_is_missing "$GIT_DIR/BISECT_LOG" &&
925         test_path_is_missing "$GIT_DIR/BISECT_RUN" &&
926         test_path_is_missing "$GIT_DIR/BISECT_TERMS" &&
927         test_path_is_missing "$GIT_DIR/head-name" &&
928         test_path_is_missing "$GIT_DIR/BISECT_HEAD" &&
929         test_path_is_missing "$GIT_DIR/BISECT_START"
930 '
931
932 test_done