Merge branch 'jk/dont-count-existing-objects-twice'
[git] / git-bisect.sh
1 #!/bin/sh
2
3 USAGE='[help|start|bad|good|new|old|terms|skip|next|reset|visualize|view|replay|log|run]'
4 LONG_USAGE='git bisect help
5         print this long help message.
6 git bisect start [--term-{new,bad}=<term> --term-{old,good}=<term>]
7                  [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<pathspec>...]
8         reset bisect state and start bisection.
9 git bisect (bad|new) [<rev>]
10         mark <rev> a known-bad revision/
11                 a revision after change in a given property.
12 git bisect (good|old) [<rev>...]
13         mark <rev>... known-good revisions/
14                 revisions before change in a given property.
15 git bisect terms [--term-good | --term-bad]
16         show the terms used for old and new commits (default: bad, good)
17 git bisect skip [(<rev>|<range>)...]
18         mark <rev>... untestable revisions.
19 git bisect next
20         find next bisection to test and check it out.
21 git bisect reset [<commit>]
22         finish bisection search and go back to commit.
23 git bisect (visualize|view)
24         show bisect status in gitk.
25 git bisect replay <logfile>
26         replay bisection log.
27 git bisect log
28         show bisect log.
29 git bisect run <cmd>...
30         use <cmd>... to automatically bisect.
31
32 Please use "git help bisect" to get the full man page.'
33
34 OPTIONS_SPEC=
35 . git-sh-setup
36
37 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
38 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
39 TERM_BAD=bad
40 TERM_GOOD=good
41
42 bisect_head()
43 {
44         if git rev-parse --verify -q BISECT_HEAD > /dev/null
45         then
46                 echo BISECT_HEAD
47         else
48                 echo HEAD
49         fi
50 }
51
52 bisect_autostart() {
53         test -s "$GIT_DIR/BISECT_START" || {
54                 gettextln "You need to start by \"git bisect start\"" >&2
55                 if test -t 0
56                 then
57                         # TRANSLATORS: Make sure to include [Y] and [n] in your
58                         # translation. The program will only accept English input
59                         # at this point.
60                         gettext "Do you want me to do it for you [Y/n]? " >&2
61                         read yesno
62                         case "$yesno" in
63                         [Nn]*)
64                                 exit ;;
65                         esac
66                         bisect_start
67                 else
68                         exit 1
69                 fi
70         }
71 }
72
73 bisect_start() {
74         git bisect--helper --bisect-start $@ || exit
75
76         #
77         # Change state.
78         # In case of mistaken revs or checkout error, or signals received,
79         # "bisect_auto_next" below may exit or misbehave.
80         # We have to trap this to be able to clean up using
81         # "bisect_clean_state".
82         #
83         trap 'git bisect--helper --bisect-clean-state' 0
84         trap 'exit 255' 1 2 3 15
85
86         #
87         # Check if we can proceed to the next bisect state.
88         #
89         get_terms
90         bisect_auto_next
91
92         trap '-' 0
93 }
94
95 bisect_skip() {
96         all=''
97         for arg in "$@"
98         do
99                 case "$arg" in
100                 *..*)
101                         revs=$(git rev-list "$arg") || die "$(eval_gettext "Bad rev input: \$arg")" ;;
102                 *)
103                         revs=$(git rev-parse --sq-quote "$arg") ;;
104                 esac
105                 all="$all $revs"
106         done
107         eval bisect_state 'skip' $all
108 }
109
110 bisect_state() {
111         bisect_autostart
112         state=$1
113         git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
114         get_terms
115         case "$#,$state" in
116         0,*)
117                 die "Please call 'bisect_state' with at least one argument." ;;
118         1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip)
119                 bisected_head=$(bisect_head)
120                 rev=$(git rev-parse --verify "$bisected_head") ||
121                         die "$(eval_gettext "Bad rev input: \$bisected_head")"
122                 git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
123                 git bisect--helper --check-expected-revs "$rev" ;;
124         2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
125                 shift
126                 hash_list=''
127                 for rev in "$@"
128                 do
129                         sha=$(git rev-parse --verify "$rev^{commit}") ||
130                                 die "$(eval_gettext "Bad rev input: \$rev")"
131                         hash_list="$hash_list $sha"
132                 done
133                 for rev in $hash_list
134                 do
135                         git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
136                 done
137                 git bisect--helper --check-expected-revs $hash_list ;;
138         *,"$TERM_BAD")
139                 die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
140         *)
141                 usage ;;
142         esac
143         bisect_auto_next
144 }
145
146 bisect_auto_next() {
147         git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD && bisect_next || :
148 }
149
150 bisect_next() {
151         case "$#" in 0) ;; *) usage ;; esac
152         bisect_autostart
153         git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit
154
155         # Perform all bisection computation, display and checkout
156         git bisect--helper --next-all
157         res=$?
158
159         # Check if we should exit because bisection is finished
160         if test $res -eq 10
161         then
162                 bad_rev=$(git show-ref --hash --verify refs/bisect/$TERM_BAD)
163                 bad_commit=$(git show-branch $bad_rev)
164                 echo "# first $TERM_BAD commit: $bad_commit" >>"$GIT_DIR/BISECT_LOG"
165                 exit 0
166         elif test $res -eq 2
167         then
168                 echo "# only skipped commits left to test" >>"$GIT_DIR/BISECT_LOG"
169                 good_revs=$(git for-each-ref --format="%(objectname)" "refs/bisect/$TERM_GOOD-*")
170                 for skipped in $(git rev-list refs/bisect/$TERM_BAD --not $good_revs)
171                 do
172                         skipped_commit=$(git show-branch $skipped)
173                         echo "# possible first $TERM_BAD commit: $skipped_commit" >>"$GIT_DIR/BISECT_LOG"
174                 done
175                 exit $res
176         fi
177
178         # Check for an error in the bisection process
179         test $res -ne 0 && exit $res
180
181         return 0
182 }
183
184 bisect_visualize() {
185         git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
186
187         if test $# = 0
188         then
189                 if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
190                         type gitk >/dev/null 2>&1
191                 then
192                         set gitk
193                 else
194                         set git log
195                 fi
196         else
197                 case "$1" in
198                 git*|tig) ;;
199                 -*)     set git log "$@" ;;
200                 *)      set git "$@" ;;
201                 esac
202         fi
203
204         eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
205 }
206
207 bisect_replay () {
208         file="$1"
209         test "$#" -eq 1 || die "$(gettext "No logfile given")"
210         test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")"
211         git bisect--helper --bisect-reset || exit
212         oIFS="$IFS" IFS="$IFS$(printf '\015')"
213         while read git bisect command rev tail
214         do
215                 test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue
216                 if test "$git" = "git-bisect"
217                 then
218                         rev="$command"
219                         command="$bisect"
220                 fi
221                 get_terms
222                 git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit
223                 get_terms
224                 case "$command" in
225                 start)
226                         cmd="bisect_start $rev $tail"
227                         eval "$cmd" ;;
228                 "$TERM_GOOD"|"$TERM_BAD"|skip)
229                         git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;;
230                 terms)
231                         git bisect--helper --bisect-terms $rev || exit;;
232                 *)
233                         die "$(gettext "?? what are you talking about?")" ;;
234                 esac
235         done <"$file"
236         IFS="$oIFS"
237         bisect_auto_next
238 }
239
240 bisect_run () {
241         git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
242
243         test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"
244
245         while true
246         do
247                 command="$@"
248                 eval_gettextln "running \$command"
249                 "$@"
250                 res=$?
251
252                 # Check for really bad run error.
253                 if [ $res -lt 0 -o $res -ge 128 ]
254                 then
255                         eval_gettextln "bisect run failed:
256 exit code \$res from '\$command' is < 0 or >= 128" >&2
257                         exit $res
258                 fi
259
260                 # Find current state depending on run success or failure.
261                 # A special exit code of 125 means cannot test.
262                 if [ $res -eq 125 ]
263                 then
264                         state='skip'
265                 elif [ $res -gt 0 ]
266                 then
267                         state="$TERM_BAD"
268                 else
269                         state="$TERM_GOOD"
270                 fi
271
272                 # We have to use a subshell because "bisect_state" can exit.
273                 ( bisect_state $state >"$GIT_DIR/BISECT_RUN" )
274                 res=$?
275
276                 cat "$GIT_DIR/BISECT_RUN"
277
278                 if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
279                         >/dev/null
280                 then
281                         gettextln "bisect run cannot continue any more" >&2
282                         exit $res
283                 fi
284
285                 if [ $res -ne 0 ]
286                 then
287                         eval_gettextln "bisect run failed:
288 'bisect_state \$state' exited with error code \$res" >&2
289                         exit $res
290                 fi
291
292                 if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
293                 then
294                         gettextln "bisect run success"
295                         exit 0;
296                 fi
297
298         done
299 }
300
301 bisect_log () {
302         test -s "$GIT_DIR/BISECT_LOG" || die "$(gettext "We are not bisecting.")"
303         cat "$GIT_DIR/BISECT_LOG"
304 }
305
306 get_terms () {
307         if test -s "$GIT_DIR/BISECT_TERMS"
308         then
309                 {
310                 read TERM_BAD
311                 read TERM_GOOD
312                 } <"$GIT_DIR/BISECT_TERMS"
313         fi
314 }
315
316 case "$#" in
317 0)
318         usage ;;
319 *)
320         cmd="$1"
321         get_terms
322         shift
323         case "$cmd" in
324         help)
325                 git bisect -h ;;
326         start)
327                 bisect_start "$@" ;;
328         bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
329                 bisect_state "$cmd" "$@" ;;
330         skip)
331                 bisect_skip "$@" ;;
332         next)
333                 # Not sure we want "next" at the UI level anymore.
334                 bisect_next "$@" ;;
335         visualize|view)
336                 bisect_visualize "$@" ;;
337         reset)
338                 git bisect--helper --bisect-reset "$@" ;;
339         replay)
340                 bisect_replay "$@" ;;
341         log)
342                 bisect_log ;;
343         run)
344                 bisect_run "$@" ;;
345         terms)
346                 git bisect--helper --bisect-terms "$@" || exit;;
347         *)
348                 usage ;;
349         esac
350 esac