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