Merge branch 'sb/committer' into next
[git/platforms/storm.git] / git-bisect.sh
blobb5171c9e2e0d4c3a543a504a6b38adb326d8afd6
1 #!/bin/sh
3 USAGE='[help|start|bad|good|skip|next|reset|visualize|replay|log|run]'
4 LONG_USAGE='git bisect help
5 print this long help message.
6 git bisect start [<bad> [<good>...]] [--] [<pathspec>...]
7 reset bisect state and start bisection.
8 git bisect bad [<rev>]
9 mark <rev> a known-bad revision.
10 git bisect good [<rev>...]
11 mark <rev>... known-good revisions.
12 git bisect skip [<rev>...]
13 mark <rev>... untestable revisions.
14 git bisect next
15 find next bisection to test and check it out.
16 git bisect reset [<branch>]
17 finish bisection search and go back to branch.
18 git bisect visualize
19 show bisect status in gitk.
20 git bisect replay <logfile>
21 replay bisection log.
22 git bisect log
23 show bisect log.
24 git bisect run <cmd>...
25 use <cmd>... to automatically bisect.
27 Please use "git help bisect" to get the full man page.'
29 OPTIONS_SPEC=
30 . git-sh-setup
31 require_work_tree
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"
36 sq() {
37 @@PERL@@ -e '
38 for (@ARGV) {
39 s/'\''/'\'\\\\\'\''/g;
40 print " '\''$_'\''";
42 print "\n";
43 ' "$@"
46 bisect_autostart() {
47 test -f "$GIT_DIR/BISECT_NAMES" || {
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
64 bisect_start() {
66 # Verify HEAD. If we were bisecting before this, reset to the
67 # top-of-line master first!
69 head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
70 head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
71 die "Bad HEAD - I need a HEAD"
73 # Check that we either already have BISECT_START, or that the
74 # branches bisect, new-bisect don't exist, to not override them.
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.'
81 start_head=''
82 case "$head" in
83 refs/heads/bisect)
84 branch=`cat "$GIT_DIR/BISECT_START"`
85 git checkout $branch || exit
87 refs/heads/*|$_x40)
88 # This error message should only be triggered by cogito usage,
89 # and cogito users should understand it relates to cg-seek.
90 [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
91 start_head="${head#refs/heads/}"
94 die "Bad HEAD - strange symbolic ref"
96 esac
99 # Get rid of any old bisect state
101 bisect_clean_state
104 # Check for one bad and then some good revisions.
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
112 eval=''
113 while [ $# -gt 0 ]; do
114 arg="$1"
115 case "$arg" in
117 shift
118 break
121 rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || {
122 test $has_double_dash -eq 1 &&
123 die "'$arg' does not appear to be a valid revision"
124 break
126 case $bad_seen in
127 0) state='bad' ; bad_seen=1 ;;
128 *) state='good' ;;
129 esac
130 eval="$eval bisect_write '$state' '$rev' 'nolog'; "
131 shift
133 esac
134 done
136 sq "$@" >"$GIT_DIR/BISECT_NAMES"
137 test -n "$start_head" && echo "$start_head" >"$GIT_DIR/BISECT_START"
138 eval "$eval"
139 echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG"
140 bisect_auto_next
143 bisect_write() {
144 state="$1"
145 rev="$2"
146 nolog="$3"
147 case "$state" in
148 bad) tag="$state" ;;
149 good|skip) tag="$state"-"$rev" ;;
150 *) die "Bad bisect_write argument: $state" ;;
151 esac
152 git update-ref "refs/bisect/$tag" "$rev"
153 echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG"
154 test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
157 bisect_state() {
158 bisect_autostart
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" ;;
167 2,bad|*,good|*,skip)
168 shift
169 eval=''
170 for rev in "$@"
172 sha=$(git rev-parse --verify "$rev^{commit}") ||
173 die "Bad rev input: $rev"
174 eval="$eval bisect_write '$state' '$sha'; "
175 done
176 eval "$eval" ;;
177 *,bad)
178 die "'git bisect bad' can take only one argument." ;;
180 usage ;;
181 esac
182 bisect_auto_next
185 bisect_next_check() {
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
190 case "$missing_good,$missing_bad,$1" in
191 ,,*)
192 : have both good and bad - ok
195 # do not have both but not asked to fail - just report.
196 false
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
207 : bisect without good...
210 THEN=''
211 test -f "$GIT_DIR/BISECT_NAMES" || {
212 echo >&2 'You need to start by "git bisect start".'
213 THEN='then '
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 ;;
220 esac
223 bisect_auto_next() {
224 bisect_next_check && bisect_next || :
227 filter_skipped() {
228 _eval="$1"
229 _skip="$2"
231 if [ -z "$_skip" ]; then
232 eval $_eval
233 return
236 # Let's parse the output of:
237 # "git rev-list --bisect-vars --bisect-all ..."
238 eval $_eval | while read hash line
240 case "$VARS,$FOUND,$TRIED,$hash" in
241 # We display some vars.
242 1,*,*,*) echo "$hash $line" ;;
244 # Split line.
245 ,*,*,---*) ;;
247 # We had nothing to search.
248 ,,,bisect_rev*)
249 echo "bisect_rev="
250 VARS=1
253 # We did not find a good bisect rev.
254 # This should happen only if the "bad"
255 # commit is also a "skip" commit.
256 ,,*,bisect_rev*)
257 echo "bisect_rev=$TRIED"
258 VARS=1
261 # We are searching.
262 ,,*,*)
263 TRIED="${TRIED:+$TRIED|}$hash"
264 case "$_skip" in
265 *$hash*) ;;
267 echo "bisect_rev=$hash"
268 echo "bisect_tried=\"$TRIED\""
269 FOUND=1
271 esac
274 # We have already found a rev to be tested.
275 ,1,*,bisect_rev*) VARS=1 ;;
276 ,1,*,*) ;;
278 # ???
279 *) die "filter_skipped error " \
280 "VARS: '$VARS' " \
281 "FOUND: '$FOUND' " \
282 "TRIED: '$TRIED' " \
283 "hash: '$hash' " \
284 "line: '$line'"
286 esac
287 done
290 exit_if_skipped_commits () {
291 _tried=$1
292 if expr "$_tried" : ".*[|].*" > /dev/null ; then
293 echo "There are only 'skip'ped commit left to test."
294 echo "The first bad commit could be any of:"
295 echo "$_tried" | tr '[|]' '[\012]'
296 echo "We cannot bisect more!"
297 exit 2
301 bisect_next() {
302 case "$#" in 0) ;; *) usage ;; esac
303 bisect_autostart
304 bisect_next_check good
306 skip=$(git for-each-ref --format='%(objectname)' \
307 "refs/bisect/skip-*" | tr '\012' ' ') || exit
309 BISECT_OPT=''
310 test -n "$skip" && BISECT_OPT='--bisect-all'
312 bad=$(git rev-parse --verify refs/bisect/bad) &&
313 good=$(git for-each-ref --format='^%(objectname)' \
314 "refs/bisect/good-*" | tr '\012' ' ') &&
315 eval="git rev-list --bisect-vars $BISECT_OPT $good $bad --" &&
316 eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" &&
317 eval=$(filter_skipped "$eval" "$skip") &&
318 eval "$eval" || exit
320 if [ -z "$bisect_rev" ]; then
321 echo "$bad was both good and bad"
322 exit 1
324 if [ "$bisect_rev" = "$bad" ]; then
325 exit_if_skipped_commits "$bisect_tried"
326 echo "$bisect_rev is first bad commit"
327 git diff-tree --pretty $bisect_rev
328 exit 0
331 # We should exit here only if the "bad"
332 # commit is also a "skip" commit (see above).
333 exit_if_skipped_commits "$bisect_rev"
335 echo "Bisecting: $bisect_nr revisions left to test after this"
336 git branch -D new-bisect 2> /dev/null
337 git checkout -q -b new-bisect "$bisect_rev" || exit
338 git branch -M new-bisect bisect
339 git show-branch "$bisect_rev"
342 bisect_visualize() {
343 bisect_next_check fail
345 if test $# = 0
346 then
347 case "${DISPLAY+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" in
348 '') set git log ;;
349 set*) set gitk ;;
350 esac
351 else
352 case "$1" in
353 git*|tig) ;;
354 -*) set git log "$@" ;;
355 *) set git "$@" ;;
356 esac
359 not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*")
360 eval '"$@"' refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
363 bisect_reset() {
364 test -f "$GIT_DIR/BISECT_NAMES" || {
365 echo "We are not bisecting."
366 return
368 case "$#" in
369 0) if [ -s "$GIT_DIR/BISECT_START" ]; then
370 branch=`cat "$GIT_DIR/BISECT_START"`
371 else
372 branch=master
373 fi ;;
374 1) git show-ref --verify --quiet -- "refs/heads/$1" ||
375 die "$1 does not seem to be a valid branch"
376 branch="$1" ;;
378 usage ;;
379 esac
380 if git checkout "$branch"; then
381 # Cleanup head-name if it got left by an old version of git-bisect
382 rm -f "$GIT_DIR/head-name"
383 rm -f "$GIT_DIR/BISECT_START"
384 bisect_clean_state
388 bisect_clean_state() {
389 # There may be some refs packed during bisection.
390 git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* refs/heads/bisect |
391 while read ref hash
393 git update-ref -d $ref $hash
394 done
395 rm -f "$GIT_DIR/BISECT_LOG"
396 rm -f "$GIT_DIR/BISECT_NAMES"
397 rm -f "$GIT_DIR/BISECT_RUN"
400 bisect_replay () {
401 test -r "$1" || die "cannot read $1 for replaying"
402 bisect_reset
403 while read bisect command rev
405 test "$bisect" = "git-bisect" || continue
406 case "$command" in
407 start)
408 cmd="bisect_start $rev"
409 eval "$cmd" ;;
410 good|bad|skip)
411 bisect_write "$command" "$rev" ;;
413 die "?? what are you talking about?" ;;
414 esac
415 done <"$1"
416 bisect_auto_next
419 bisect_run () {
420 bisect_next_check fail
422 while true
424 echo "running $@"
425 "$@"
426 res=$?
428 # Check for really bad run error.
429 if [ $res -lt 0 -o $res -ge 128 ]; then
430 echo >&2 "bisect run failed:"
431 echo >&2 "exit code $res from '$@' is < 0 or >= 128"
432 exit $res
435 # Find current state depending on run success or failure.
436 # A special exit code of 125 means cannot test.
437 if [ $res -eq 125 ]; then
438 state='skip'
439 elif [ $res -gt 0 ]; then
440 state='bad'
441 else
442 state='good'
445 # We have to use a subshell because "bisect_state" can exit.
446 ( bisect_state $state > "$GIT_DIR/BISECT_RUN" )
447 res=$?
449 cat "$GIT_DIR/BISECT_RUN"
451 if grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \
452 > /dev/null; then
453 echo >&2 "bisect run cannot continue any more"
454 exit $res
457 if [ $res -ne 0 ]; then
458 echo >&2 "bisect run failed:"
459 echo >&2 "'bisect_state $state' exited with error code $res"
460 exit $res
463 if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
464 echo "bisect run success"
465 exit 0;
468 done
472 case "$#" in
474 usage ;;
476 cmd="$1"
477 shift
478 case "$cmd" in
479 help)
480 git bisect -h ;;
481 start)
482 bisect_start "$@" ;;
483 bad|good|skip)
484 bisect_state "$cmd" "$@" ;;
485 next)
486 # Not sure we want "next" at the UI level anymore.
487 bisect_next "$@" ;;
488 visualize|view)
489 bisect_visualize "$@" ;;
490 reset)
491 bisect_reset "$@" ;;
492 replay)
493 bisect_replay "$@" ;;
494 log)
495 cat "$GIT_DIR/BISECT_LOG" ;;
496 run)
497 bisect_run "$@" ;;
499 usage ;;
500 esac
501 esac