diff: reset color before printing newline
[git/dscho.git] / git-bisect.sh
blobb1800edaf713f972131431fcd26b5e7f64f089fc
1 #!/bin/sh
3 USAGE='[start|bad|good|skip|next|reset|visualize|replay|log|run]'
4 LONG_USAGE='git bisect start [<bad> [<good>...]] [--] [<pathspec>...]
5 reset bisect state and start bisection.
6 git bisect bad [<rev>]
7 mark <rev> a known-bad revision.
8 git bisect good [<rev>...]
9 mark <rev>... known-good revisions.
10 git bisect skip [<rev>...]
11 mark <rev>... untestable revisions.
12 git bisect next
13 find next bisection to test and check it out.
14 git bisect reset [<branch>]
15 finish bisection search and go back to branch.
16 git bisect visualize
17 show bisect status in gitk.
18 git bisect replay <logfile>
19 replay bisection log.
20 git bisect log
21 show bisect log.
22 git bisect run <cmd>...
23 use <cmd>... to automatically bisect.'
25 OPTIONS_SPEC=
26 . git-sh-setup
27 require_work_tree
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"
32 sq() {
33 @@PERL@@ -e '
34 for (@ARGV) {
35 s/'\''/'\'\\\\\'\''/g;
36 print " '\''$_'\''";
38 print "\n";
39 ' "$@"
42 bisect_autostart() {
43 test -f "$GIT_DIR/BISECT_NAMES" || {
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
60 bisect_start() {
62 # Verify HEAD. If we were bisecting before this, reset to the
63 # top-of-line master first!
65 head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
66 head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
67 die "Bad HEAD - I need a HEAD"
69 # Check that we either already have BISECT_START, or that the
70 # branches bisect, new-bisect don't exist, to not override them.
72 test -s "$GIT_DIR/BISECT_START" ||
73 if git show-ref --verify -q refs/heads/bisect ||
74 git show-ref --verify -q refs/heads/new-bisect; then
75 die 'The branches "bisect" and "new-bisect" must not exist.'
77 start_head=''
78 case "$head" in
79 refs/heads/bisect)
80 branch=`cat "$GIT_DIR/BISECT_START"`
81 git checkout $branch || exit
83 refs/heads/*|$_x40)
84 # This error message should only be triggered by cogito usage,
85 # and cogito users should understand it relates to cg-seek.
86 [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
87 start_head="${head#refs/heads/}"
90 die "Bad HEAD - strange symbolic ref"
92 esac
95 # Get rid of any old bisect state
97 bisect_clean_state
100 # Check for one bad and then some good revisions.
102 has_double_dash=0
103 for arg; do
104 case "$arg" in --) has_double_dash=1; break ;; esac
105 done
106 orig_args=$(sq "$@")
107 bad_seen=0
108 eval=''
109 while [ $# -gt 0 ]; do
110 arg="$1"
111 case "$arg" in
113 shift
114 break
117 rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || {
118 test $has_double_dash -eq 1 &&
119 die "'$arg' does not appear to be a valid revision"
120 break
122 case $bad_seen in
123 0) state='bad' ; bad_seen=1 ;;
124 *) state='good' ;;
125 esac
126 eval="$eval bisect_write '$state' '$rev' 'nolog'; "
127 shift
129 esac
130 done
132 sq "$@" >"$GIT_DIR/BISECT_NAMES"
133 test -n "$start_head" && echo "$start_head" >"$GIT_DIR/BISECT_START"
134 eval "$eval"
135 echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG"
136 bisect_auto_next
139 bisect_write() {
140 state="$1"
141 rev="$2"
142 nolog="$3"
143 case "$state" in
144 bad) tag="$state" ;;
145 good|skip) tag="$state"-"$rev" ;;
146 *) die "Bad bisect_write argument: $state" ;;
147 esac
148 git update-ref "refs/bisect/$tag" "$rev"
149 echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG"
150 test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
153 bisect_state() {
154 bisect_autostart
155 state=$1
156 case "$#,$state" in
157 0,*)
158 die "Please call 'bisect_state' with at least one argument." ;;
159 1,bad|1,good|1,skip)
160 rev=$(git rev-parse --verify HEAD) ||
161 die "Bad rev input: HEAD"
162 bisect_write "$state" "$rev" ;;
163 2,bad|*,good|*,skip)
164 shift
165 eval=''
166 for rev in "$@"
168 sha=$(git rev-parse --verify "$rev^{commit}") ||
169 die "Bad rev input: $rev"
170 eval="$eval bisect_write '$state' '$sha'; "
171 done
172 eval "$eval" ;;
173 *,bad)
174 die "'git bisect bad' can take only one argument." ;;
176 usage ;;
177 esac
178 bisect_auto_next
181 bisect_next_check() {
182 missing_good= missing_bad=
183 git show-ref -q --verify refs/bisect/bad || missing_bad=t
184 test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t
186 case "$missing_good,$missing_bad,$1" in
187 ,,*)
188 : have both good and bad - ok
191 # do not have both but not asked to fail - just report.
192 false
194 t,,good)
195 # have bad but not good. we could bisect although
196 # this is less optimum.
197 echo >&2 'Warning: bisecting only with a bad commit.'
198 if test -t 0
199 then
200 printf >&2 'Are you sure [Y/n]? '
201 case "$(read yesno)" in [Nn]*) exit 1 ;; esac
203 : bisect without good...
206 THEN=''
207 test -f "$GIT_DIR/BISECT_NAMES" || {
208 echo >&2 'You need to start by "git bisect start".'
209 THEN='then '
211 echo >&2 'You '$THEN'need to give me at least one good' \
212 'and one bad revisions.'
213 echo >&2 '(You can use "git bisect bad" and' \
214 '"git bisect good" for that.)'
215 exit 1 ;;
216 esac
219 bisect_auto_next() {
220 bisect_next_check && bisect_next || :
223 eval_rev_list() {
224 _eval="$1"
226 eval $_eval
227 res=$?
229 if [ $res -ne 0 ]; then
230 echo >&2 "'git rev-list --bisect-vars' failed:"
231 echo >&2 "maybe you mistake good and bad revs?"
232 exit $res
235 return $res
238 filter_skipped() {
239 _eval="$1"
240 _skip="$2"
242 if [ -z "$_skip" ]; then
243 eval_rev_list "$_eval"
244 return
247 # Let's parse the output of:
248 # "git rev-list --bisect-vars --bisect-all ..."
249 eval_rev_list "$_eval" | while read hash line
251 case "$VARS,$FOUND,$TRIED,$hash" in
252 # We display some vars.
253 1,*,*,*) echo "$hash $line" ;;
255 # Split line.
256 ,*,*,---*) ;;
258 # We had nothing to search.
259 ,,,bisect_rev*)
260 echo "bisect_rev="
261 VARS=1
264 # We did not find a good bisect rev.
265 # This should happen only if the "bad"
266 # commit is also a "skip" commit.
267 ,,*,bisect_rev*)
268 echo "bisect_rev=$TRIED"
269 VARS=1
272 # We are searching.
273 ,,*,*)
274 TRIED="${TRIED:+$TRIED|}$hash"
275 case "$_skip" in
276 *$hash*) ;;
278 echo "bisect_rev=$hash"
279 echo "bisect_tried=\"$TRIED\""
280 FOUND=1
282 esac
285 # We have already found a rev to be tested.
286 ,1,*,bisect_rev*) VARS=1 ;;
287 ,1,*,*) ;;
289 # ???
290 *) die "filter_skipped error " \
291 "VARS: '$VARS' " \
292 "FOUND: '$FOUND' " \
293 "TRIED: '$TRIED' " \
294 "hash: '$hash' " \
295 "line: '$line'"
297 esac
298 done
301 exit_if_skipped_commits () {
302 _tried=$1
303 if expr "$_tried" : ".*[|].*" > /dev/null ; then
304 echo "There are only 'skip'ped commit left to test."
305 echo "The first bad commit could be any of:"
306 echo "$_tried" | tr '[|]' '[\012]'
307 echo "We cannot bisect more!"
308 exit 2
312 bisect_next() {
313 case "$#" in 0) ;; *) usage ;; esac
314 bisect_autostart
315 bisect_next_check good
317 skip=$(git for-each-ref --format='%(objectname)' \
318 "refs/bisect/skip-*" | tr '\012' ' ') || exit
320 BISECT_OPT=''
321 test -n "$skip" && BISECT_OPT='--bisect-all'
323 bad=$(git rev-parse --verify refs/bisect/bad) &&
324 good=$(git for-each-ref --format='^%(objectname)' \
325 "refs/bisect/good-*" | tr '\012' ' ') &&
326 eval="git rev-list --bisect-vars $BISECT_OPT $good $bad --" &&
327 eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" &&
328 eval=$(filter_skipped "$eval" "$skip") &&
329 eval "$eval" || exit
331 if [ -z "$bisect_rev" ]; then
332 echo "$bad was both good and bad"
333 exit 1
335 if [ "$bisect_rev" = "$bad" ]; then
336 exit_if_skipped_commits "$bisect_tried"
337 echo "$bisect_rev is first bad commit"
338 git diff-tree --pretty $bisect_rev
339 exit 0
342 # We should exit here only if the "bad"
343 # commit is also a "skip" commit (see above).
344 exit_if_skipped_commits "$bisect_rev"
346 echo "Bisecting: $bisect_nr revisions left to test after this"
347 git branch -D new-bisect 2> /dev/null
348 git checkout -q -b new-bisect "$bisect_rev" || exit
349 git branch -M new-bisect bisect
350 git show-branch "$bisect_rev"
353 bisect_visualize() {
354 bisect_next_check fail
356 if test $# = 0
357 then
358 case "${DISPLAY+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" in
359 '') set git log ;;
360 set*) set gitk ;;
361 esac
362 else
363 case "$1" in
364 git*|tig) ;;
365 -*) set git log "$@" ;;
366 *) set git "$@" ;;
367 esac
370 not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*")
371 eval '"$@"' refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
374 bisect_reset() {
375 test -f "$GIT_DIR/BISECT_NAMES" || {
376 echo "We are not bisecting."
377 return
379 case "$#" in
380 0) if [ -s "$GIT_DIR/BISECT_START" ]; then
381 branch=`cat "$GIT_DIR/BISECT_START"`
382 else
383 branch=master
384 fi ;;
385 1) git show-ref --verify --quiet -- "refs/heads/$1" ||
386 die "$1 does not seem to be a valid branch"
387 branch="$1" ;;
389 usage ;;
390 esac
391 if git checkout "$branch"; then
392 # Cleanup head-name if it got left by an old version of git-bisect
393 rm -f "$GIT_DIR/head-name"
394 rm -f "$GIT_DIR/BISECT_START"
395 bisect_clean_state
399 bisect_clean_state() {
400 # There may be some refs packed during bisection.
401 git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* refs/heads/bisect |
402 while read ref hash
404 git update-ref -d $ref $hash
405 done
406 rm -f "$GIT_DIR/BISECT_LOG"
407 rm -f "$GIT_DIR/BISECT_NAMES"
408 rm -f "$GIT_DIR/BISECT_RUN"
411 bisect_replay () {
412 test -r "$1" || die "cannot read $1 for replaying"
413 bisect_reset
414 while read bisect command rev
416 test "$bisect" = "git-bisect" || continue
417 case "$command" in
418 start)
419 cmd="bisect_start $rev"
420 eval "$cmd" ;;
421 good|bad|skip)
422 bisect_write "$command" "$rev" ;;
424 die "?? what are you talking about?" ;;
425 esac
426 done <"$1"
427 bisect_auto_next
430 bisect_run () {
431 bisect_next_check fail
433 while true
435 echo "running $@"
436 "$@"
437 res=$?
439 # Check for really bad run error.
440 if [ $res -lt 0 -o $res -ge 128 ]; then
441 echo >&2 "bisect run failed:"
442 echo >&2 "exit code $res from '$@' is < 0 or >= 128"
443 exit $res
446 # Find current state depending on run success or failure.
447 # A special exit code of 125 means cannot test.
448 if [ $res -eq 125 ]; then
449 state='skip'
450 elif [ $res -gt 0 ]; then
451 state='bad'
452 else
453 state='good'
456 # We have to use a subshell because "bisect_state" can exit.
457 ( bisect_state $state > "$GIT_DIR/BISECT_RUN" )
458 res=$?
460 cat "$GIT_DIR/BISECT_RUN"
462 if grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \
463 > /dev/null; then
464 echo >&2 "bisect run cannot continue any more"
465 exit $res
468 if [ $res -ne 0 ]; then
469 echo >&2 "bisect run failed:"
470 echo >&2 "'bisect_state $state' exited with error code $res"
471 exit $res
474 if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
475 echo "bisect run success"
476 exit 0;
479 done
483 case "$#" in
485 usage ;;
487 cmd="$1"
488 shift
489 case "$cmd" in
490 start)
491 bisect_start "$@" ;;
492 bad|good|skip)
493 bisect_state "$cmd" "$@" ;;
494 next)
495 # Not sure we want "next" at the UI level anymore.
496 bisect_next "$@" ;;
497 visualize|view)
498 bisect_visualize "$@" ;;
499 reset)
500 bisect_reset "$@" ;;
501 replay)
502 bisect_replay "$@" ;;
503 log)
504 cat "$GIT_DIR/BISECT_LOG" ;;
505 run)
506 bisect_run "$@" ;;
508 usage ;;
509 esac
510 esac