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