1#!/bin/sh
23
USAGE='[start|bad|good|next|reset|visualize|replay|log|run]'
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.
12git bisect run <cmd>... use <cmd>... to automatically bisect.'
1314
. git-sh-setup
15require_work_tree
1617
sq() {
18@@PERL@@ -e '
19for (@ARGV) {
20s/'\''/'\'\\\\\'\''/g;
21print " '\''$_'\''";
22}
23print "\n";
24' "$@"
25}
2627
bisect_autostart() {
28test -d "$GIT_DIR/refs/bisect" || {
29echo >&2 'You need to start by "git bisect start"'
30if test -t 0
31then
32echo >&2 -n 'Do you want me to do it for you [Y/n]? '
33read yesno
34case "$yesno" in
35[Nn]*)
36exit ;;
37esac
38bisect_start
39else
40exit 1
41fi
42}
43}
4445
bisect_start() {
46#
47# Verify HEAD. If we were bisecting before this, reset to the
48# top-of-line master first!
49#
50head=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD) ||
51die "Bad HEAD - I need a symbolic ref"
52case "$head" in
53refs/heads/bisect)
54if [ -s "$GIT_DIR/head-name" ]; then
55branch=`cat "$GIT_DIR/head-name"`
56else
57branch=master
58fi
59git checkout $branch || exit
60;;
61refs/heads/*)
62[ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
63echo "$head" | sed 's#^refs/heads/##' >"$GIT_DIR/head-name"
64;;
65*)
66die "Bad HEAD - strange symbolic ref"
67;;
68esac
6970
#
71# Get rid of any old bisect state
72#
73rm -f "$GIT_DIR/refs/heads/bisect"
74rm -rf "$GIT_DIR/refs/bisect/"
75mkdir "$GIT_DIR/refs/bisect"
76{
77printf "git-bisect start"
78sq "$@"
79} >"$GIT_DIR/BISECT_LOG"
80sq "$@" >"$GIT_DIR/BISECT_NAMES"
81}
8283
bisect_bad() {
84bisect_autostart
85case "$#" in
860)
87rev=$(git-rev-parse --verify HEAD) ;;
881)
89rev=$(git-rev-parse --verify "$1^{commit}") ;;
90*)
91usage ;;
92esac || exit
93echo "$rev" >"$GIT_DIR/refs/bisect/bad"
94echo "# bad: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
95echo "git-bisect bad $rev" >>"$GIT_DIR/BISECT_LOG"
96bisect_auto_next
97}
9899
bisect_good() {
100bisect_autostart
101case "$#" in
1020) revs=$(git-rev-parse --verify HEAD) || exit ;;
103*) revs=$(git-rev-parse --revs-only --no-flags "$@") &&
104test '' != "$revs" || die "Bad rev input: $@" ;;
105esac
106for rev in $revs
107do
108rev=$(git-rev-parse --verify "$rev^{commit}") || exit
109echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev"
110echo "# good: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
111echo "git-bisect good $rev" >>"$GIT_DIR/BISECT_LOG"
112done
113bisect_auto_next
114}
115116
bisect_next_check() {
117next_ok=no
118test -f "$GIT_DIR/refs/bisect/bad" &&
119case "$(cd "$GIT_DIR" && echo refs/bisect/good-*)" in
120refs/bisect/good-\*) ;;
121*) next_ok=yes ;;
122esac
123case "$next_ok,$1" in
124no,) false ;;
125no,fail)
126THEN=''
127test -d "$GIT_DIR/refs/bisect" || {
128echo >&2 'You need to start by "git bisect start".'
129THEN='then '
130}
131echo >&2 'You '$THEN'need to give me at least one good' \
132'and one bad revisions.'
133echo >&2 '(You can use "git bisect bad" and' \
134'"git bisect good" for that.)'
135exit 1 ;;
136*)
137true ;;
138esac
139}
140141
bisect_auto_next() {
142bisect_next_check && bisect_next || :
143}
144145
bisect_next() {
146case "$#" in 0) ;; *) usage ;; esac
147bisect_autostart
148bisect_next_check fail
149bad=$(git-rev-parse --verify refs/bisect/bad) &&
150good=$(git-rev-parse --sq --revs-only --not \
151$(cd "$GIT_DIR" && ls refs/bisect/good-*)) &&
152rev=$(eval "git-rev-list --bisect $good $bad -- $(cat "$GIT_DIR/BISECT_NAMES")") || exit
153if [ -z "$rev" ]; then
154echo "$bad was both good and bad"
155exit 1
156fi
157if [ "$rev" = "$bad" ]; then
158echo "$rev is first bad commit"
159git-diff-tree --pretty $rev
160exit 0
161fi
162nr=$(eval "git-rev-list $rev $good -- $(cat $GIT_DIR/BISECT_NAMES)" | wc -l) || exit
163echo "Bisecting: $nr revisions left to test after this"
164echo "$rev" > "$GIT_DIR/refs/heads/new-bisect"
165git checkout -q new-bisect || exit
166mv "$GIT_DIR/refs/heads/new-bisect" "$GIT_DIR/refs/heads/bisect" &&
167GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD refs/heads/bisect
168git-show-branch "$rev"
169}
170171
bisect_visualize() {
172bisect_next_check fail
173not=`cd "$GIT_DIR/refs" && echo bisect/good-*`
174eval gitk bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
175}
176177
bisect_reset() {
178case "$#" in
1790) if [ -s "$GIT_DIR/head-name" ]; then
180branch=`cat "$GIT_DIR/head-name"`
181else
182branch=master
183fi ;;
1841) git-show-ref --verify --quiet -- "refs/heads/$1" || {
185echo >&2 "$1 does not seem to be a valid branch"
186exit 1
187}
188branch="$1" ;;
189*)
190usage ;;
191esac
192if git checkout "$branch"; then
193rm -fr "$GIT_DIR/refs/bisect"
194rm -f "$GIT_DIR/refs/heads/bisect" "$GIT_DIR/head-name"
195rm -f "$GIT_DIR/BISECT_LOG"
196rm -f "$GIT_DIR/BISECT_NAMES"
197rm -f "$GIT_DIR/BISECT_RUN"
198fi
199}
200201
bisect_replay () {
202test -r "$1" || {
203echo >&2 "cannot read $1 for replaying"
204exit 1
205}
206bisect_reset
207while read bisect command rev
208do
209test "$bisect" = "git-bisect" || continue
210case "$command" in
211start)
212cmd="bisect_start $rev"
213eval "$cmd"
214;;
215good)
216echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev"
217echo "# good: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
218echo "git-bisect good $rev" >>"$GIT_DIR/BISECT_LOG"
219;;
220bad)
221echo "$rev" >"$GIT_DIR/refs/bisect/bad"
222echo "# bad: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
223echo "git-bisect bad $rev" >>"$GIT_DIR/BISECT_LOG"
224;;
225*)
226echo >&2 "?? what are you talking about?"
227exit 1 ;;
228esac
229done <"$1"
230bisect_auto_next
231}
232233
bisect_run () {
234bisect_next_check fail
235236
while true
237do
238echo "running $@"
239"$@"
240res=$?
241242
# Check for really bad run error.
243if [ $res -lt 0 -o $res -ge 128 ]; then
244echo >&2 "bisect run failed:"
245echo >&2 "exit code $res from '$@' is < 0 or >= 128"
246exit $res
247fi
248249
# Use "bisect_good" or "bisect_bad"
250# depending on run success or failure.
251if [ $res -gt 0 ]; then
252next_bisect='bisect_bad'
253else
254next_bisect='bisect_good'
255fi
256257
# We have to use a subshell because bisect_good or
258# bisect_bad functions can exit.
259( $next_bisect > "$GIT_DIR/BISECT_RUN" )
260res=$?
261262
cat "$GIT_DIR/BISECT_RUN"
263264
if [ $res -ne 0 ]; then
265echo >&2 "bisect run failed:"
266echo >&2 "$next_bisect exited with error code $res"
267exit $res
268fi
269270
if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
271echo "bisect run success"
272exit 0;
273fi
274275
done
276}
277278
279
case "$#" in
2800)
281usage ;;
282*)
283cmd="$1"
284shift
285case "$cmd" in
286start)
287bisect_start "$@" ;;
288bad)
289bisect_bad "$@" ;;
290good)
291bisect_good "$@" ;;
292next)
293# Not sure we want "next" at the UI level anymore.
294bisect_next "$@" ;;
295visualize)
296bisect_visualize "$@" ;;
297reset)
298bisect_reset "$@" ;;
299replay)
300bisect_replay "$@" ;;
301log)
302cat "$GIT_DIR/BISECT_LOG" ;;
303run)
304bisect_run "$@" ;;
305*)
306usage ;;
307esac
308esac