Code

Merge branch 'maint'
[git.git] / git-bisect.sh
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 " '\''$_'\''";
41                 }
42                 print "\n";
43         ' "$@"
44 }
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
60                 fi
61         }
62 }
64 bisect_start() {
65         #
66         # Verify HEAD. If we were bisecting before this, reset to the
67         # top-of-line master first!
68         #
69         head=$(GIT_DIR="$GIT_DIR" git symbolic-ref HEAD) ||
70         head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
71         die "Bad HEAD - I need a HEAD"
72         case "$head" in
73         refs/heads/bisect)
74                 if [ -s "$GIT_DIR/BISECT_START" ]; then
75                     branch=`cat "$GIT_DIR/BISECT_START"`
76                 else
77                     branch=master
78                 fi
79                 git checkout $branch || exit
80                 ;;
81         refs/heads/*|$_x40)
82                 # This error message should only be triggered by cogito usage,
83                 # and cogito users should understand it relates to cg-seek.
84                 [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
85                 echo "${head#refs/heads/}" >"$GIT_DIR/BISECT_START"
86                 ;;
87         *)
88                 die "Bad HEAD - strange symbolic ref"
89                 ;;
90         esac
92         #
93         # Get rid of any old bisect state
94         #
95         bisect_clean_state
97         #
98         # Check for one bad and then some good revisions.
99         #
100         has_double_dash=0
101         for arg; do
102             case "$arg" in --) has_double_dash=1; break ;; esac
103         done
104         orig_args=$(sq "$@")
105         bad_seen=0
106         while [ $# -gt 0 ]; do
107             arg="$1"
108             case "$arg" in
109             --)
110                 shift
111                 break
112                 ;;
113             *)
114                 rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || {
115                     test $has_double_dash -eq 1 &&
116                         die "'$arg' does not appear to be a valid revision"
117                     break
118                 }
119                 case $bad_seen in
120                 0) state='bad' ; bad_seen=1 ;;
121                 *) state='good' ;;
122                 esac
123                 bisect_write "$state" "$rev" 'nolog'
124                 shift
125                 ;;
126             esac
127         done
129         sq "$@" >"$GIT_DIR/BISECT_NAMES"
130         echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG"
131         bisect_auto_next
134 bisect_write() {
135         state="$1"
136         rev="$2"
137         nolog="$3"
138         case "$state" in
139                 bad)            tag="$state" ;;
140                 good|skip)      tag="$state"-"$rev" ;;
141                 *)              die "Bad bisect_write argument: $state" ;;
142         esac
143         git update-ref "refs/bisect/$tag" "$rev"
144         echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG"
145         test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
148 bisect_state() {
149         bisect_autostart
150         state=$1
151         case "$#,$state" in
152         0,*)
153                 die "Please call 'bisect_state' with at least one argument." ;;
154         1,bad|1,good|1,skip)
155                 rev=$(git rev-parse --verify HEAD) ||
156                         die "Bad rev input: HEAD"
157                 bisect_write "$state" "$rev" ;;
158         2,bad|*,good|*,skip)
159                 shift
160                 for rev in "$@"
161                 do
162                         rev=$(git rev-parse --verify "$rev^{commit}") ||
163                                 die "Bad rev input: $rev"
164                         bisect_write "$state" "$rev"
165                 done ;;
166         *,bad)
167                 die "'git bisect bad' can take only one argument." ;;
168         *)
169                 usage ;;
170         esac
171         bisect_auto_next
174 bisect_next_check() {
175         missing_good= missing_bad=
176         git show-ref -q --verify refs/bisect/bad || missing_bad=t
177         test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t
179         case "$missing_good,$missing_bad,$1" in
180         ,,*)
181                 : have both good and bad - ok
182                 ;;
183         *,)
184                 # do not have both but not asked to fail - just report.
185                 false
186                 ;;
187         t,,good)
188                 # have bad but not good.  we could bisect although
189                 # this is less optimum.
190                 echo >&2 'Warning: bisecting only with a bad commit.'
191                 if test -t 0
192                 then
193                         printf >&2 'Are you sure [Y/n]? '
194                         case "$(read yesno)" in [Nn]*) exit 1 ;; esac
195                 fi
196                 : bisect without good...
197                 ;;
198         *)
199                 THEN=''
200                 test -f "$GIT_DIR/BISECT_NAMES" || {
201                         echo >&2 'You need to start by "git bisect start".'
202                         THEN='then '
203                 }
204                 echo >&2 'You '$THEN'need to give me at least one good' \
205                         'and one bad revisions.'
206                 echo >&2 '(You can use "git bisect bad" and' \
207                         '"git bisect good" for that.)'
208                 exit 1 ;;
209         esac
212 bisect_auto_next() {
213         bisect_next_check && bisect_next || :
216 filter_skipped() {
217         _eval="$1"
218         _skip="$2"
220         if [ -z "$_skip" ]; then
221                 eval $_eval
222                 return
223         fi
225         # Let's parse the output of:
226         # "git rev-list --bisect-vars --bisect-all ..."
227         eval $_eval | while read hash line
228         do
229                 case "$VARS,$FOUND,$TRIED,$hash" in
230                         # We display some vars.
231                         1,*,*,*) echo "$hash $line" ;;
233                         # Split line.
234                         ,*,*,---*) ;;
236                         # We had nothing to search.
237                         ,,,bisect_rev*)
238                                 echo "bisect_rev="
239                                 VARS=1
240                                 ;;
242                         # We did not find a good bisect rev.
243                         # This should happen only if the "bad"
244                         # commit is also a "skip" commit.
245                         ,,*,bisect_rev*)
246                                 echo "bisect_rev=$TRIED"
247                                 VARS=1
248                                 ;;
250                         # We are searching.
251                         ,,*,*)
252                                 TRIED="${TRIED:+$TRIED|}$hash"
253                                 case "$_skip" in
254                                 *$hash*) ;;
255                                 *)
256                                         echo "bisect_rev=$hash"
257                                         echo "bisect_tried=\"$TRIED\""
258                                         FOUND=1
259                                         ;;
260                                 esac
261                                 ;;
263                         # We have already found a rev to be tested.
264                         ,1,*,bisect_rev*) VARS=1 ;;
265                         ,1,*,*) ;;
267                         # ???
268                         *) die "filter_skipped error " \
269                             "VARS: '$VARS' " \
270                             "FOUND: '$FOUND' " \
271                             "TRIED: '$TRIED' " \
272                             "hash: '$hash' " \
273                             "line: '$line'"
274                         ;;
275                 esac
276         done
279 exit_if_skipped_commits () {
280         _tried=$1
281         if expr "$_tried" : ".*[|].*" > /dev/null ; then
282                 echo "There are only 'skip'ped commit left to test."
283                 echo "The first bad commit could be any of:"
284                 echo "$_tried" | tr '[|]' '[\012]'
285                 echo "We cannot bisect more!"
286                 exit 2
287         fi
290 bisect_next() {
291         case "$#" in 0) ;; *) usage ;; esac
292         bisect_autostart
293         bisect_next_check good
295         skip=$(git for-each-ref --format='%(objectname)' \
296                 "refs/bisect/skip-*" | tr '\012' ' ') || exit
298         BISECT_OPT=''
299         test -n "$skip" && BISECT_OPT='--bisect-all'
301         bad=$(git rev-parse --verify refs/bisect/bad) &&
302         good=$(git for-each-ref --format='^%(objectname)' \
303                 "refs/bisect/good-*" | tr '\012' ' ') &&
304         eval="git rev-list --bisect-vars $BISECT_OPT $good $bad --" &&
305         eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" &&
306         eval=$(filter_skipped "$eval" "$skip") &&
307         eval "$eval" || exit
309         if [ -z "$bisect_rev" ]; then
310                 echo "$bad was both good and bad"
311                 exit 1
312         fi
313         if [ "$bisect_rev" = "$bad" ]; then
314                 exit_if_skipped_commits "$bisect_tried"
315                 echo "$bisect_rev is first bad commit"
316                 git diff-tree --pretty $bisect_rev
317                 exit 0
318         fi
320         # We should exit here only if the "bad"
321         # commit is also a "skip" commit (see above).
322         exit_if_skipped_commits "$bisect_rev"
324         echo "Bisecting: $bisect_nr revisions left to test after this"
325         git branch -f new-bisect "$bisect_rev"
326         git checkout -q new-bisect || exit
327         git branch -M new-bisect bisect
328         git show-branch "$bisect_rev"
331 bisect_visualize() {
332         bisect_next_check fail
334         if test $# = 0
335         then
336                 case "${DISPLAY+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" in
337                 '')     set git log ;;
338                 set*)   set gitk ;;
339                 esac
340         else
341                 case "$1" in
342                 git*|tig) ;;
343                 -*)     set git log "$@" ;;
344                 *)      set git "$@" ;;
345                 esac
346         fi
348         not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*")
349         eval '"$@"' refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
352 bisect_reset() {
353         test -f "$GIT_DIR/BISECT_NAMES" || {
354                 echo "We are not bisecting."
355                 return
356         }
357         case "$#" in
358         0) if [ -s "$GIT_DIR/BISECT_START" ]; then
359                branch=`cat "$GIT_DIR/BISECT_START"`
360            else
361                branch=master
362            fi ;;
363         1) git show-ref --verify --quiet -- "refs/heads/$1" ||
364                die "$1 does not seem to be a valid branch"
365            branch="$1" ;;
366         *)
367             usage ;;
368         esac
369         if git checkout "$branch"; then
370                 # Cleanup head-name if it got left by an old version of git-bisect
371                 rm -f "$GIT_DIR/head-name"
372                 rm -f "$GIT_DIR/BISECT_START"
373                 bisect_clean_state
374         fi
377 bisect_clean_state() {
378         # There may be some refs packed during bisection.
379         git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* refs/heads/bisect |
380         while read ref hash
381         do
382                 git update-ref -d $ref $hash
383         done
384         rm -f "$GIT_DIR/BISECT_LOG"
385         rm -f "$GIT_DIR/BISECT_NAMES"
386         rm -f "$GIT_DIR/BISECT_RUN"
389 bisect_replay () {
390         test -r "$1" || die "cannot read $1 for replaying"
391         bisect_reset
392         while read bisect command rev
393         do
394                 test "$bisect" = "git-bisect" || continue
395                 case "$command" in
396                 start)
397                         cmd="bisect_start $rev"
398                         eval "$cmd" ;;
399                 good|bad|skip)
400                         bisect_write "$command" "$rev" ;;
401                 *)
402                         die "?? what are you talking about?" ;;
403                 esac
404         done <"$1"
405         bisect_auto_next
408 bisect_run () {
409     bisect_next_check fail
411     while true
412     do
413       echo "running $@"
414       "$@"
415       res=$?
417       # Check for really bad run error.
418       if [ $res -lt 0 -o $res -ge 128 ]; then
419           echo >&2 "bisect run failed:"
420           echo >&2 "exit code $res from '$@' is < 0 or >= 128"
421           exit $res
422       fi
424       # Find current state depending on run success or failure.
425       # A special exit code of 125 means cannot test.
426       if [ $res -eq 125 ]; then
427           state='skip'
428       elif [ $res -gt 0 ]; then
429           state='bad'
430       else
431           state='good'
432       fi
434       # We have to use a subshell because "bisect_state" can exit.
435       ( bisect_state $state > "$GIT_DIR/BISECT_RUN" )
436       res=$?
438       cat "$GIT_DIR/BISECT_RUN"
440       if grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \
441                 > /dev/null; then
442           echo >&2 "bisect run cannot continue any more"
443           exit $res
444       fi
446       if [ $res -ne 0 ]; then
447           echo >&2 "bisect run failed:"
448           echo >&2 "'bisect_state $state' exited with error code $res"
449           exit $res
450       fi
452       if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
453           echo "bisect run success"
454           exit 0;
455       fi
457     done
461 case "$#" in
462 0)
463     usage ;;
464 *)
465     cmd="$1"
466     shift
467     case "$cmd" in
468     help)
469         git bisect -h ;;
470     start)
471         bisect_start "$@" ;;
472     bad|good|skip)
473         bisect_state "$cmd" "$@" ;;
474     next)
475         # Not sure we want "next" at the UI level anymore.
476         bisect_next "$@" ;;
477     visualize|view)
478         bisect_visualize "$@" ;;
479     reset)
480         bisect_reset "$@" ;;
481     replay)
482         bisect_replay "$@" ;;
483     log)
484         cat "$GIT_DIR/BISECT_LOG" ;;
485     run)
486         bisect_run "$@" ;;
487     *)
488         usage ;;
489     esac
490 esac