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