Fix config key miscount in url.*.insteadOf
[git/gitweb-caching.git] / git-bisect.sh
blob5177a2a11482af276ae5243c4d550b68bb0e2a28
1 #!/bin/sh
3 USAGE='[start|bad|good|skip|next|reset|visualize|replay|log|run]'
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.
10 git bisect skip [<rev>...]
11 mark <rev>... untestable revisions.
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.'
25 OPTIONS_SPEC=
26 . git-sh-setup
27 require_work_tree
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"
32 sq() {
33 @@PERL@@ -e '
34 for (@ARGV) {
35 s/'\''/'\'\\\\\'\''/g;
36 print " '\''$_'\''";
38 print "\n";
39 ' "$@"
42 bisect_autostart() {
43 test -f "$GIT_DIR/BISECT_NAMES" || {
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
60 bisect_start() {
62 # Verify HEAD. If we were bisecting before this, reset to the
63 # top-of-line master first!
65 head=$(GIT_DIR="$GIT_DIR" git symbolic-ref HEAD) ||
66 head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
67 die "Bad HEAD - I need a HEAD"
68 case "$head" in
69 refs/heads/bisect)
70 if [ -s "$GIT_DIR/BISECT_START" ]; then
71 branch=`cat "$GIT_DIR/BISECT_START"`
72 else
73 branch=master
75 git checkout $branch || exit
77 refs/heads/*|$_x40)
78 # This error message should only be triggered by cogito usage,
79 # and cogito users should understand it relates to cg-seek.
80 [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
81 echo "${head#refs/heads/}" >"$GIT_DIR/BISECT_START"
84 die "Bad HEAD - strange symbolic ref"
86 esac
89 # Get rid of any old bisect state
91 bisect_clean_state
94 # Check for one bad and then some good revisions.
96 has_double_dash=0
97 for arg; do
98 case "$arg" in --) has_double_dash=1; break ;; esac
99 done
100 orig_args=$(sq "$@")
101 bad_seen=0
102 while [ $# -gt 0 ]; do
103 arg="$1"
104 case "$arg" in
106 shift
107 break
110 rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || {
111 test $has_double_dash -eq 1 &&
112 die "'$arg' does not appear to be a valid revision"
113 break
115 case $bad_seen in
116 0) state='bad' ; bad_seen=1 ;;
117 *) state='good' ;;
118 esac
119 bisect_write "$state" "$rev" 'nolog'
120 shift
122 esac
123 done
125 sq "$@" >"$GIT_DIR/BISECT_NAMES"
126 echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG"
127 bisect_auto_next
130 bisect_write() {
131 state="$1"
132 rev="$2"
133 nolog="$3"
134 case "$state" in
135 bad) tag="$state" ;;
136 good|skip) tag="$state"-"$rev" ;;
137 *) die "Bad bisect_write argument: $state" ;;
138 esac
139 git update-ref "refs/bisect/$tag" "$rev"
140 echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG"
141 test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
144 bisect_state() {
145 bisect_autostart
146 state=$1
147 case "$#,$state" in
148 0,*)
149 die "Please call 'bisect_state' with at least one argument." ;;
150 1,bad|1,good|1,skip)
151 rev=$(git rev-parse --verify HEAD) ||
152 die "Bad rev input: HEAD"
153 bisect_write "$state" "$rev" ;;
154 2,bad|*,good|*,skip)
155 shift
156 for rev in "$@"
158 sha=$(git rev-parse --verify "$rev^{commit}") ||
159 die "Bad rev input: $rev"
160 bisect_write "$state" "$sha"
161 done ;;
162 *,bad)
163 die "'git bisect bad' can take only one argument." ;;
165 usage ;;
166 esac
167 bisect_auto_next
170 bisect_next_check() {
171 missing_good= missing_bad=
172 git show-ref -q --verify refs/bisect/bad || missing_bad=t
173 test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t
175 case "$missing_good,$missing_bad,$1" in
176 ,,*)
177 : have both good and bad - ok
180 # do not have both but not asked to fail - just report.
181 false
183 t,,good)
184 # have bad but not good. we could bisect although
185 # this is less optimum.
186 echo >&2 'Warning: bisecting only with a bad commit.'
187 if test -t 0
188 then
189 printf >&2 'Are you sure [Y/n]? '
190 case "$(read yesno)" in [Nn]*) exit 1 ;; esac
192 : bisect without good...
195 THEN=''
196 test -f "$GIT_DIR/BISECT_NAMES" || {
197 echo >&2 'You need to start by "git bisect start".'
198 THEN='then '
200 echo >&2 'You '$THEN'need to give me at least one good' \
201 'and one bad revisions.'
202 echo >&2 '(You can use "git bisect bad" and' \
203 '"git bisect good" for that.)'
204 exit 1 ;;
205 esac
208 bisect_auto_next() {
209 bisect_next_check && bisect_next || :
212 filter_skipped() {
213 _eval="$1"
214 _skip="$2"
216 if [ -z "$_skip" ]; then
217 eval $_eval
218 return
221 # Let's parse the output of:
222 # "git rev-list --bisect-vars --bisect-all ..."
223 eval $_eval | while read hash line
225 case "$VARS,$FOUND,$TRIED,$hash" in
226 # We display some vars.
227 1,*,*,*) echo "$hash $line" ;;
229 # Split line.
230 ,*,*,---*) ;;
232 # We had nothing to search.
233 ,,,bisect_rev*)
234 echo "bisect_rev="
235 VARS=1
238 # We did not find a good bisect rev.
239 # This should happen only if the "bad"
240 # commit is also a "skip" commit.
241 ,,*,bisect_rev*)
242 echo "bisect_rev=$TRIED"
243 VARS=1
246 # We are searching.
247 ,,*,*)
248 TRIED="${TRIED:+$TRIED|}$hash"
249 case "$_skip" in
250 *$hash*) ;;
252 echo "bisect_rev=$hash"
253 echo "bisect_tried=\"$TRIED\""
254 FOUND=1
256 esac
259 # We have already found a rev to be tested.
260 ,1,*,bisect_rev*) VARS=1 ;;
261 ,1,*,*) ;;
263 # ???
264 *) die "filter_skipped error " \
265 "VARS: '$VARS' " \
266 "FOUND: '$FOUND' " \
267 "TRIED: '$TRIED' " \
268 "hash: '$hash' " \
269 "line: '$line'"
271 esac
272 done
275 exit_if_skipped_commits () {
276 _tried=$1
277 if expr "$_tried" : ".*[|].*" > /dev/null ; then
278 echo "There are only 'skip'ped commit left to test."
279 echo "The first bad commit could be any of:"
280 echo "$_tried" | tr '[|]' '[\012]'
281 echo "We cannot bisect more!"
282 exit 2
286 bisect_next() {
287 case "$#" in 0) ;; *) usage ;; esac
288 bisect_autostart
289 bisect_next_check good
291 skip=$(git for-each-ref --format='%(objectname)' \
292 "refs/bisect/skip-*" | tr '\012' ' ') || exit
294 BISECT_OPT=''
295 test -n "$skip" && BISECT_OPT='--bisect-all'
297 bad=$(git rev-parse --verify refs/bisect/bad) &&
298 good=$(git for-each-ref --format='^%(objectname)' \
299 "refs/bisect/good-*" | tr '\012' ' ') &&
300 eval="git rev-list --bisect-vars $BISECT_OPT $good $bad --" &&
301 eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" &&
302 eval=$(filter_skipped "$eval" "$skip") &&
303 eval "$eval" || exit
305 if [ -z "$bisect_rev" ]; then
306 echo "$bad was both good and bad"
307 exit 1
309 if [ "$bisect_rev" = "$bad" ]; then
310 exit_if_skipped_commits "$bisect_tried"
311 echo "$bisect_rev is first bad commit"
312 git diff-tree --pretty $bisect_rev
313 exit 0
316 # We should exit here only if the "bad"
317 # commit is also a "skip" commit (see above).
318 exit_if_skipped_commits "$bisect_rev"
320 echo "Bisecting: $bisect_nr revisions left to test after this"
321 git branch -f new-bisect "$bisect_rev"
322 git checkout -q new-bisect || exit
323 git branch -M new-bisect bisect
324 git show-branch "$bisect_rev"
327 bisect_visualize() {
328 bisect_next_check fail
330 if test $# = 0
331 then
332 case "${DISPLAY+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" in
333 '') set git log ;;
334 set*) set gitk ;;
335 esac
336 else
337 case "$1" in
338 git*|tig) ;;
339 -*) set git log "$@" ;;
340 *) set git "$@" ;;
341 esac
344 not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*")
345 eval '"$@"' refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
348 bisect_reset() {
349 test -f "$GIT_DIR/BISECT_NAMES" || {
350 echo "We are not bisecting."
351 return
353 case "$#" in
354 0) if [ -s "$GIT_DIR/BISECT_START" ]; then
355 branch=`cat "$GIT_DIR/BISECT_START"`
356 else
357 branch=master
358 fi ;;
359 1) git show-ref --verify --quiet -- "refs/heads/$1" ||
360 die "$1 does not seem to be a valid branch"
361 branch="$1" ;;
363 usage ;;
364 esac
365 if git checkout "$branch"; then
366 # Cleanup head-name if it got left by an old version of git-bisect
367 rm -f "$GIT_DIR/head-name"
368 rm -f "$GIT_DIR/BISECT_START"
369 bisect_clean_state
373 bisect_clean_state() {
374 # There may be some refs packed during bisection.
375 git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* refs/heads/bisect |
376 while read ref hash
378 git update-ref -d $ref $hash
379 done
380 rm -f "$GIT_DIR/BISECT_LOG"
381 rm -f "$GIT_DIR/BISECT_NAMES"
382 rm -f "$GIT_DIR/BISECT_RUN"
385 bisect_replay () {
386 test -r "$1" || die "cannot read $1 for replaying"
387 bisect_reset
388 while read bisect command rev
390 test "$bisect" = "git-bisect" || continue
391 case "$command" in
392 start)
393 cmd="bisect_start $rev"
394 eval "$cmd" ;;
395 good|bad|skip)
396 bisect_write "$command" "$rev" ;;
398 die "?? what are you talking about?" ;;
399 esac
400 done <"$1"
401 bisect_auto_next
404 bisect_run () {
405 bisect_next_check fail
407 while true
409 echo "running $@"
410 "$@"
411 res=$?
413 # Check for really bad run error.
414 if [ $res -lt 0 -o $res -ge 128 ]; then
415 echo >&2 "bisect run failed:"
416 echo >&2 "exit code $res from '$@' is < 0 or >= 128"
417 exit $res
420 # Find current state depending on run success or failure.
421 # A special exit code of 125 means cannot test.
422 if [ $res -eq 125 ]; then
423 state='skip'
424 elif [ $res -gt 0 ]; then
425 state='bad'
426 else
427 state='good'
430 # We have to use a subshell because "bisect_state" can exit.
431 ( bisect_state $state > "$GIT_DIR/BISECT_RUN" )
432 res=$?
434 cat "$GIT_DIR/BISECT_RUN"
436 if grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \
437 > /dev/null; then
438 echo >&2 "bisect run cannot continue any more"
439 exit $res
442 if [ $res -ne 0 ]; then
443 echo >&2 "bisect run failed:"
444 echo >&2 "'bisect_state $state' exited with error code $res"
445 exit $res
448 if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
449 echo "bisect run success"
450 exit 0;
453 done
457 case "$#" in
459 usage ;;
461 cmd="$1"
462 shift
463 case "$cmd" in
464 start)
465 bisect_start "$@" ;;
466 bad|good|skip)
467 bisect_state "$cmd" "$@" ;;
468 next)
469 # Not sure we want "next" at the UI level anymore.
470 bisect_next "$@" ;;
471 visualize|view)
472 bisect_visualize "$@" ;;
473 reset)
474 bisect_reset "$@" ;;
475 replay)
476 bisect_replay "$@" ;;
477 log)
478 cat "$GIT_DIR/BISECT_LOG" ;;
479 run)
480 bisect_run "$@" ;;
482 usage ;;
483 esac
484 esac