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