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