git-bisect.shon commit GIT 1.5.5.3 (28bc302)
   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 -q HEAD) ||
  66        head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
  67        die "Bad HEAD - I need a HEAD"
  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
  77        start_head=''
  78        case "$head" in
  79        refs/heads/bisect)
  80                branch=`cat "$GIT_DIR/BISECT_START"`
  81                git checkout $branch || exit
  82                ;;
  83        refs/heads/*|$_x40)
  84                # This error message should only be triggered by cogito usage,
  85                # and cogito users should understand it relates to cg-seek.
  86                [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
  87                start_head="${head#refs/heads/}"
  88                ;;
  89        *)
  90                die "Bad HEAD - strange symbolic ref"
  91                ;;
  92        esac
  93
  94        #
  95        # Get rid of any old bisect state
  96        #
  97        bisect_clean_state
  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
 108        eval=''
 109        while [ $# -gt 0 ]; do
 110            arg="$1"
 111            case "$arg" in
 112            --)
 113                shift
 114                break
 115                ;;
 116            *)
 117                rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || {
 118                    test $has_double_dash -eq 1 &&
 119                        die "'$arg' does not appear to be a valid revision"
 120                    break
 121                }
 122                case $bad_seen in
 123                0) state='bad' ; bad_seen=1 ;;
 124                *) state='good' ;;
 125                esac
 126                eval="$eval bisect_write '$state' '$rev' 'nolog'; "
 127                shift
 128                ;;
 129            esac
 130        done
 131
 132        sq "$@" >"$GIT_DIR/BISECT_NAMES"
 133        test -n "$start_head" && echo "$start_head" >"$GIT_DIR/BISECT_START"
 134        eval "$eval"
 135        echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG"
 136        bisect_auto_next
 137}
 138
 139bisect_write() {
 140        state="$1"
 141        rev="$2"
 142        nolog="$3"
 143        case "$state" in
 144                bad)            tag="$state" ;;
 145                good|skip)      tag="$state"-"$rev" ;;
 146                *)              die "Bad bisect_write argument: $state" ;;
 147        esac
 148        git update-ref "refs/bisect/$tag" "$rev"
 149        echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG"
 150        test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
 151}
 152
 153bisect_state() {
 154        bisect_autostart
 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" ;;
 163        2,bad|*,good|*,skip)
 164                shift
 165                eval=''
 166                for rev in "$@"
 167                do
 168                        sha=$(git rev-parse --verify "$rev^{commit}") ||
 169                                die "Bad rev input: $rev"
 170                        eval="$eval bisect_write '$state' '$sha'; "
 171                done
 172                eval "$eval" ;;
 173        *,bad)
 174                die "'git bisect bad' can take only one argument." ;;
 175        *)
 176                usage ;;
 177        esac
 178        bisect_auto_next
 179}
 180
 181bisect_next_check() {
 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
 185
 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                ;;
 205        *)
 206                THEN=''
 207                test -f "$GIT_DIR/BISECT_NAMES" || {
 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 ;;
 216        esac
 217}
 218
 219bisect_auto_next() {
 220        bisect_next_check && bisect_next || :
 221}
 222
 223eval_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
 238filter_skipped() {
 239        _eval="$1"
 240        _skip="$2"
 241
 242        if [ -z "$_skip" ]; then
 243                eval_rev_list "$_eval"
 244                return
 245        fi
 246
 247        # Let's parse the output of:
 248        # "git rev-list --bisect-vars --bisect-all ..."
 249        eval_rev_list "$_eval" | while read hash line
 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
 301exit_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:"
 306                echo "$_tried" | tr '[|]' '[\012]'
 307                echo "We cannot bisect more!"
 308                exit 2
 309        fi
 310}
 311
 312bisect_next() {
 313        case "$#" in 0) ;; *) usage ;; esac
 314        bisect_autostart
 315        bisect_next_check good
 316
 317        skip=$(git for-each-ref --format='%(objectname)' \
 318                "refs/bisect/skip-*" | tr '\012' ' ') || exit
 319
 320        BISECT_OPT=''
 321        test -n "$skip" && BISECT_OPT='--bisect-all'
 322
 323        bad=$(git rev-parse --verify refs/bisect/bad) &&
 324        good=$(git for-each-ref --format='^%(objectname)' \
 325                "refs/bisect/good-*" | tr '\012' ' ') &&
 326        eval="git rev-list --bisect-vars $BISECT_OPT $good $bad --" &&
 327        eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" &&
 328        eval=$(filter_skipped "$eval" "$skip") &&
 329        eval "$eval" || exit
 330
 331        if [ -z "$bisect_rev" ]; then
 332                echo "$bad was both good and bad"
 333                exit 1
 334        fi
 335        if [ "$bisect_rev" = "$bad" ]; then
 336                exit_if_skipped_commits "$bisect_tried"
 337                echo "$bisect_rev is first bad commit"
 338                git diff-tree --pretty $bisect_rev
 339                exit 0
 340        fi
 341
 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
 346        echo "Bisecting: $bisect_nr revisions left to test after this"
 347        git branch -D new-bisect 2> /dev/null
 348        git checkout -q -b new-bisect "$bisect_rev" || exit
 349        git branch -M new-bisect bisect
 350        git show-branch "$bisect_rev"
 351}
 352
 353bisect_visualize() {
 354        bisect_next_check fail
 355
 356        if test $# = 0
 357        then
 358                case "${DISPLAY+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" in
 359                '')     set git log ;;
 360                set*)   set gitk ;;
 361                esac
 362        else
 363                case "$1" in
 364                git*|tig) ;;
 365                -*)     set git log "$@" ;;
 366                *)      set git "$@" ;;
 367                esac
 368        fi
 369
 370        not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*")
 371        eval '"$@"' refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
 372}
 373
 374bisect_reset() {
 375        test -f "$GIT_DIR/BISECT_NAMES" || {
 376                echo "We are not bisecting."
 377                return
 378        }
 379        case "$#" in
 380        0) if [ -s "$GIT_DIR/BISECT_START" ]; then
 381               branch=`cat "$GIT_DIR/BISECT_START"`
 382           else
 383               branch=master
 384           fi ;;
 385        1) git show-ref --verify --quiet -- "refs/heads/$1" ||
 386               die "$1 does not seem to be a valid branch"
 387           branch="$1" ;;
 388        *)
 389            usage ;;
 390        esac
 391        if git checkout "$branch"; then
 392                # Cleanup head-name if it got left by an old version of git-bisect
 393                rm -f "$GIT_DIR/head-name"
 394                rm -f "$GIT_DIR/BISECT_START"
 395                bisect_clean_state
 396        fi
 397}
 398
 399bisect_clean_state() {
 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
 406        rm -f "$GIT_DIR/BISECT_LOG"
 407        rm -f "$GIT_DIR/BISECT_NAMES"
 408        rm -f "$GIT_DIR/BISECT_RUN"
 409}
 410
 411bisect_replay () {
 412        test -r "$1" || die "cannot read $1 for replaying"
 413        bisect_reset
 414        while read bisect command rev
 415        do
 416                test "$bisect" = "git-bisect" || continue
 417                case "$command" in
 418                start)
 419                        cmd="bisect_start $rev"
 420                        eval "$cmd" ;;
 421                good|bad|skip)
 422                        bisect_write "$command" "$rev" ;;
 423                *)
 424                        die "?? what are you talking about?" ;;
 425                esac
 426        done <"$1"
 427        bisect_auto_next
 428}
 429
 430bisect_run () {
 431    bisect_next_check fail
 432
 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
 446      # Find current state depending on run success or failure.
 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
 451          state='bad'
 452      else
 453          state='good'
 454      fi
 455
 456      # We have to use a subshell because "bisect_state" can exit.
 457      ( bisect_state $state > "$GIT_DIR/BISECT_RUN" )
 458      res=$?
 459
 460      cat "$GIT_DIR/BISECT_RUN"
 461
 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
 468      if [ $res -ne 0 ]; then
 469          echo >&2 "bisect run failed:"
 470          echo >&2 "'bisect_state $state' exited with error code $res"
 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
 483case "$#" in
 4840)
 485    usage ;;
 486*)
 487    cmd="$1"
 488    shift
 489    case "$cmd" in
 490    start)
 491        bisect_start "$@" ;;
 492    bad|good|skip)
 493        bisect_state "$cmd" "$@" ;;
 494    next)
 495        # Not sure we want "next" at the UI level anymore.
 496        bisect_next "$@" ;;
 497    visualize|view)
 498        bisect_visualize "$@" ;;
 499    reset)
 500        bisect_reset "$@" ;;
 501    replay)
 502        bisect_replay "$@" ;;
 503    log)
 504        cat "$GIT_DIR/BISECT_LOG" ;;
 505    run)
 506        bisect_run "$@" ;;
 507    *)
 508        usage ;;
 509    esac
 510esac