]>
Commit | Line | Data |
---|---|---|
8cc6a083 | 1 | #!/bin/sh |
d025524d | 2 | |
6ca8b977 | 3 | USAGE='[start|bad|good|skip|next|reset|visualize|replay|log|run]' |
38a47fd6 CC |
4 | LONG_USAGE='git bisect start [<bad> [<good>...]] [--] [<pathspec>...] |
5 | reset bisect state and start bisection. | |
6 | git bisect bad [<rev>] | |
7 | mark <rev> a known-bad revision. | |
8 | git bisect good [<rev>...] | |
9 | mark <rev>... known-good revisions. | |
6ca8b977 CC |
10 | git bisect skip [<rev>...] |
11 | mark <rev>... untestable revisions. | |
38a47fd6 CC |
12 | git bisect next |
13 | find next bisection to test and check it out. | |
14 | git bisect reset [<branch>] | |
15 | finish bisection search and go back to branch. | |
16 | git bisect visualize | |
17 | show bisect status in gitk. | |
18 | git bisect replay <logfile> | |
19 | replay bisection log. | |
20 | git bisect log | |
21 | show bisect log. | |
22 | git bisect run <cmd>... | |
23 | use <cmd>... to automatically bisect.' | |
d025524d | 24 | |
8f321a39 | 25 | OPTIONS_SPEC= |
ae2b0f15 | 26 | . git-sh-setup |
4c550686 | 27 | require_work_tree |
8cc6a083 | 28 | |
e9a45d75 | 29 | sq() { |
d9bffc08 | 30 | @@PERL@@ -e ' |
e9a45d75 JH |
31 | for (@ARGV) { |
32 | s/'\''/'\'\\\\\'\''/g; | |
33 | print " '\''$_'\''"; | |
34 | } | |
35 | print "\n"; | |
36 | ' "$@" | |
37 | } | |
38 | ||
8cc6a083 LT |
39 | bisect_autostart() { |
40 | test -d "$GIT_DIR/refs/bisect" || { | |
41 | echo >&2 'You need to start by "git bisect start"' | |
42 | if test -t 0 | |
43 | then | |
44 | echo >&2 -n 'Do you want me to do it for you [Y/n]? ' | |
45 | read yesno | |
46 | case "$yesno" in | |
47 | [Nn]*) | |
48 | exit ;; | |
49 | esac | |
50 | bisect_start | |
51 | else | |
52 | exit 1 | |
53 | fi | |
54 | } | |
55 | } | |
56 | ||
57 | bisect_start() { | |
8cc6a083 LT |
58 | # |
59 | # Verify HEAD. If we were bisecting before this, reset to the | |
60 | # top-of-line master first! | |
61 | # | |
5be60078 | 62 | head=$(GIT_DIR="$GIT_DIR" git symbolic-ref HEAD) || |
8098a178 | 63 | die "Bad HEAD - I need a symbolic ref" |
8cc6a083 | 64 | case "$head" in |
673e5838 | 65 | refs/heads/bisect) |
810255fd PB |
66 | if [ -s "$GIT_DIR/head-name" ]; then |
67 | branch=`cat "$GIT_DIR/head-name"` | |
68 | else | |
69 | branch=master | |
8fe26f44 | 70 | fi |
810255fd | 71 | git checkout $branch || exit |
8cc6a083 LT |
72 | ;; |
73 | refs/heads/*) | |
810255fd PB |
74 | [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree" |
75 | echo "$head" | sed 's#^refs/heads/##' >"$GIT_DIR/head-name" | |
8cc6a083 LT |
76 | ;; |
77 | *) | |
8098a178 | 78 | die "Bad HEAD - strange symbolic ref" |
8cc6a083 LT |
79 | ;; |
80 | esac | |
81 | ||
82 | # | |
83 | # Get rid of any old bisect state | |
84 | # | |
38a47fd6 | 85 | bisect_clean_state |
8cc6a083 | 86 | mkdir "$GIT_DIR/refs/bisect" |
38a47fd6 CC |
87 | |
88 | # | |
89 | # Check for one bad and then some good revisions. | |
90 | # | |
91 | has_double_dash=0 | |
92 | for arg; do | |
93 | case "$arg" in --) has_double_dash=1; break ;; esac | |
94 | done | |
95 | orig_args=$(sq "$@") | |
96 | bad_seen=0 | |
97 | while [ $# -gt 0 ]; do | |
98 | arg="$1" | |
99 | case "$arg" in | |
100 | --) | |
8fe26f44 | 101 | shift |
38a47fd6 CC |
102 | break |
103 | ;; | |
104 | *) | |
8fe26f44 | 105 | rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || { |
38a47fd6 CC |
106 | test $has_double_dash -eq 1 && |
107 | die "'$arg' does not appear to be a valid revision" | |
108 | break | |
109 | } | |
737c74ee CC |
110 | case $bad_seen in |
111 | 0) state='bad' ; bad_seen=1 ;; | |
112 | *) state='good' ;; | |
113 | esac | |
114 | bisect_write "$state" "$rev" 'nolog' | |
8fe26f44 | 115 | shift |
38a47fd6 CC |
116 | ;; |
117 | esac | |
8fe26f44 | 118 | done |
38a47fd6 CC |
119 | |
120 | sq "$@" >"$GIT_DIR/BISECT_NAMES" | |
b8652b4d | 121 | echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" |
38a47fd6 | 122 | bisect_auto_next |
8cc6a083 LT |
123 | } |
124 | ||
55624f9a CC |
125 | bisect_write() { |
126 | state="$1" | |
127 | rev="$2" | |
737c74ee | 128 | nolog="$3" |
55624f9a CC |
129 | case "$state" in |
130 | bad) tag="$state" ;; | |
131 | good|skip) tag="$state"-"$rev" ;; | |
132 | *) die "Bad bisect_write argument: $state" ;; | |
133 | esac | |
134 | echo "$rev" >"$GIT_DIR/refs/bisect/$tag" | |
135 | echo "# $state: "$(git show-branch $rev) >>"$GIT_DIR/BISECT_LOG" | |
737c74ee | 136 | test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG" |
55624f9a CC |
137 | } |
138 | ||
155fc795 | 139 | bisect_state() { |
8cc6a083 | 140 | bisect_autostart |
155fc795 CC |
141 | state=$1 |
142 | case "$#,$state" in | |
143 | 0,*) | |
144 | die "Please call 'bisect_state' with at least one argument." ;; | |
145 | 1,bad|1,good|1,skip) | |
146 | rev=$(git rev-parse --verify HEAD) || | |
147 | die "Bad rev input: HEAD" | |
148 | bisect_write "$state" "$rev" ;; | |
149 | 2,bad) | |
150 | rev=$(git rev-parse --verify "$2^{commit}") || | |
151 | die "Bad rev input: $2" | |
152 | bisect_write "$state" "$rev" ;; | |
153 | *,good|*,skip) | |
154 | shift | |
155 | revs=$(git rev-parse --revs-only --no-flags "$@") && | |
156 | test '' != "$revs" || die "Bad rev input: $@" | |
157 | for rev in $revs | |
158 | do | |
159 | rev=$(git rev-parse --verify "$rev^{commit}") || | |
160 | die "Bad rev commit: $rev^{commit}" | |
161 | bisect_write "$state" "$rev" | |
162 | done ;; | |
cc9f24d0 JH |
163 | *) |
164 | usage ;; | |
8cc6a083 | 165 | esac |
97e1c51e CC |
166 | bisect_auto_next |
167 | } | |
168 | ||
8cc6a083 | 169 | bisect_next_check() { |
0a5280a9 JH |
170 | missing_good= missing_bad= |
171 | git show-ref -q --verify refs/bisect/bad || missing_bad=t | |
172 | test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t | |
6fecf191 | 173 | |
0a5280a9 JH |
174 | case "$missing_good,$missing_bad,$1" in |
175 | ,,*) | |
176 | : have both good and bad - ok | |
177 | ;; | |
178 | *,) | |
179 | # do not have both but not asked to fail - just report. | |
180 | false | |
181 | ;; | |
182 | t,,good) | |
183 | # have bad but not good. we could bisect although | |
184 | # this is less optimum. | |
185 | echo >&2 'Warning: bisecting only with a bad commit.' | |
186 | if test -t 0 | |
187 | then | |
188 | printf >&2 'Are you sure [Y/n]? ' | |
189 | case "$(read yesno)" in [Nn]*) exit 1 ;; esac | |
190 | fi | |
191 | : bisect without good... | |
192 | ;; | |
8cc6a083 | 193 | *) |
0a5280a9 JH |
194 | THEN='' |
195 | test -d "$GIT_DIR/refs/bisect" || { | |
196 | echo >&2 'You need to start by "git bisect start".' | |
197 | THEN='then ' | |
198 | } | |
199 | echo >&2 'You '$THEN'need to give me at least one good' \ | |
200 | 'and one bad revisions.' | |
201 | echo >&2 '(You can use "git bisect bad" and' \ | |
202 | '"git bisect good" for that.)' | |
203 | exit 1 ;; | |
8cc6a083 LT |
204 | esac |
205 | } | |
206 | ||
207 | bisect_auto_next() { | |
434d036f | 208 | bisect_next_check && bisect_next || : |
8cc6a083 LT |
209 | } |
210 | ||
97e1c51e CC |
211 | filter_skipped() { |
212 | _eval="$1" | |
213 | _skip="$2" | |
214 | ||
215 | if [ -z "$_skip" ]; then | |
216 | eval $_eval | |
217 | return | |
218 | fi | |
219 | ||
220 | # Let's parse the output of: | |
221 | # "git rev-list --bisect-vars --bisect-all ..." | |
222 | eval $_eval | while read hash line | |
223 | do | |
224 | case "$VARS,$FOUND,$TRIED,$hash" in | |
225 | # We display some vars. | |
226 | 1,*,*,*) echo "$hash $line" ;; | |
227 | ||
228 | # Split line. | |
229 | ,*,*,---*) ;; | |
230 | ||
231 | # We had nothing to search. | |
232 | ,,,bisect_rev*) | |
233 | echo "bisect_rev=" | |
234 | VARS=1 | |
235 | ;; | |
236 | ||
237 | # We did not find a good bisect rev. | |
238 | # This should happen only if the "bad" | |
239 | # commit is also a "skip" commit. | |
240 | ,,*,bisect_rev*) | |
241 | echo "bisect_rev=$TRIED" | |
242 | VARS=1 | |
243 | ;; | |
244 | ||
245 | # We are searching. | |
246 | ,,*,*) | |
247 | TRIED="${TRIED:+$TRIED|}$hash" | |
248 | case "$_skip" in | |
249 | *$hash*) ;; | |
250 | *) | |
251 | echo "bisect_rev=$hash" | |
252 | echo "bisect_tried=\"$TRIED\"" | |
253 | FOUND=1 | |
254 | ;; | |
255 | esac | |
256 | ;; | |
257 | ||
258 | # We have already found a rev to be tested. | |
259 | ,1,*,bisect_rev*) VARS=1 ;; | |
260 | ,1,*,*) ;; | |
261 | ||
262 | # ??? | |
263 | *) die "filter_skipped error " \ | |
264 | "VARS: '$VARS' " \ | |
265 | "FOUND: '$FOUND' " \ | |
266 | "TRIED: '$TRIED' " \ | |
267 | "hash: '$hash' " \ | |
268 | "line: '$line'" | |
269 | ;; | |
270 | esac | |
271 | done | |
272 | } | |
273 | ||
274 | exit_if_skipped_commits () { | |
275 | _tried=$1 | |
276 | if expr "$_tried" : ".*[|].*" > /dev/null ; then | |
277 | echo "There are only 'skip'ped commit left to test." | |
278 | echo "The first bad commit could be any of:" | |
063036af RW |
279 | echo "$_tried" | sed -e 's/[|]/\ |
280 | /g' | |
97e1c51e CC |
281 | echo "We cannot bisect more!" |
282 | exit 2 | |
283 | fi | |
284 | } | |
285 | ||
8cc6a083 | 286 | bisect_next() { |
8fe26f44 | 287 | case "$#" in 0) ;; *) usage ;; esac |
8cc6a083 | 288 | bisect_autostart |
0a5280a9 JH |
289 | bisect_next_check good |
290 | ||
97e1c51e CC |
291 | skip=$(git for-each-ref --format='%(objectname)' \ |
292 | "refs/bisect/skip-*" | tr '[\012]' ' ') || exit | |
293 | ||
294 | BISECT_OPT='' | |
295 | test -n "$skip" && BISECT_OPT='--bisect-all' | |
296 | ||
5be60078 | 297 | bad=$(git rev-parse --verify refs/bisect/bad) && |
0a5280a9 JH |
298 | good=$(git for-each-ref --format='^%(objectname)' \ |
299 | "refs/bisect/good-*" | tr '[\012]' ' ') && | |
97e1c51e | 300 | eval="git rev-list --bisect-vars $BISECT_OPT $good $bad --" && |
0a5280a9 | 301 | eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" && |
97e1c51e | 302 | eval=$(filter_skipped "$eval" "$skip") && |
0a5280a9 JH |
303 | eval "$eval" || exit |
304 | ||
305 | if [ -z "$bisect_rev" ]; then | |
306 | echo "$bad was both good and bad" | |
307 | exit 1 | |
670f5fe3 | 308 | fi |
0a5280a9 | 309 | if [ "$bisect_rev" = "$bad" ]; then |
97e1c51e | 310 | exit_if_skipped_commits "$bisect_tried" |
0a5280a9 | 311 | echo "$bisect_rev is first bad commit" |
5be60078 | 312 | git diff-tree --pretty $bisect_rev |
0a5280a9 | 313 | exit 0 |
8cc6a083 | 314 | fi |
0a5280a9 | 315 | |
97e1c51e CC |
316 | # We should exit here only if the "bad" |
317 | # commit is also a "skip" commit (see above). | |
318 | exit_if_skipped_commits "$bisect_rev" | |
319 | ||
0a5280a9 JH |
320 | echo "Bisecting: $bisect_nr revisions left to test after this" |
321 | echo "$bisect_rev" >"$GIT_DIR/refs/heads/new-bisect" | |
08f16750 | 322 | git checkout -q new-bisect || exit |
8cc6a083 | 323 | mv "$GIT_DIR/refs/heads/new-bisect" "$GIT_DIR/refs/heads/bisect" && |
5be60078 JH |
324 | GIT_DIR="$GIT_DIR" git symbolic-ref HEAD refs/heads/bisect |
325 | git show-branch "$bisect_rev" | |
8cc6a083 LT |
326 | } |
327 | ||
cc9f24d0 JH |
328 | bisect_visualize() { |
329 | bisect_next_check fail | |
e9a45d75 JH |
330 | not=`cd "$GIT_DIR/refs" && echo bisect/good-*` |
331 | eval gitk bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES") | |
cc9f24d0 JH |
332 | } |
333 | ||
8cc6a083 LT |
334 | bisect_reset() { |
335 | case "$#" in | |
810255fd PB |
336 | 0) if [ -s "$GIT_DIR/head-name" ]; then |
337 | branch=`cat "$GIT_DIR/head-name"` | |
338 | else | |
339 | branch=master | |
340 | fi ;; | |
737c74ee CC |
341 | 1) git show-ref --verify --quiet -- "refs/heads/$1" || |
342 | die "$1 does not seem to be a valid branch" | |
8cc6a083 | 343 | branch="$1" ;; |
8fe26f44 | 344 | *) |
8cc6a083 LT |
345 | usage ;; |
346 | esac | |
9b709e47 | 347 | if git checkout "$branch"; then |
38a47fd6 CC |
348 | rm -f "$GIT_DIR/head-name" |
349 | bisect_clean_state | |
9b709e47 | 350 | fi |
e204de28 JH |
351 | } |
352 | ||
38a47fd6 CC |
353 | bisect_clean_state() { |
354 | rm -fr "$GIT_DIR/refs/bisect" | |
355 | rm -f "$GIT_DIR/refs/heads/bisect" | |
356 | rm -f "$GIT_DIR/BISECT_LOG" | |
357 | rm -f "$GIT_DIR/BISECT_NAMES" | |
358 | rm -f "$GIT_DIR/BISECT_RUN" | |
359 | } | |
360 | ||
e204de28 | 361 | bisect_replay () { |
737c74ee | 362 | test -r "$1" || die "cannot read $1 for replaying" |
e204de28 JH |
363 | bisect_reset |
364 | while read bisect command rev | |
365 | do | |
366 | test "$bisect" = "git-bisect" || continue | |
367 | case "$command" in | |
368 | start) | |
e9a45d75 | 369 | cmd="bisect_start $rev" |
737c74ee CC |
370 | eval "$cmd" ;; |
371 | good|bad|skip) | |
372 | bisect_write "$command" "$rev" ;; | |
e204de28 | 373 | *) |
737c74ee | 374 | die "?? what are you talking about?" ;; |
e204de28 JH |
375 | esac |
376 | done <"$1" | |
377 | bisect_auto_next | |
8cc6a083 LT |
378 | } |
379 | ||
a17c4101 | 380 | bisect_run () { |
83020120 CC |
381 | bisect_next_check fail |
382 | ||
a17c4101 CC |
383 | while true |
384 | do | |
385 | echo "running $@" | |
386 | "$@" | |
387 | res=$? | |
388 | ||
389 | # Check for really bad run error. | |
390 | if [ $res -lt 0 -o $res -ge 128 ]; then | |
391 | echo >&2 "bisect run failed:" | |
392 | echo >&2 "exit code $res from '$@' is < 0 or >= 128" | |
393 | exit $res | |
394 | fi | |
395 | ||
155fc795 | 396 | # Find current state depending on run success or failure. |
71b0251c CC |
397 | # A special exit code of 125 means cannot test. |
398 | if [ $res -eq 125 ]; then | |
399 | state='skip' | |
400 | elif [ $res -gt 0 ]; then | |
155fc795 | 401 | state='bad' |
a17c4101 | 402 | else |
155fc795 | 403 | state='good' |
a17c4101 CC |
404 | fi |
405 | ||
155fc795 CC |
406 | # We have to use a subshell because "bisect_state" can exit. |
407 | ( bisect_state $state > "$GIT_DIR/BISECT_RUN" ) | |
a17c4101 CC |
408 | res=$? |
409 | ||
410 | cat "$GIT_DIR/BISECT_RUN" | |
411 | ||
71b0251c CC |
412 | if grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \ |
413 | > /dev/null; then | |
414 | echo >&2 "bisect run cannot continue any more" | |
415 | exit $res | |
416 | fi | |
417 | ||
a17c4101 CC |
418 | if [ $res -ne 0 ]; then |
419 | echo >&2 "bisect run failed:" | |
155fc795 | 420 | echo >&2 "'bisect_state $state' exited with error code $res" |
a17c4101 CC |
421 | exit $res |
422 | fi | |
423 | ||
424 | if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then | |
425 | echo "bisect run success" | |
426 | exit 0; | |
427 | fi | |
428 | ||
429 | done | |
430 | } | |
431 | ||
432 | ||
8cc6a083 LT |
433 | case "$#" in |
434 | 0) | |
435 | usage ;; | |
436 | *) | |
437 | cmd="$1" | |
438 | shift | |
439 | case "$cmd" in | |
440 | start) | |
441 | bisect_start "$@" ;; | |
155fc795 CC |
442 | bad|good|skip) |
443 | bisect_state "$cmd" "$@" ;; | |
8cc6a083 LT |
444 | next) |
445 | # Not sure we want "next" at the UI level anymore. | |
446 | bisect_next "$@" ;; | |
cc9f24d0 JH |
447 | visualize) |
448 | bisect_visualize "$@" ;; | |
8cc6a083 LT |
449 | reset) |
450 | bisect_reset "$@" ;; | |
e204de28 JH |
451 | replay) |
452 | bisect_replay "$@" ;; | |
453 | log) | |
454 | cat "$GIT_DIR/BISECT_LOG" ;; | |
a17c4101 CC |
455 | run) |
456 | bisect_run "$@" ;; | |
8cc6a083 LT |
457 | *) |
458 | usage ;; | |
459 | esac | |
460 | esac |