Rename the test trash directory to contain spaces.
[git/bdonlan.git] / git-bisect.sh
blobd8d9bfde4cdd4b558992c68cb7cdaaa1b8a1212d
1 #!/bin/sh
3 USAGE='[help|start|bad|good|skip|next|reset|visualize|replay|log|run]'
4 LONG_USAGE='git bisect help
5 print this long help message.
6 git bisect start [<bad> [<good>...]] [--] [<pathspec>...]
7 reset bisect state and start bisection.
8 git bisect bad [<rev>]
9 mark <rev> a known-bad revision.
10 git bisect good [<rev>...]
11 mark <rev>... known-good revisions.
12 git bisect skip [<rev>...]
13 mark <rev>... untestable revisions.
14 git bisect next
15 find next bisection to test and check it out.
16 git bisect reset [<branch>]
17 finish bisection search and go back to branch.
18 git bisect visualize
19 show bisect status in gitk.
20 git bisect replay <logfile>
21 replay bisection log.
22 git bisect log
23 show bisect log.
24 git bisect run <cmd>...
25 use <cmd>... to automatically bisect.
27 Please use "git help bisect" to get the full man page.'
29 OPTIONS_SPEC=
30 . git-sh-setup
31 require_work_tree
33 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
34 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
36 sq() {
37 @@PERL@@ -e '
38 for (@ARGV) {
39 s/'\''/'\'\\\\\'\''/g;
40 print " '\''$_'\''";
42 print "\n";
43 ' "$@"
46 bisect_autostart() {
47 test -f "$GIT_DIR/BISECT_NAMES" || {
48 echo >&2 'You need to start by "git bisect start"'
49 if test -t 0
50 then
51 echo >&2 -n 'Do you want me to do it for you [Y/n]? '
52 read yesno
53 case "$yesno" in
54 [Nn]*)
55 exit ;;
56 esac
57 bisect_start
58 else
59 exit 1
64 bisect_start() {
66 # Verify HEAD. If we were bisecting before this, reset to the
67 # top-of-line master first!
69 head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
70 head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
71 die "Bad HEAD - I need a HEAD"
72 start_head=''
73 case "$head" in
74 refs/heads/bisect)
75 if [ -s "$GIT_DIR/BISECT_START" ]; then
76 branch=`cat "$GIT_DIR/BISECT_START"`
77 else
78 branch=master
80 git checkout $branch || exit
82 refs/heads/*|$_x40)
83 # This error message should only be triggered by cogito usage,
84 # and cogito users should understand it relates to cg-seek.
85 [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
86 start_head="${head#refs/heads/}"
89 die "Bad HEAD - strange symbolic ref"
91 esac
94 # Get rid of any old bisect state
96 bisect_clean_state
99 # Check for one bad and then some good revisions.
101 has_double_dash=0
102 for arg; do
103 case "$arg" in --) has_double_dash=1; break ;; esac
104 done
105 orig_args=$(sq "$@")
106 bad_seen=0
107 eval=''
108 while [ $# -gt 0 ]; do
109 arg="$1"
110 case "$arg" in
112 shift
113 break
116 rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || {
117 test $has_double_dash -eq 1 &&
118 die "'$arg' does not appear to be a valid revision"
119 break
121 case $bad_seen in
122 0) state='bad' ; bad_seen=1 ;;
123 *) state='good' ;;
124 esac
125 eval="$eval bisect_write '$state' '$rev' 'nolog'; "
126 shift
128 esac
129 done
131 sq "$@" >"$GIT_DIR/BISECT_NAMES"
132 test -n "$start_head" && echo "$start_head" >"$GIT_DIR/BISECT_START"
133 eval "$eval"
134 echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG"
135 bisect_auto_next
138 bisect_write() {
139 state="$1"
140 rev="$2"
141 nolog="$3"
142 case "$state" in
143 bad) tag="$state" ;;
144 good|skip) tag="$state"-"$rev" ;;
145 *) die "Bad bisect_write argument: $state" ;;
146 esac
147 git update-ref "refs/bisect/$tag" "$rev"
148 echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG"
149 test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
152 bisect_state() {
153 bisect_autostart
154 state=$1
155 case "$#,$state" in
156 0,*)
157 die "Please call 'bisect_state' with at least one argument." ;;
158 1,bad|1,good|1,skip)
159 rev=$(git rev-parse --verify HEAD) ||
160 die "Bad rev input: HEAD"
161 bisect_write "$state" "$rev" ;;
162 2,bad|*,good|*,skip)
163 shift
164 eval=''
165 for rev in "$@"
167 sha=$(git rev-parse --verify "$rev^{commit}") ||
168 die "Bad rev input: $rev"
169 eval="$eval bisect_write '$state' '$sha'; "
170 done
171 eval "$eval" ;;
172 *,bad)
173 die "'git bisect bad' can take only one argument." ;;
175 usage ;;
176 esac
177 bisect_auto_next
180 bisect_next_check() {
181 missing_good= missing_bad=
182 git show-ref -q --verify refs/bisect/bad || missing_bad=t
183 test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t
185 case "$missing_good,$missing_bad,$1" in
186 ,,*)
187 : have both good and bad - ok
190 # do not have both but not asked to fail - just report.
191 false
193 t,,good)
194 # have bad but not good. we could bisect although
195 # this is less optimum.
196 echo >&2 'Warning: bisecting only with a bad commit.'
197 if test -t 0
198 then
199 printf >&2 'Are you sure [Y/n]? '
200 case "$(read yesno)" in [Nn]*) exit 1 ;; esac
202 : bisect without good...
205 THEN=''
206 test -f "$GIT_DIR/BISECT_NAMES" || {
207 echo >&2 'You need to start by "git bisect start".'
208 THEN='then '
210 echo >&2 'You '$THEN'need to give me at least one good' \
211 'and one bad revisions.'
212 echo >&2 '(You can use "git bisect bad" and' \
213 '"git bisect good" for that.)'
214 exit 1 ;;
215 esac
218 bisect_auto_next() {
219 bisect_next_check && bisect_next || :
222 filter_skipped() {
223 _eval="$1"
224 _skip="$2"
226 if [ -z "$_skip" ]; then
227 eval $_eval
228 return
231 # Let's parse the output of:
232 # "git rev-list --bisect-vars --bisect-all ..."
233 eval $_eval | while read hash line
235 case "$VARS,$FOUND,$TRIED,$hash" in
236 # We display some vars.
237 1,*,*,*) echo "$hash $line" ;;
239 # Split line.
240 ,*,*,---*) ;;
242 # We had nothing to search.
243 ,,,bisect_rev*)
244 echo "bisect_rev="
245 VARS=1
248 # We did not find a good bisect rev.
249 # This should happen only if the "bad"
250 # commit is also a "skip" commit.
251 ,,*,bisect_rev*)
252 echo "bisect_rev=$TRIED"
253 VARS=1
256 # We are searching.
257 ,,*,*)
258 TRIED="${TRIED:+$TRIED|}$hash"
259 case "$_skip" in
260 *$hash*) ;;
262 echo "bisect_rev=$hash"
263 echo "bisect_tried=\"$TRIED\""
264 FOUND=1
266 esac
269 # We have already found a rev to be tested.
270 ,1,*,bisect_rev*) VARS=1 ;;
271 ,1,*,*) ;;
273 # ???
274 *) die "filter_skipped error " \
275 "VARS: '$VARS' " \
276 "FOUND: '$FOUND' " \
277 "TRIED: '$TRIED' " \
278 "hash: '$hash' " \
279 "line: '$line'"
281 esac
282 done
285 exit_if_skipped_commits () {
286 _tried=$1
287 if expr "$_tried" : ".*[|].*" > /dev/null ; then
288 echo "There are only 'skip'ped commit left to test."
289 echo "The first bad commit could be any of:"
290 echo "$_tried" | tr '[|]' '[\012]'
291 echo "We cannot bisect more!"
292 exit 2
296 bisect_next() {
297 case "$#" in 0) ;; *) usage ;; esac
298 bisect_autostart
299 bisect_next_check good
301 skip=$(git for-each-ref --format='%(objectname)' \
302 "refs/bisect/skip-*" | tr '\012' ' ') || exit
304 BISECT_OPT=''
305 test -n "$skip" && BISECT_OPT='--bisect-all'
307 bad=$(git rev-parse --verify refs/bisect/bad) &&
308 good=$(git for-each-ref --format='^%(objectname)' \
309 "refs/bisect/good-*" | tr '\012' ' ') &&
310 eval="git rev-list --bisect-vars $BISECT_OPT $good $bad --" &&
311 eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" &&
312 eval=$(filter_skipped "$eval" "$skip") &&
313 eval "$eval" || exit
315 if [ -z "$bisect_rev" ]; then
316 echo "$bad was both good and bad"
317 exit 1
319 if [ "$bisect_rev" = "$bad" ]; then
320 exit_if_skipped_commits "$bisect_tried"
321 echo "$bisect_rev is first bad commit"
322 git diff-tree --pretty $bisect_rev
323 exit 0
326 # We should exit here only if the "bad"
327 # commit is also a "skip" commit (see above).
328 exit_if_skipped_commits "$bisect_rev"
330 echo "Bisecting: $bisect_nr revisions left to test after this"
331 git branch -f new-bisect "$bisect_rev"
332 git checkout -q new-bisect || exit
333 git branch -M new-bisect bisect
334 git show-branch "$bisect_rev"
337 bisect_visualize() {
338 bisect_next_check fail
340 if test $# = 0
341 then
342 case "${DISPLAY+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" in
343 '') set git log ;;
344 set*) set gitk ;;
345 esac
346 else
347 case "$1" in
348 git*|tig) ;;
349 -*) set git log "$@" ;;
350 *) set git "$@" ;;
351 esac
354 not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*")
355 eval '"$@"' refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
358 bisect_reset() {
359 test -f "$GIT_DIR/BISECT_NAMES" || {
360 echo "We are not bisecting."
361 return
363 case "$#" in
364 0) if [ -s "$GIT_DIR/BISECT_START" ]; then
365 branch=`cat "$GIT_DIR/BISECT_START"`
366 else
367 branch=master
368 fi ;;
369 1) git show-ref --verify --quiet -- "refs/heads/$1" ||
370 die "$1 does not seem to be a valid branch"
371 branch="$1" ;;
373 usage ;;
374 esac
375 if git checkout "$branch"; then
376 # Cleanup head-name if it got left by an old version of git-bisect
377 rm -f "$GIT_DIR/head-name"
378 rm -f "$GIT_DIR/BISECT_START"
379 bisect_clean_state
383 bisect_clean_state() {
384 # There may be some refs packed during bisection.
385 git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* refs/heads/bisect |
386 while read ref hash
388 git update-ref -d $ref $hash
389 done
390 rm -f "$GIT_DIR/BISECT_LOG"
391 rm -f "$GIT_DIR/BISECT_NAMES"
392 rm -f "$GIT_DIR/BISECT_RUN"
395 bisect_replay () {
396 test -r "$1" || die "cannot read $1 for replaying"
397 bisect_reset
398 while read bisect command rev
400 test "$bisect" = "git-bisect" || continue
401 case "$command" in
402 start)
403 cmd="bisect_start $rev"
404 eval "$cmd" ;;
405 good|bad|skip)
406 bisect_write "$command" "$rev" ;;
408 die "?? what are you talking about?" ;;
409 esac
410 done <"$1"
411 bisect_auto_next
414 bisect_run () {
415 bisect_next_check fail
417 while true
419 echo "running $@"
420 "$@"
421 res=$?
423 # Check for really bad run error.
424 if [ $res -lt 0 -o $res -ge 128 ]; then
425 echo >&2 "bisect run failed:"
426 echo >&2 "exit code $res from '$@' is < 0 or >= 128"
427 exit $res
430 # Find current state depending on run success or failure.
431 # A special exit code of 125 means cannot test.
432 if [ $res -eq 125 ]; then
433 state='skip'
434 elif [ $res -gt 0 ]; then
435 state='bad'
436 else
437 state='good'
440 # We have to use a subshell because "bisect_state" can exit.
441 ( bisect_state $state > "$GIT_DIR/BISECT_RUN" )
442 res=$?
444 cat "$GIT_DIR/BISECT_RUN"
446 if grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \
447 > /dev/null; then
448 echo >&2 "bisect run cannot continue any more"
449 exit $res
452 if [ $res -ne 0 ]; then
453 echo >&2 "bisect run failed:"
454 echo >&2 "'bisect_state $state' exited with error code $res"
455 exit $res
458 if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
459 echo "bisect run success"
460 exit 0;
463 done
467 case "$#" in
469 usage ;;
471 cmd="$1"
472 shift
473 case "$cmd" in
474 help)
475 git bisect -h ;;
476 start)
477 bisect_start "$@" ;;
478 bad|good|skip)
479 bisect_state "$cmd" "$@" ;;
480 next)
481 # Not sure we want "next" at the UI level anymore.
482 bisect_next "$@" ;;
483 visualize|view)
484 bisect_visualize "$@" ;;
485 reset)
486 bisect_reset "$@" ;;
487 replay)
488 bisect_replay "$@" ;;
489 log)
490 cat "$GIT_DIR/BISECT_LOG" ;;
491 run)
492 bisect_run "$@" ;;
494 usage ;;
495 esac
496 esac