doc: log, gitk: move '-L' description to 'line-range-options.txt'
[git] / git-bisect.sh
1 #!/bin/sh
2
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.
31
32 Please use "git help bisect" to get the full man page.'
33
34 OPTIONS_SPEC=
35 . git-sh-setup
36
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
41
42 bisect_head()
43 {
44         if git rev-parse --verify -q BISECT_HEAD > /dev/null
45         then
46                 echo BISECT_HEAD
47         else
48                 echo HEAD
49         fi
50 }
51
52 bisect_start() {
53         git bisect--helper --bisect-start $@ || exit
54
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         #
62         trap 'git bisect--helper --bisect-clean-state' 0
63         trap 'exit 255' 1 2 3 15
64
65         #
66         # Check if we can proceed to the next bisect state.
67         #
68         git bisect--helper --bisect-auto-next || exit
69
70         trap '-' 0
71 }
72
73 bisect_skip() {
74         all=''
75         for arg in "$@"
76         do
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
86 }
87
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 "$@"
106                 do
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
112                 do
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.")" ;;
118         *)
119                 usage ;;
120         esac
121         git bisect--helper --bisect-auto-next
122 }
123
124 bisect_visualize() {
125         git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
126
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
135                 fi
136         else
137                 case "$1" in
138                 git*|tig) ;;
139                 -*)     set git log "$@" ;;
140                 *)      set git "$@" ;;
141                 esac
142         fi
143
144         eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
145 }
146
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
154         do
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"
160                 fi
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;;
172                 *)
173                         die "$(gettext "?? what are you talking about?")" ;;
174                 esac
175         done <"$file"
176         IFS="$oIFS"
177         git bisect--helper --bisect-auto-next || exit
178 }
179
180 bisect_run () {
181         git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
182
183         test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"
184
185         while true
186         do
187                 command="$@"
188                 eval_gettextln "running \$command"
189                 "$@"
190                 res=$?
191
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
198                 fi
199
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"
210                 fi
211
212                 # We have to use a subshell because "bisect_state" can exit.
213                 ( bisect_state $state >"$GIT_DIR/BISECT_RUN" )
214                 res=$?
215
216                 cat "$GIT_DIR/BISECT_RUN"
217
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
223                 fi
224
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
230                 fi
231
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;
236                 fi
237
238         done
239 }
240
241 bisect_log () {
242         test -s "$GIT_DIR/BISECT_LOG" || die "$(gettext "We are not bisecting.")"
243         cat "$GIT_DIR/BISECT_LOG"
244 }
245
246 get_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
256 case "$#" in
257 0)
258         usage ;;
259 *)
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;;
287         *)
288                 usage ;;
289         esac
290 esac