git-bisect.shon commit bisect: fix bad rev checking in "git bisect good" (e338907)
   1#!/bin/sh
   2
   3USAGE='[start|bad|good|skip|next|reset|visualize|replay|log|run]'
   4LONG_USAGE='git bisect start [<bad> [<good>...]] [--] [<pathspec>...]
   5        reset bisect state and start bisection.
   6git bisect bad [<rev>]
   7        mark <rev> a known-bad revision.
   8git bisect good [<rev>...]
   9        mark <rev>... known-good revisions.
  10git bisect skip [<rev>...]
  11        mark <rev>... untestable revisions.
  12git bisect next
  13        find next bisection to test and check it out.
  14git bisect reset [<branch>]
  15        finish bisection search and go back to branch.
  16git bisect visualize
  17        show bisect status in gitk.
  18git bisect replay <logfile>
  19        replay bisection log.
  20git bisect log
  21        show bisect log.
  22git bisect run <cmd>...
  23        use <cmd>... to automatically bisect.'
  24
  25OPTIONS_SPEC=
  26. git-sh-setup
  27require_work_tree
  28
  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
  32sq() {
  33        @@PERL@@ -e '
  34                for (@ARGV) {
  35                        s/'\''/'\'\\\\\'\''/g;
  36                        print " '\''$_'\''";
  37                }
  38                print "\n";
  39        ' "$@"
  40}
  41
  42bisect_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
  56                fi
  57        }
  58}
  59
  60bisect_start() {
  61        #
  62        # Verify HEAD. If we were bisecting before this, reset to the
  63        # top-of-line master first!
  64        #
  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
  74                fi
  75                git checkout $branch || exit
  76                ;;
  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"
  82                ;;
  83        *)
  84                die "Bad HEAD - strange symbolic ref"
  85                ;;
  86        esac
  87
  88        #
  89        # Get rid of any old bisect state
  90        #
  91        bisect_clean_state
  92
  93        #
  94        # Check for one bad and then some good revisions.
  95        #
  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
 105            --)
 106                shift
 107                break
 108                ;;
 109            *)
 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
 114                }
 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
 121                ;;
 122            esac
 123        done
 124
 125        sq "$@" >"$GIT_DIR/BISECT_NAMES"
 126        echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG"
 127        bisect_auto_next
 128}
 129
 130bisect_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"
 142}
 143
 144bisect_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 "$@"
 157                do
 158                        rev=$(git rev-parse --verify "$rev^{commit}") ||
 159                                die "Bad rev input: $rev"
 160                        bisect_write "$state" "$rev"
 161                done ;;
 162        *,bad)
 163                die "'git bisect bad' can take only one argument." ;;
 164        *)
 165                usage ;;
 166        esac
 167        bisect_auto_next
 168}
 169
 170bisect_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
 174
 175        case "$missing_good,$missing_bad,$1" in
 176        ,,*)
 177                : have both good and bad - ok
 178                ;;
 179        *,)
 180                # do not have both but not asked to fail - just report.
 181                false
 182                ;;
 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
 191                fi
 192                : bisect without good...
 193                ;;
 194        *)
 195                THEN=''
 196                test -f "$GIT_DIR/BISECT_NAMES" || {
 197                        echo >&2 'You need to start by "git bisect start".'
 198                        THEN='then '
 199                }
 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
 206}
 207
 208bisect_auto_next() {
 209        bisect_next_check && bisect_next || :
 210}
 211
 212filter_skipped() {
 213        _eval="$1"
 214        _skip="$2"
 215
 216        if [ -z "$_skip" ]; then
 217                eval $_eval
 218                return
 219        fi
 220
 221        # Let's parse the output of:
 222        # "git rev-list --bisect-vars --bisect-all ..."
 223        eval $_eval | while read hash line
 224        do
 225                case "$VARS,$FOUND,$TRIED,$hash" in
 226                        # We display some vars.
 227                        1,*,*,*) echo "$hash $line" ;;
 228
 229                        # Split line.
 230                        ,*,*,---*) ;;
 231
 232                        # We had nothing to search.
 233                        ,,,bisect_rev*)
 234                                echo "bisect_rev="
 235                                VARS=1
 236                                ;;
 237
 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
 244                                ;;
 245
 246                        # We are searching.
 247                        ,,*,*)
 248                                TRIED="${TRIED:+$TRIED|}$hash"
 249                                case "$_skip" in
 250                                *$hash*) ;;
 251                                *)
 252                                        echo "bisect_rev=$hash"
 253                                        echo "bisect_tried=\"$TRIED\""
 254                                        FOUND=1
 255                                        ;;
 256                                esac
 257                                ;;
 258
 259                        # We have already found a rev to be tested.
 260                        ,1,*,bisect_rev*) VARS=1 ;;
 261                        ,1,*,*) ;;
 262
 263                        # ???
 264                        *) die "filter_skipped error " \
 265                            "VARS: '$VARS' " \
 266                            "FOUND: '$FOUND' " \
 267                            "TRIED: '$TRIED' " \
 268                            "hash: '$hash' " \
 269                            "line: '$line'"
 270                        ;;
 271                esac
 272        done
 273}
 274
 275exit_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
 283        fi
 284}
 285
 286bisect_next() {
 287        case "$#" in 0) ;; *) usage ;; esac
 288        bisect_autostart
 289        bisect_next_check good
 290
 291        skip=$(git for-each-ref --format='%(objectname)' \
 292                "refs/bisect/skip-*" | tr '[\012]' ' ') || exit
 293
 294        BISECT_OPT=''
 295        test -n "$skip" && BISECT_OPT='--bisect-all'
 296
 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
 304
 305        if [ -z "$bisect_rev" ]; then
 306                echo "$bad was both good and bad"
 307                exit 1
 308        fi
 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
 314        fi
 315
 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"
 319
 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"
 325}
 326
 327bisect_visualize() {
 328        bisect_next_check fail
 329
 330        if test $# = 0
 331        then
 332                case "${DISPLAY+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
 342        fi
 343
 344        not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*")
 345        eval '"$@"' refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
 346}
 347
 348bisect_reset() {
 349        test -f "$GIT_DIR/BISECT_NAMES" || {
 350                echo "We are not bisecting."
 351                return
 352        }
 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" ;;
 362        *)
 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
 370        fi
 371}
 372
 373bisect_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
 377        do
 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"
 383}
 384
 385bisect_replay () {
 386        test -r "$1" || die "cannot read $1 for replaying"
 387        bisect_reset
 388        while read bisect command rev
 389        do
 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" ;;
 397                *)
 398                        die "?? what are you talking about?" ;;
 399                esac
 400        done <"$1"
 401        bisect_auto_next
 402}
 403
 404bisect_run () {
 405    bisect_next_check fail
 406
 407    while true
 408    do
 409      echo "running $@"
 410      "$@"
 411      res=$?
 412
 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
 418      fi
 419
 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'
 428      fi
 429
 430      # We have to use a subshell because "bisect_state" can exit.
 431      ( bisect_state $state > "$GIT_DIR/BISECT_RUN" )
 432      res=$?
 433
 434      cat "$GIT_DIR/BISECT_RUN"
 435
 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
 440      fi
 441
 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
 446      fi
 447
 448      if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
 449          echo "bisect run success"
 450          exit 0;
 451      fi
 452
 453    done
 454}
 455
 456
 457case "$#" in
 4580)
 459    usage ;;
 460*)
 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 "$@" ;;
 481    *)
 482        usage ;;
 483    esac
 484esac