Merge branch 'maint'
[git/git.git] / git-bisect.sh
CommitLineData
8cc6a083 1#!/bin/sh
d025524d
FK
2
3USAGE='[start|bad|good|next|reset|visualize]'
4LONG_USAGE='git bisect start [<pathspec>] reset bisect state and start bisection.
5git bisect bad [<rev>] mark <rev> a known-bad revision.
6git bisect good [<rev>...] mark <rev>... known-good revisions.
7git bisect next find next bisection to test and check it out.
8git bisect reset [<branch>] finish bisection search and go back to branch.
9git bisect visualize show bisect status in gitk.
10git bisect replay <logfile> replay bisection log
11git bisect log show bisect log.'
12
ae2b0f15 13. git-sh-setup
8cc6a083 14
e9a45d75 15sq() {
d9bffc08 16 @@PERL@@ -e '
e9a45d75
JH
17 for (@ARGV) {
18 s/'\''/'\'\\\\\'\''/g;
19 print " '\''$_'\''";
20 }
21 print "\n";
22 ' "$@"
23}
24
8cc6a083
LT
25bisect_autostart() {
26 test -d "$GIT_DIR/refs/bisect" || {
27 echo >&2 'You need to start by "git bisect start"'
28 if test -t 0
29 then
30 echo >&2 -n 'Do you want me to do it for you [Y/n]? '
31 read yesno
32 case "$yesno" in
33 [Nn]*)
34 exit ;;
35 esac
36 bisect_start
37 else
38 exit 1
39 fi
40 }
41}
42
43bisect_start() {
8cc6a083
LT
44 #
45 # Verify HEAD. If we were bisecting before this, reset to the
46 # top-of-line master first!
47 #
8098a178
JH
48 head=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD) ||
49 die "Bad HEAD - I need a symbolic ref"
8cc6a083
LT
50 case "$head" in
51 refs/heads/bisect*)
810255fd
PB
52 if [ -s "$GIT_DIR/head-name" ]; then
53 branch=`cat "$GIT_DIR/head-name"`
54 else
55 branch=master
56 fi
57 git checkout $branch || exit
8cc6a083
LT
58 ;;
59 refs/heads/*)
810255fd
PB
60 [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
61 echo "$head" | sed 's#^refs/heads/##' >"$GIT_DIR/head-name"
8cc6a083
LT
62 ;;
63 *)
8098a178 64 die "Bad HEAD - strange symbolic ref"
8cc6a083
LT
65 ;;
66 esac
67
68 #
69 # Get rid of any old bisect state
70 #
71 rm -f "$GIT_DIR/refs/heads/bisect"
72 rm -rf "$GIT_DIR/refs/bisect/"
73 mkdir "$GIT_DIR/refs/bisect"
e9a45d75 74 {
9754563c 75 printf "git-bisect start"
e9a45d75
JH
76 sq "$@"
77 } >"$GIT_DIR/BISECT_LOG"
78 sq "$@" >"$GIT_DIR/BISECT_NAMES"
8cc6a083
LT
79}
80
81bisect_bad() {
82 bisect_autostart
cc9f24d0
JH
83 case "$#" in
84 0)
85 rev=$(git-rev-parse --verify HEAD) ;;
86 1)
87 rev=$(git-rev-parse --verify "$1") ;;
88 *)
89 usage ;;
90 esac || exit
e204de28
JH
91 echo "$rev" >"$GIT_DIR/refs/bisect/bad"
92 echo "# bad: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
93 echo "git-bisect bad $rev" >>"$GIT_DIR/BISECT_LOG"
8cc6a083
LT
94 bisect_auto_next
95}
96
97bisect_good() {
98 bisect_autostart
99 case "$#" in
100 0) revs=$(git-rev-parse --verify HEAD) || exit ;;
cc9f24d0
JH
101 *) revs=$(git-rev-parse --revs-only --no-flags "$@") &&
102 test '' != "$revs" || die "Bad rev input: $@" ;;
8cc6a083
LT
103 esac
104 for rev in $revs
105 do
cc9f24d0
JH
106 rev=$(git-rev-parse --verify "$rev") || exit
107 echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev"
e204de28
JH
108 echo "# good: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
109 echo "git-bisect good $rev" >>"$GIT_DIR/BISECT_LOG"
8cc6a083
LT
110 done
111 bisect_auto_next
112}
113
114bisect_next_check() {
115 next_ok=no
116 test -f "$GIT_DIR/refs/bisect/bad" &&
117 case "$(cd "$GIT_DIR" && echo refs/bisect/good-*)" in
118 refs/bisect/good-\*) ;;
119 *) next_ok=yes ;;
120 esac
121 case "$next_ok,$1" in
122 no,) false ;;
123 no,fail)
124 echo >&2 'You need to give me at least one good and one bad revisions.'
125 exit 1 ;;
126 *)
127 true ;;
128 esac
129}
130
131bisect_auto_next() {
434d036f 132 bisect_next_check && bisect_next || :
8cc6a083
LT
133}
134
135bisect_next() {
136 case "$#" in 0) ;; *) usage ;; esac
137 bisect_autostart
138 bisect_next_check fail
139 bad=$(git-rev-parse --verify refs/bisect/bad) &&
140 good=$(git-rev-parse --sq --revs-only --not \
141 $(cd "$GIT_DIR" && ls refs/bisect/good-*)) &&
b3cfd939 142 rev=$(eval "git-rev-list --bisect $good $bad -- $(cat $GIT_DIR/BISECT_NAMES)") || exit
670f5fe3
LT
143 if [ -z "$rev" ]; then
144 echo "$bad was both good and bad"
145 exit 1
146 fi
147 if [ "$rev" = "$bad" ]; then
7f47e72f
JH
148 echo "$rev is first bad commit"
149 git-diff-tree --pretty $rev
8cc6a083
LT
150 exit 0
151 fi
b3cfd939 152 nr=$(eval "git-rev-list $rev $good -- $(cat $GIT_DIR/BISECT_NAMES)" | wc -l) || exit
8cc6a083
LT
153 echo "Bisecting: $nr revisions left to test after this"
154 echo "$rev" > "$GIT_DIR/refs/heads/new-bisect"
155 git checkout new-bisect || exit
156 mv "$GIT_DIR/refs/heads/new-bisect" "$GIT_DIR/refs/heads/bisect" &&
8098a178 157 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD refs/heads/bisect
e204de28 158 git-show-branch "$rev"
8cc6a083
LT
159}
160
cc9f24d0
JH
161bisect_visualize() {
162 bisect_next_check fail
e9a45d75
JH
163 not=`cd "$GIT_DIR/refs" && echo bisect/good-*`
164 eval gitk bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
cc9f24d0
JH
165}
166
8cc6a083
LT
167bisect_reset() {
168 case "$#" in
810255fd
PB
169 0) if [ -s "$GIT_DIR/head-name" ]; then
170 branch=`cat "$GIT_DIR/head-name"`
171 else
172 branch=master
173 fi ;;
8cc6a083
LT
174 1) test -f "$GIT_DIR/refs/heads/$1" || {
175 echo >&2 "$1 does not seem to be a valid branch"
176 exit 1
177 }
178 branch="$1" ;;
179 *)
180 usage ;;
181 esac
182 git checkout "$branch" &&
183 rm -fr "$GIT_DIR/refs/bisect"
810255fd 184 rm -f "$GIT_DIR/refs/heads/bisect" "$GIT_DIR/head-name"
e204de28 185 rm -f "$GIT_DIR/BISECT_LOG"
4631c003 186 rm -f "$GIT_DIR/BISECT_NAMES"
e204de28
JH
187}
188
189bisect_replay () {
190 test -r "$1" || {
191 echo >&2 "cannot read $1 for replaying"
192 exit 1
193 }
194 bisect_reset
195 while read bisect command rev
196 do
197 test "$bisect" = "git-bisect" || continue
198 case "$command" in
199 start)
e9a45d75
JH
200 cmd="bisect_start $rev"
201 eval "$cmd"
e204de28
JH
202 ;;
203 good)
204 echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev"
205 echo "# good: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
206 echo "git-bisect good $rev" >>"$GIT_DIR/BISECT_LOG"
207 ;;
208 bad)
209 echo "$rev" >"$GIT_DIR/refs/bisect/bad"
210 echo "# bad: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
211 echo "git-bisect bad $rev" >>"$GIT_DIR/BISECT_LOG"
212 ;;
213 *)
214 echo >&2 "?? what are you talking about?"
215 exit 1 ;;
216 esac
217 done <"$1"
218 bisect_auto_next
8cc6a083
LT
219}
220
221case "$#" in
2220)
223 usage ;;
224*)
225 cmd="$1"
226 shift
227 case "$cmd" in
228 start)
229 bisect_start "$@" ;;
230 bad)
231 bisect_bad "$@" ;;
232 good)
233 bisect_good "$@" ;;
234 next)
235 # Not sure we want "next" at the UI level anymore.
236 bisect_next "$@" ;;
cc9f24d0
JH
237 visualize)
238 bisect_visualize "$@" ;;
8cc6a083
LT
239 reset)
240 bisect_reset "$@" ;;
e204de28
JH
241 replay)
242 bisect_replay "$@" ;;
243 log)
244 cat "$GIT_DIR/BISECT_LOG" ;;
8cc6a083
LT
245 *)
246 usage ;;
247 esac
248esac