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