GIT v1.5.0-rc3
[git/mingw/4msysgit/kblees.git] / git-bisect.sh
blob6da31e87a01af9b883958b3b0bae4304bfa94520
1 #!/bin/sh
3 USAGE='[start|bad|good|next|reset|visualize]'
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.'
13 . git-sh-setup
15 sq() {
16 @@PERL@@ -e '
17 for (@ARGV) {
18 s/'\''/'\'\\\\\'\''/g;
19 print " '\''$_'\''";
21 print "\n";
22 ' "$@"
25 bisect_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
43 bisect_start() {
45 # Verify HEAD. If we were bisecting before this, reset to the
46 # top-of-line master first!
48 head=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD) ||
49 die "Bad HEAD - I need a symbolic ref"
50 case "$head" in
51 refs/heads/bisect*)
52 if [ -s "$GIT_DIR/head-name" ]; then
53 branch=`cat "$GIT_DIR/head-name"`
54 else
55 branch=master
57 git checkout $branch || exit
59 refs/heads/*)
60 [ -s "$GIT_DIR/head-name" ] && die "won't bisect on seeked tree"
61 echo "$head" | sed 's#^refs/heads/##' >"$GIT_DIR/head-name"
64 die "Bad HEAD - strange symbolic ref"
66 esac
69 # Get rid of any old bisect state
71 rm -f "$GIT_DIR/refs/heads/bisect"
72 rm -rf "$GIT_DIR/refs/bisect/"
73 mkdir "$GIT_DIR/refs/bisect"
75 printf "git-bisect start"
76 sq "$@"
77 } >"$GIT_DIR/BISECT_LOG"
78 sq "$@" >"$GIT_DIR/BISECT_NAMES"
81 bisect_bad() {
82 bisect_autostart
83 case "$#" in
85 rev=$(git-rev-parse --verify HEAD) ;;
87 rev=$(git-rev-parse --verify "$1") ;;
89 usage ;;
90 esac || exit
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"
94 bisect_auto_next
97 bisect_good() {
98 bisect_autostart
99 case "$#" in
100 0) revs=$(git-rev-parse --verify HEAD) || exit ;;
101 *) revs=$(git-rev-parse --revs-only --no-flags "$@") &&
102 test '' != "$revs" || die "Bad rev input: $@" ;;
103 esac
104 for rev in $revs
106 rev=$(git-rev-parse --verify "$rev") || exit
107 echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev"
108 echo "# good: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
109 echo "git-bisect good $rev" >>"$GIT_DIR/BISECT_LOG"
110 done
111 bisect_auto_next
114 bisect_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 ;;
127 true ;;
128 esac
131 bisect_auto_next() {
132 bisect_next_check && bisect_next || :
135 bisect_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-*)) &&
142 rev=$(eval "git-rev-list --bisect $good $bad -- $(cat $GIT_DIR/BISECT_NAMES)") || exit
143 if [ -z "$rev" ]; then
144 echo "$bad was both good and bad"
145 exit 1
147 if [ "$rev" = "$bad" ]; then
148 echo "$rev is first bad commit"
149 git-diff-tree --pretty $rev
150 exit 0
152 nr=$(eval "git-rev-list $rev $good -- $(cat $GIT_DIR/BISECT_NAMES)" | wc -l) || exit
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" &&
157 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD refs/heads/bisect
158 git-show-branch "$rev"
161 bisect_visualize() {
162 bisect_next_check fail
163 not=`cd "$GIT_DIR/refs" && echo bisect/good-*`
164 eval gitk bisect/bad --not $not -- $(cat "$GIT_DIR/BISECT_NAMES")
167 bisect_reset() {
168 case "$#" in
169 0) if [ -s "$GIT_DIR/head-name" ]; then
170 branch=`cat "$GIT_DIR/head-name"`
171 else
172 branch=master
173 fi ;;
174 1) test -f "$GIT_DIR/refs/heads/$1" || {
175 echo >&2 "$1 does not seem to be a valid branch"
176 exit 1
178 branch="$1" ;;
180 usage ;;
181 esac
182 if git checkout "$branch"; then
183 rm -fr "$GIT_DIR/refs/bisect"
184 rm -f "$GIT_DIR/refs/heads/bisect" "$GIT_DIR/head-name"
185 rm -f "$GIT_DIR/BISECT_LOG"
186 rm -f "$GIT_DIR/BISECT_NAMES"
190 bisect_replay () {
191 test -r "$1" || {
192 echo >&2 "cannot read $1 for replaying"
193 exit 1
195 bisect_reset
196 while read bisect command rev
198 test "$bisect" = "git-bisect" || continue
199 case "$command" in
200 start)
201 cmd="bisect_start $rev"
202 eval "$cmd"
204 good)
205 echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev"
206 echo "# good: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
207 echo "git-bisect good $rev" >>"$GIT_DIR/BISECT_LOG"
209 bad)
210 echo "$rev" >"$GIT_DIR/refs/bisect/bad"
211 echo "# bad: "$(git-show-branch $rev) >>"$GIT_DIR/BISECT_LOG"
212 echo "git-bisect bad $rev" >>"$GIT_DIR/BISECT_LOG"
215 echo >&2 "?? what are you talking about?"
216 exit 1 ;;
217 esac
218 done <"$1"
219 bisect_auto_next
222 case "$#" in
224 usage ;;
226 cmd="$1"
227 shift
228 case "$cmd" in
229 start)
230 bisect_start "$@" ;;
231 bad)
232 bisect_bad "$@" ;;
233 good)
234 bisect_good "$@" ;;
235 next)
236 # Not sure we want "next" at the UI level anymore.
237 bisect_next "$@" ;;
238 visualize)
239 bisect_visualize "$@" ;;
240 reset)
241 bisect_reset "$@" ;;
242 replay)
243 bisect_replay "$@" ;;
244 log)
245 cat "$GIT_DIR/BISECT_LOG" ;;
247 usage ;;
248 esac
249 esac