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