]> git.ipfire.org Git - thirdparty/git.git/blame - git-bisect.sh
bisect: call 'clear_commit_marks_all()' in 'bisect_next_all()'
[thirdparty/git.git] / git-bisect.sh
CommitLineData
8cc6a083 1#!/bin/sh
d025524d 2
dbc349bb 3USAGE='[help|start|bad|good|new|old|terms|skip|next|reset|visualize|view|replay|log|run]'
243a60fb 4LONG_USAGE='git bisect help
6021be86 5 print this long help message.
06e6a745
MM
6git bisect start [--term-{old,good}=<term> --term-{new,bad}=<term>]
7 [--no-checkout] [<bad> [<good>...]] [--] [<pathspec>...]
6021be86 8 reset bisect state and start bisection.
21e5cfd8
AD
9git bisect (bad|new) [<rev>]
10 mark <rev> a known-bad revision/
11 a revision after change in a given property.
12git bisect (good|old) [<rev>...]
13 mark <rev>... known-good revisions/
14 revisions before change in a given property.
21b55e33
MM
15git bisect terms [--term-good | --term-bad]
16 show the terms used for old and new commits (default: bad, good)
5413812f 17git bisect skip [(<rev>|<range>)...]
6021be86 18 mark <rev>... untestable revisions.
38a47fd6 19git bisect next
6021be86 20 find next bisection to test and check it out.
6b87ce23 21git bisect reset [<commit>]
6021be86 22 finish bisection search and go back to commit.
dbc349bb 23git bisect (visualize|view)
6021be86 24 show bisect status in gitk.
38a47fd6 25git bisect replay <logfile>
6021be86 26 replay bisection log.
38a47fd6 27git bisect log
6021be86 28 show bisect log.
38a47fd6 29git bisect run <cmd>...
6021be86 30 use <cmd>... to automatically bisect.
243a60fb
CC
31
32Please use "git help bisect" to get the full man page.'
d025524d 33
8f321a39 34OPTIONS_SPEC=
ae2b0f15 35. git-sh-setup
8cc6a083 36
ce32660e
JS
37_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
38_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
43f9d9f3
AD
39TERM_BAD=bad
40TERM_GOOD=good
ce32660e 41
4796e823
JS
42bisect_head()
43{
de966e39 44 if git rev-parse --verify -q BISECT_HEAD > /dev/null
4796e823
JS
45 then
46 echo BISECT_HEAD
47 else
48 echo HEAD
49 fi
50}
51
8cc6a083 52bisect_start() {
06f5608c 53 git bisect--helper --bisect-start $@ || exit
38a47fd6 54
ba963de8
CC
55 #
56 # Change state.
57 # In case of mistaken revs or checkout error, or signals received,
58 # "bisect_auto_next" below may exit or misbehave.
59 # We have to trap this to be able to clean up using
60 # "bisect_clean_state".
61 #
fb71a329 62 trap 'git bisect--helper --bisect-clean-state' 0
ba963de8
CC
63 trap 'exit 255' 1 2 3 15
64
ba963de8
CC
65 #
66 # Check if we can proceed to the next bisect state.
67 #
06f5608c 68 get_terms
38a47fd6 69 bisect_auto_next
ba963de8
CC
70
71 trap '-' 0
8cc6a083
LT
72}
73
ee2314f5 74bisect_skip() {
6021be86 75 all=''
ee2314f5
CC
76 for arg in "$@"
77 do
6021be86
JS
78 case "$arg" in
79 *..*)
80 revs=$(git rev-list "$arg") || die "$(eval_gettext "Bad rev input: \$arg")" ;;
81 *)
82 revs=$(git rev-parse --sq-quote "$arg") ;;
83 esac
84 all="$all $revs"
85 done
86 eval bisect_state 'skip' $all
ee2314f5
CC
87}
88
155fc795 89bisect_state() {
09535f05 90 git bisect--helper --bisect-autostart || exit
155fc795 91 state=$1
4fbdbd5b
PB
92 git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
93 get_terms
155fc795
CC
94 case "$#,$state" in
95 0,*)
f813fb41 96 die "Please call 'bisect_state' with at least one argument." ;;
43f9d9f3 97 1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip)
57984dd9
VA
98 bisected_head=$(bisect_head)
99 rev=$(git rev-parse --verify "$bisected_head") ||
100 die "$(eval_gettext "Bad rev input: \$bisected_head")"
0f30233a 101 git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
b903674b 102 git bisect--helper --check-expected-revs "$rev" ;;
43f9d9f3 103 2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
155fc795 104 shift
6bc02d56 105 hash_list=''
e3389075 106 for rev in "$@"
155fc795 107 do
a179a303 108 sha=$(git rev-parse --verify "$rev^{commit}") ||
15eaa049 109 die "$(eval_gettext "Bad rev input: \$rev")"
6bc02d56 110 hash_list="$hash_list $sha"
d3e54c88 111 done
6bc02d56
CC
112 for rev in $hash_list
113 do
0f30233a 114 git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
6bc02d56 115 done
b903674b 116 git bisect--helper --check-expected-revs $hash_list ;;
43f9d9f3
AD
117 *,"$TERM_BAD")
118 die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
cc9f24d0
JH
119 *)
120 usage ;;
8cc6a083 121 esac
97e1c51e
CC
122 bisect_auto_next
123}
124
8cc6a083 125bisect_auto_next() {
129a6cf3 126 git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD && bisect_next || :
8cc6a083
LT
127}
128
129bisect_next() {
8fe26f44 130 case "$#" in 0) ;; *) usage ;; esac
09535f05 131 git bisect--helper --bisect-autostart || exit
129a6cf3 132 git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit
0a5280a9 133
0871984d 134 # Perform all bisection computation, display and checkout
be5fe200 135 git bisect--helper --next-all
5a1d31c7 136 res=$?
0a5280a9 137
6021be86 138 # Check if we should exit because bisection is finished
a7f8b8ac
TH
139 if test $res -eq 10
140 then
43f9d9f3 141 bad_rev=$(git show-ref --hash --verify refs/bisect/$TERM_BAD)
a7f8b8ac 142 bad_commit=$(git show-branch $bad_rev)
43f9d9f3 143 echo "# first $TERM_BAD commit: $bad_commit" >>"$GIT_DIR/BISECT_LOG"
a7f8b8ac 144 exit 0
f989cac9
TH
145 elif test $res -eq 2
146 then
147 echo "# only skipped commits left to test" >>"$GIT_DIR/BISECT_LOG"
43f9d9f3
AD
148 good_revs=$(git for-each-ref --format="%(objectname)" "refs/bisect/$TERM_GOOD-*")
149 for skipped in $(git rev-list refs/bisect/$TERM_BAD --not $good_revs)
f989cac9
TH
150 do
151 skipped_commit=$(git show-branch $skipped)
43f9d9f3 152 echo "# possible first $TERM_BAD commit: $skipped_commit" >>"$GIT_DIR/BISECT_LOG"
f989cac9
TH
153 done
154 exit $res
a7f8b8ac 155 fi
0a5280a9 156
5a1d31c7
CC
157 # Check for an error in the bisection process
158 test $res -ne 0 && exit $res
159
160 return 0
8cc6a083
LT
161}
162
cc9f24d0 163bisect_visualize() {
129a6cf3 164 git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
235997c9
JH
165
166 if test $# = 0
167 then
c4e4644e 168 if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
43b8ff4b 169 type gitk >/dev/null 2>&1
eef12a9a 170 then
c4e4644e
JK
171 set gitk
172 else
173 set git log
174 fi
235997c9
JH
175 else
176 case "$1" in
177 git*|tig) ;;
178 -*) set git log "$@" ;;
179 *) set git "$@" ;;
180 esac
181 fi
182
fc13aa3d 183 eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
cc9f24d0
JH
184}
185
e204de28 186bisect_replay () {
55a9fc80
ÆAB
187 file="$1"
188 test "$#" -eq 1 || die "$(gettext "No logfile given")"
189 test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")"
5e82c3dd 190 git bisect--helper --bisect-reset || exit
6c722cbe 191 oIFS="$IFS" IFS="$IFS$(printf '\015')"
173cb08d 192 while read git bisect command rev tail
e204de28 193 do
c82af12a 194 test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue
eef12a9a
JS
195 if test "$git" = "git-bisect"
196 then
6c98c054
MV
197 rev="$command"
198 command="$bisect"
199 fi
cb46d630 200 get_terms
4fbdbd5b
PB
201 git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit
202 get_terms
e204de28
JH
203 case "$command" in
204 start)
173cb08d 205 cmd="bisect_start $rev $tail"
737c74ee 206 eval "$cmd" ;;
cb46d630 207 "$TERM_GOOD"|"$TERM_BAD"|skip)
0f30233a 208 git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;;
21b55e33 209 terms)
450ebb73 210 git bisect--helper --bisect-terms $rev || exit;;
e204de28 211 *)
9570fc1e 212 die "$(gettext "?? what are you talking about?")" ;;
e204de28 213 esac
55a9fc80 214 done <"$file"
6c722cbe 215 IFS="$oIFS"
e204de28 216 bisect_auto_next
8cc6a083
LT
217}
218
a17c4101 219bisect_run () {
129a6cf3 220 git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
6021be86 221
fecd2dd3
SB
222 test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"
223
6021be86
JS
224 while true
225 do
226 command="$@"
3145b1a2 227 eval_gettextln "running \$command"
6021be86
JS
228 "$@"
229 res=$?
230
231 # Check for really bad run error.
eef12a9a
JS
232 if [ $res -lt 0 -o $res -ge 128 ]
233 then
3145b1a2
JS
234 eval_gettextln "bisect run failed:
235exit code \$res from '\$command' is < 0 or >= 128" >&2
6021be86
JS
236 exit $res
237 fi
238
239 # Find current state depending on run success or failure.
240 # A special exit code of 125 means cannot test.
eef12a9a
JS
241 if [ $res -eq 125 ]
242 then
6021be86 243 state='skip'
eef12a9a
JS
244 elif [ $res -gt 0 ]
245 then
43f9d9f3 246 state="$TERM_BAD"
6021be86 247 else
43f9d9f3 248 state="$TERM_GOOD"
6021be86
JS
249 fi
250
251 # We have to use a subshell because "bisect_state" can exit.
305a233c 252 ( bisect_state $state >"$GIT_DIR/BISECT_RUN" )
6021be86
JS
253 res=$?
254
255 cat "$GIT_DIR/BISECT_RUN"
256
43f9d9f3 257 if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
305a233c 258 >/dev/null
eef12a9a 259 then
3145b1a2 260 gettextln "bisect run cannot continue any more" >&2
6021be86
JS
261 exit $res
262 fi
263
eef12a9a
JS
264 if [ $res -ne 0 ]
265 then
3145b1a2
JS
266 eval_gettextln "bisect run failed:
267'bisect_state \$state' exited with error code \$res" >&2
6021be86
JS
268 exit $res
269 fi
a17c4101 270
43f9d9f3 271 if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
eef12a9a 272 then
3145b1a2 273 gettextln "bisect run success"
6021be86
JS
274 exit 0;
275 fi
a17c4101 276
6021be86 277 done
a17c4101
CC
278}
279
412ff738 280bisect_log () {
9570fc1e 281 test -s "$GIT_DIR/BISECT_LOG" || die "$(gettext "We are not bisecting.")"
412ff738
SG
282 cat "$GIT_DIR/BISECT_LOG"
283}
a17c4101 284
cb46d630
AD
285get_terms () {
286 if test -s "$GIT_DIR/BISECT_TERMS"
287 then
288 {
289 read TERM_BAD
290 read TERM_GOOD
291 } <"$GIT_DIR/BISECT_TERMS"
292 fi
293}
294
8cc6a083
LT
295case "$#" in
2960)
6021be86 297 usage ;;
8cc6a083 298*)
6021be86 299 cmd="$1"
cb46d630 300 get_terms
6021be86
JS
301 shift
302 case "$cmd" in
303 help)
304 git bisect -h ;;
305 start)
306 bisect_start "$@" ;;
21e5cfd8 307 bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
6021be86
JS
308 bisect_state "$cmd" "$@" ;;
309 skip)
310 bisect_skip "$@" ;;
311 next)
312 # Not sure we want "next" at the UI level anymore.
313 bisect_next "$@" ;;
314 visualize|view)
315 bisect_visualize "$@" ;;
316 reset)
5e82c3dd 317 git bisect--helper --bisect-reset "$@" ;;
6021be86
JS
318 replay)
319 bisect_replay "$@" ;;
320 log)
321 bisect_log ;;
322 run)
323 bisect_run "$@" ;;
21b55e33 324 terms)
450ebb73 325 git bisect--helper --bisect-terms "$@" || exit;;
6021be86
JS
326 *)
327 usage ;;
328 esac
8cc6a083 329esac