Bisect: use "$GIT_DIR/BISECT_NAMES" to check if we are bisecting.
[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
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 38bisect_autostart() {
6459c7c6 39 test -f "$GIT_DIR/BISECT_NAMES" || {
8cc6a083
LT
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 73 [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
e23cb8c0 74 echo "${head#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
38a47fd6
CC
85
86 #
87 # Check for one bad and then some good revisions.
88 #
89 has_double_dash=0
90 for arg; do
91 case "$arg" in --) has_double_dash=1; break ;; esac
92 done
93 orig_args=$(sq "$@")
94 bad_seen=0
95 while [ $# -gt 0 ]; do
96 arg="$1"
97 case "$arg" in
98 --)
8fe26f44 99 shift
38a47fd6
CC
100 break
101 ;;
102 *)
8fe26f44 103 rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || {
38a47fd6
CC
104 test $has_double_dash -eq 1 &&
105 die "'$arg' does not appear to be a valid revision"
106 break
107 }
737c74ee
CC
108 case $bad_seen in
109 0) state='bad' ; bad_seen=1 ;;
110 *) state='good' ;;
111 esac
112 bisect_write "$state" "$rev" 'nolog'
8fe26f44 113 shift
38a47fd6
CC
114 ;;
115 esac
8fe26f44 116 done
38a47fd6
CC
117
118 sq "$@" >"$GIT_DIR/BISECT_NAMES"
b8652b4d 119 echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG"
38a47fd6 120 bisect_auto_next
8cc6a083
LT
121}
122
55624f9a
CC
123bisect_write() {
124 state="$1"
125 rev="$2"
737c74ee 126 nolog="$3"
55624f9a
CC
127 case "$state" in
128 bad) tag="$state" ;;
129 good|skip) tag="$state"-"$rev" ;;
130 *) die "Bad bisect_write argument: $state" ;;
131 esac
3d7cd64c 132 git update-ref "refs/bisect/$tag" "$rev"
55624f9a 133 echo "# $state: "$(git show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
737c74ee 134 test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
55624f9a
CC
135}
136
155fc795 137bisect_state() {
8cc6a083 138 bisect_autostart
155fc795
CC
139 state=$1
140 case "$#,$state" in
141 0,*)
142 die "Please call 'bisect_state' with at least one argument." ;;
143 1,bad|1,good|1,skip)
144 rev=$(git rev-parse --verify HEAD) ||
145 die "Bad rev input: HEAD"
146 bisect_write "$state" "$rev" ;;
147 2,bad)
148 rev=$(git rev-parse --verify "$2^{commit}") ||
149 die "Bad rev input: $2"
150 bisect_write "$state" "$rev" ;;
151 *,good|*,skip)
152 shift
153 revs=$(git rev-parse --revs-only --no-flags "$@") &&
154 test '' != "$revs" || die "Bad rev input: $@"
155 for rev in $revs
156 do
157 rev=$(git rev-parse --verify "$rev^{commit}") ||
158 die "Bad rev commit: $rev^{commit}"
159 bisect_write "$state" "$rev"
160 done ;;
cc9f24d0
JH
161 *)
162 usage ;;
8cc6a083 163 esac
97e1c51e
CC
164 bisect_auto_next
165}
166
8cc6a083 167bisect_next_check() {
0a5280a9
JH
168 missing_good= missing_bad=
169 git show-ref -q --verify refs/bisect/bad || missing_bad=t
170 test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t
6fecf191 171
0a5280a9
JH
172 case "$missing_good,$missing_bad,$1" in
173 ,,*)
174 : have both good and bad - ok
175 ;;
176 *,)
177 # do not have both but not asked to fail - just report.
178 false
179 ;;
180 t,,good)
181 # have bad but not good. we could bisect although
182 # this is less optimum.
183 echo >&2 'Warning: bisecting only with a bad commit.'
184 if test -t 0
185 then
186 printf >&2 'Are you sure [Y/n]? '
187 case "$(read yesno)" in [Nn]*) exit 1 ;; esac
188 fi
189 : bisect without good...
190 ;;
8cc6a083 191 *)
0a5280a9 192 THEN=''
6459c7c6 193 test -f "$GIT_DIR/BISECT_NAMES" || {
0a5280a9
JH
194 echo >&2 'You need to start by "git bisect start".'
195 THEN='then '
196 }
197 echo >&2 'You '$THEN'need to give me at least one good' \
198 'and one bad revisions.'
199 echo >&2 '(You can use "git bisect bad" and' \
200 '"git bisect good" for that.)'
201 exit 1 ;;
8cc6a083
LT
202 esac
203}
204
205bisect_auto_next() {
434d036f 206 bisect_next_check && bisect_next || :
8cc6a083
LT
207}
208
97e1c51e
CC
209filter_skipped() {
210 _eval="$1"
211 _skip="$2"
212
213 if [ -z "$_skip" ]; then
214 eval $_eval
215 return
216 fi
217
218 # Let's parse the output of:
219 # "git rev-list --bisect-vars --bisect-all ..."
220 eval $_eval | while read hash line
221 do
222 case "$VARS,$FOUND,$TRIED,$hash" in
223 # We display some vars.
224 1,*,*,*) echo "$hash $line" ;;
225
226 # Split line.
227 ,*,*,---*) ;;
228
229 # We had nothing to search.
230 ,,,bisect_rev*)
231 echo "bisect_rev="
232 VARS=1
233 ;;
234
235 # We did not find a good bisect rev.
236 # This should happen only if the "bad"
237 # commit is also a "skip" commit.
238 ,,*,bisect_rev*)
239 echo "bisect_rev=$TRIED"
240 VARS=1
241 ;;
242
243 # We are searching.
244 ,,*,*)
245 TRIED="${TRIED:+$TRIED|}$hash"
246 case "$_skip" in
247 *$hash*) ;;
248 *)
249 echo "bisect_rev=$hash"
250 echo "bisect_tried=\"$TRIED\""
251 FOUND=1
252 ;;
253 esac
254 ;;
255
256 # We have already found a rev to be tested.
257 ,1,*,bisect_rev*) VARS=1 ;;
258 ,1,*,*) ;;
259
260 # ???
261 *) die "filter_skipped error " \
262 "VARS: '$VARS' " \
263 "FOUND: '$FOUND' " \
264 "TRIED: '$TRIED' " \
265 "hash: '$hash' " \
266 "line: '$line'"
267 ;;
268 esac
269 done
270}
271
272exit_if_skipped_commits () {
273 _tried=$1
274 if expr "$_tried" : ".*[|].*" > /dev/null ; then
275 echo "There are only 'skip'ped commit left to test."
276 echo "The first bad commit could be any of:"
e23cb8c0 277 echo "$_tried" | tr '[|]' '[\012]'
97e1c51e
CC
278 echo "We cannot bisect more!"
279 exit 2
280 fi
281}
282
8cc6a083 283bisect_next() {
8fe26f44 284 case "$#" in 0) ;; *) usage ;; esac
8cc6a083 285 bisect_autostart
0a5280a9
JH
286 bisect_next_check good
287
97e1c51e
CC
288 skip=$(git for-each-ref --format='%(objectname)' \
289 "refs/bisect/skip-*" | tr '[\012]' ' ') || exit
290
291 BISECT_OPT=''
292 test -n "$skip" && BISECT_OPT='--bisect-all'
293
5be60078 294 bad=$(git rev-parse --verify refs/bisect/bad) &&
0a5280a9
JH
295 good=$(git for-each-ref --format='^%(objectname)' \
296 "refs/bisect/good-*" | tr '[\012]' ' ') &&
97e1c51e 297 eval="git rev-list --bisect-vars $BISECT_OPT $good $bad --" &&
0a5280a9 298 eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" &&
97e1c51e 299 eval=$(filter_skipped "$eval" "$skip") &&
0a5280a9
JH
300 eval "$eval" || exit
301
302 if [ -z "$bisect_rev" ]; then
303 echo "$bad was both good and bad"
304 exit 1
670f5fe3 305 fi
0a5280a9 306 if [ "$bisect_rev" = "$bad" ]; then
97e1c51e 307 exit_if_skipped_commits "$bisect_tried"
0a5280a9 308 echo "$bisect_rev is first bad commit"
5be60078 309 git diff-tree --pretty $bisect_rev
0a5280a9 310 exit 0
8cc6a083 311 fi
0a5280a9 312
97e1c51e
CC
313 # We should exit here only if the "bad"
314 # commit is also a "skip" commit (see above).
315 exit_if_skipped_commits "$bisect_rev"
316
0a5280a9 317 echo "Bisecting: $bisect_nr revisions left to test after this"
0bee49c6 318 git branch -f new-bisect "$bisect_rev"
08f16750 319 git checkout -q new-bisect || exit
0bee49c6 320 git branch -M new-bisect bisect
5be60078 321 git show-branch "$bisect_rev"
8cc6a083
LT
322}
323
cc9f24d0
JH
324bisect_visualize() {
325 bisect_next_check fail
e3f062bf
CC
326 not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*")
327 eval gitk refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
cc9f24d0
JH
328}
329
8cc6a083
LT
330bisect_reset() {
331 case "$#" in
810255fd
PB
332 0) if [ -s "$GIT_DIR/head-name" ]; then
333 branch=`cat "$GIT_DIR/head-name"`
334 else
335 branch=master
336 fi ;;
737c74ee
CC
337 1) git show-ref --verify --quiet -- "refs/heads/$1" ||
338 die "$1 does not seem to be a valid branch"
8cc6a083 339 branch="$1" ;;
8fe26f44 340 *)
8cc6a083
LT
341 usage ;;
342 esac
9b709e47 343 if git checkout "$branch"; then
38a47fd6
CC
344 rm -f "$GIT_DIR/head-name"
345 bisect_clean_state
9b709e47 346 fi
e204de28
JH
347}
348
38a47fd6 349bisect_clean_state() {
947a604b
CC
350 # There may be some refs packed during bisection.
351 git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* refs/heads/bisect |
352 while read ref hash
353 do
354 git update-ref -d $ref $hash
355 done
38a47fd6
CC
356 rm -f "$GIT_DIR/BISECT_LOG"
357 rm -f "$GIT_DIR/BISECT_NAMES"
358 rm -f "$GIT_DIR/BISECT_RUN"
359}
360
e204de28 361bisect_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 380bisect_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
433case "$#" in
4340)
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
460esac