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_must_fail ls .git/BISECT_* &&
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_failure
'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_failure
'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_failure
'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/*")"
169 # This cleanup is needed whatever the result of the above test.
170 git checkout HEAD hello
172 # $HASH1 is good, $HASH4 is bad, we skip $HASH3
174 # so we should find $HASH2 as the first bad commit
175 test_expect_success
'bisect skip: successfull result' '
177 git bisect start $HASH4 $HASH1 &&
179 git bisect bad > my_bisect_log.txt &&
180 grep "$HASH2 is first bad commit" my_bisect_log.txt &&
184 # $HASH1 is good, $HASH4 is bad, we skip $HASH3 and $HASH2
185 # so we should not be able to tell the first bad commit
186 # among $HASH2, $HASH3 and $HASH4
187 test_expect_success
'bisect skip: cannot tell between 3 commits' '
188 git bisect start $HASH4 $HASH1 &&
189 git bisect skip || return 1
191 if git bisect skip > my_bisect_log.txt
193 echo Oops, should have failed.
197 grep "first bad commit could be any of" my_bisect_log.txt &&
198 ! grep $HASH1 my_bisect_log.txt &&
199 grep $HASH2 my_bisect_log.txt &&
200 grep $HASH3 my_bisect_log.txt &&
201 grep $HASH4 my_bisect_log.txt &&
206 # $HASH1 is good, $HASH4 is bad, we skip $HASH3
207 # but $HASH2 is good,
208 # so we should not be able to tell the first bad commit
209 # among $HASH3 and $HASH4
210 test_expect_success
'bisect skip: cannot tell between 2 commits' '
211 git bisect start $HASH4 $HASH1 &&
212 git bisect skip || return 1
214 if git bisect good > my_bisect_log.txt
216 echo Oops, should have failed.
220 grep "first bad commit could be any of" my_bisect_log.txt &&
221 ! grep $HASH1 my_bisect_log.txt &&
222 ! grep $HASH2 my_bisect_log.txt &&
223 grep $HASH3 my_bisect_log.txt &&
224 grep $HASH4 my_bisect_log.txt &&
229 # We want to automatically find the commit that
230 # introduced "Another" into hello.
231 test_expect_success \
232 '"git bisect run" simple case' \
233 'echo "#"\!"/bin/sh" > test_script.sh &&
234 echo "grep Another hello > /dev/null" >> test_script.sh &&
235 echo "test \$? -ne 0" >> test_script.sh &&
236 chmod +x test_script.sh &&
238 git bisect good $HASH1 &&
239 git bisect bad $HASH4 &&
240 git bisect run ./test_script.sh > my_bisect_log.txt &&
241 grep "$HASH3 is first bad commit" my_bisect_log.txt &&
244 # We want to automatically find the commit that
245 # introduced "Ciao" into hello.
246 test_expect_success \
247 '"git bisect run" with more complex "git bisect start"' \
248 'echo "#"\!"/bin/sh" > test_script.sh &&
249 echo "grep Ciao hello > /dev/null" >> test_script.sh &&
250 echo "test \$? -ne 0" >> test_script.sh &&
251 chmod +x test_script.sh &&
252 git bisect start $HASH4 $HASH1 &&
253 git bisect run ./test_script.sh > my_bisect_log.txt &&
254 grep "$HASH4 is first bad commit" my_bisect_log.txt &&
257 # $HASH1 is good, $HASH5 is bad, we skip $HASH3
258 # but $HASH4 is good,
259 # so we should find $HASH5 as the first bad commit
261 test_expect_success
'bisect skip: add line and then a new test' '
262 add_line_into_file "5: Another new line." hello &&
263 HASH5=$(git rev-parse --verify HEAD) &&
264 git bisect start $HASH5 $HASH1 &&
266 git bisect good > my_bisect_log.txt &&
267 grep "$HASH5 is first bad commit" my_bisect_log.txt &&
268 git bisect log > log_to_replay.txt &&
272 test_expect_success
'bisect skip and bisect replay' '
273 git bisect replay log_to_replay.txt > my_bisect_log.txt &&
274 grep "$HASH5 is first bad commit" my_bisect_log.txt &&
279 test_expect_success
'bisect run & skip: cannot tell between 2' '
280 add_line_into_file "6: Yet a line." hello &&
281 HASH6=$(git rev-parse --verify HEAD) &&
282 echo "#"\!"/bin/sh" > test_script.sh &&
283 echo "sed -ne \\\$p hello | grep Ciao > /dev/null && exit 125" >> test_script.sh &&
284 echo "grep line hello > /dev/null" >> test_script.sh &&
285 echo "test \$? -ne 0" >> test_script.sh &&
286 chmod +x test_script.sh &&
287 git bisect start $HASH6 $HASH1 &&
288 if git bisect run ./test_script.sh > my_bisect_log.txt
290 echo Oops, should have failed.
294 grep "first bad commit could be any of" my_bisect_log.txt &&
295 ! grep $HASH3 my_bisect_log.txt &&
296 ! grep $HASH6 my_bisect_log.txt &&
297 grep $HASH4 my_bisect_log.txt &&
298 grep $HASH5 my_bisect_log.txt
303 test_expect_success
'bisect run & skip: find first bad' '
305 add_line_into_file "7: Should be the last line." hello &&
306 HASH7=$(git rev-parse --verify HEAD) &&
307 echo "#"\!"/bin/sh" > test_script.sh &&
308 echo "sed -ne \\\$p hello | grep Ciao > /dev/null && exit 125" >> test_script.sh &&
309 echo "sed -ne \\\$p hello | grep day > /dev/null && exit 125" >> test_script.sh &&
310 echo "grep Yet hello > /dev/null" >> test_script.sh &&
311 echo "test \$? -ne 0" >> test_script.sh &&
312 chmod +x test_script.sh &&
313 git bisect start $HASH7 $HASH1 &&
314 git bisect run ./test_script.sh > my_bisect_log.txt &&
315 grep "$HASH6 is first bad commit" my_bisect_log.txt
318 test_expect_success
'bisect starting with a detached HEAD' '
321 git checkout master^ &&
322 HEAD=$(git rev-parse --verify HEAD) &&
324 test $HEAD = $(cat .git/BISECT_START) &&
326 test $HEAD = $(git rev-parse --verify HEAD)
330 test_expect_success
'bisect refuses to start if branch bisect exists' '
333 test_must_fail git bisect start &&
334 git branch -d bisect &&
335 git checkout -b bisect &&
336 test_must_fail git bisect start &&
337 git checkout master &&
341 test_expect_success
'bisect refuses to start if branch new-bisect exists' '
343 git branch new-bisect &&
344 test_must_fail git bisect start &&
345 git branch -d new-bisect
348 test_expect_success
'bisect errors out if bad and good are mistaken' '
350 test_must_fail git bisect start $HASH2 $HASH4 2> rev_list_error &&
351 grep "mistake good and bad" rev_list_error &&