git-bisect.shon commit Bisect reset: do nothing when not bisecting. (fce0499)
   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
  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 -f "$GIT_DIR/BISECT_NAMES" || {
  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#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
  86        #
  87        # Check for one bad and then some good revisions.
  88        #
  89        has_double_dash=0
  90        for arg; do
  91            case "$arg" in --) has_double_dash=1; break ;; esac
  92        done
  93        orig_args=$(sq "$@")
  94        bad_seen=0
  95        while [ $# -gt 0 ]; do
  96            arg="$1"
  97            case "$arg" in
  98            --)
  99                shift
 100                break
 101                ;;
 102            *)
 103                rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || {
 104                    test $has_double_dash -eq 1 &&
 105                        die "'$arg' does not appear to be a valid revision"
 106                    break
 107                }
 108                case $bad_seen in
 109                0) state='bad' ; bad_seen=1 ;;
 110                *) state='good' ;;
 111                esac
 112                bisect_write "$state" "$rev" 'nolog'
 113                shift
 114                ;;
 115            esac
 116        done
 117
 118        sq "$@" >"$GIT_DIR/BISECT_NAMES"
 119        echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG"
 120        bisect_auto_next
 121}
 122
 123bisect_write() {
 124        state="$1"
 125        rev="$2"
 126        nolog="$3"
 127        case "$state" in
 128                bad)            tag="$state" ;;
 129                good|skip)      tag="$state"-"$rev" ;;
 130                *)              die "Bad bisect_write argument: $state" ;;
 131        esac
 132        git update-ref "refs/bisect/$tag" "$rev"
 133        echo "# $state: "$(git show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
 134        test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
 135}
 136
 137bisect_state() {
 138        bisect_autostart
 139        state=$1
 140        case "$#,$state" in
 141        0,*)
 142                die "Please call 'bisect_state' with at least one argument." ;;
 143        1,bad|1,good|1,skip)
 144                rev=$(git rev-parse --verify HEAD) ||
 145                        die "Bad rev input: HEAD"
 146                bisect_write "$state" "$rev" ;;
 147        2,bad)
 148                rev=$(git rev-parse --verify "$2^{commit}") ||
 149                        die "Bad rev input: $2"
 150                bisect_write "$state" "$rev" ;;
 151        *,good|*,skip)
 152                shift
 153                revs=$(git rev-parse --revs-only --no-flags "$@") &&
 154                        test '' != "$revs" || die "Bad rev input: $@"
 155                for rev in $revs
 156                do
 157                        rev=$(git rev-parse --verify "$rev^{commit}") ||
 158                                die "Bad rev commit: $rev^{commit}"
 159                        bisect_write "$state" "$rev"
 160                done ;;
 161        *)
 162                usage ;;
 163        esac
 164        bisect_auto_next
 165}
 166
 167bisect_next_check() {
 168        missing_good= missing_bad=
 169        git show-ref -q --verify refs/bisect/bad || missing_bad=t
 170        test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t
 171
 172        case "$missing_good,$missing_bad,$1" in
 173        ,,*)
 174                : have both good and bad - ok
 175                ;;
 176        *,)
 177                # do not have both but not asked to fail - just report.
 178                false
 179                ;;
 180        t,,good)
 181                # have bad but not good.  we could bisect although
 182                # this is less optimum.
 183                echo >&2 'Warning: bisecting only with a bad commit.'
 184                if test -t 0
 185                then
 186                        printf >&2 'Are you sure [Y/n]? '
 187                        case "$(read yesno)" in [Nn]*) exit 1 ;; esac
 188                fi
 189                : bisect without good...
 190                ;;
 191        *)
 192                THEN=''
 193                test -f "$GIT_DIR/BISECT_NAMES" || {
 194                        echo >&2 'You need to start by "git bisect start".'
 195                        THEN='then '
 196                }
 197                echo >&2 'You '$THEN'need to give me at least one good' \
 198                        'and one bad revisions.'
 199                echo >&2 '(You can use "git bisect bad" and' \
 200                        '"git bisect good" for that.)'
 201                exit 1 ;;
 202        esac
 203}
 204
 205bisect_auto_next() {
 206        bisect_next_check && bisect_next || :
 207}
 208
 209filter_skipped() {
 210        _eval="$1"
 211        _skip="$2"
 212
 213        if [ -z "$_skip" ]; then
 214                eval $_eval
 215                return
 216        fi
 217
 218        # Let's parse the output of:
 219        # "git rev-list --bisect-vars --bisect-all ..."
 220        eval $_eval | while read hash line
 221        do
 222                case "$VARS,$FOUND,$TRIED,$hash" in
 223                        # We display some vars.
 224                        1,*,*,*) echo "$hash $line" ;;
 225
 226                        # Split line.
 227                        ,*,*,---*) ;;
 228
 229                        # We had nothing to search.
 230                        ,,,bisect_rev*)
 231                                echo "bisect_rev="
 232                                VARS=1
 233                                ;;
 234
 235                        # We did not find a good bisect rev.
 236                        # This should happen only if the "bad"
 237                        # commit is also a "skip" commit.
 238                        ,,*,bisect_rev*)
 239                                echo "bisect_rev=$TRIED"
 240                                VARS=1
 241                                ;;
 242
 243                        # We are searching.
 244                        ,,*,*)
 245                                TRIED="${TRIED:+$TRIED|}$hash"
 246                                case "$_skip" in
 247                                *$hash*) ;;
 248                                *)
 249                                        echo "bisect_rev=$hash"
 250                                        echo "bisect_tried=\"$TRIED\""
 251                                        FOUND=1
 252                                        ;;
 253                                esac
 254                                ;;
 255
 256                        # We have already found a rev to be tested.
 257                        ,1,*,bisect_rev*) VARS=1 ;;
 258                        ,1,*,*) ;;
 259
 260                        # ???
 261                        *) die "filter_skipped error " \
 262                            "VARS: '$VARS' " \
 263                            "FOUND: '$FOUND' " \
 264                            "TRIED: '$TRIED' " \
 265                            "hash: '$hash' " \
 266                            "line: '$line'"
 267                        ;;
 268                esac
 269        done
 270}
 271
 272exit_if_skipped_commits () {
 273        _tried=$1
 274        if expr "$_tried" : ".*[|].*" > /dev/null ; then
 275                echo "There are only 'skip'ped commit left to test."
 276                echo "The first bad commit could be any of:"
 277                echo "$_tried" | tr '[|]' '[\012]'
 278                echo "We cannot bisect more!"
 279                exit 2
 280        fi
 281}
 282
 283bisect_next() {
 284        case "$#" in 0) ;; *) usage ;; esac
 285        bisect_autostart
 286        bisect_next_check good
 287
 288        skip=$(git for-each-ref --format='%(objectname)' \
 289                "refs/bisect/skip-*" | tr '[\012]' ' ') || exit
 290
 291        BISECT_OPT=''
 292        test -n "$skip" && BISECT_OPT='--bisect-all'
 293
 294        bad=$(git rev-parse --verify refs/bisect/bad) &&
 295        good=$(git for-each-ref --format='^%(objectname)' \
 296                "refs/bisect/good-*" | tr '[\012]' ' ') &&
 297        eval="git rev-list --bisect-vars $BISECT_OPT $good $bad --" &&
 298        eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" &&
 299        eval=$(filter_skipped "$eval" "$skip") &&
 300        eval "$eval" || exit
 301
 302        if [ -z "$bisect_rev" ]; then
 303                echo "$bad was both good and bad"
 304                exit 1
 305        fi
 306        if [ "$bisect_rev" = "$bad" ]; then
 307                exit_if_skipped_commits "$bisect_tried"
 308                echo "$bisect_rev is first bad commit"
 309                git diff-tree --pretty $bisect_rev
 310                exit 0
 311        fi
 312
 313        # We should exit here only if the "bad"
 314        # commit is also a "skip" commit (see above).
 315        exit_if_skipped_commits "$bisect_rev"
 316
 317        echo "Bisecting: $bisect_nr revisions left to test after this"
 318        git branch -f new-bisect "$bisect_rev"
 319        git checkout -q new-bisect || exit
 320        git branch -M new-bisect bisect
 321        git show-branch "$bisect_rev"
 322}
 323
 324bisect_visualize() {
 325        bisect_next_check fail
 326        not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*")
 327        eval gitk refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
 328}
 329
 330bisect_reset() {
 331        test -f "$GIT_DIR/BISECT_NAMES" || {
 332                echo "We are not bisecting."
 333                return
 334        }
 335        case "$#" in
 336        0) if [ -s "$GIT_DIR/head-name" ]; then
 337               branch=`cat "$GIT_DIR/head-name"`
 338           else
 339               branch=master
 340           fi ;;
 341        1) git show-ref --verify --quiet -- "refs/heads/$1" ||
 342               die "$1 does not seem to be a valid branch"
 343           branch="$1" ;;
 344        *)
 345            usage ;;
 346        esac
 347        if git checkout "$branch"; then
 348                rm -f "$GIT_DIR/head-name"
 349                bisect_clean_state
 350        fi
 351}
 352
 353bisect_clean_state() {
 354        # There may be some refs packed during bisection.
 355        git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* refs/heads/bisect |
 356        while read ref hash
 357        do
 358                git update-ref -d $ref $hash
 359        done
 360        rm -f "$GIT_DIR/BISECT_LOG"
 361        rm -f "$GIT_DIR/BISECT_NAMES"
 362        rm -f "$GIT_DIR/BISECT_RUN"
 363}
 364
 365bisect_replay () {
 366        test -r "$1" || die "cannot read $1 for replaying"
 367        bisect_reset
 368        while read bisect command rev
 369        do
 370                test "$bisect" = "git-bisect" || continue
 371                case "$command" in
 372                start)
 373                        cmd="bisect_start $rev"
 374                        eval "$cmd" ;;
 375                good|bad|skip)
 376                        bisect_write "$command" "$rev" ;;
 377                *)
 378                        die "?? what are you talking about?" ;;
 379                esac
 380        done <"$1"
 381        bisect_auto_next
 382}
 383
 384bisect_run () {
 385    bisect_next_check fail
 386
 387    while true
 388    do
 389      echo "running $@"
 390      "$@"
 391      res=$?
 392
 393      # Check for really bad run error.
 394      if [ $res -lt 0 -o $res -ge 128 ]; then
 395          echo >&2 "bisect run failed:"
 396          echo >&2 "exit code $res from '$@' is < 0 or >= 128"
 397          exit $res
 398      fi
 399
 400      # Find current state depending on run success or failure.
 401      # A special exit code of 125 means cannot test.
 402      if [ $res -eq 125 ]; then
 403          state='skip'
 404      elif [ $res -gt 0 ]; then
 405          state='bad'
 406      else
 407          state='good'
 408      fi
 409
 410      # We have to use a subshell because "bisect_state" can exit.
 411      ( bisect_state $state > "$GIT_DIR/BISECT_RUN" )
 412      res=$?
 413
 414      cat "$GIT_DIR/BISECT_RUN"
 415
 416      if grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \
 417                > /dev/null; then
 418          echo >&2 "bisect run cannot continue any more"
 419          exit $res
 420      fi
 421
 422      if [ $res -ne 0 ]; then
 423          echo >&2 "bisect run failed:"
 424          echo >&2 "'bisect_state $state' exited with error code $res"
 425          exit $res
 426      fi
 427
 428      if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
 429          echo "bisect run success"
 430          exit 0;
 431      fi
 432
 433    done
 434}
 435
 436
 437case "$#" in
 4380)
 439    usage ;;
 440*)
 441    cmd="$1"
 442    shift
 443    case "$cmd" in
 444    start)
 445        bisect_start "$@" ;;
 446    bad|good|skip)
 447        bisect_state "$cmd" "$@" ;;
 448    next)
 449        # Not sure we want "next" at the UI level anymore.
 450        bisect_next "$@" ;;
 451    visualize)
 452        bisect_visualize "$@" ;;
 453    reset)
 454        bisect_reset "$@" ;;
 455    replay)
 456        bisect_replay "$@" ;;
 457    log)
 458        cat "$GIT_DIR/BISECT_LOG" ;;
 459    run)
 460        bisect_run "$@" ;;
 461    *)
 462        usage ;;
 463    esac
 464esac