Code

54bfd71dad0c2f587de68742945ca52909df0c95
[git.git] / git-bisect.sh
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 " '\''$_'\''";
37                 }
38                 print "\n";
39         ' "$@"
40 }
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
56                 fi
57         }
58 }
60 bisect_start() {
61         #
62         # Verify HEAD. If we were bisecting before this, reset to the
63         # top-of-line master first!
64         #
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"
68         #
69         # Check that we either already have BISECT_START, or that the
70         # branches bisect, new-bisect don't exist, to not override them.
71         #
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.'
76                 fi
77         start_head=''
78         case "$head" in
79         refs/heads/bisect)
80                 branch=`cat "$GIT_DIR/BISECT_START"`
81                 git checkout $branch || exit
82                 ;;
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/}"
88                 ;;
89         *)
90                 die "Bad HEAD - strange symbolic ref"
91                 ;;
92         esac
94         #
95         # Get rid of any old bisect state
96         #
97         bisect_clean_state
99         #
100         # Check for one bad and then some good revisions.
101         #
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
112             --)
113                 shift
114                 break
115                 ;;
116             *)
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
121                 }
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
128                 ;;
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 "$@"
167                 do
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." ;;
175         *)
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
189                 ;;
190         *,)
191                 # do not have both but not asked to fail - just report.
192                 false
193                 ;;
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
202                 fi
203                 : bisect without good...
204                 ;;
205         *)
206                 THEN=''
207                 test -f "$GIT_DIR/BISECT_NAMES" || {
208                         echo >&2 'You need to start by "git bisect start".'
209                         THEN='then '
210                 }
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 filter_skipped() {
224         _eval="$1"
225         _skip="$2"
227         if [ -z "$_skip" ]; then
228                 eval $_eval
229                 return
230         fi
232         # Let's parse the output of:
233         # "git rev-list --bisect-vars --bisect-all ..."
234         eval $_eval | while read hash line
235         do
236                 case "$VARS,$FOUND,$TRIED,$hash" in
237                         # We display some vars.
238                         1,*,*,*) echo "$hash $line" ;;
240                         # Split line.
241                         ,*,*,---*) ;;
243                         # We had nothing to search.
244                         ,,,bisect_rev*)
245                                 echo "bisect_rev="
246                                 VARS=1
247                                 ;;
249                         # We did not find a good bisect rev.
250                         # This should happen only if the "bad"
251                         # commit is also a "skip" commit.
252                         ,,*,bisect_rev*)
253                                 echo "bisect_rev=$TRIED"
254                                 VARS=1
255                                 ;;
257                         # We are searching.
258                         ,,*,*)
259                                 TRIED="${TRIED:+$TRIED|}$hash"
260                                 case "$_skip" in
261                                 *$hash*) ;;
262                                 *)
263                                         echo "bisect_rev=$hash"
264                                         echo "bisect_tried=\"$TRIED\""
265                                         FOUND=1
266                                         ;;
267                                 esac
268                                 ;;
270                         # We have already found a rev to be tested.
271                         ,1,*,bisect_rev*) VARS=1 ;;
272                         ,1,*,*) ;;
274                         # ???
275                         *) die "filter_skipped error " \
276                             "VARS: '$VARS' " \
277                             "FOUND: '$FOUND' " \
278                             "TRIED: '$TRIED' " \
279                             "hash: '$hash' " \
280                             "line: '$line'"
281                         ;;
282                 esac
283         done
286 exit_if_skipped_commits () {
287         _tried=$1
288         if expr "$_tried" : ".*[|].*" > /dev/null ; then
289                 echo "There are only 'skip'ped commit left to test."
290                 echo "The first bad commit could be any of:"
291                 echo "$_tried" | tr '[|]' '[\012]'
292                 echo "We cannot bisect more!"
293                 exit 2
294         fi
297 bisect_next() {
298         case "$#" in 0) ;; *) usage ;; esac
299         bisect_autostart
300         bisect_next_check good
302         skip=$(git for-each-ref --format='%(objectname)' \
303                 "refs/bisect/skip-*" | tr '\012' ' ') || exit
305         BISECT_OPT=''
306         test -n "$skip" && BISECT_OPT='--bisect-all'
308         bad=$(git rev-parse --verify refs/bisect/bad) &&
309         good=$(git for-each-ref --format='^%(objectname)' \
310                 "refs/bisect/good-*" | tr '\012' ' ') &&
311         eval="git rev-list --bisect-vars $BISECT_OPT $good $bad --" &&
312         eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" &&
313         eval=$(filter_skipped "$eval" "$skip") &&
314         eval "$eval" || exit
316         if [ -z "$bisect_rev" ]; then
317                 echo "$bad was both good and bad"
318                 exit 1
319         fi
320         if [ "$bisect_rev" = "$bad" ]; then
321                 exit_if_skipped_commits "$bisect_tried"
322                 echo "$bisect_rev is first bad commit"
323                 git diff-tree --pretty $bisect_rev
324                 exit 0
325         fi
327         # We should exit here only if the "bad"
328         # commit is also a "skip" commit (see above).
329         exit_if_skipped_commits "$bisect_rev"
331         echo "Bisecting: $bisect_nr revisions left to test after this"
332         git branch -D new-bisect 2> /dev/null
333         git checkout -q -b new-bisect "$bisect_rev" || exit
334         git branch -M new-bisect bisect
335         git show-branch "$bisect_rev"
338 bisect_visualize() {
339         bisect_next_check fail
341         if test $# = 0
342         then
343                 case "${DISPLAY+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" in
344                 '')     set git log ;;
345                 set*)   set gitk ;;
346                 esac
347         else
348                 case "$1" in
349                 git*|tig) ;;
350                 -*)     set git log "$@" ;;
351                 *)      set git "$@" ;;
352                 esac
353         fi
355         not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*")
356         eval '"$@"' refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
359 bisect_reset() {
360         test -f "$GIT_DIR/BISECT_NAMES" || {
361                 echo "We are not bisecting."
362                 return
363         }
364         case "$#" in
365         0) if [ -s "$GIT_DIR/BISECT_START" ]; then
366                branch=`cat "$GIT_DIR/BISECT_START"`
367            else
368                branch=master
369            fi ;;
370         1) git show-ref --verify --quiet -- "refs/heads/$1" ||
371                die "$1 does not seem to be a valid branch"
372            branch="$1" ;;
373         *)
374             usage ;;
375         esac
376         if git checkout "$branch"; then
377                 # Cleanup head-name if it got left by an old version of git-bisect
378                 rm -f "$GIT_DIR/head-name"
379                 rm -f "$GIT_DIR/BISECT_START"
380                 bisect_clean_state
381         fi
384 bisect_clean_state() {
385         # There may be some refs packed during bisection.
386         git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* refs/heads/bisect |
387         while read ref hash
388         do
389                 git update-ref -d $ref $hash
390         done
391         rm -f "$GIT_DIR/BISECT_LOG"
392         rm -f "$GIT_DIR/BISECT_NAMES"
393         rm -f "$GIT_DIR/BISECT_RUN"
396 bisect_replay () {
397         test -r "$1" || die "cannot read $1 for replaying"
398         bisect_reset
399         while read bisect command rev
400         do
401                 test "$bisect" = "git-bisect" || continue
402                 case "$command" in
403                 start)
404                         cmd="bisect_start $rev"
405                         eval "$cmd" ;;
406                 good|bad|skip)
407                         bisect_write "$command" "$rev" ;;
408                 *)
409                         die "?? what are you talking about?" ;;
410                 esac
411         done <"$1"
412         bisect_auto_next
415 bisect_run () {
416     bisect_next_check fail
418     while true
419     do
420       echo "running $@"
421       "$@"
422       res=$?
424       # Check for really bad run error.
425       if [ $res -lt 0 -o $res -ge 128 ]; then
426           echo >&2 "bisect run failed:"
427           echo >&2 "exit code $res from '$@' is < 0 or >= 128"
428           exit $res
429       fi
431       # Find current state depending on run success or failure.
432       # A special exit code of 125 means cannot test.
433       if [ $res -eq 125 ]; then
434           state='skip'
435       elif [ $res -gt 0 ]; then
436           state='bad'
437       else
438           state='good'
439       fi
441       # We have to use a subshell because "bisect_state" can exit.
442       ( bisect_state $state > "$GIT_DIR/BISECT_RUN" )
443       res=$?
445       cat "$GIT_DIR/BISECT_RUN"
447       if grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \
448                 > /dev/null; then
449           echo >&2 "bisect run cannot continue any more"
450           exit $res
451       fi
453       if [ $res -ne 0 ]; then
454           echo >&2 "bisect run failed:"
455           echo >&2 "'bisect_state $state' exited with error code $res"
456           exit $res
457       fi
459       if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
460           echo "bisect run success"
461           exit 0;
462       fi
464     done
468 case "$#" in
469 0)
470     usage ;;
471 *)
472     cmd="$1"
473     shift
474     case "$cmd" in
475     start)
476         bisect_start "$@" ;;
477     bad|good|skip)
478         bisect_state "$cmd" "$@" ;;
479     next)
480         # Not sure we want "next" at the UI level anymore.
481         bisect_next "$@" ;;
482     visualize|view)
483         bisect_visualize "$@" ;;
484     reset)
485         bisect_reset "$@" ;;
486     replay)
487         bisect_replay "$@" ;;
488     log)
489         cat "$GIT_DIR/BISECT_LOG" ;;
490     run)
491         bisect_run "$@" ;;
492     *)
493         usage ;;
494     esac
495 esac