]> git.ipfire.org Git - thirdparty/git.git/blame - git-bisect.sh
Use new HASHMAP_INIT macro to simplify hashmap initialization
[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.
ef4d9f8a 6git bisect start [--term-{new,bad}=<term> --term-{old,good}=<term>]
6028f5f6 7 [--no-checkout] [--first-parent] [<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 #
517ecb31 68 git bisect--helper --bisect-auto-next || exit
ba963de8
CC
69
70 trap '-' 0
8cc6a083
LT
71}
72
ee2314f5 73bisect_skip() {
6021be86 74 all=''
ee2314f5
CC
75 for arg in "$@"
76 do
6021be86
JS
77 case "$arg" in
78 *..*)
79 revs=$(git rev-list "$arg") || die "$(eval_gettext "Bad rev input: \$arg")" ;;
80 *)
81 revs=$(git rev-parse --sq-quote "$arg") ;;
82 esac
83 all="$all $revs"
84 done
85 eval bisect_state 'skip' $all
ee2314f5
CC
86}
87
155fc795 88bisect_state() {
09535f05 89 git bisect--helper --bisect-autostart || exit
155fc795 90 state=$1
4fbdbd5b
PB
91 git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
92 get_terms
155fc795
CC
93 case "$#,$state" in
94 0,*)
f813fb41 95 die "Please call 'bisect_state' with at least one argument." ;;
43f9d9f3 96 1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip)
57984dd9
VA
97 bisected_head=$(bisect_head)
98 rev=$(git rev-parse --verify "$bisected_head") ||
99 die "$(eval_gettext "Bad rev input: \$bisected_head")"
0f30233a 100 git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
b903674b 101 git bisect--helper --check-expected-revs "$rev" ;;
43f9d9f3 102 2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
155fc795 103 shift
6bc02d56 104 hash_list=''
e3389075 105 for rev in "$@"
155fc795 106 do
a179a303 107 sha=$(git rev-parse --verify "$rev^{commit}") ||
15eaa049 108 die "$(eval_gettext "Bad rev input: \$rev")"
6bc02d56 109 hash_list="$hash_list $sha"
d3e54c88 110 done
6bc02d56
CC
111 for rev in $hash_list
112 do
0f30233a 113 git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
6bc02d56 114 done
b903674b 115 git bisect--helper --check-expected-revs $hash_list ;;
43f9d9f3
AD
116 *,"$TERM_BAD")
117 die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
cc9f24d0
JH
118 *)
119 usage ;;
8cc6a083 120 esac
517ecb31 121 git bisect--helper --bisect-auto-next
8cc6a083
LT
122}
123
cc9f24d0 124bisect_visualize() {
129a6cf3 125 git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
235997c9
JH
126
127 if test $# = 0
128 then
c4e4644e 129 if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
43b8ff4b 130 type gitk >/dev/null 2>&1
eef12a9a 131 then
c4e4644e
JK
132 set gitk
133 else
134 set git log
135 fi
235997c9
JH
136 else
137 case "$1" in
138 git*|tig) ;;
139 -*) set git log "$@" ;;
140 *) set git "$@" ;;
141 esac
142 fi
143
fc13aa3d 144 eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
cc9f24d0
JH
145}
146
e204de28 147bisect_replay () {
55a9fc80
ÆAB
148 file="$1"
149 test "$#" -eq 1 || die "$(gettext "No logfile given")"
150 test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")"
5e82c3dd 151 git bisect--helper --bisect-reset || exit
6c722cbe 152 oIFS="$IFS" IFS="$IFS$(printf '\015')"
173cb08d 153 while read git bisect command rev tail
e204de28 154 do
c82af12a 155 test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue
eef12a9a
JS
156 if test "$git" = "git-bisect"
157 then
6c98c054
MV
158 rev="$command"
159 command="$bisect"
160 fi
cb46d630 161 get_terms
4fbdbd5b
PB
162 git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit
163 get_terms
e204de28
JH
164 case "$command" in
165 start)
173cb08d 166 cmd="bisect_start $rev $tail"
737c74ee 167 eval "$cmd" ;;
cb46d630 168 "$TERM_GOOD"|"$TERM_BAD"|skip)
0f30233a 169 git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;;
21b55e33 170 terms)
450ebb73 171 git bisect--helper --bisect-terms $rev || exit;;
e204de28 172 *)
9570fc1e 173 die "$(gettext "?? what are you talking about?")" ;;
e204de28 174 esac
55a9fc80 175 done <"$file"
6c722cbe 176 IFS="$oIFS"
517ecb31 177 git bisect--helper --bisect-auto-next || exit
8cc6a083
LT
178}
179
a17c4101 180bisect_run () {
129a6cf3 181 git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
6021be86 182
fecd2dd3
SB
183 test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"
184
6021be86
JS
185 while true
186 do
187 command="$@"
3145b1a2 188 eval_gettextln "running \$command"
6021be86
JS
189 "$@"
190 res=$?
191
192 # Check for really bad run error.
eef12a9a
JS
193 if [ $res -lt 0 -o $res -ge 128 ]
194 then
3145b1a2
JS
195 eval_gettextln "bisect run failed:
196exit code \$res from '\$command' is < 0 or >= 128" >&2
6021be86
JS
197 exit $res
198 fi
199
200 # Find current state depending on run success or failure.
201 # A special exit code of 125 means cannot test.
eef12a9a
JS
202 if [ $res -eq 125 ]
203 then
6021be86 204 state='skip'
eef12a9a
JS
205 elif [ $res -gt 0 ]
206 then
43f9d9f3 207 state="$TERM_BAD"
6021be86 208 else
43f9d9f3 209 state="$TERM_GOOD"
6021be86
JS
210 fi
211
212 # We have to use a subshell because "bisect_state" can exit.
305a233c 213 ( bisect_state $state >"$GIT_DIR/BISECT_RUN" )
6021be86
JS
214 res=$?
215
216 cat "$GIT_DIR/BISECT_RUN"
217
43f9d9f3 218 if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
305a233c 219 >/dev/null
eef12a9a 220 then
3145b1a2 221 gettextln "bisect run cannot continue any more" >&2
6021be86
JS
222 exit $res
223 fi
224
eef12a9a
JS
225 if [ $res -ne 0 ]
226 then
3145b1a2
JS
227 eval_gettextln "bisect run failed:
228'bisect_state \$state' exited with error code \$res" >&2
6021be86
JS
229 exit $res
230 fi
a17c4101 231
43f9d9f3 232 if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
eef12a9a 233 then
3145b1a2 234 gettextln "bisect run success"
6021be86
JS
235 exit 0;
236 fi
a17c4101 237
6021be86 238 done
a17c4101
CC
239}
240
412ff738 241bisect_log () {
9570fc1e 242 test -s "$GIT_DIR/BISECT_LOG" || die "$(gettext "We are not bisecting.")"
412ff738
SG
243 cat "$GIT_DIR/BISECT_LOG"
244}
a17c4101 245
cb46d630
AD
246get_terms () {
247 if test -s "$GIT_DIR/BISECT_TERMS"
248 then
249 {
250 read TERM_BAD
251 read TERM_GOOD
252 } <"$GIT_DIR/BISECT_TERMS"
253 fi
254}
255
8cc6a083
LT
256case "$#" in
2570)
6021be86 258 usage ;;
8cc6a083 259*)
6021be86 260 cmd="$1"
cb46d630 261 get_terms
6021be86
JS
262 shift
263 case "$cmd" in
264 help)
265 git bisect -h ;;
266 start)
267 bisect_start "$@" ;;
21e5cfd8 268 bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
6021be86
JS
269 bisect_state "$cmd" "$@" ;;
270 skip)
271 bisect_skip "$@" ;;
272 next)
273 # Not sure we want "next" at the UI level anymore.
517ecb31 274 git bisect--helper --bisect-next "$@" || exit ;;
6021be86
JS
275 visualize|view)
276 bisect_visualize "$@" ;;
277 reset)
5e82c3dd 278 git bisect--helper --bisect-reset "$@" ;;
6021be86
JS
279 replay)
280 bisect_replay "$@" ;;
281 log)
282 bisect_log ;;
283 run)
284 bisect_run "$@" ;;
21b55e33 285 terms)
450ebb73 286 git bisect--helper --bisect-terms "$@" || exit;;
6021be86
JS
287 *)
288 usage ;;
289 esac
8cc6a083 290esac