blob: 54cbfecc5ab0531513ff9e069be55d74339ad427 [file] [log] [blame]
Linus Torvalds8cc6a082005-07-30 10:08:20 -07001#!/bin/sh
Fredrik Kuivinend0255242005-12-11 10:55:49 +01002
Robert P. J. Daydbc349b2017-11-12 04:30:38 -05003USAGE='[help|start|bad|good|new|old|terms|skip|next|reset|visualize|view|replay|log|run]'
Christian Couder243a60f2008-04-11 05:55:21 +02004LONG_USAGE='git bisect help
Jon Seymour6021be82011-08-05 21:31:30 +10005 print this long help message.
Matthieu Moy06e6a742015-06-29 17:40:35 +02006git bisect start [--term-{old,good}=<term> --term-{new,bad}=<term>]
7 [--no-checkout] [<bad> [<good>...]] [--] [<pathspec>...]
Jon Seymour6021be82011-08-05 21:31:30 +10008 reset bisect state and start bisection.
Antoine Delaite21e5cfd2015-06-29 17:40:33 +02009git bisect (bad|new) [<rev>]
10 mark <rev> a known-bad revision/
11 a revision after change in a given property.
12git bisect (good|old) [<rev>...]
13 mark <rev>... known-good revisions/
14 revisions before change in a given property.
Matthieu Moy21b55e32015-06-29 17:40:34 +020015git bisect terms [--term-good | --term-bad]
16 show the terms used for old and new commits (default: bad, good)
Christian Couder54138122008-12-02 14:53:51 +010017git bisect skip [(<rev>|<range>)...]
Jon Seymour6021be82011-08-05 21:31:30 +100018 mark <rev>... untestable revisions.
Christian Couder38a47fd2007-04-04 07:12:02 +020019git bisect next
Jon Seymour6021be82011-08-05 21:31:30 +100020 find next bisection to test and check it out.
Anders Kaseorg6b87ce22009-10-13 17:02:24 -040021git bisect reset [<commit>]
Jon Seymour6021be82011-08-05 21:31:30 +100022 finish bisection search and go back to commit.
Robert P. J. Daydbc349b2017-11-12 04:30:38 -050023git bisect (visualize|view)
Jon Seymour6021be82011-08-05 21:31:30 +100024 show bisect status in gitk.
Christian Couder38a47fd2007-04-04 07:12:02 +020025git bisect replay <logfile>
Jon Seymour6021be82011-08-05 21:31:30 +100026 replay bisection log.
Christian Couder38a47fd2007-04-04 07:12:02 +020027git bisect log
Jon Seymour6021be82011-08-05 21:31:30 +100028 show bisect log.
Christian Couder38a47fd2007-04-04 07:12:02 +020029git bisect run <cmd>...
Jon Seymour6021be82011-08-05 21:31:30 +100030 use <cmd>... to automatically bisect.
Christian Couder243a60f2008-04-11 05:55:21 +020031
32Please use "git help bisect" to get the full man page.'
Fredrik Kuivinend0255242005-12-11 10:55:49 +010033
Junio C Hamano8f321a32007-11-06 01:50:02 -080034OPTIONS_SPEC=
Junio C Hamanoae2b0f12005-11-24 00:12:11 -080035. git-sh-setup
Linus Torvalds8cc6a082005-07-30 10:08:20 -070036
Johannes Schindelince326602008-02-10 13:59:50 +000037_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"
Antoine Delaite43f9d9f2015-06-29 17:40:29 +020039TERM_BAD=bad
40TERM_GOOD=good
Johannes Schindelince326602008-02-10 13:59:50 +000041
Jon Seymour4796e822011-08-04 22:01:01 +100042bisect_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
Linus Torvalds8cc6a082005-07-30 10:08:20 -070052bisect_autostart() {
Christian Couder823ea122008-05-28 18:57:02 +020053 test -s "$GIT_DIR/BISECT_START" || {
Jon Seymour3145b1a2011-08-31 09:09:47 +100054 gettextln "You need to start by \"git bisect start\"" >&2
Linus Torvalds8cc6a082005-07-30 10:08:20 -070055 if test -t 0
56 then
Ævar Arnfjörð Bjarmason04de0992011-05-21 18:44:28 +000057 # TRANSLATORS: Make sure to include [Y] and [n] in your
58 # translation. The program will only accept English input
59 # at this point.
Jon Seymour6021be82011-08-05 21:31:30 +100060 gettext "Do you want me to do it for you [Y/n]? " >&2
Linus Torvalds8cc6a082005-07-30 10:08:20 -070061 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
73bisect_start() {
Linus Torvalds8cc6a082005-07-30 10:08:20 -070074 #
Jon Seymour4764f462011-08-04 22:00:57 +100075 # Check for one bad and then some good revisions.
76 #
77 has_double_dash=0
78 for arg; do
Jon Seymour6021be82011-08-05 21:31:30 +100079 case "$arg" in --) has_double_dash=1; break ;; esac
Jon Seymour4764f462011-08-04 22:00:57 +100080 done
81 orig_args=$(git rev-parse --sq-quote "$@")
82 bad_seen=0
83 eval=''
Antoine Delaitecb46d632015-06-29 17:40:30 +020084 must_write_terms=0
Matthieu Moy4a6ada32015-06-29 17:40:31 +020085 revs=''
Jon Seymour24c51282011-08-09 12:11:54 +100086 if test "z$(git rev-parse --is-bare-repository)" != zfalse
87 then
88 mode=--no-checkout
89 else
90 mode=''
91 fi
Jon Seymour4764f462011-08-04 22:00:57 +100092 while [ $# -gt 0 ]; do
Jon Seymour6021be82011-08-05 21:31:30 +100093 arg="$1"
94 case "$arg" in
95 --)
96 shift
97 break
Jon Seymour4764f462011-08-04 22:00:57 +100098 ;;
Jon Seymour6021be82011-08-05 21:31:30 +100099 --no-checkout)
100 mode=--no-checkout
101 shift ;;
Matthieu Moy06e6a742015-06-29 17:40:35 +0200102 --term-good|--term-old)
103 shift
104 must_write_terms=1
105 TERM_GOOD=$1
106 shift ;;
107 --term-good=*|--term-old=*)
108 must_write_terms=1
109 TERM_GOOD=${1#*=}
110 shift ;;
111 --term-bad|--term-new)
112 shift
113 must_write_terms=1
114 TERM_BAD=$1
115 shift ;;
116 --term-bad=*|--term-new=*)
117 must_write_terms=1
118 TERM_BAD=${1#*=}
119 shift ;;
Jon Seymour6021be82011-08-05 21:31:30 +1000120 --*)
121 die "$(eval_gettext "unrecognised option: '\$arg'")" ;;
122 *)
123 rev=$(git rev-parse -q --verify "$arg^{commit}") || {
Junio C Hamano43b8ff42011-08-05 10:09:23 -0700124 test $has_double_dash -eq 1 &&
125 die "$(eval_gettext "'\$arg' does not appear to be a valid revision")"
126 break
Jon Seymour6021be82011-08-05 21:31:30 +1000127 }
Matthieu Moy4a6ada32015-06-29 17:40:31 +0200128 revs="$revs $rev"
Jon Seymour6021be82011-08-05 21:31:30 +1000129 shift
130 ;;
Jon Seymour4764f462011-08-04 22:00:57 +1000131 esac
Jon Seymour4764f462011-08-04 22:00:57 +1000132 done
133
Matthieu Moy4a6ada32015-06-29 17:40:31 +0200134 for rev in $revs
135 do
136 # The user ran "git bisect start <sha1>
137 # <sha1>", hence did not explicitly specify
138 # the terms, but we are already starting to
139 # set references named with the default terms,
140 # and won't be able to change afterwards.
141 must_write_terms=1
142
143 case $bad_seen in
144 0) state=$TERM_BAD ; bad_seen=1 ;;
145 *) state=$TERM_GOOD ;;
146 esac
147 eval="$eval bisect_write '$state' '$rev' 'nolog' &&"
148 done
Jon Seymour4764f462011-08-04 22:00:57 +1000149 #
Christian Couder634f2462008-05-23 01:28:57 +0200150 # Verify HEAD.
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700151 #
Christian Couder48949a12008-04-16 04:09:49 +0200152 head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
Johannes Schindelince326602008-02-10 13:59:50 +0000153 head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
Ævar Arnfjörð Bjarmason9570fc12011-05-21 18:44:23 +0000154 die "$(gettext "Bad HEAD - I need a HEAD")"
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700155
156 #
Christian Couder634f2462008-05-23 01:28:57 +0200157 # Check if we are bisecting.
158 #
159 start_head=''
160 if test -s "$GIT_DIR/BISECT_START"
161 then
162 # Reset to the rev from where we started.
163 start_head=$(cat "$GIT_DIR/BISECT_START")
Jon Seymour4796e822011-08-04 22:01:01 +1000164 if test "z$mode" != "z--no-checkout"
165 then
Christian Couder1acf1172011-09-21 07:17:24 +0200166 git checkout "$start_head" -- ||
Alex Henrie9c9b4f22015-01-13 00:44:47 -0700167 die "$(eval_gettext "Checking out '\$start_head' failed. Try 'git bisect reset <valid-branch>'.")"
Jon Seymour4796e822011-08-04 22:01:01 +1000168 fi
Christian Couder634f2462008-05-23 01:28:57 +0200169 else
170 # Get rev from where we start.
171 case "$head" in
172 refs/heads/*|$_x40)
173 # This error message should only be triggered by
174 # cogito usage, and cogito users should understand
175 # it relates to cg-seek.
176 [ -s "$GIT_DIR/head-name" ] &&
Masanari Iida382d20e2013-11-13 00:17:42 +0900177 die "$(gettext "won't bisect on cg-seek'ed tree")"
Christian Couder634f2462008-05-23 01:28:57 +0200178 start_head="${head#refs/heads/}"
179 ;;
180 *)
Ævar Arnfjörð Bjarmason9570fc12011-05-21 18:44:23 +0000181 die "$(gettext "Bad HEAD - strange symbolic ref")"
Christian Couder634f2462008-05-23 01:28:57 +0200182 ;;
183 esac
184 fi
185
186 #
187 # Get rid of any old bisect state.
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700188 #
Pranit Bauvafb71a322017-09-29 06:49:39 +0000189 git bisect--helper --bisect-clean-state || exit
Christian Couder38a47fd2007-04-04 07:12:02 +0200190
191 #
Christian Couderba963de2008-05-23 00:39:22 +0200192 # Change state.
193 # In case of mistaken revs or checkout error, or signals received,
194 # "bisect_auto_next" below may exit or misbehave.
195 # We have to trap this to be able to clean up using
196 # "bisect_clean_state".
197 #
Pranit Bauvafb71a322017-09-29 06:49:39 +0000198 trap 'git bisect--helper --bisect-clean-state' 0
Christian Couderba963de2008-05-23 00:39:22 +0200199 trap 'exit 255' 1 2 3 15
200
201 #
202 # Write new start state.
203 #
Jon Seymour4796e822011-08-04 22:01:01 +1000204 echo "$start_head" >"$GIT_DIR/BISECT_START" && {
205 test "z$mode" != "z--no-checkout" ||
206 git update-ref --no-deref BISECT_HEAD "$start_head"
207 } &&
Christian Couderde52f5a2009-04-24 08:29:00 +0200208 git rev-parse --sq-quote "$@" >"$GIT_DIR/BISECT_NAMES" &&
Jon Seymour6ba7acf2011-08-04 22:00:58 +1000209 eval "$eval true" &&
Antoine Delaitecb46d632015-06-29 17:40:30 +0200210 if test $must_write_terms -eq 1
211 then
Pranit Bauvaecb3f372017-09-29 06:49:39 +0000212 git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
Antoine Delaitecb46d632015-06-29 17:40:30 +0200213 fi &&
Miklos Vajna6c98c052008-07-11 18:01:29 +0200214 echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit
Christian Couderba963de2008-05-23 00:39:22 +0200215 #
216 # Check if we can proceed to the next bisect state.
217 #
Christian Couder38a47fd2007-04-04 07:12:02 +0200218 bisect_auto_next
Christian Couderba963de2008-05-23 00:39:22 +0200219
220 trap '-' 0
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700221}
222
Christian Couder55624f92007-10-24 07:01:05 +0200223bisect_write() {
224 state="$1"
225 rev="$2"
Christian Couder737c74e2007-10-24 07:01:13 +0200226 nolog="$3"
Christian Couder55624f92007-10-24 07:01:05 +0200227 case "$state" in
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200228 "$TERM_BAD")
229 tag="$state" ;;
230 "$TERM_GOOD"|skip)
231 tag="$state"-"$rev" ;;
232 *)
233 die "$(eval_gettext "Bad bisect_write argument: \$state")" ;;
Christian Couder55624f92007-10-24 07:01:05 +0200234 esac
Christian Couderba963de2008-05-23 00:39:22 +0200235 git update-ref "refs/bisect/$tag" "$rev" || exit
Johannes Schindelinf454cdc2008-02-12 19:50:57 +0000236 echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG"
Miklos Vajna6c98c052008-07-11 18:01:29 +0200237 test -n "$nolog" || echo "git bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
Christian Couder55624f92007-10-24 07:01:05 +0200238}
239
Christian Couderee2314f2008-11-23 22:02:49 +0100240bisect_skip() {
Jon Seymour6021be82011-08-05 21:31:30 +1000241 all=''
Christian Couderee2314f2008-11-23 22:02:49 +0100242 for arg in "$@"
243 do
Jon Seymour6021be82011-08-05 21:31:30 +1000244 case "$arg" in
245 *..*)
246 revs=$(git rev-list "$arg") || die "$(eval_gettext "Bad rev input: \$arg")" ;;
247 *)
248 revs=$(git rev-parse --sq-quote "$arg") ;;
249 esac
250 all="$all $revs"
251 done
252 eval bisect_state 'skip' $all
Christian Couderee2314f2008-11-23 22:02:49 +0100253}
254
Christian Couder155fc792007-10-24 07:01:21 +0200255bisect_state() {
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700256 bisect_autostart
Christian Couder155fc792007-10-24 07:01:21 +0200257 state=$1
Antoine Delaitecb46d632015-06-29 17:40:30 +0200258 check_and_set_terms $state
Christian Couder155fc792007-10-24 07:01:21 +0200259 case "$#,$state" in
260 0,*)
Vasco Almeidaf813fb42016-06-17 21:54:14 +0000261 die "Please call 'bisect_state' with at least one argument." ;;
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200262 1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip)
Vasco Almeida57984dd2016-06-17 20:21:00 +0000263 bisected_head=$(bisect_head)
264 rev=$(git rev-parse --verify "$bisected_head") ||
265 die "$(eval_gettext "Bad rev input: \$bisected_head")"
Christian Couderc9c4e2d2008-08-22 05:52:29 +0200266 bisect_write "$state" "$rev"
Pranit Bauvab9036742017-09-29 06:49:40 +0000267 git bisect--helper --check-expected-revs "$rev" ;;
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200268 2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
Christian Couder155fc792007-10-24 07:01:21 +0200269 shift
Christian Couder6bc02d52014-12-25 19:25:32 +0100270 hash_list=''
Christian Coudere3389072008-04-12 07:53:59 +0200271 for rev in "$@"
Christian Couder155fc792007-10-24 07:01:21 +0200272 do
Christian Coudera179a302008-04-12 02:17:36 -0700273 sha=$(git rev-parse --verify "$rev^{commit}") ||
Ævar Arnfjörð Bjarmason15eaa042011-05-21 18:44:24 +0000274 die "$(eval_gettext "Bad rev input: \$rev")"
Christian Couder6bc02d52014-12-25 19:25:32 +0100275 hash_list="$hash_list $sha"
Christian Couderd3e54c82008-04-14 05:41:45 +0200276 done
Christian Couder6bc02d52014-12-25 19:25:32 +0100277 for rev in $hash_list
278 do
279 bisect_write "$state" "$rev"
280 done
Pranit Bauvab9036742017-09-29 06:49:40 +0000281 git bisect--helper --check-expected-revs $hash_list ;;
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200282 *,"$TERM_BAD")
283 die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
Junio C Hamanocc9f24d2005-08-30 12:45:41 -0700284 *)
285 usage ;;
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700286 esac
Christian Couder97e1c512007-10-22 07:48:36 +0200287 bisect_auto_next
288}
289
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700290bisect_next_check() {
Junio C Hamano0a5280a2007-04-05 23:27:44 -0700291 missing_good= missing_bad=
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200292 git show-ref -q --verify refs/bisect/$TERM_BAD || missing_bad=t
293 test -n "$(git for-each-ref "refs/bisect/$TERM_GOOD-*")" || missing_good=t
Junio C Hamano6fecf192007-04-05 22:51:14 -0700294
Junio C Hamano0a5280a2007-04-05 23:27:44 -0700295 case "$missing_good,$missing_bad,$1" in
296 ,,*)
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200297 : have both $TERM_GOOD and $TERM_BAD - ok
Junio C Hamano0a5280a2007-04-05 23:27:44 -0700298 ;;
299 *,)
300 # do not have both but not asked to fail - just report.
301 false
302 ;;
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200303 t,,"$TERM_GOOD")
Antoine Delaite21e5cfd2015-06-29 17:40:33 +0200304 # have bad (or new) but not good (or old). we could bisect although
Junio C Hamano0a5280a2007-04-05 23:27:44 -0700305 # this is less optimum.
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200306 eval_gettextln "Warning: bisecting only with a \$TERM_BAD commit." >&2
Junio C Hamano0a5280a2007-04-05 23:27:44 -0700307 if test -t 0
308 then
Ævar Arnfjörð Bjarmason04de0992011-05-21 18:44:28 +0000309 # TRANSLATORS: Make sure to include [Y] and [n] in your
310 # translation. The program will only accept English input
311 # at this point.
312 gettext "Are you sure [Y/n]? " >&2
Francis Moreaue5d3afd2008-08-11 19:37:46 +0200313 read yesno
314 case "$yesno" in [Nn]*) exit 1 ;; esac
Junio C Hamano0a5280a2007-04-05 23:27:44 -0700315 fi
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200316 : bisect without $TERM_GOOD...
Junio C Hamano0a5280a2007-04-05 23:27:44 -0700317 ;;
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700318 *)
Antoine Delaitecb46d632015-06-29 17:40:30 +0200319 bad_syn=$(bisect_voc bad)
320 good_syn=$(bisect_voc good)
Ævar Arnfjörð Bjarmasonbe508d32011-05-21 18:44:29 +0000321 if test -s "$GIT_DIR/BISECT_START"
322 then
Antoine Delaitecb46d632015-06-29 17:40:30 +0200323
324 eval_gettextln "You need to give me at least one \$bad_syn and one \$good_syn revision.
325(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2
Ævar Arnfjörð Bjarmasonbe508d32011-05-21 18:44:29 +0000326 else
Antoine Delaitecb46d632015-06-29 17:40:30 +0200327 eval_gettextln "You need to start by \"git bisect start\".
328You then need to give me at least one \$good_syn and one \$bad_syn revision.
329(You can use \"git bisect \$bad_syn\" and \"git bisect \$good_syn\" for that.)" >&2
Ævar Arnfjörð Bjarmasonbe508d32011-05-21 18:44:29 +0000330 fi
Junio C Hamano0a5280a2007-04-05 23:27:44 -0700331 exit 1 ;;
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700332 esac
333}
334
335bisect_auto_next() {
Junio C Hamano434d0362005-09-17 13:51:03 -0700336 bisect_next_check && bisect_next || :
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700337}
338
339bisect_next() {
Christian Couder8fe26f42007-10-22 07:48:23 +0200340 case "$#" in 0) ;; *) usage ;; esac
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700341 bisect_autostart
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200342 bisect_next_check $TERM_GOOD
Junio C Hamano0a5280a2007-04-05 23:27:44 -0700343
Christian Couder08719842009-05-09 17:55:47 +0200344 # Perform all bisection computation, display and checkout
Jon Seymour4796e822011-08-04 22:01:01 +1000345 git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout)
Christian Couder5a1d31c2009-04-19 11:56:16 +0200346 res=$?
Junio C Hamano0a5280a2007-04-05 23:27:44 -0700347
Jon Seymour6021be82011-08-05 21:31:30 +1000348 # Check if we should exit because bisection is finished
Torstein Heggea7f8b8a2013-04-13 17:22:57 +0200349 if test $res -eq 10
350 then
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200351 bad_rev=$(git show-ref --hash --verify refs/bisect/$TERM_BAD)
Torstein Heggea7f8b8a2013-04-13 17:22:57 +0200352 bad_commit=$(git show-branch $bad_rev)
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200353 echo "# first $TERM_BAD commit: $bad_commit" >>"$GIT_DIR/BISECT_LOG"
Torstein Heggea7f8b8a2013-04-13 17:22:57 +0200354 exit 0
Torstein Heggef989cac2013-04-22 23:02:29 +0200355 elif test $res -eq 2
356 then
357 echo "# only skipped commits left to test" >>"$GIT_DIR/BISECT_LOG"
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200358 good_revs=$(git for-each-ref --format="%(objectname)" "refs/bisect/$TERM_GOOD-*")
359 for skipped in $(git rev-list refs/bisect/$TERM_BAD --not $good_revs)
Torstein Heggef989cac2013-04-22 23:02:29 +0200360 do
361 skipped_commit=$(git show-branch $skipped)
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200362 echo "# possible first $TERM_BAD commit: $skipped_commit" >>"$GIT_DIR/BISECT_LOG"
Torstein Heggef989cac2013-04-22 23:02:29 +0200363 done
364 exit $res
Torstein Heggea7f8b8a2013-04-13 17:22:57 +0200365 fi
Junio C Hamano0a5280a2007-04-05 23:27:44 -0700366
Christian Couder5a1d31c2009-04-19 11:56:16 +0200367 # Check for an error in the bisection process
368 test $res -ne 0 && exit $res
369
370 return 0
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700371}
372
Junio C Hamanocc9f24d2005-08-30 12:45:41 -0700373bisect_visualize() {
374 bisect_next_check fail
Junio C Hamano235997c2007-12-07 02:25:34 -0800375
376 if test $# = 0
377 then
Jeff Kingc4e4644e2011-03-21 09:14:22 -0400378 if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
Junio C Hamano43b8ff42011-08-05 10:09:23 -0700379 type gitk >/dev/null 2>&1
Jon Seymoureef12a92011-08-05 21:31:31 +1000380 then
Jeff Kingc4e4644e2011-03-21 09:14:22 -0400381 set gitk
382 else
383 set git log
384 fi
Junio C Hamano235997c2007-12-07 02:25:34 -0800385 else
386 case "$1" in
387 git*|tig) ;;
388 -*) set git log "$@" ;;
389 *) set git "$@" ;;
390 esac
391 fi
392
Christian Couderfc13aa32009-11-23 05:16:14 +0100393 eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
Junio C Hamanocc9f24d2005-08-30 12:45:41 -0700394}
395
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700396bisect_reset() {
Christian Couder823ea122008-05-28 18:57:02 +0200397 test -s "$GIT_DIR/BISECT_START" || {
Jon Seymour3145b1a2011-08-31 09:09:47 +1000398 gettextln "We are not bisecting."
Christian Couderfce04992007-11-20 06:39:53 +0100399 return
400 }
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700401 case "$#" in
Christian Couder823ea122008-05-28 18:57:02 +0200402 0) branch=$(cat "$GIT_DIR/BISECT_START") ;;
Jacopo Notarstefano305a2332014-03-04 01:21:43 +0100403 1) git rev-parse --quiet --verify "$1^{commit}" >/dev/null || {
Jon Seymour6021be82011-08-05 21:31:30 +1000404 invalid="$1"
405 die "$(eval_gettext "'\$invalid' is not a valid commit")"
406 }
407 branch="$1" ;;
Christian Couder8fe26f42007-10-22 07:48:23 +0200408 *)
Jon Seymour6021be82011-08-05 21:31:30 +1000409 usage ;;
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700410 esac
Junio C Hamano43b8ff42011-08-05 10:09:23 -0700411
412 if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" --
Jon Seymour4796e822011-08-04 22:01:01 +1000413 then
Junio C Hamano43b8ff42011-08-05 10:09:23 -0700414 die "$(eval_gettext "Could not check out original HEAD '\$branch'.
Ævar Arnfjörð Bjarmason15eaa042011-05-21 18:44:24 +0000415Try 'git bisect reset <commit>'.")"
SZEDER Gábor3bb8cf82010-10-10 23:48:57 +0200416 fi
Pranit Bauvafb71a322017-09-29 06:49:39 +0000417 git bisect--helper --bisect-clean-state || exit
Christian Couder38a47fd2007-04-04 07:12:02 +0200418}
419
Junio C Hamanoe204de22005-09-10 15:18:31 -0700420bisect_replay () {
Ævar Arnfjörð Bjarmason55a9fc82011-05-21 18:44:27 +0000421 file="$1"
422 test "$#" -eq 1 || die "$(gettext "No logfile given")"
423 test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")"
Junio C Hamanoe204de22005-09-10 15:18:31 -0700424 bisect_reset
Miklos Vajna6c98c052008-07-11 18:01:29 +0200425 while read git bisect command rev
Junio C Hamanoe204de22005-09-10 15:18:31 -0700426 do
Elia Pintoc82af122014-06-06 07:55:50 -0700427 test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue
Jon Seymoureef12a92011-08-05 21:31:31 +1000428 if test "$git" = "git-bisect"
429 then
Miklos Vajna6c98c052008-07-11 18:01:29 +0200430 rev="$command"
431 command="$bisect"
432 fi
Antoine Delaitecb46d632015-06-29 17:40:30 +0200433 get_terms
434 check_and_set_terms "$command"
Junio C Hamanoe204de22005-09-10 15:18:31 -0700435 case "$command" in
436 start)
Junio C Hamanoe9a45d72005-11-27 17:42:05 -0800437 cmd="bisect_start $rev"
Christian Couder737c74e2007-10-24 07:01:13 +0200438 eval "$cmd" ;;
Antoine Delaitecb46d632015-06-29 17:40:30 +0200439 "$TERM_GOOD"|"$TERM_BAD"|skip)
Christian Couder737c74e2007-10-24 07:01:13 +0200440 bisect_write "$command" "$rev" ;;
Matthieu Moy21b55e32015-06-29 17:40:34 +0200441 terms)
442 bisect_terms $rev ;;
Junio C Hamanoe204de22005-09-10 15:18:31 -0700443 *)
Ævar Arnfjörð Bjarmason9570fc12011-05-21 18:44:23 +0000444 die "$(gettext "?? what are you talking about?")" ;;
Junio C Hamanoe204de22005-09-10 15:18:31 -0700445 esac
Ævar Arnfjörð Bjarmason55a9fc82011-05-21 18:44:27 +0000446 done <"$file"
Junio C Hamanoe204de22005-09-10 15:18:31 -0700447 bisect_auto_next
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700448}
449
Christian Coudera17c4102007-03-23 08:49:59 +0100450bisect_run () {
Jon Seymour6021be82011-08-05 21:31:30 +1000451 bisect_next_check fail
Christian Couder83020122007-03-27 06:49:57 +0200452
Stephan Beyerfecd2dd2017-11-12 21:55:33 +0100453 test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"
454
Jon Seymour6021be82011-08-05 21:31:30 +1000455 while true
456 do
457 command="$@"
Jon Seymour3145b1a2011-08-31 09:09:47 +1000458 eval_gettextln "running \$command"
Jon Seymour6021be82011-08-05 21:31:30 +1000459 "$@"
460 res=$?
Christian Coudera17c4102007-03-23 08:49:59 +0100461
Jon Seymour6021be82011-08-05 21:31:30 +1000462 # Check for really bad run error.
Jon Seymoureef12a92011-08-05 21:31:31 +1000463 if [ $res -lt 0 -o $res -ge 128 ]
464 then
Jon Seymour3145b1a2011-08-31 09:09:47 +1000465 eval_gettextln "bisect run failed:
466exit code \$res from '\$command' is < 0 or >= 128" >&2
Jon Seymour6021be82011-08-05 21:31:30 +1000467 exit $res
468 fi
Christian Coudera17c4102007-03-23 08:49:59 +0100469
Jon Seymour6021be82011-08-05 21:31:30 +1000470 # Find current state depending on run success or failure.
471 # A special exit code of 125 means cannot test.
Jon Seymoureef12a92011-08-05 21:31:31 +1000472 if [ $res -eq 125 ]
473 then
Jon Seymour6021be82011-08-05 21:31:30 +1000474 state='skip'
Jon Seymoureef12a92011-08-05 21:31:31 +1000475 elif [ $res -gt 0 ]
476 then
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200477 state="$TERM_BAD"
Jon Seymour6021be82011-08-05 21:31:30 +1000478 else
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200479 state="$TERM_GOOD"
Jon Seymour6021be82011-08-05 21:31:30 +1000480 fi
Christian Coudera17c4102007-03-23 08:49:59 +0100481
Jon Seymour6021be82011-08-05 21:31:30 +1000482 # We have to use a subshell because "bisect_state" can exit.
Jacopo Notarstefano305a2332014-03-04 01:21:43 +0100483 ( bisect_state $state >"$GIT_DIR/BISECT_RUN" )
Jon Seymour6021be82011-08-05 21:31:30 +1000484 res=$?
Christian Coudera17c4102007-03-23 08:49:59 +0100485
Jon Seymour6021be82011-08-05 21:31:30 +1000486 cat "$GIT_DIR/BISECT_RUN"
Christian Coudera17c4102007-03-23 08:49:59 +0100487
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200488 if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
Jacopo Notarstefano305a2332014-03-04 01:21:43 +0100489 >/dev/null
Jon Seymoureef12a92011-08-05 21:31:31 +1000490 then
Jon Seymour3145b1a2011-08-31 09:09:47 +1000491 gettextln "bisect run cannot continue any more" >&2
Jon Seymour6021be82011-08-05 21:31:30 +1000492 exit $res
493 fi
Christian Couder71b02512007-10-26 05:39:37 +0200494
Jon Seymoureef12a92011-08-05 21:31:31 +1000495 if [ $res -ne 0 ]
496 then
Jon Seymour3145b1a2011-08-31 09:09:47 +1000497 eval_gettextln "bisect run failed:
498'bisect_state \$state' exited with error code \$res" >&2
Jon Seymour6021be82011-08-05 21:31:30 +1000499 exit $res
500 fi
Christian Coudera17c4102007-03-23 08:49:59 +0100501
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200502 if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
Jon Seymoureef12a92011-08-05 21:31:31 +1000503 then
Jon Seymour3145b1a2011-08-31 09:09:47 +1000504 gettextln "bisect run success"
Jon Seymour6021be82011-08-05 21:31:30 +1000505 exit 0;
506 fi
Christian Coudera17c4102007-03-23 08:49:59 +0100507
Jon Seymour6021be82011-08-05 21:31:30 +1000508 done
Christian Coudera17c4102007-03-23 08:49:59 +0100509}
510
SZEDER Gábor412ff732010-10-10 23:48:56 +0200511bisect_log () {
Ævar Arnfjörð Bjarmason9570fc12011-05-21 18:44:23 +0000512 test -s "$GIT_DIR/BISECT_LOG" || die "$(gettext "We are not bisecting.")"
SZEDER Gábor412ff732010-10-10 23:48:56 +0200513 cat "$GIT_DIR/BISECT_LOG"
514}
Christian Coudera17c4102007-03-23 08:49:59 +0100515
Antoine Delaitecb46d632015-06-29 17:40:30 +0200516get_terms () {
517 if test -s "$GIT_DIR/BISECT_TERMS"
518 then
519 {
520 read TERM_BAD
521 read TERM_GOOD
522 } <"$GIT_DIR/BISECT_TERMS"
523 fi
524}
525
Antoine Delaitecb46d632015-06-29 17:40:30 +0200526check_and_set_terms () {
527 cmd="$1"
528 case "$cmd" in
529 skip|start|terms) ;;
530 *)
531 if test -s "$GIT_DIR/BISECT_TERMS" && test "$cmd" != "$TERM_BAD" && test "$cmd" != "$TERM_GOOD"
532 then
533 die "$(eval_gettext "Invalid command: you're currently in a \$TERM_BAD/\$TERM_GOOD bisect.")"
534 fi
535 case "$cmd" in
536 bad|good)
537 if ! test -s "$GIT_DIR/BISECT_TERMS"
538 then
Pranit Bauvaecb3f372017-09-29 06:49:39 +0000539 TERM_BAD=bad
540 TERM_GOOD=good
541 git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
Antoine Delaitecb46d632015-06-29 17:40:30 +0200542 fi
543 ;;
Antoine Delaite21e5cfd2015-06-29 17:40:33 +0200544 new|old)
545 if ! test -s "$GIT_DIR/BISECT_TERMS"
546 then
Pranit Bauvaecb3f372017-09-29 06:49:39 +0000547 TERM_BAD=new
548 TERM_GOOD=old
549 git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
Antoine Delaite21e5cfd2015-06-29 17:40:33 +0200550 fi
551 ;;
Antoine Delaitecb46d632015-06-29 17:40:30 +0200552 esac ;;
553 esac
554}
555
556bisect_voc () {
557 case "$1" in
Antoine Delaite21e5cfd2015-06-29 17:40:33 +0200558 bad) echo "bad|new" ;;
559 good) echo "good|old" ;;
Antoine Delaitecb46d632015-06-29 17:40:30 +0200560 esac
561}
562
Matthieu Moy21b55e32015-06-29 17:40:34 +0200563bisect_terms () {
564 get_terms
565 if ! test -s "$GIT_DIR/BISECT_TERMS"
566 then
567 die "$(gettext "no terms defined")"
568 fi
569 case "$#" in
570 0)
571 gettextln "Your current terms are $TERM_GOOD for the old state
572and $TERM_BAD for the new state."
573 ;;
574 1)
575 arg=$1
576 case "$arg" in
577 --term-good|--term-old)
578 printf '%s\n' "$TERM_GOOD"
579 ;;
580 --term-bad|--term-new)
581 printf '%s\n' "$TERM_BAD"
582 ;;
583 *)
584 die "$(eval_gettext "invalid argument \$arg for 'git bisect terms'.
585Supported options are: --term-good|--term-old and --term-bad|--term-new.")"
586 ;;
587 esac
588 ;;
589 *)
590 usage ;;
591 esac
592}
593
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700594case "$#" in
5950)
Jon Seymour6021be82011-08-05 21:31:30 +1000596 usage ;;
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700597*)
Jon Seymour6021be82011-08-05 21:31:30 +1000598 cmd="$1"
Antoine Delaitecb46d632015-06-29 17:40:30 +0200599 get_terms
Jon Seymour6021be82011-08-05 21:31:30 +1000600 shift
601 case "$cmd" in
602 help)
603 git bisect -h ;;
604 start)
605 bisect_start "$@" ;;
Antoine Delaite21e5cfd2015-06-29 17:40:33 +0200606 bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
Jon Seymour6021be82011-08-05 21:31:30 +1000607 bisect_state "$cmd" "$@" ;;
608 skip)
609 bisect_skip "$@" ;;
610 next)
611 # Not sure we want "next" at the UI level anymore.
612 bisect_next "$@" ;;
613 visualize|view)
614 bisect_visualize "$@" ;;
615 reset)
616 bisect_reset "$@" ;;
617 replay)
618 bisect_replay "$@" ;;
619 log)
620 bisect_log ;;
621 run)
622 bisect_run "$@" ;;
Matthieu Moy21b55e32015-06-29 17:40:34 +0200623 terms)
624 bisect_terms "$@" ;;
Jon Seymour6021be82011-08-05 21:31:30 +1000625 *)
626 usage ;;
627 esac
Linus Torvalds8cc6a082005-07-30 10:08:20 -0700628esac