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