]>
Commit | Line | Data |
---|---|---|
8cc6a083 | 1 | #!/bin/sh |
d025524d | 2 | |
a17c4101 | 3 | USAGE='[start|bad|good|next|reset|visualize|replay|log|run]' |
38a47fd6 CC |
4 | LONG_USAGE='git bisect start [<bad> [<good>...]] [--] [<pathspec>...] |
5 | reset bisect state and start bisection. | |
6 | git bisect bad [<rev>] | |
7 | mark <rev> a known-bad revision. | |
8 | git bisect good [<rev>...] | |
9 | mark <rev>... known-good revisions. | |
10 | git bisect next | |
11 | find next bisection to test and check it out. | |
12 | git bisect reset [<branch>] | |
13 | finish bisection search and go back to branch. | |
14 | git bisect visualize | |
15 | show bisect status in gitk. | |
16 | git bisect replay <logfile> | |
17 | replay bisection log. | |
18 | git bisect log | |
19 | show bisect log. | |
20 | git bisect run <cmd>... | |
21 | use <cmd>... to automatically bisect.' | |
d025524d | 22 | |
ae2b0f15 | 23 | . git-sh-setup |
4c550686 | 24 | require_work_tree |
8cc6a083 | 25 | |
e9a45d75 | 26 | sq() { |
d9bffc08 | 27 | @@PERL@@ -e ' |
e9a45d75 JH |
28 | for (@ARGV) { |
29 | s/'\''/'\'\\\\\'\''/g; | |
30 | print " '\''$_'\''"; | |
31 | } | |
32 | print "\n"; | |
33 | ' "$@" | |
34 | } | |
35 | ||
8cc6a083 LT |
36 | bisect_autostart() { |
37 | test -d "$GIT_DIR/refs/bisect" || { | |
38 | echo >&2 'You need to start by "git bisect start"' | |
39 | if test -t 0 | |
40 | then | |
41 | echo >&2 -n 'Do you want me to do it for you [Y/n]? ' | |
42 | read yesno | |
43 | case "$yesno" in | |
44 | [Nn]*) | |
45 | exit ;; | |
46 | esac | |
47 | bisect_start | |
48 | else | |
49 | exit 1 | |
50 | fi | |
51 | } | |
52 | } | |
53 | ||
54 | bisect_start() { | |
8cc6a083 LT |
55 | # |
56 | # Verify HEAD. If we were bisecting before this, reset to the | |
57 | # top-of-line master first! | |
58 | # | |
5be60078 | 59 | head=$(GIT_DIR="$GIT_DIR" git symbolic-ref HEAD) || |
8098a178 | 60 | die "Bad HEAD - I need a symbolic ref" |
8cc6a083 | 61 | case "$head" in |
673e5838 | 62 | refs/heads/bisect) |
810255fd PB |
63 | if [ -s "$GIT_DIR/head-name" ]; then |
64 | branch=`cat "$GIT_DIR/head-name"` | |
65 | else | |
66 | branch=master | |
8fe26f44 | 67 | fi |
810255fd | 68 | git checkout $branch || exit |
8cc6a083 LT |
69 | ;; |
70 | refs/heads/*) | |
810255fd PB |
71 | [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree" |
72 | echo "$head" | sed 's#^refs/heads/##' >"$GIT_DIR/head-name" | |
8cc6a083 LT |
73 | ;; |
74 | *) | |
8098a178 | 75 | die "Bad HEAD - strange symbolic ref" |
8cc6a083 LT |
76 | ;; |
77 | esac | |
78 | ||
79 | # | |
80 | # Get rid of any old bisect state | |
81 | # | |
38a47fd6 | 82 | bisect_clean_state |
8cc6a083 | 83 | mkdir "$GIT_DIR/refs/bisect" |
38a47fd6 CC |
84 | |
85 | # | |
86 | # Check for one bad and then some good revisions. | |
87 | # | |
88 | has_double_dash=0 | |
89 | for arg; do | |
90 | case "$arg" in --) has_double_dash=1; break ;; esac | |
91 | done | |
92 | orig_args=$(sq "$@") | |
93 | bad_seen=0 | |
94 | while [ $# -gt 0 ]; do | |
95 | arg="$1" | |
96 | case "$arg" in | |
97 | --) | |
8fe26f44 | 98 | shift |
38a47fd6 CC |
99 | break |
100 | ;; | |
101 | *) | |
8fe26f44 | 102 | rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || { |
38a47fd6 CC |
103 | test $has_double_dash -eq 1 && |
104 | die "'$arg' does not appear to be a valid revision" | |
105 | break | |
106 | } | |
107 | if [ $bad_seen -eq 0 ]; then | |
108 | bad_seen=1 | |
109 | bisect_write_bad "$rev" | |
110 | else | |
111 | bisect_write_good "$rev" | |
112 | fi | |
8fe26f44 | 113 | shift |
38a47fd6 CC |
114 | ;; |
115 | esac | |
8fe26f44 | 116 | done |
38a47fd6 CC |
117 | |
118 | sq "$@" >"$GIT_DIR/BISECT_NAMES" | |
b8652b4d | 119 | echo "git-bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" |
38a47fd6 | 120 | bisect_auto_next |
8cc6a083 LT |
121 | } |
122 | ||
123 | bisect_bad() { | |
124 | bisect_autostart | |
cc9f24d0 JH |
125 | case "$#" in |
126 | 0) | |
5be60078 | 127 | rev=$(git rev-parse --verify HEAD) ;; |
cc9f24d0 | 128 | 1) |
5be60078 | 129 | rev=$(git rev-parse --verify "$1^{commit}") ;; |
cc9f24d0 JH |
130 | *) |
131 | usage ;; | |
132 | esac || exit | |
38a47fd6 | 133 | bisect_write_bad "$rev" |
e204de28 | 134 | echo "git-bisect bad $rev" >>"$GIT_DIR/BISECT_LOG" |
8cc6a083 LT |
135 | bisect_auto_next |
136 | } | |
137 | ||
38a47fd6 CC |
138 | bisect_write_bad() { |
139 | rev="$1" | |
140 | echo "$rev" >"$GIT_DIR/refs/bisect/bad" | |
5be60078 | 141 | echo "# bad: "$(git show-branch $rev) >>"$GIT_DIR/BISECT_LOG" |
38a47fd6 CC |
142 | } |
143 | ||
8cc6a083 LT |
144 | bisect_good() { |
145 | bisect_autostart | |
8fe26f44 | 146 | case "$#" in |
5be60078 JH |
147 | 0) revs=$(git rev-parse --verify HEAD) || exit ;; |
148 | *) revs=$(git rev-parse --revs-only --no-flags "$@") && | |
cc9f24d0 | 149 | test '' != "$revs" || die "Bad rev input: $@" ;; |
8cc6a083 LT |
150 | esac |
151 | for rev in $revs | |
152 | do | |
5be60078 | 153 | rev=$(git rev-parse --verify "$rev^{commit}") || exit |
38a47fd6 | 154 | bisect_write_good "$rev" |
e204de28 | 155 | echo "git-bisect good $rev" >>"$GIT_DIR/BISECT_LOG" |
8cc6a083 LT |
156 | done |
157 | bisect_auto_next | |
158 | } | |
159 | ||
38a47fd6 CC |
160 | bisect_write_good() { |
161 | rev="$1" | |
162 | echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev" | |
5be60078 | 163 | echo "# good: "$(git show-branch $rev) >>"$GIT_DIR/BISECT_LOG" |
38a47fd6 CC |
164 | } |
165 | ||
8cc6a083 | 166 | bisect_next_check() { |
0a5280a9 JH |
167 | missing_good= missing_bad= |
168 | git show-ref -q --verify refs/bisect/bad || missing_bad=t | |
169 | test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t | |
6fecf191 | 170 | |
0a5280a9 JH |
171 | case "$missing_good,$missing_bad,$1" in |
172 | ,,*) | |
173 | : have both good and bad - ok | |
174 | ;; | |
175 | *,) | |
176 | # do not have both but not asked to fail - just report. | |
177 | false | |
178 | ;; | |
179 | t,,good) | |
180 | # have bad but not good. we could bisect although | |
181 | # this is less optimum. | |
182 | echo >&2 'Warning: bisecting only with a bad commit.' | |
183 | if test -t 0 | |
184 | then | |
185 | printf >&2 'Are you sure [Y/n]? ' | |
186 | case "$(read yesno)" in [Nn]*) exit 1 ;; esac | |
187 | fi | |
188 | : bisect without good... | |
189 | ;; | |
8cc6a083 | 190 | *) |
0a5280a9 JH |
191 | THEN='' |
192 | test -d "$GIT_DIR/refs/bisect" || { | |
193 | echo >&2 'You need to start by "git bisect start".' | |
194 | THEN='then ' | |
195 | } | |
196 | echo >&2 'You '$THEN'need to give me at least one good' \ | |
197 | 'and one bad revisions.' | |
198 | echo >&2 '(You can use "git bisect bad" and' \ | |
199 | '"git bisect good" for that.)' | |
200 | exit 1 ;; | |
8cc6a083 LT |
201 | esac |
202 | } | |
203 | ||
204 | bisect_auto_next() { | |
434d036f | 205 | bisect_next_check && bisect_next || : |
8cc6a083 LT |
206 | } |
207 | ||
208 | bisect_next() { | |
8fe26f44 | 209 | case "$#" in 0) ;; *) usage ;; esac |
8cc6a083 | 210 | bisect_autostart |
0a5280a9 JH |
211 | bisect_next_check good |
212 | ||
5be60078 | 213 | bad=$(git rev-parse --verify refs/bisect/bad) && |
0a5280a9 JH |
214 | good=$(git for-each-ref --format='^%(objectname)' \ |
215 | "refs/bisect/good-*" | tr '[\012]' ' ') && | |
5be60078 | 216 | eval="git rev-list --bisect-vars $good $bad --" && |
0a5280a9 JH |
217 | eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" && |
218 | eval=$(eval "$eval") && | |
219 | eval "$eval" || exit | |
220 | ||
221 | if [ -z "$bisect_rev" ]; then | |
222 | echo "$bad was both good and bad" | |
223 | exit 1 | |
670f5fe3 | 224 | fi |
0a5280a9 JH |
225 | if [ "$bisect_rev" = "$bad" ]; then |
226 | echo "$bisect_rev is first bad commit" | |
5be60078 | 227 | git diff-tree --pretty $bisect_rev |
0a5280a9 | 228 | exit 0 |
8cc6a083 | 229 | fi |
0a5280a9 JH |
230 | |
231 | echo "Bisecting: $bisect_nr revisions left to test after this" | |
232 | echo "$bisect_rev" >"$GIT_DIR/refs/heads/new-bisect" | |
08f16750 | 233 | git checkout -q new-bisect || exit |
8cc6a083 | 234 | mv "$GIT_DIR/refs/heads/new-bisect" "$GIT_DIR/refs/heads/bisect" && |
5be60078 JH |
235 | GIT_DIR="$GIT_DIR" git symbolic-ref HEAD refs/heads/bisect |
236 | git show-branch "$bisect_rev" | |
8cc6a083 LT |
237 | } |
238 | ||
cc9f24d0 JH |
239 | bisect_visualize() { |
240 | bisect_next_check fail | |
e9a45d75 JH |
241 | not=`cd "$GIT_DIR/refs" && echo bisect/good-*` |
242 | eval gitk bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES") | |
cc9f24d0 JH |
243 | } |
244 | ||
8cc6a083 LT |
245 | bisect_reset() { |
246 | case "$#" in | |
810255fd PB |
247 | 0) if [ -s "$GIT_DIR/head-name" ]; then |
248 | branch=`cat "$GIT_DIR/head-name"` | |
249 | else | |
250 | branch=master | |
251 | fi ;; | |
5be60078 | 252 | 1) git show-ref --verify --quiet -- "refs/heads/$1" || { |
8cc6a083 LT |
253 | echo >&2 "$1 does not seem to be a valid branch" |
254 | exit 1 | |
255 | } | |
256 | branch="$1" ;; | |
8fe26f44 | 257 | *) |
8cc6a083 LT |
258 | usage ;; |
259 | esac | |
9b709e47 | 260 | if git checkout "$branch"; then |
38a47fd6 CC |
261 | rm -f "$GIT_DIR/head-name" |
262 | bisect_clean_state | |
9b709e47 | 263 | fi |
e204de28 JH |
264 | } |
265 | ||
38a47fd6 CC |
266 | bisect_clean_state() { |
267 | rm -fr "$GIT_DIR/refs/bisect" | |
268 | rm -f "$GIT_DIR/refs/heads/bisect" | |
269 | rm -f "$GIT_DIR/BISECT_LOG" | |
270 | rm -f "$GIT_DIR/BISECT_NAMES" | |
271 | rm -f "$GIT_DIR/BISECT_RUN" | |
272 | } | |
273 | ||
e204de28 JH |
274 | bisect_replay () { |
275 | test -r "$1" || { | |
276 | echo >&2 "cannot read $1 for replaying" | |
277 | exit 1 | |
278 | } | |
279 | bisect_reset | |
280 | while read bisect command rev | |
281 | do | |
282 | test "$bisect" = "git-bisect" || continue | |
283 | case "$command" in | |
284 | start) | |
e9a45d75 JH |
285 | cmd="bisect_start $rev" |
286 | eval "$cmd" | |
e204de28 JH |
287 | ;; |
288 | good) | |
289 | echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev" | |
5be60078 | 290 | echo "# good: "$(git show-branch $rev) >>"$GIT_DIR/BISECT_LOG" |
e204de28 JH |
291 | echo "git-bisect good $rev" >>"$GIT_DIR/BISECT_LOG" |
292 | ;; | |
293 | bad) | |
294 | echo "$rev" >"$GIT_DIR/refs/bisect/bad" | |
5be60078 | 295 | echo "# bad: "$(git show-branch $rev) >>"$GIT_DIR/BISECT_LOG" |
e204de28 JH |
296 | echo "git-bisect bad $rev" >>"$GIT_DIR/BISECT_LOG" |
297 | ;; | |
298 | *) | |
299 | echo >&2 "?? what are you talking about?" | |
300 | exit 1 ;; | |
301 | esac | |
302 | done <"$1" | |
303 | bisect_auto_next | |
8cc6a083 LT |
304 | } |
305 | ||
a17c4101 | 306 | bisect_run () { |
83020120 CC |
307 | bisect_next_check fail |
308 | ||
a17c4101 CC |
309 | while true |
310 | do | |
311 | echo "running $@" | |
312 | "$@" | |
313 | res=$? | |
314 | ||
315 | # Check for really bad run error. | |
316 | if [ $res -lt 0 -o $res -ge 128 ]; then | |
317 | echo >&2 "bisect run failed:" | |
318 | echo >&2 "exit code $res from '$@' is < 0 or >= 128" | |
319 | exit $res | |
320 | fi | |
321 | ||
322 | # Use "bisect_good" or "bisect_bad" | |
323 | # depending on run success or failure. | |
324 | if [ $res -gt 0 ]; then | |
325 | next_bisect='bisect_bad' | |
326 | else | |
327 | next_bisect='bisect_good' | |
328 | fi | |
329 | ||
330 | # We have to use a subshell because bisect_good or | |
331 | # bisect_bad functions can exit. | |
332 | ( $next_bisect > "$GIT_DIR/BISECT_RUN" ) | |
333 | res=$? | |
334 | ||
335 | cat "$GIT_DIR/BISECT_RUN" | |
336 | ||
337 | if [ $res -ne 0 ]; then | |
338 | echo >&2 "bisect run failed:" | |
339 | echo >&2 "$next_bisect exited with error code $res" | |
340 | exit $res | |
341 | fi | |
342 | ||
343 | if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then | |
344 | echo "bisect run success" | |
345 | exit 0; | |
346 | fi | |
347 | ||
348 | done | |
349 | } | |
350 | ||
351 | ||
8cc6a083 LT |
352 | case "$#" in |
353 | 0) | |
354 | usage ;; | |
355 | *) | |
356 | cmd="$1" | |
357 | shift | |
358 | case "$cmd" in | |
359 | start) | |
360 | bisect_start "$@" ;; | |
361 | bad) | |
362 | bisect_bad "$@" ;; | |
363 | good) | |
364 | bisect_good "$@" ;; | |
365 | next) | |
366 | # Not sure we want "next" at the UI level anymore. | |
367 | bisect_next "$@" ;; | |
cc9f24d0 JH |
368 | visualize) |
369 | bisect_visualize "$@" ;; | |
8cc6a083 LT |
370 | reset) |
371 | bisect_reset "$@" ;; | |
e204de28 JH |
372 | replay) |
373 | bisect_replay "$@" ;; | |
374 | log) | |
375 | cat "$GIT_DIR/BISECT_LOG" ;; | |
a17c4101 CC |
376 | run) |
377 | bisect_run "$@" ;; | |
8cc6a083 LT |
378 | *) |
379 | usage ;; | |
380 | esac | |
381 | esac |