Code

git-bisect: use update-ref to mark good/bad commits
[git.git] / t / t6030-bisect-porcelain.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Christian Couder
4 #
5 test_description='Tests git-bisect functionality'
7 exec </dev/null
9 . ./test-lib.sh
11 add_line_into_file()
12 {
13     _line=$1
14     _file=$2
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
25     test_tick
26     git-commit --quiet -m "$MSG" $_file
27 }
29 HASH1=
30 HASH2=
31 HASH3=
32 HASH4=
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 '
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 '
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 || return 1
57         if git bisect next
58         then
59                 echo Oops, should have failed.
60                 false
61         else
62                 :
63         fi
64 '
66 test_expect_success 'bisect start with one bad and good' '
67         git bisect reset &&
68         git bisect start &&
69         git bisect good $HASH1 &&
70         git bisect bad $HASH4 &&
71         git bisect next
72 '
74 test_expect_success 'bisect reset removes packed refs' '
75         git bisect reset &&
76         git bisect start &&
77         git bisect good $HASH1 &&
78         git bisect bad $HASH3 &&
79         git pack-refs --all --prune &&
80         git bisect next &&
81         git bisect reset &&
82         test -z "$(git for-each-ref "refs/bisect/*")" &&
83         test -z "$(git for-each-ref "refs/heads/bisect")"
84 '
86 # $HASH1 is good, $HASH4 is bad, we skip $HASH3
87 # but $HASH2 is bad,
88 # so we should find $HASH2 as the first bad commit
89 test_expect_success 'bisect skip: successfull result' '
90         git bisect reset &&
91         git bisect start $HASH4 $HASH1 &&
92         git bisect skip &&
93         git bisect bad > my_bisect_log.txt &&
94         grep "$HASH2 is first bad commit" my_bisect_log.txt &&
95         git bisect reset
96 '
98 # $HASH1 is good, $HASH4 is bad, we skip $HASH3 and $HASH2
99 # so we should not be able to tell the first bad commit
100 # among $HASH2, $HASH3 and $HASH4
101 test_expect_success 'bisect skip: cannot tell between 3 commits' '
102         git bisect start $HASH4 $HASH1 &&
103         git bisect skip || return 1
105         if git bisect skip > my_bisect_log.txt
106         then
107                 echo Oops, should have failed.
108                 false
109         else
110                 test $? -eq 2 &&
111                 grep "first bad commit could be any of" my_bisect_log.txt &&
112                 ! grep $HASH1 my_bisect_log.txt &&
113                 grep $HASH2 my_bisect_log.txt &&
114                 grep $HASH3 my_bisect_log.txt &&
115                 grep $HASH4 my_bisect_log.txt &&
116                 git bisect reset
117         fi
120 # $HASH1 is good, $HASH4 is bad, we skip $HASH3
121 # but $HASH2 is good,
122 # so we should not be able to tell the first bad commit
123 # among $HASH3 and $HASH4
124 test_expect_success 'bisect skip: cannot tell between 2 commits' '
125         git bisect start $HASH4 $HASH1 &&
126         git bisect skip || return 1
128         if git bisect good > my_bisect_log.txt
129         then
130                 echo Oops, should have failed.
131                 false
132         else
133                 test $? -eq 2 &&
134                 grep "first bad commit could be any of" my_bisect_log.txt &&
135                 ! grep $HASH1 my_bisect_log.txt &&
136                 ! grep $HASH2 my_bisect_log.txt &&
137                 grep $HASH3 my_bisect_log.txt &&
138                 grep $HASH4 my_bisect_log.txt &&
139                 git bisect reset
140         fi
143 # We want to automatically find the commit that
144 # introduced "Another" into hello.
145 test_expect_success \
146     '"git bisect run" simple case' \
147     'echo "#"\!"/bin/sh" > test_script.sh &&
148      echo "grep Another hello > /dev/null" >> test_script.sh &&
149      echo "test \$? -ne 0" >> test_script.sh &&
150      chmod +x test_script.sh &&
151      git bisect start &&
152      git bisect good $HASH1 &&
153      git bisect bad $HASH4 &&
154      git bisect run ./test_script.sh > my_bisect_log.txt &&
155      grep "$HASH3 is first bad commit" my_bisect_log.txt &&
156      git bisect reset'
158 # We want to automatically find the commit that
159 # introduced "Ciao" into hello.
160 test_expect_success \
161     '"git bisect run" with more complex "git bisect start"' \
162     'echo "#"\!"/bin/sh" > test_script.sh &&
163      echo "grep Ciao hello > /dev/null" >> test_script.sh &&
164      echo "test \$? -ne 0" >> test_script.sh &&
165      chmod +x test_script.sh &&
166      git bisect start $HASH4 $HASH1 &&
167      git bisect run ./test_script.sh > my_bisect_log.txt &&
168      grep "$HASH4 is first bad commit" my_bisect_log.txt &&
169      git bisect reset'
171 # $HASH1 is good, $HASH5 is bad, we skip $HASH3
172 # but $HASH4 is good,
173 # so we should find $HASH5 as the first bad commit
174 HASH5=
175 test_expect_success 'bisect skip: add line and then a new test' '
176         add_line_into_file "5: Another new line." hello &&
177         HASH5=$(git rev-parse --verify HEAD) &&
178         git bisect start $HASH5 $HASH1 &&
179         git bisect skip &&
180         git bisect good > my_bisect_log.txt &&
181         grep "$HASH5 is first bad commit" my_bisect_log.txt &&
182         git bisect log > log_to_replay.txt
183         git bisect reset
186 test_expect_success 'bisect skip and bisect replay' '
187         git bisect replay log_to_replay.txt > my_bisect_log.txt &&
188         grep "$HASH5 is first bad commit" my_bisect_log.txt &&
189         git bisect reset
192 HASH6=
193 test_expect_success 'bisect run & skip: cannot tell between 2' '
194         add_line_into_file "6: Yet a line." hello &&
195         HASH6=$(git rev-parse --verify HEAD) &&
196         echo "#"\!"/bin/sh" > test_script.sh &&
197         echo "tail -1 hello | grep Ciao > /dev/null && exit 125" >> test_script.sh &&
198         echo "grep line hello > /dev/null" >> test_script.sh &&
199         echo "test \$? -ne 0" >> test_script.sh &&
200         chmod +x test_script.sh &&
201         git bisect start $HASH6 $HASH1 &&
202         if git bisect run ./test_script.sh > my_bisect_log.txt
203         then
204                 echo Oops, should have failed.
205                 false
206         else
207                 test $? -eq 2 &&
208                 grep "first bad commit could be any of" my_bisect_log.txt &&
209                 ! grep $HASH3 my_bisect_log.txt &&
210                 ! grep $HASH6 my_bisect_log.txt &&
211                 grep $HASH4 my_bisect_log.txt &&
212                 grep $HASH5 my_bisect_log.txt
213         fi
216 HASH7=
217 test_expect_success 'bisect run & skip: find first bad' '
218         git bisect reset &&
219         add_line_into_file "7: Should be the last line." hello &&
220         HASH7=$(git rev-parse --verify HEAD) &&
221         echo "#"\!"/bin/sh" > test_script.sh &&
222         echo "tail -1 hello | grep Ciao > /dev/null && exit 125" >> test_script.sh &&
223         echo "tail -1 hello | grep day > /dev/null && exit 125" >> test_script.sh &&
224         echo "grep Yet hello > /dev/null" >> test_script.sh &&
225         echo "test \$? -ne 0" >> test_script.sh &&
226         chmod +x test_script.sh &&
227         git bisect start $HASH7 $HASH1 &&
228         git bisect run ./test_script.sh > my_bisect_log.txt &&
229         grep "$HASH6 is first bad commit" my_bisect_log.txt
234 test_done