i18n: git-bisect [Y/n] messages
[git/git.git] / git-bisect.sh
CommitLineData
8cc6a083 1#!/bin/sh
d025524d 2
243a60fb
CC
3USAGE='[help|start|bad|good|skip|next|reset|visualize|replay|log|run]'
4LONG_USAGE='git bisect help
5 print this long help message.
6git bisect start [<bad> [<good>...]] [--] [<pathspec>...]
38a47fd6
CC
7 reset bisect state and start bisection.
8git bisect bad [<rev>]
9 mark <rev> a known-bad revision.
10git bisect good [<rev>...]
11 mark <rev>... known-good revisions.
5413812f 12git bisect skip [(<rev>|<range>)...]
6ca8b977 13 mark <rev>... untestable revisions.
38a47fd6
CC
14git bisect next
15 find next bisection to test and check it out.
6b87ce23
AK
16git bisect reset [<commit>]
17 finish bisection search and go back to commit.
38a47fd6
CC
18git bisect visualize
19 show bisect status in gitk.
20git bisect replay <logfile>
21 replay bisection log.
22git bisect log
23 show bisect log.
24git bisect run <cmd>...
243a60fb
CC
25 use <cmd>... to automatically bisect.
26
27Please use "git help bisect" to get the full man page.'
d025524d 28
8f321a39 29OPTIONS_SPEC=
ae2b0f15 30. git-sh-setup
dcf9c2e5 31. git-sh-i18n
4c550686 32require_work_tree
8cc6a083 33
ce32660e
JS
34_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
35_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
36
8cc6a083 37bisect_autostart() {
823ea121 38 test -s "$GIT_DIR/BISECT_START" || {
ddd7a7c2
ÆAB
39 (
40 gettext "You need to start by \"git bisect start\"" &&
41 echo
42 ) >&2
8cc6a083
LT
43 if test -t 0
44 then
04de0996
ÆAB
45 # TRANSLATORS: Make sure to include [Y] and [n] in your
46 # translation. The program will only accept English input
47 # at this point.
48 gettext "Do you want me to do it for you [Y/n]? " >&2
8cc6a083
LT
49 read yesno
50 case "$yesno" in
51 [Nn]*)
52 exit ;;
53 esac
54 bisect_start
55 else
56 exit 1
57 fi
58 }
59}
60
61bisect_start() {
8cc6a083 62 #
634f2464 63 # Verify HEAD.
8cc6a083 64 #
48949a18 65 head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
ce32660e 66 head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
9570fc1e 67 die "$(gettext "Bad HEAD - I need a HEAD")"
634f2464 68
ee831f7d 69 #
634f2464 70 # Check if we are bisecting.
ee831f7d 71 #
d3e54c88 72 start_head=''
634f2464
CC
73 if test -s "$GIT_DIR/BISECT_START"
74 then
75 # Reset to the rev from where we started.
9d0cd91c 76 start_head=$(cat "$GIT_DIR/BISECT_START")
f3a186ff 77 git checkout "$start_head" -- || exit
634f2464
CC
78 else
79 # Get rev from where we start.
80 case "$head" in
81 refs/heads/*|$_x40)
82 # This error message should only be triggered by
83 # cogito usage, and cogito users should understand
84 # it relates to cg-seek.
85 [ -s "$GIT_DIR/head-name" ] &&
9570fc1e 86 die "$(gettext "won't bisect on seeked tree")"
634f2464
CC
87 start_head="${head#refs/heads/}"
88 ;;
89 *)
9570fc1e 90 die "$(gettext "Bad HEAD - strange symbolic ref")"
634f2464
CC
91 ;;
92 esac
93 fi
8cc6a083
LT
94
95 #
634f2464 96 # Get rid of any old bisect state.
8cc6a083 97 #
823ea121 98 bisect_clean_state || exit
38a47fd6
CC
99
100 #
101 # Check for one bad and then some good revisions.
102 #
103 has_double_dash=0
104 for arg; do
105 case "$arg" in --) has_double_dash=1; break ;; esac
106 done
de52f5a8 107 orig_args=$(git rev-parse --sq-quote "$@")
38a47fd6 108 bad_seen=0
d3e54c88 109 eval=''
38a47fd6
CC
110 while [ $# -gt 0 ]; do
111 arg="$1"
112 case "$arg" in
113 --)
8fe26f44 114 shift
38a47fd6
CC
115 break
116 ;;
117 *)
634f2464 118 rev=$(git rev-parse -q --verify "$arg^{commit}") || {
38a47fd6 119 test $has_double_dash -eq 1 &&
15eaa049 120 die "$(eval_gettext "'\$arg' does not appear to be a valid revision")"
38a47fd6
CC
121 break
122 }
737c74ee
CC
123 case $bad_seen in
124 0) state='bad' ; bad_seen=1 ;;
125 *) state='good' ;;
126 esac
d3e54c88 127 eval="$eval bisect_write '$state' '$rev' 'nolog'; "
8fe26f44 128 shift
38a47fd6
CC
129 ;;
130 esac
8fe26f44 131 done
38a47fd6 132
ba963de8
CC
133 #
134 # Change state.
135 # In case of mistaken revs or checkout error, or signals received,
136 # "bisect_auto_next" below may exit or misbehave.
137 # We have to trap this to be able to clean up using
138 # "bisect_clean_state".
139 #
140 trap 'bisect_clean_state' 0
141 trap 'exit 255' 1 2 3 15
142
143 #
144 # Write new start state.
145 #
ba963de8 146 echo "$start_head" >"$GIT_DIR/BISECT_START" &&
de52f5a8 147 git rev-parse --sq-quote "$@" >"$GIT_DIR/BISECT_NAMES" &&
ba963de8 148 eval "$eval" &&
6c98c054 149 echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit
ba963de8
CC
150 #
151 # Check if we can proceed to the next bisect state.
152 #
38a47fd6 153 bisect_auto_next
ba963de8
CC
154
155 trap '-' 0
8cc6a083
LT
156}
157
55624f9a
CC
158bisect_write() {
159 state="$1"
160 rev="$2"
737c74ee 161 nolog="$3"
55624f9a
CC
162 case "$state" in
163 bad) tag="$state" ;;
164 good|skip) tag="$state"-"$rev" ;;
15eaa049 165 *) die "$(eval_gettext "Bad bisect_write argument: \$state")" ;;
55624f9a 166 esac
ba963de8 167 git update-ref "refs/bisect/$tag" "$rev" || exit
f454cdc4 168 echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG"
6c98c054 169 test -n "$nolog" || echo "git bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
55624f9a
CC
170}
171
c9c4e2d5
CC
172is_expected_rev() {
173 test -f "$GIT_DIR/BISECT_EXPECTED_REV" &&
174 test "$1" = $(cat "$GIT_DIR/BISECT_EXPECTED_REV")
175}
176
c9c4e2d5
CC
177check_expected_revs() {
178 for _rev in "$@"; do
179 if ! is_expected_rev "$_rev"; then
180 rm -f "$GIT_DIR/BISECT_ANCESTORS_OK"
181 rm -f "$GIT_DIR/BISECT_EXPECTED_REV"
182 return
183 fi
184 done
185}
186
ee2314f5
CC
187bisect_skip() {
188 all=''
189 for arg in "$@"
190 do
191 case "$arg" in
192 *..*)
15eaa049 193 revs=$(git rev-list "$arg") || die "$(eval_gettext "Bad rev input: \$arg")" ;;
ee2314f5 194 *)
de52f5a8 195 revs=$(git rev-parse --sq-quote "$arg") ;;
ee2314f5
CC
196 esac
197 all="$all $revs"
198 done
1a66a489 199 eval bisect_state 'skip' $all
ee2314f5
CC
200}
201
155fc795 202bisect_state() {
8cc6a083 203 bisect_autostart
155fc795
CC
204 state=$1
205 case "$#,$state" in
206 0,*)
9570fc1e 207 die "$(gettext "Please call 'bisect_state' with at least one argument.")" ;;
155fc795
CC
208 1,bad|1,good|1,skip)
209 rev=$(git rev-parse --verify HEAD) ||
9570fc1e 210 die "$(gettext "Bad rev input: HEAD")"
c9c4e2d5
CC
211 bisect_write "$state" "$rev"
212 check_expected_revs "$rev" ;;
e3389075 213 2,bad|*,good|*,skip)
155fc795 214 shift
d3e54c88 215 eval=''
e3389075 216 for rev in "$@"
155fc795 217 do
a179a303 218 sha=$(git rev-parse --verify "$rev^{commit}") ||
15eaa049 219 die "$(eval_gettext "Bad rev input: \$rev")"
d3e54c88
CC
220 eval="$eval bisect_write '$state' '$sha'; "
221 done
c9c4e2d5
CC
222 eval "$eval"
223 check_expected_revs "$@" ;;
e3389075 224 *,bad)
9570fc1e 225 die "$(gettext "'git bisect bad' can take only one argument.")" ;;
cc9f24d0
JH
226 *)
227 usage ;;
8cc6a083 228 esac
97e1c51e
CC
229 bisect_auto_next
230}
231
8cc6a083 232bisect_next_check() {
0a5280a9
JH
233 missing_good= missing_bad=
234 git show-ref -q --verify refs/bisect/bad || missing_bad=t
235 test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t
6fecf191 236
0a5280a9
JH
237 case "$missing_good,$missing_bad,$1" in
238 ,,*)
239 : have both good and bad - ok
240 ;;
241 *,)
242 # do not have both but not asked to fail - just report.
243 false
244 ;;
245 t,,good)
246 # have bad but not good. we could bisect although
247 # this is less optimum.
ddd7a7c2
ÆAB
248 (
249 gettext "Warning: bisecting only with a bad commit." &&
250 echo
251 ) >&2
0a5280a9
JH
252 if test -t 0
253 then
04de0996
ÆAB
254 # TRANSLATORS: Make sure to include [Y] and [n] in your
255 # translation. The program will only accept English input
256 # at this point.
257 gettext "Are you sure [Y/n]? " >&2
e5d3afd7
FM
258 read yesno
259 case "$yesno" in [Nn]*) exit 1 ;; esac
0a5280a9
JH
260 fi
261 : bisect without good...
262 ;;
8cc6a083 263 *)
0a5280a9 264 THEN=''
823ea121 265 test -s "$GIT_DIR/BISECT_START" || {
0a5280a9
JH
266 echo >&2 'You need to start by "git bisect start".'
267 THEN='then '
268 }
269 echo >&2 'You '$THEN'need to give me at least one good' \
270 'and one bad revisions.'
271 echo >&2 '(You can use "git bisect bad" and' \
272 '"git bisect good" for that.)'
273 exit 1 ;;
8cc6a083
LT
274 esac
275}
276
277bisect_auto_next() {
434d036f 278 bisect_next_check && bisect_next || :
8cc6a083
LT
279}
280
281bisect_next() {
8fe26f44 282 case "$#" in 0) ;; *) usage ;; esac
8cc6a083 283 bisect_autostart
0a5280a9
JH
284 bisect_next_check good
285
0871984d
CC
286 # Perform all bisection computation, display and checkout
287 git bisect--helper --next-all
5a1d31c7 288 res=$?
0a5280a9 289
5a1d31c7
CC
290 # Check if we should exit because bisection is finished
291 test $res -eq 10 && exit 0
0a5280a9 292
5a1d31c7
CC
293 # Check for an error in the bisection process
294 test $res -ne 0 && exit $res
295
296 return 0
8cc6a083
LT
297}
298
cc9f24d0
JH
299bisect_visualize() {
300 bisect_next_check fail
235997c9
JH
301
302 if test $# = 0
303 then
c4e4644e
JK
304 if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
305 type gitk >/dev/null 2>&1; then
306 set gitk
307 else
308 set git log
309 fi
235997c9
JH
310 else
311 case "$1" in
312 git*|tig) ;;
313 -*) set git log "$@" ;;
314 *) set git "$@" ;;
315 esac
316 fi
317
fc13aa3d 318 eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
cc9f24d0
JH
319}
320
8cc6a083 321bisect_reset() {
823ea121 322 test -s "$GIT_DIR/BISECT_START" || {
d0238a88 323 gettext "We are not bisecting."; echo
fce0499f
CC
324 return
325 }
8cc6a083 326 case "$#" in
823ea121 327 0) branch=$(cat "$GIT_DIR/BISECT_START") ;;
7d0c2d6f
ÆAB
328 1) git rev-parse --quiet --verify "$1^{commit}" > /dev/null || {
329 invalid="$1"
330 die "$(eval_gettext "'\$invalid' is not a valid commit")"
331 }
8cc6a083 332 branch="$1" ;;
8fe26f44 333 *)
8cc6a083
LT
334 usage ;;
335 esac
3bb8cf88
SG
336 if git checkout "$branch" -- ; then
337 bisect_clean_state
338 else
15eaa049
ÆAB
339 die "$(eval_gettext "Could not check out original HEAD '\$branch'.
340Try 'git bisect reset <commit>'.")"
3bb8cf88 341 fi
e204de28
JH
342}
343
38a47fd6 344bisect_clean_state() {
947a604b 345 # There may be some refs packed during bisection.
634f2464 346 git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* |
947a604b
CC
347 while read ref hash
348 do
823ea121 349 git update-ref -d $ref $hash || exit
947a604b 350 done
c9c4e2d5
CC
351 rm -f "$GIT_DIR/BISECT_EXPECTED_REV" &&
352 rm -f "$GIT_DIR/BISECT_ANCESTORS_OK" &&
823ea121
CC
353 rm -f "$GIT_DIR/BISECT_LOG" &&
354 rm -f "$GIT_DIR/BISECT_NAMES" &&
355 rm -f "$GIT_DIR/BISECT_RUN" &&
9d0cd91c 356 # Cleanup head-name if it got left by an old version of git-bisect
823ea121
CC
357 rm -f "$GIT_DIR/head-name" &&
358
359 rm -f "$GIT_DIR/BISECT_START"
38a47fd6
CC
360}
361
e204de28 362bisect_replay () {
55a9fc80
ÆAB
363 file="$1"
364 test "$#" -eq 1 || die "$(gettext "No logfile given")"
365 test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")"
e204de28 366 bisect_reset
6c98c054 367 while read git bisect command rev
e204de28 368 do
6c98c054
MV
369 test "$git $bisect" = "git bisect" -o "$git" = "git-bisect" || continue
370 if test "$git" = "git-bisect"; then
371 rev="$command"
372 command="$bisect"
373 fi
e204de28
JH
374 case "$command" in
375 start)
e9a45d75 376 cmd="bisect_start $rev"
737c74ee
CC
377 eval "$cmd" ;;
378 good|bad|skip)
379 bisect_write "$command" "$rev" ;;
e204de28 380 *)
9570fc1e 381 die "$(gettext "?? what are you talking about?")" ;;
e204de28 382 esac
55a9fc80 383 done <"$file"
e204de28 384 bisect_auto_next
8cc6a083
LT
385}
386
a17c4101 387bisect_run () {
83020120
CC
388 bisect_next_check fail
389
a17c4101
CC
390 while true
391 do
09209255
ÆAB
392 command="$@"
393 eval_gettext "running \$command"; echo
a17c4101
CC
394 "$@"
395 res=$?
396
397 # Check for really bad run error.
398 if [ $res -lt 0 -o $res -ge 128 ]; then
09209255
ÆAB
399 (
400 eval_gettext "bisect run failed:
401exit code \$res from '\$command' is < 0 or >= 128" &&
402 echo
403 ) >&2
a17c4101
CC
404 exit $res
405 fi
406
155fc795 407 # Find current state depending on run success or failure.
71b0251c
CC
408 # A special exit code of 125 means cannot test.
409 if [ $res -eq 125 ]; then
410 state='skip'
411 elif [ $res -gt 0 ]; then
155fc795 412 state='bad'
a17c4101 413 else
155fc795 414 state='good'
a17c4101
CC
415 fi
416
155fc795
CC
417 # We have to use a subshell because "bisect_state" can exit.
418 ( bisect_state $state > "$GIT_DIR/BISECT_RUN" )
a17c4101
CC
419 res=$?
420
421 cat "$GIT_DIR/BISECT_RUN"
422
e1622bfc 423 if sane_grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \
71b0251c 424 > /dev/null; then
ddd7a7c2
ÆAB
425 (
426 gettext "bisect run cannot continue any more" &&
427 echo
428 ) >&2
71b0251c
CC
429 exit $res
430 fi
431
a17c4101 432 if [ $res -ne 0 ]; then
c6649c92
ÆAB
433 (
434 eval_gettext "bisect run failed:
435'bisect_state \$state' exited with error code \$res" &&
436 echo
437 ) >&2
a17c4101
CC
438 exit $res
439 fi
440
e1622bfc 441 if sane_grep "is the first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
d0238a88 442 gettext "bisect run success"; echo
a17c4101
CC
443 exit 0;
444 fi
445
446 done
447}
448
412ff738 449bisect_log () {
9570fc1e 450 test -s "$GIT_DIR/BISECT_LOG" || die "$(gettext "We are not bisecting.")"
412ff738
SG
451 cat "$GIT_DIR/BISECT_LOG"
452}
a17c4101 453
8cc6a083
LT
454case "$#" in
4550)
456 usage ;;
457*)
458 cmd="$1"
459 shift
460 case "$cmd" in
243a60fb
CC
461 help)
462 git bisect -h ;;
8cc6a083
LT
463 start)
464 bisect_start "$@" ;;
ee2314f5 465 bad|good)
155fc795 466 bisect_state "$cmd" "$@" ;;
ee2314f5
CC
467 skip)
468 bisect_skip "$@" ;;
8cc6a083
LT
469 next)
470 # Not sure we want "next" at the UI level anymore.
471 bisect_next "$@" ;;
235997c9 472 visualize|view)
cc9f24d0 473 bisect_visualize "$@" ;;
8cc6a083
LT
474 reset)
475 bisect_reset "$@" ;;
e204de28
JH
476 replay)
477 bisect_replay "$@" ;;
478 log)
412ff738 479 bisect_log ;;
a17c4101
CC
480 run)
481 bisect_run "$@" ;;
8cc6a083
LT
482 *)
483 usage ;;
484 esac
485esac