3 USAGE='[start|bad|good|next|reset|visualize|replay|log|run]'
4 LONG_USAGE='git bisect start [<pathspec>] reset bisect state and start bisection.
5 git bisect bad [<rev>] mark <rev> a known-bad revision.
6 git bisect good [<rev>...] mark <rev>... known-good revisions.
7 git bisect next find next bisection to test and check it out.
8 git bisect reset [<branch>] finish bisection search and go back to branch.
9 git bisect visualize show bisect status in gitk.
10 git bisect replay <logfile> replay bisection log.
11 git bisect log show bisect log.
12 git bisect run <cmd>... use <cmd>... to automatically bisect.'
20 s/'\''/'\'\\\\\'\''/g;
28 test -d "$GIT_DIR/refs/bisect" || {
29 echo >&2 'You need to start by "git bisect start"'
32 echo >&2 -n 'Do you want me to do it for you [Y/n]? '
47 # Verify HEAD. If we were bisecting before this, reset to the
48 # top-of-line master first!
50 head=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD) ||
51 die "Bad HEAD - I need a symbolic ref"
54 if [ -s "$GIT_DIR/head-name" ]; then
55 branch=`cat "$GIT_DIR/head-name"`
59 git checkout $branch || exit
62 [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
63 echo "$head" | sed 's#^refs/heads/##' >"$GIT_DIR/head-name"
66 die "Bad HEAD - strange symbolic ref"
71 # Get rid of any old bisect state
73 rm -f "$GIT_DIR/refs/heads/bisect"
74 rm -rf "$GIT_DIR/refs/bisect/"
75 mkdir "$GIT_DIR/refs/bisect"
77 printf "git-bisect start"
79 } >"$GIT_DIR/BISECT_LOG"
80 sq "$@" >"$GIT_DIR/BISECT_NAMES"
87 rev=$(git-rev-parse --verify HEAD) ;;
89 rev=$(git-rev-parse --verify "$1^{commit}") ;;
93 echo "$rev" >"$GIT_DIR/refs/bisect/bad"
94 echo "# bad: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
95 echo "git-bisect bad $rev" >>"$GIT_DIR/BISECT_LOG"
102 0) revs=$(git-rev-parse --verify HEAD) || exit ;;
103 *) revs=$(git-rev-parse --revs-only --no-flags "$@") &&
104 test '' != "$revs" || die "Bad rev input: $@" ;;
108 rev=$(git-rev-parse --verify "$rev^{commit}") || exit
109 echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev"
110 echo "# good: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
111 echo "git-bisect good $rev" >>"$GIT_DIR/BISECT_LOG"
116 bisect_next_check() {
118 test -f "$GIT_DIR/refs/bisect/bad" &&
119 case "$(cd "$GIT_DIR" && echo refs/bisect/good-*)" in
120 refs/bisect/good-\*) ;;
123 case "$next_ok,$1" in
127 test -d "$GIT_DIR/refs/bisect" || {
128 echo >&2 'You need to start by "git bisect start".'
131 echo >&2 'You '$THEN'need to give me at least one good' \
132 'and one bad revisions.'
133 echo >&2 '(You can use "git bisect bad" and' \
134 '"git bisect good" for that.)'
142 bisect_next_check && bisect_next || :
146 case "$#" in 0) ;; *) usage ;; esac
148 bisect_next_check fail
149 bad=$(git-rev-parse --verify refs/bisect/bad) &&
150 good=$(git-rev-parse --sq --revs-only --not \
151 $(cd "$GIT_DIR" && ls refs/bisect/good-*)) &&
152 rev=$(eval "git-rev-list --bisect $good $bad -- $(cat "$GIT_DIR/BISECT_NAMES")") || exit
153 if [ -z "$rev" ]; then
154 echo "$bad was both good and bad"
157 if [ "$rev" = "$bad" ]; then
158 echo "$rev is first bad commit"
159 git-diff-tree --pretty $rev
162 nr=$(eval "git-rev-list $rev $good -- $(cat $GIT_DIR/BISECT_NAMES)" | wc -l) || exit
163 echo "Bisecting: $nr revisions left to test after this"
164 echo "$rev" > "$GIT_DIR/refs/heads/new-bisect"
165 git checkout -q new-bisect || exit
166 mv "$GIT_DIR/refs/heads/new-bisect" "$GIT_DIR/refs/heads/bisect" &&
167 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD refs/heads/bisect
168 git-show-branch "$rev"
172 bisect_next_check fail
173 not=`cd "$GIT_DIR/refs" && echo bisect/good-*`
174 eval gitk bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
179 0) if [ -s "$GIT_DIR/head-name" ]; then
180 branch=`cat "$GIT_DIR/head-name"`
184 1) git-show-ref --verify --quiet -- "refs/heads/$1" || {
185 echo >&2 "$1 does not seem to be a valid branch"
192 if git checkout "$branch"; then
193 rm -fr "$GIT_DIR/refs/bisect"
194 rm -f "$GIT_DIR/refs/heads/bisect" "$GIT_DIR/head-name"
195 rm -f "$GIT_DIR/BISECT_LOG"
196 rm -f "$GIT_DIR/BISECT_NAMES"
197 rm -f "$GIT_DIR/BISECT_RUN"
203 echo >&2 "cannot read $1 for replaying"
207 while read bisect command rev
209 test "$bisect" = "git-bisect" || continue
212 cmd="bisect_start $rev"
216 echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev"
217 echo "# good: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
218 echo "git-bisect good $rev" >>"$GIT_DIR/BISECT_LOG"
221 echo "$rev" >"$GIT_DIR/refs/bisect/bad"
222 echo "# bad: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
223 echo "git-bisect bad $rev" >>"$GIT_DIR/BISECT_LOG"
226 echo >&2 "?? what are you talking about?"
234 bisect_next_check fail
242 # Check for really bad run error.
243 if [ $res -lt 0 -o $res -ge 128 ]; then
244 echo >&2 "bisect run failed:"
245 echo >&2 "exit code $res from '$@' is < 0 or >= 128"
249 # Use "bisect_good" or "bisect_bad"
250 # depending on run success or failure.
251 if [ $res -gt 0 ]; then
252 next_bisect='bisect_bad'
254 next_bisect='bisect_good'
257 # We have to use a subshell because bisect_good or
258 # bisect_bad functions can exit.
259 ( $next_bisect > "$GIT_DIR/BISECT_RUN" )
262 cat "$GIT_DIR/BISECT_RUN"
264 if [ $res -ne 0 ]; then
265 echo >&2 "bisect run failed:"
266 echo >&2 "$next_bisect exited with error code $res"
270 if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
271 echo "bisect run success"
293 # Not sure we want "next" at the UI level anymore.
296 bisect_visualize "$@" ;;
300 bisect_replay "$@" ;;
302 cat "$GIT_DIR/BISECT_LOG" ;;