]> Pileus Git - ~andy/git/blobdiff - git-bisect.sh
wt-status.h: declare global variables as extern
[~andy/git] / git-bisect.sh
index 4b74a7bb421e35627bf749316027707237210e7a..b42f94cd39ef087313134361b5c8752f82c0e762 100755 (executable)
@@ -22,9 +22,13 @@ git bisect log
 git bisect run <cmd>...
         use <cmd>... to automatically bisect.'
 
+OPTIONS_SPEC=
 . git-sh-setup
 require_work_tree
 
+_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
+_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
+
 sq() {
        @@PERL@@ -e '
                for (@ARGV) {
@@ -36,7 +40,7 @@ sq() {
 }
 
 bisect_autostart() {
-       test -d "$GIT_DIR/refs/bisect" || {
+       test -f "$GIT_DIR/BISECT_NAMES" || {
                echo >&2 'You need to start by "git bisect start"'
                if test -t 0
                then
@@ -58,20 +62,24 @@ bisect_start() {
        # Verify HEAD. If we were bisecting before this, reset to the
        # top-of-line master first!
        #
-       head=$(GIT_DIR="$GIT_DIR" git symbolic-ref HEAD) ||
-       die "Bad HEAD - I need a symbolic ref"
+       head=$(GIT_DIR="$GIT_DIR" git symbolic-ref -q HEAD) ||
+       head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
+       die "Bad HEAD - I need a HEAD"
+       start_head=''
        case "$head" in
        refs/heads/bisect)
-               if [ -s "$GIT_DIR/head-name" ]; then
-                   branch=`cat "$GIT_DIR/head-name"`
+               if [ -s "$GIT_DIR/BISECT_START" ]; then
+                   branch=`cat "$GIT_DIR/BISECT_START"`
                else
                    branch=master
                fi
                git checkout $branch || exit
                ;;
-       refs/heads/*)
+       refs/heads/*|$_x40)
+               # This error message should only be triggered by cogito usage,
+               # and cogito users should understand it relates to cg-seek.
                [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
-               echo "${head#refs/heads/}" >"$GIT_DIR/head-name"
+               start_head="${head#refs/heads/}"
                ;;
        *)
                die "Bad HEAD - strange symbolic ref"
@@ -82,7 +90,6 @@ bisect_start() {
        # Get rid of any old bisect state
        #
        bisect_clean_state
-       mkdir "$GIT_DIR/refs/bisect"
 
        #
        # Check for one bad and then some good revisions.
@@ -93,6 +100,7 @@ bisect_start() {
        done
        orig_args=$(sq "$@")
        bad_seen=0
+       eval=''
        while [ $# -gt 0 ]; do
            arg="$1"
            case "$arg" in
@@ -110,13 +118,15 @@ bisect_start() {
                0) state='bad' ; bad_seen=1 ;;
                *) state='good' ;;
                esac
-               bisect_write "$state" "$rev" 'nolog'
+               eval="$eval bisect_write '$state' '$rev' 'nolog'; "
                shift
                ;;
            esac
        done
 
        sq "$@" >"$GIT_DIR/BISECT_NAMES"
+       test -n "$start_head" && echo "$start_head" >"$GIT_DIR/BISECT_START"
+       eval "$eval"
        echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG"
        bisect_auto_next
 }
@@ -131,7 +141,7 @@ bisect_write() {
                *)              die "Bad bisect_write argument: $state" ;;
        esac
        git update-ref "refs/bisect/$tag" "$rev"
-       echo "# $state: "$(git show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
+       echo "# $state: $(git show-branch $rev)" >>"$GIT_DIR/BISECT_LOG"
        test -z "$nolog" && echo "git-bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
 }
 
@@ -145,20 +155,18 @@ bisect_state() {
                rev=$(git rev-parse --verify HEAD) ||
                        die "Bad rev input: HEAD"
                bisect_write "$state" "$rev" ;;
-       2,bad)
-               rev=$(git rev-parse --verify "$2^{commit}") ||
-                       die "Bad rev input: $2"
-               bisect_write "$state" "$rev" ;;
-       *,good|*,skip)
+       2,bad|*,good|*,skip)
                shift
-               revs=$(git rev-parse --revs-only --no-flags "$@") &&
-                       test '' != "$revs" || die "Bad rev input: $@"
-               for rev in $revs
+               eval=''
+               for rev in "$@"
                do
-                       rev=$(git rev-parse --verify "$rev^{commit}") ||
-                               die "Bad rev commit: $rev^{commit}"
-                       bisect_write "$state" "$rev"
-               done ;;
+                       sha=$(git rev-parse --verify "$rev^{commit}") ||
+                               die "Bad rev input: $rev"
+                       eval="$eval bisect_write '$state' '$sha'; "
+               done
+               eval "$eval" ;;
+       *,bad)
+               die "'git bisect bad' can take only one argument." ;;
        *)
                usage ;;
        esac
@@ -191,7 +199,7 @@ bisect_next_check() {
                ;;
        *)
                THEN=''
-               test -d "$GIT_DIR/refs/bisect" || {
+               test -f "$GIT_DIR/BISECT_NAMES" || {
                        echo >&2 'You need to start by "git bisect start".'
                        THEN='then '
                }
@@ -316,23 +324,41 @@ bisect_next() {
        exit_if_skipped_commits "$bisect_rev"
 
        echo "Bisecting: $bisect_nr revisions left to test after this"
-       echo "$bisect_rev" >"$GIT_DIR/refs/heads/new-bisect"
+       git branch -f new-bisect "$bisect_rev"
        git checkout -q new-bisect || exit
-       mv "$GIT_DIR/refs/heads/new-bisect" "$GIT_DIR/refs/heads/bisect" &&
-       GIT_DIR="$GIT_DIR" git symbolic-ref HEAD refs/heads/bisect
+       git branch -M new-bisect bisect
        git show-branch "$bisect_rev"
 }
 
 bisect_visualize() {
        bisect_next_check fail
-       not=`cd "$GIT_DIR/refs" && echo bisect/good-*`
-       eval gitk bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
+
+       if test $# = 0
+       then
+               case "${DISPLAY+set}" in
+               '')     set git log ;;
+               set)    set gitk ;;
+               esac
+       else
+               case "$1" in
+               git*|tig) ;;
+               -*)     set git log "$@" ;;
+               *)      set git "$@" ;;
+               esac
+       fi
+
+       not=$(git for-each-ref --format='%(refname)' "refs/bisect/good-*")
+       eval '"$@"' refs/bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
 }
 
 bisect_reset() {
+       test -f "$GIT_DIR/BISECT_NAMES" || {
+               echo "We are not bisecting."
+               return
+       }
        case "$#" in
-       0) if [ -s "$GIT_DIR/head-name" ]; then
-              branch=`cat "$GIT_DIR/head-name"`
+       0) if [ -s "$GIT_DIR/BISECT_START" ]; then
+              branch=`cat "$GIT_DIR/BISECT_START"`
           else
               branch=master
           fi ;;
@@ -343,14 +369,14 @@ bisect_reset() {
            usage ;;
        esac
        if git checkout "$branch"; then
+               # Cleanup head-name if it got left by an old version of git-bisect
                rm -f "$GIT_DIR/head-name"
+               rm -f "$GIT_DIR/BISECT_START"
                bisect_clean_state
        fi
 }
 
 bisect_clean_state() {
-       rm -fr "$GIT_DIR/refs/bisect"
-
        # There may be some refs packed during bisection.
        git for-each-ref --format='%(refname) %(objectname)' refs/bisect/\* refs/heads/bisect |
        while read ref hash
@@ -448,7 +474,7 @@ case "$#" in
     next)
         # Not sure we want "next" at the UI level anymore.
         bisect_next "$@" ;;
-    visualize)
+    visualize|view)
        bisect_visualize "$@" ;;
     reset)
         bisect_reset "$@" ;;