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