Merge branch 'jc/fmt-merge-msg-suppress-destination'
[git/debian.git] / git-bisect.sh
blobea7e684ebb55481b8118adaab6dd2324cf7bf3e8
1 #!/bin/sh
3 USAGE='[help|start|bad|good|new|old|terms|skip|next|reset|visualize|view|replay|log|run]'
4 LONG_USAGE='git bisect help
5 print this long help message.
6 git bisect start [--term-{new,bad}=<term> --term-{old,good}=<term>]
7 [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<pathspec>...]
8 reset bisect state and start bisection.
9 git bisect (bad|new) [<rev>]
10 mark <rev> a known-bad revision/
11 a revision after change in a given property.
12 git bisect (good|old) [<rev>...]
13 mark <rev>... known-good revisions/
14 revisions before change in a given property.
15 git bisect terms [--term-good | --term-bad]
16 show the terms used for old and new commits (default: bad, good)
17 git bisect skip [(<rev>|<range>)...]
18 mark <rev>... untestable revisions.
19 git bisect next
20 find next bisection to test and check it out.
21 git bisect reset [<commit>]
22 finish bisection search and go back to commit.
23 git bisect (visualize|view)
24 show bisect status in gitk.
25 git bisect replay <logfile>
26 replay bisection log.
27 git bisect log
28 show bisect log.
29 git bisect run <cmd>...
30 use <cmd>... to automatically bisect.
32 Please use "git help bisect" to get the full man page.'
34 OPTIONS_SPEC=
35 . git-sh-setup
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"
39 TERM_BAD=bad
40 TERM_GOOD=good
42 bisect_head()
44 if git rev-parse --verify -q BISECT_HEAD > /dev/null
45 then
46 echo BISECT_HEAD
47 else
48 echo HEAD
52 bisect_start() {
53 git bisect--helper --bisect-start $@ || exit
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".
62 trap 'git bisect--helper --bisect-clean-state' 0
63 trap 'exit 255' 1 2 3 15
66 # Check if we can proceed to the next bisect state.
68 git bisect--helper --bisect-auto-next || exit
70 trap '-' 0
73 bisect_skip() {
74 all=''
75 for arg in "$@"
77 case "$arg" in
78 *..*)
79 revs=$(git rev-list "$arg") || die "$(eval_gettext "Bad rev input: \$arg")" ;;
81 revs=$(git rev-parse --sq-quote "$arg") ;;
82 esac
83 all="$all $revs"
84 done
85 eval bisect_state 'skip' $all
88 bisect_state() {
89 git bisect--helper --bisect-autostart || exit
90 state=$1
91 git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
92 get_terms
93 case "$#,$state" in
94 0,*)
95 die "Please call 'bisect_state' with at least one argument." ;;
96 1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip)
97 bisected_head=$(bisect_head)
98 rev=$(git rev-parse --verify "$bisected_head") ||
99 die "$(eval_gettext "Bad rev input: \$bisected_head")"
100 git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
101 git bisect--helper --check-expected-revs "$rev" ;;
102 2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
103 shift
104 hash_list=''
105 for rev in "$@"
107 sha=$(git rev-parse --verify "$rev^{commit}") ||
108 die "$(eval_gettext "Bad rev input: \$rev")"
109 hash_list="$hash_list $sha"
110 done
111 for rev in $hash_list
113 git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
114 done
115 git bisect--helper --check-expected-revs $hash_list ;;
116 *,"$TERM_BAD")
117 die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
119 usage ;;
120 esac
121 git bisect--helper --bisect-auto-next
124 bisect_visualize() {
125 git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
127 if test $# = 0
128 then
129 if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
130 type gitk >/dev/null 2>&1
131 then
132 set gitk
133 else
134 set git log
136 else
137 case "$1" in
138 git*|tig) ;;
139 -*) set git log "$@" ;;
140 *) set git "$@" ;;
141 esac
144 eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
147 bisect_replay () {
148 file="$1"
149 test "$#" -eq 1 || die "$(gettext "No logfile given")"
150 test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")"
151 git bisect--helper --bisect-reset || exit
152 oIFS="$IFS" IFS="$IFS$(printf '\015')"
153 while read git bisect command rev tail
155 test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue
156 if test "$git" = "git-bisect"
157 then
158 rev="$command"
159 command="$bisect"
161 get_terms
162 git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit
163 get_terms
164 case "$command" in
165 start)
166 cmd="bisect_start $rev $tail"
167 eval "$cmd" ;;
168 "$TERM_GOOD"|"$TERM_BAD"|skip)
169 git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;;
170 terms)
171 git bisect--helper --bisect-terms $rev || exit;;
173 die "$(gettext "?? what are you talking about?")" ;;
174 esac
175 done <"$file"
176 IFS="$oIFS"
177 git bisect--helper --bisect-auto-next || exit
180 bisect_run () {
181 git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
183 test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"
185 while true
187 command="$@"
188 eval_gettextln "running \$command"
189 "$@"
190 res=$?
192 # Check for really bad run error.
193 if [ $res -lt 0 -o $res -ge 128 ]
194 then
195 eval_gettextln "bisect run failed:
196 exit code \$res from '\$command' is < 0 or >= 128" >&2
197 exit $res
200 # Find current state depending on run success or failure.
201 # A special exit code of 125 means cannot test.
202 if [ $res -eq 125 ]
203 then
204 state='skip'
205 elif [ $res -gt 0 ]
206 then
207 state="$TERM_BAD"
208 else
209 state="$TERM_GOOD"
212 # We have to use a subshell because "bisect_state" can exit.
213 ( bisect_state $state >"$GIT_DIR/BISECT_RUN" )
214 res=$?
216 cat "$GIT_DIR/BISECT_RUN"
218 if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
219 >/dev/null
220 then
221 gettextln "bisect run cannot continue any more" >&2
222 exit $res
225 if [ $res -ne 0 ]
226 then
227 eval_gettextln "bisect run failed:
228 'bisect_state \$state' exited with error code \$res" >&2
229 exit $res
232 if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
233 then
234 gettextln "bisect run success"
235 exit 0;
238 done
241 bisect_log () {
242 test -s "$GIT_DIR/BISECT_LOG" || die "$(gettext "We are not bisecting.")"
243 cat "$GIT_DIR/BISECT_LOG"
246 get_terms () {
247 if test -s "$GIT_DIR/BISECT_TERMS"
248 then
250 read TERM_BAD
251 read TERM_GOOD
252 } <"$GIT_DIR/BISECT_TERMS"
256 case "$#" in
258 usage ;;
260 cmd="$1"
261 get_terms
262 shift
263 case "$cmd" in
264 help)
265 git bisect -h ;;
266 start)
267 bisect_start "$@" ;;
268 bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
269 bisect_state "$cmd" "$@" ;;
270 skip)
271 bisect_skip "$@" ;;
272 next)
273 # Not sure we want "next" at the UI level anymore.
274 git bisect--helper --bisect-next "$@" || exit ;;
275 visualize|view)
276 bisect_visualize "$@" ;;
277 reset)
278 git bisect--helper --bisect-reset "$@" ;;
279 replay)
280 bisect_replay "$@" ;;
281 log)
282 bisect_log ;;
283 run)
284 bisect_run "$@" ;;
285 terms)
286 git bisect--helper --bisect-terms "$@" || exit;;
288 usage ;;
289 esac
290 esac