t7610-mergetool: use test_cmp instead of test $(cat file) = $txt
[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-{old,good}=<term> --term-{new,bad}=<term>]
7                  [--no-checkout] [<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 test -f "$GIT_DIR/BISECT_HEAD"
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 $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout)
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         while read git bisect command rev
213         do
214                 test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue
215                 if test "$git" = "git-bisect"
216                 then
217                         rev="$command"
218                         command="$bisect"
219                 fi
220                 get_terms
221                 git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit
222                 get_terms
223                 case "$command" in
224                 start)
225                         cmd="bisect_start $rev"
226                         eval "$cmd" ;;
227                 "$TERM_GOOD"|"$TERM_BAD"|skip)
228                         git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;;
229                 terms)
230                         git bisect--helper --bisect-terms $rev || exit;;
231                 *)
232                         die "$(gettext "?? what are you talking about?")" ;;
233                 esac
234         done <"$file"
235         bisect_auto_next
236 }
237
238 bisect_run () {
239         git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
240
241         test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"
242
243         while true
244         do
245                 command="$@"
246                 eval_gettextln "running \$command"
247                 "$@"
248                 res=$?
249
250                 # Check for really bad run error.
251                 if [ $res -lt 0 -o $res -ge 128 ]
252                 then
253                         eval_gettextln "bisect run failed:
254 exit code \$res from '\$command' is < 0 or >= 128" >&2
255                         exit $res
256                 fi
257
258                 # Find current state depending on run success or failure.
259                 # A special exit code of 125 means cannot test.
260                 if [ $res -eq 125 ]
261                 then
262                         state='skip'
263                 elif [ $res -gt 0 ]
264                 then
265                         state="$TERM_BAD"
266                 else
267                         state="$TERM_GOOD"
268                 fi
269
270                 # We have to use a subshell because "bisect_state" can exit.
271                 ( bisect_state $state >"$GIT_DIR/BISECT_RUN" )
272                 res=$?
273
274                 cat "$GIT_DIR/BISECT_RUN"
275
276                 if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
277                         >/dev/null
278                 then
279                         gettextln "bisect run cannot continue any more" >&2
280                         exit $res
281                 fi
282
283                 if [ $res -ne 0 ]
284                 then
285                         eval_gettextln "bisect run failed:
286 'bisect_state \$state' exited with error code \$res" >&2
287                         exit $res
288                 fi
289
290                 if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
291                 then
292                         gettextln "bisect run success"
293                         exit 0;
294                 fi
295
296         done
297 }
298
299 bisect_log () {
300         test -s "$GIT_DIR/BISECT_LOG" || die "$(gettext "We are not bisecting.")"
301         cat "$GIT_DIR/BISECT_LOG"
302 }
303
304 get_terms () {
305         if test -s "$GIT_DIR/BISECT_TERMS"
306         then
307                 {
308                 read TERM_BAD
309                 read TERM_GOOD
310                 } <"$GIT_DIR/BISECT_TERMS"
311         fi
312 }
313
314 case "$#" in
315 0)
316         usage ;;
317 *)
318         cmd="$1"
319         get_terms
320         shift
321         case "$cmd" in
322         help)
323                 git bisect -h ;;
324         start)
325                 bisect_start "$@" ;;
326         bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
327                 bisect_state "$cmd" "$@" ;;
328         skip)
329                 bisect_skip "$@" ;;
330         next)
331                 # Not sure we want "next" at the UI level anymore.
332                 bisect_next "$@" ;;
333         visualize|view)
334                 bisect_visualize "$@" ;;
335         reset)
336                 git bisect--helper --bisect-reset "$@" ;;
337         replay)
338                 bisect_replay "$@" ;;
339         log)
340                 bisect_log ;;
341         run)
342                 bisect_run "$@" ;;
343         terms)
344                 git bisect--helper --bisect-terms "$@" || exit;;
345         *)
346                 usage ;;
347         esac
348 esac