]>
Commit | Line | Data |
---|---|---|
8cc6a083 | 1 | #!/bin/sh |
d025524d | 2 | |
6ca8b977 | 3 | USAGE='[start|bad|good|skip|next|reset|visualize|replay|log|run]' |
38a47fd6 CC |
4 | LONG_USAGE='git bisect start [<bad> [<good>...]] [--] [<pathspec>...] |
5 | reset bisect state and start bisection. | |
6 | git bisect bad [<rev>] | |
7 | mark <rev> a known-bad revision. | |
8 | git bisect good [<rev>...] | |
9 | mark <rev>... known-good revisions. | |
6ca8b977 CC |
10 | git bisect skip [<rev>...] |
11 | mark <rev>... untestable revisions. | |
38a47fd6 CC |
12 | git bisect next |
13 | find next bisection to test and check it out. | |
14 | git bisect reset [<branch>] | |
15 | finish bisection search and go back to branch. | |
16 | git bisect visualize | |
17 | show bisect status in gitk. | |
18 | git bisect replay <logfile> | |
19 | replay bisection log. | |
20 | git bisect log | |
21 | show bisect log. | |
22 | git bisect run <cmd>... | |
23 | use <cmd>... to automatically bisect.' | |
d025524d | 24 | |
8f321a39 | 25 | OPTIONS_SPEC= |
ae2b0f15 | 26 | . git-sh-setup |
4c550686 | 27 | require_work_tree |
8cc6a083 | 28 | |
e9a45d75 | 29 | sq() { |
d9bffc08 | 30 | @@PERL@@ -e ' |
e9a45d75 JH |
31 | for (@ARGV) { |
32 | s/'\''/'\'\\\\\'\''/g; | |
33 | print " '\''$_'\''"; | |
34 | } | |
35 | print "\n"; | |
36 | ' "$@" | |
37 | } | |
38 | ||
8cc6a083 | 39 | bisect_autostart() { |
6459c7c6 | 40 | test -f "$GIT_DIR/BISECT_NAMES" || { |
8cc6a083 LT |
41 | echo >&2 'You need to start by "git bisect start"' |
42 | if test -t 0 | |
43 | then | |
44 | echo >&2 -n 'Do you want me to do it for you [Y/n]? ' | |
45 | read yesno | |
46 | case "$yesno" in | |
47 | [Nn]*) | |
48 | exit ;; | |
49 | esac | |
50 | bisect_start | |
51 | else | |
52 | exit 1 | |
53 | fi | |
54 | } | |
55 | } | |
56 | ||
57 | bisect_start() { | |
8cc6a083 LT |
58 | # |
59 | # Verify HEAD. If we were bisecting before this, reset to the | |
60 | # top-of-line master first! | |
61 | # | |
5be60078 | 62 | head=$(GIT_DIR="$GIT_DIR" git symbolic-ref HEAD) || |
8098a178 | 63 | die "Bad HEAD - I need a symbolic ref" |
8cc6a083 | 64 | case "$head" in |
673e5838 | 65 | refs/heads/bisect) |
810255fd PB |
66 | if [ -s "$GIT_DIR/head-name" ]; then |
67 | branch=`cat "$GIT_DIR/head-name"` | |
68 | else | |
69 | branch=master | |
8fe26f44 | 70 | fi |
810255fd | 71 | git checkout $branch || exit |
8cc6a083 LT |
72 | ;; |
73 | refs/heads/*) | |
810255fd | 74 | [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree" |
e23cb8c0 | 75 | echo "${head#refs/heads/}" >"$GIT_DIR/head-name" |
8cc6a083 LT |
76 | ;; |
77 | *) | |
8098a178 | 78 | die "Bad HEAD - strange symbolic ref" |
8cc6a083 LT |
79 | ;; |
80 | esac | |
81 | ||
82 | # | |
83 | # Get rid of any old bisect state | |
84 | # | |
38a47fd6 | 85 | bisect_clean_state |
38a47fd6 CC |
86 | |
87 | # | |
88 | # Check for one bad and then some good revisions. | |
89 | # | |
90 | has_double_dash=0 | |
91 | for arg; do | |
92 | case "$arg" in --) has_double_dash=1; break ;; esac | |
93 | done | |
94 | orig_args=$(sq "$@") | |
95 | bad_seen=0 | |
96 | while [ $# -gt 0 ]; do | |
97 | arg="$1" | |
98 | case "$arg" in | |
99 | --) | |
8fe26f44 | 100 | shift |
38a47fd6 CC |
101 | break |
102 | ;; | |
103 | *) | |
8fe26f44 | 104 | rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || { |
38a47fd6 CC |
105 | test $has_double_dash -eq 1 && |
106 | die "'$arg' does not appear to be a valid revision" | |
107 | break | |
108 | } | |
737c74ee CC |
109 | case $bad_seen in |
110 | 0) state='bad' ; bad_seen=1 ;; | |
111 | *) state='good' ;; | |
112 | esac | |
113 | bisect_write "$state" "$rev" 'nolog' | |
8fe26f44 | 114 | shift |
38a47fd6 CC |
115 | ;; |
116 | esac | |
8fe26f44 | 117 | done |
38a47fd6 CC |
118 | |
119 | sq "$@" >"$GIT_DIR/BISECT_NAMES" | |
b8652b4d | 120 | echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" |
38a47fd6 | 121 | bisect_auto_next |
8cc6a083 LT |
122 | } |
123 | ||
55624f9a CC |
124 | bisect_write() { |
125 | state="$1" | |
126 | rev="$2" | |
737c74ee | 127 | nolog="$3" |
55624f9a CC |
128 | case "$state" in |
129 | bad) tag="$state" ;; | |
130 | good|skip) tag="$state"-"$rev" ;; | |
131 | *) die "Bad bisect_write argument: $state" ;; | |
132 | esac | |
3d7cd64c | 133 | git update-ref "refs/bisect/$tag" "$rev" |
55624f9a | 134 | echo "# $state: "$(git show-branch $rev) >>"$GIT_DIR/BISECT_LOG" |
737c74ee | 135 | test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG" |
55624f9a CC |
136 | } |
137 | ||
155fc795 | 138 | bisect_state() { |
8cc6a083 | 139 | bisect_autostart |
155fc795 CC |
140 | state=$1 |
141 | case "$#,$state" in | |
142 | 0,*) | |
143 | die "Please call 'bisect_state' with at least one argument." ;; | |
144 | 1,bad|1,good|1,skip) | |
145 | rev=$(git rev-parse --verify HEAD) || | |
146 | die "Bad rev input: HEAD" | |
147 | bisect_write "$state" "$rev" ;; | |
148 | 2,bad) | |
149 | rev=$(git rev-parse --verify "$2^{commit}") || | |
150 | die "Bad rev input: $2" | |
151 | bisect_write "$state" "$rev" ;; | |
152 | *,good|*,skip) | |
153 | shift | |
154 | revs=$(git rev-parse --revs-only --no-flags "$@") && | |
155 | test '' != "$revs" || die "Bad rev input: $@" | |
156 | for rev in $revs | |
157 | do | |
158 | rev=$(git rev-parse --verify "$rev^{commit}") || | |
159 | die "Bad rev commit: $rev^{commit}" | |
160 | bisect_write "$state" "$rev" | |
161 | done ;; | |
cc9f24d0 JH |
162 | *) |
163 | usage ;; | |
8cc6a083 | 164 | esac |
97e1c51e CC |
165 | bisect_auto_next |
166 | } | |
167 | ||
8cc6a083 | 168 | bisect_next_check() { |
0a5280a9 JH |
169 | missing_good= missing_bad= |
170 | git show-ref -q --verify refs/bisect/bad || missing_bad=t | |
171 | test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t | |
6fecf191 | 172 | |
0a5280a9 JH |
173 | case "$missing_good,$missing_bad,$1" in |
174 | ,,*) | |
175 | : have both good and bad - ok | |
176 | ;; | |
177 | *,) | |
178 | # do not have both but not asked to fail - just report. | |
179 | false | |
180 | ;; | |
181 | t,,good) | |
182 | # have bad but not good. we could bisect although | |
183 | # this is less optimum. | |
184 | echo >&2 'Warning: bisecting only with a bad commit.' | |
185 | if test -t 0 | |
186 | then | |
187 | printf >&2 'Are you sure [Y/n]? ' | |
188 | case "$(read yesno)" in [Nn]*) exit 1 ;; esac | |
189 | fi | |
190 | : bisect without good... | |
191 | ;; | |
8cc6a083 | 192 | *) |
0a5280a9 | 193 | THEN='' |
6459c7c6 | 194 | test -f "$GIT_DIR/BISECT_NAMES" || { |
0a5280a9 JH |
195 | echo >&2 'You need to start by "git bisect start".' |
196 | THEN='then ' | |
197 | } | |
198 | echo >&2 'You '$THEN'need to give me at least one good' \ | |
199 | 'and one bad revisions.' | |
200 | echo >&2 '(You can use "git bisect bad" and' \ | |
201 | '"git bisect good" for that.)' | |
202 | exit 1 ;; | |
8cc6a083 LT |
203 | esac |
204 | } | |
205 | ||
206 | bisect_auto_next() { | |
434d036f | 207 | bisect_next_check && bisect_next || : |
8cc6a083 LT |
208 | } |
209 | ||
97e1c51e CC |
210 | filter_skipped() { |
211 | _eval="$1" | |
212 | _skip="$2" | |
213 | ||
214 | if [ -z "$_skip" ]; then | |
215 | eval $_eval | |
216 | return | |
217 | fi | |
218 | ||
219 | # Let's parse the output of: | |
220 | # "git rev-list --bisect-vars --bisect-all ..." | |
221 | eval $_eval | while read hash line | |
222 | do | |
223 | case "$VARS,$FOUND,$TRIED,$hash" in | |
224 | # We display some vars. | |
225 | 1,*,*,*) echo "$hash $line" ;; | |
226 | ||
227 | # Split line. | |
228 | ,*,*,---*) ;; | |
229 | ||
230 | # We had nothing to search. | |
231 | ,,,bisect_rev*) | |
232 | echo "bisect_rev=" | |
233 | VARS=1 | |
234 | ;; | |
235 | ||
236 | # We did not find a good bisect rev. | |
237 | # This should happen only if the "bad" | |
238 | # commit is also a "skip" commit. | |
239 | ,,*,bisect_rev*) | |
240 | echo "bisect_rev=$TRIED" | |
241 | VARS=1 | |
242 | ;; | |
243 | ||
244 | # We are searching. | |
245 | ,,*,*) | |
246 | TRIED="${TRIED:+$TRIED|}$hash" | |
247 | case "$_skip" in | |
248 | *$hash*) ;; | |
249 | *) | |
250 | echo "bisect_rev=$hash" | |
251 | echo "bisect_tried=\"$TRIED\"" | |
252 | FOUND=1 | |
253 | ;; | |
254 | esac | |
255 | ;; | |
256 | ||
257 | # We have already found a rev to be tested. | |
258 | ,1,*,bisect_rev*) VARS=1 ;; | |
259 | ,1,*,*) ;; | |
260 | ||
261 | # ??? | |
262 | *) die "filter_skipped error " \ | |
263 | "VARS: '$VARS' " \ | |
264 | "FOUND: '$FOUND' " \ | |
265 | "TRIED: '$TRIED' " \ | |
266 | "hash: '$hash' " \ | |
267 | "line: '$line'" | |
268 | ;; | |
269 | esac | |
270 | done | |
271 | } | |
272 | ||
273 | exit_if_skipped_commits () { | |
274 | _tried=$1 | |
275 | if expr "$_tried" : ".*[|].*" > /dev/null ; then | |
276 | echo "There are only 'skip'ped commit left to test." | |
277 | echo "The first bad commit could be any of:" | |
e23cb8c0 | 278 | echo "$_tried" | tr '[|]' '[\012]' |
97e1c51e CC |
279 | echo "We cannot bisect more!" |
280 | exit 2 | |
281 | fi | |
282 | } | |
283 | ||
8cc6a083 | 284 | bisect_next() { |
8fe26f44 | 285 | case "$#" in 0) ;; *) usage ;; esac |
8cc6a083 | 286 | bisect_autostart |
0a5280a9 JH |
287 | bisect_next_check good |
288 | ||
97e1c51e CC |
289 | skip=$(git for-each-ref --format='%(objectname)' \ |
290 | "refs/bisect/skip-*" | tr '[\012]' ' ') || exit | |
291 | ||
292 | BISECT_OPT='' | |
293 | test -n "$skip" && BISECT_OPT='--bisect-all' | |
294 | ||
5be60078 | 295 | bad=$(git rev-parse --verify refs/bisect/bad) && |
0a5280a9 JH |
296 | good=$(git for-each-ref --format='^%(objectname)' \ |
297 | "refs/bisect/good-*" | tr '[\012]' ' ') && | |
97e1c51e | 298 | eval="git rev-list --bisect-vars $BISECT_OPT $good $bad --" && |
0a5280a9 | 299 | eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" && |
97e1c51e | 300 | eval=$(filter_skipped "$eval" "$skip") && |
0a5280a9 JH |
301 | eval "$eval" || exit |
302 | ||
303 | if [ -z "$bisect_rev" ]; then | |
304 | echo "$bad was both good and bad" | |
305 | exit 1 | |
670f5fe3 | 306 | fi |
0a5280a9 | 307 | if [ "$bisect_rev" = "$bad" ]; then |
97e1c51e | 308 | exit_if_skipped_commits "$bisect_tried" |
0a5280a9 | 309 | echo "$bisect_rev is first bad commit" |
5be60078 | 310 | git diff-tree --pretty $bisect_rev |
0a5280a9 | 311 | exit 0 |
8cc6a083 | 312 | fi |
0a5280a9 | 313 | |
97e1c51e CC |
314 | # We should exit here only if the "bad" |
315 | # commit is also a "skip" commit (see above). | |
316 | exit_if_skipped_commits "$bisect_rev" | |
317 | ||
0a5280a9 | 318 | echo "Bisecting: $bisect_nr revisions left to test after this" |
0bee49c6 | 319 | git branch -f new-bisect "$bisect_rev" |
08f16750 | 320 | git checkout -q new-bisect || exit |
0bee49c6 | 321 | git branch -M new-bisect bisect |
5be60078 | 322 | git show-branch "$bisect_rev" |
8cc6a083 LT |
323 | } |
324 | ||
cc9f24d0 JH |
325 | bisect_visualize() { |
326 | bisect_next_check fail | |
e3f062bf CC |
327 | not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*") |
328 | eval gitk refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES") | |
cc9f24d0 JH |
329 | } |
330 | ||
8cc6a083 | 331 | bisect_reset() { |
fce0499f CC |
332 | test -f "$GIT_DIR/BISECT_NAMES" || { |
333 | echo "We are not bisecting." | |
334 | return | |
335 | } | |
8cc6a083 | 336 | case "$#" in |
810255fd PB |
337 | 0) if [ -s "$GIT_DIR/head-name" ]; then |
338 | branch=`cat "$GIT_DIR/head-name"` | |
339 | else | |
340 | branch=master | |
341 | fi ;; | |
737c74ee CC |
342 | 1) git show-ref --verify --quiet -- "refs/heads/$1" || |
343 | die "$1 does not seem to be a valid branch" | |
8cc6a083 | 344 | branch="$1" ;; |
8fe26f44 | 345 | *) |
8cc6a083 LT |
346 | usage ;; |
347 | esac | |
9b709e47 | 348 | if git checkout "$branch"; then |
38a47fd6 CC |
349 | rm -f "$GIT_DIR/head-name" |
350 | bisect_clean_state | |
9b709e47 | 351 | fi |
e204de28 JH |
352 | } |
353 | ||
38a47fd6 | 354 | bisect_clean_state() { |
947a604b CC |
355 | # There may be some refs packed during bisection. |
356 | git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* refs/heads/bisect | | |
357 | while read ref hash | |
358 | do | |
359 | git update-ref -d $ref $hash | |
360 | done | |
38a47fd6 CC |
361 | rm -f "$GIT_DIR/BISECT_LOG" |
362 | rm -f "$GIT_DIR/BISECT_NAMES" | |
363 | rm -f "$GIT_DIR/BISECT_RUN" | |
364 | } | |
365 | ||
e204de28 | 366 | bisect_replay () { |
737c74ee | 367 | test -r "$1" || die "cannot read $1 for replaying" |
e204de28 JH |
368 | bisect_reset |
369 | while read bisect command rev | |
370 | do | |
371 | test "$bisect" = "git-bisect" || continue | |
372 | case "$command" in | |
373 | start) | |
e9a45d75 | 374 | cmd="bisect_start $rev" |
737c74ee CC |
375 | eval "$cmd" ;; |
376 | good|bad|skip) | |
377 | bisect_write "$command" "$rev" ;; | |
e204de28 | 378 | *) |
737c74ee | 379 | die "?? what are you talking about?" ;; |
e204de28 JH |
380 | esac |
381 | done <"$1" | |
382 | bisect_auto_next | |
8cc6a083 LT |
383 | } |
384 | ||
a17c4101 | 385 | bisect_run () { |
83020120 CC |
386 | bisect_next_check fail |
387 | ||
a17c4101 CC |
388 | while true |
389 | do | |
390 | echo "running $@" | |
391 | "$@" | |
392 | res=$? | |
393 | ||
394 | # Check for really bad run error. | |
395 | if [ $res -lt 0 -o $res -ge 128 ]; then | |
396 | echo >&2 "bisect run failed:" | |
397 | echo >&2 "exit code $res from '$@' is < 0 or >= 128" | |
398 | exit $res | |
399 | fi | |
400 | ||
155fc795 | 401 | # Find current state depending on run success or failure. |
71b0251c CC |
402 | # A special exit code of 125 means cannot test. |
403 | if [ $res -eq 125 ]; then | |
404 | state='skip' | |
405 | elif [ $res -gt 0 ]; then | |
155fc795 | 406 | state='bad' |
a17c4101 | 407 | else |
155fc795 | 408 | state='good' |
a17c4101 CC |
409 | fi |
410 | ||
155fc795 CC |
411 | # We have to use a subshell because "bisect_state" can exit. |
412 | ( bisect_state $state > "$GIT_DIR/BISECT_RUN" ) | |
a17c4101 CC |
413 | res=$? |
414 | ||
415 | cat "$GIT_DIR/BISECT_RUN" | |
416 | ||
71b0251c CC |
417 | if grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \ |
418 | > /dev/null; then | |
419 | echo >&2 "bisect run cannot continue any more" | |
420 | exit $res | |
421 | fi | |
422 | ||
a17c4101 CC |
423 | if [ $res -ne 0 ]; then |
424 | echo >&2 "bisect run failed:" | |
155fc795 | 425 | echo >&2 "'bisect_state $state' exited with error code $res" |
a17c4101 CC |
426 | exit $res |
427 | fi | |
428 | ||
429 | if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then | |
430 | echo "bisect run success" | |
431 | exit 0; | |
432 | fi | |
433 | ||
434 | done | |
435 | } | |
436 | ||
437 | ||
8cc6a083 LT |
438 | case "$#" in |
439 | 0) | |
440 | usage ;; | |
441 | *) | |
442 | cmd="$1" | |
443 | shift | |
444 | case "$cmd" in | |
445 | start) | |
446 | bisect_start "$@" ;; | |
155fc795 CC |
447 | bad|good|skip) |
448 | bisect_state "$cmd" "$@" ;; | |
8cc6a083 LT |
449 | next) |
450 | # Not sure we want "next" at the UI level anymore. | |
451 | bisect_next "$@" ;; | |
cc9f24d0 JH |
452 | visualize) |
453 | bisect_visualize "$@" ;; | |
8cc6a083 LT |
454 | reset) |
455 | bisect_reset "$@" ;; | |
e204de28 JH |
456 | replay) |
457 | bisect_replay "$@" ;; | |
458 | log) | |
459 | cat "$GIT_DIR/BISECT_LOG" ;; | |
a17c4101 CC |
460 | run) |
461 | bisect_run "$@" ;; | |
8cc6a083 LT |
462 | *) |
463 | usage ;; | |
464 | esac | |
465 | esac |