Merge with master.
[git/jrn.git] / git-bisect-script
blob29b25f4a4ce67a9d2532c617b93a9e5297cc4249
1 #!/bin/sh
2 . git-sh-setup-script || dir "Not a git archive"
4 usage() {
5 echo >&2 'usage: git bisect [start | bad | good | next | reset]
6 git bisect start reset bisect state and start bisection.
7 git bisect bad [<rev>] mark <rev> a known-bad revision.
8 git bisect good [<rev>...] mark <rev>... known-good revisions.
9 git bisect next find next bisection to test and check it out.
10 git bisect reset [<branch>] finish bisection search and go back to branch.'
11 exit 1
14 bisect_autostart() {
15 test -d "$GIT_DIR/refs/bisect" || {
16 echo >&2 'You need to start by "git bisect start"'
17 if test -t 0
18 then
19 echo >&2 -n 'Do you want me to do it for you [Y/n]? '
20 read yesno
21 case "$yesno" in
22 [Nn]*)
23 exit ;;
24 esac
25 bisect_start
26 else
27 exit 1
32 bisect_start() {
33 case "$#" in 0) ;; *) usage ;; esac
35 # Verify HEAD. If we were bisecting before this, reset to the
36 # top-of-line master first!
38 head=$(readlink $GIT_DIR/HEAD) || die "Bad HEAD - I need a symlink"
39 case "$head" in
40 refs/heads/bisect*)
41 git checkout master || exit
43 refs/heads/*)
46 die "Bad HEAD - strange symlink"
48 esac
51 # Get rid of any old bisect state
53 rm -f "$GIT_DIR/refs/heads/bisect"
54 rm -rf "$GIT_DIR/refs/bisect/"
55 mkdir "$GIT_DIR/refs/bisect"
58 bisect_bad() {
59 bisect_autostart
60 case "$#" in 0 | 1) ;; *) usage ;; esac
61 rev=$(git-rev-parse --revs-only --verify --default HEAD "$@") || exit
62 echo "$rev" > "$GIT_DIR/refs/bisect/bad"
63 bisect_auto_next
66 bisect_good() {
67 bisect_autostart
68 case "$#" in
69 0) revs=$(git-rev-parse --verify HEAD) || exit ;;
70 *) revs=$(git-rev-parse --revs-only "$@") || exit ;;
71 esac
72 for rev in $revs
74 echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev"
75 done
76 bisect_auto_next
79 bisect_next_check() {
80 next_ok=no
81 test -f "$GIT_DIR/refs/bisect/bad" &&
82 case "$(cd "$GIT_DIR" && echo refs/bisect/good-*)" in
83 refs/bisect/good-\*) ;;
84 *) next_ok=yes ;;
85 esac
86 case "$next_ok,$1" in
87 no,) false ;;
88 no,fail)
89 echo >&2 'You need to give me at least one good and one bad revisions.'
90 exit 1 ;;
92 true ;;
93 esac
96 bisect_auto_next() {
97 bisect_next_check && bisect_next
100 bisect_next() {
101 case "$#" in 0) ;; *) usage ;; esac
102 bisect_autostart
103 bisect_next_check fail
104 bad=$(git-rev-parse --verify refs/bisect/bad) &&
105 good=$(git-rev-parse --sq --revs-only --not \
106 $(cd "$GIT_DIR" && ls refs/bisect/good-*)) &&
107 rev=$(eval "git-rev-list --bisect $good $bad") || exit
108 nr=$(eval "git-rev-list $rev $good" | wc -l) || exit
109 if [ "$nr" -le "1" ]; then
110 echo "$rev is first bad commit"
111 git-diff-tree --pretty $rev
112 exit 0
114 echo "Bisecting: $nr revisions left to test after this"
115 echo "$rev" > "$GIT_DIR/refs/heads/new-bisect"
116 git checkout new-bisect || exit
117 mv "$GIT_DIR/refs/heads/new-bisect" "$GIT_DIR/refs/heads/bisect" &&
118 ln -sf refs/heads/bisect "$GIT_DIR/HEAD"
121 bisect_reset() {
122 case "$#" in
123 0) branch=master ;;
124 1) test -f "$GIT_DIR/refs/heads/$1" || {
125 echo >&2 "$1 does not seem to be a valid branch"
126 exit 1
128 branch="$1" ;;
130 usage ;;
131 esac
132 git checkout "$branch" &&
133 rm -fr "$GIT_DIR/refs/bisect"
134 rm -f "$GIT_DIR/refs/reads/bisect"
137 case "$#" in
139 usage ;;
141 cmd="$1"
142 shift
143 case "$cmd" in
144 start)
145 bisect_start "$@" ;;
146 bad)
147 bisect_bad "$@" ;;
148 good)
149 bisect_good "$@" ;;
150 next)
151 # Not sure we want "next" at the UI level anymore.
152 bisect_next "$@" ;;
153 reset)
154 bisect_reset "$@" ;;
156 usage ;;
157 esac
158 esac