Merge branch 'rs/pack-write-hashwrite-simplify'
[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_skip() {
43         all=''
44         for arg in "$@"
45         do
46                 case "$arg" in
47                 *..*)
48                         revs=$(git rev-list "$arg") || die "$(eval_gettext "Bad rev input: \$arg")" ;;
49                 *)
50                         revs=$(git rev-parse --sq-quote "$arg") ;;
51                 esac
52                 all="$all $revs"
53         done
54         eval git bisect--helper --bisect-state 'skip' $all
55 }
56
57 bisect_visualize() {
58         git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
59
60         if test $# = 0
61         then
62                 if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
63                         type gitk >/dev/null 2>&1
64                 then
65                         set gitk
66                 else
67                         set git log
68                 fi
69         else
70                 case "$1" in
71                 git*|tig) ;;
72                 -*)     set git log "$@" ;;
73                 *)      set git "$@" ;;
74                 esac
75         fi
76
77         eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
78 }
79
80 bisect_replay () {
81         file="$1"
82         test "$#" -eq 1 || die "$(gettext "No logfile given")"
83         test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")"
84         git bisect--helper --bisect-reset || exit
85         oIFS="$IFS" IFS="$IFS$(printf '\015')"
86         while read git bisect command rev tail
87         do
88                 test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue
89                 if test "$git" = "git-bisect"
90                 then
91                         rev="$command"
92                         command="$bisect"
93                 fi
94                 get_terms
95                 git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit
96                 get_terms
97                 case "$command" in
98                 start)
99                         eval "git bisect--helper --bisect-start $rev $tail" ;;
100                 "$TERM_GOOD"|"$TERM_BAD"|skip)
101                         git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;;
102                 terms)
103                         git bisect--helper --bisect-terms $rev || exit;;
104                 *)
105                         die "$(gettext "?? what are you talking about?")" ;;
106                 esac
107         done <"$file"
108         IFS="$oIFS"
109         git bisect--helper --bisect-auto-next || exit
110 }
111
112 bisect_run () {
113         git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
114
115         test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"
116
117         while true
118         do
119                 command="$@"
120                 eval_gettextln "running \$command"
121                 "$@"
122                 res=$?
123
124                 # Check for really bad run error.
125                 if [ $res -lt 0 -o $res -ge 128 ]
126                 then
127                         eval_gettextln "bisect run failed:
128 exit code \$res from '\$command' is < 0 or >= 128" >&2
129                         exit $res
130                 fi
131
132                 # Find current state depending on run success or failure.
133                 # A special exit code of 125 means cannot test.
134                 if [ $res -eq 125 ]
135                 then
136                         state='skip'
137                 elif [ $res -gt 0 ]
138                 then
139                         state="$TERM_BAD"
140                 else
141                         state="$TERM_GOOD"
142                 fi
143
144                 git bisect--helper --bisect-state $state >"$GIT_DIR/BISECT_RUN"
145                 res=$?
146
147                 cat "$GIT_DIR/BISECT_RUN"
148
149                 if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
150                         >/dev/null
151                 then
152                         gettextln "bisect run cannot continue any more" >&2
153                         exit $res
154                 fi
155
156                 if [ $res -ne 0 ]
157                 then
158                         eval_gettextln "bisect run failed:
159 'bisect-state \$state' exited with error code \$res" >&2
160                         exit $res
161                 fi
162
163                 if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
164                 then
165                         gettextln "bisect run success"
166                         exit 0;
167                 fi
168
169         done
170 }
171
172 bisect_log () {
173         test -s "$GIT_DIR/BISECT_LOG" || die "$(gettext "We are not bisecting.")"
174         cat "$GIT_DIR/BISECT_LOG"
175 }
176
177 get_terms () {
178         if test -s "$GIT_DIR/BISECT_TERMS"
179         then
180                 {
181                 read TERM_BAD
182                 read TERM_GOOD
183                 } <"$GIT_DIR/BISECT_TERMS"
184         fi
185 }
186
187 case "$#" in
188 0)
189         usage ;;
190 *)
191         cmd="$1"
192         get_terms
193         shift
194         case "$cmd" in
195         help)
196                 git bisect -h ;;
197         start)
198                 git bisect--helper --bisect-start "$@" ;;
199         bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
200                 git bisect--helper --bisect-state "$cmd" "$@" ;;
201         skip)
202                 bisect_skip "$@" ;;
203         next)
204                 # Not sure we want "next" at the UI level anymore.
205                 git bisect--helper --bisect-next "$@" || exit ;;
206         visualize|view)
207                 bisect_visualize "$@" ;;
208         reset)
209                 git bisect--helper --bisect-reset "$@" ;;
210         replay)
211                 bisect_replay "$@" ;;
212         log)
213                 bisect_log ;;
214         run)
215                 bisect_run "$@" ;;
216         terms)
217                 git bisect--helper --bisect-terms "$@" || exit;;
218         *)
219                 usage ;;
220         esac
221 esac