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