@matheusbsbs / matheus-bagliano / commits / a17c410100
Fork 0

Bisect: implement "git bisect run <cmd>..." to automatically bisect.

This idea was suggested by Bill Lear (Message-ID: <17920.38942.364466.642979@lisa.zopyra.com>) and I think it is a very good one. This patch adds a new test file for "git bisect run", but there is currently only one basic test. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Christian Couder committed Mar 23, 2007 at 08:49 UTC a17c4101003dbadc13d404a5dd5d106de1e57a3a
3 files changed +138 -3
Documentation/git-bisect.txt
+30
@@ -22,6 +22,7 @@ depending on the subcommand:
22 git bisect visualize
23 git bisect replay <logfile>
24 git bisect log
25 + git bisect run <cmd>...
26
27 This command uses 'git-rev-list --bisect' option to help drive
28 the binary search process to find which change introduced a bug,
@@ -121,6 +122,35 @@ like this:
122 $ git bisect start arch/i386 include/asm-i386
123 ------------
124
125 +If you have a script that can tell if the current
126 +source code is good or bad, you can automatically bisect using:
127 +
128 +------------
129 +$ git bisect run my_script
130 +------------
131 +
132 +Note that the "run" script (`my_script` in the above example)
133 +should exit with code 0 in
134 +case the current source code is good and with a code between 1 and 127
135 +(included) in case the current source code is bad.
136 +
137 +Any other exit code (a program that does "exit(-1)" leaves $? = 255,
138 +see exit(3) manual page, the value is chopped with "& 0377") will
139 +abort the automatic bisect process.
140 +
141 +You may often find that during bisect you want to have near-constant
142 +tweaks (e.g., s/#define DEBUG 0/#define DEBUG 1/ in a header file, or
143 +"revision that does not have this commit needs this patch applied to
144 +work around other problem this bisection is not interested in")
145 +applied to the revision being tested.
146 +
147 +To cope with such a situation, after the inner git-bisect finds the
148 +next revision to test, with the "run" script, you can apply that tweak
149 +before compiling, run the real test, and after the test decides if the
150 +revision (possibly with the needed tweaks) passed the test, rewind the
151 +tree to the pristine state. Finally the "run" script can exit with
152 +the status of the real test to let "git bisect run" command loop to
153 +know the outcome.
154
155 Author
156 ------
git-bisect.sh
+51 -3
@@ -1,14 +1,15 @@
1 #!/bin/sh
2
3 -USAGE='[start|bad|good|next|reset|visualize|replay|log]'
3 +USAGE='[start|bad|good|next|reset|visualize|replay|log|run]'
4 LONG_USAGE='git bisect start [<pathspec>] reset bisect state and start bisection.
5 git bisect bad [<rev>] mark <rev> a known-bad revision.
6 git bisect good [<rev>...] mark <rev>... known-good revisions.
7 git bisect next find next bisection to test and check it out.
8 git bisect reset [<branch>] finish bisection search and go back to branch.
9 git bisect visualize show bisect status in gitk.
10 -git bisect replay <logfile> replay bisection log
11 -git bisect log show bisect log.'
10 +git bisect replay <logfile> replay bisection log.
11 +git bisect log show bisect log.
12 +git bisect run <cmd>... use <cmd>... to automatically bisect.'
13
14 . git-sh-setup
15 require_work_tree
@@ -185,6 +186,7 @@ bisect_reset() {
186 rm -f "$GIT_DIR/refs/heads/bisect" "$GIT_DIR/head-name"
187 rm -f "$GIT_DIR/BISECT_LOG"
188 rm -f "$GIT_DIR/BISECT_NAMES"
189 + rm -f "$GIT_DIR/BISECT_RUN"
190 fi
191 }
192
@@ -220,6 +222,50 @@ bisect_replay () {
222 bisect_auto_next
223 }
224
225 +bisect_run () {
226 + while true
227 + do
228 + echo "running $@"
229 + "$@"
230 + res=$?
231 +
232 + # Check for really bad run error.
233 + if [ $res -lt 0 -o $res -ge 128 ]; then
234 + echo >&2 "bisect run failed:"
235 + echo >&2 "exit code $res from '$@' is < 0 or >= 128"
236 + exit $res
237 + fi
238 +
239 + # Use "bisect_good" or "bisect_bad"
240 + # depending on run success or failure.
241 + if [ $res -gt 0 ]; then
242 + next_bisect='bisect_bad'
243 + else
244 + next_bisect='bisect_good'
245 + fi
246 +
247 + # We have to use a subshell because bisect_good or
248 + # bisect_bad functions can exit.
249 + ( $next_bisect > "$GIT_DIR/BISECT_RUN" )
250 + res=$?
251 +
252 + cat "$GIT_DIR/BISECT_RUN"
253 +
254 + if [ $res -ne 0 ]; then
255 + echo >&2 "bisect run failed:"
256 + echo >&2 "$next_bisect exited with error code $res"
257 + exit $res
258 + fi
259 +
260 + if grep "is first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
261 + echo "bisect run success"
262 + exit 0;
263 + fi
264 +
265 + done
266 +}
267 +
268 +
269 case "$#" in
270 0)
271 usage ;;
@@ -244,6 +290,8 @@ case "$#" in
290 bisect_replay "$@" ;;
291 log)
292 cat "$GIT_DIR/BISECT_LOG" ;;
293 + run)
294 + bisect_run "$@" ;;
295 *)
296 usage ;;
297 esac
t/t6030-bisect-run.sh new
+57
@@ -0,0 +1,57 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2007 Christian Couder
4 +#
5 +test_description='Tests git-bisect run functionality'
6 +
7 +. ./test-lib.sh
8 +
9 +add_line_into_file()
10 +{
11 + _line=$1
12 + _file=$2
13 +
14 + if [ -f "$_file" ]; then
15 + echo "$_line" >> $_file || return $?
16 + MSG="Add <$_line> into <$_file>."
17 + else
18 + echo "$_line" > $_file || return $?
19 + git add $_file || return $?
20 + MSG="Create file <$_file> with <$_line> inside."
21 + fi
22 +
23 + git-commit -m "$MSG" $_file
24 +}
25 +
26 +HASH1=
27 +HASH3=
28 +HASH4=
29 +
30 +test_expect_success \
31 + 'set up basic repo with 1 file (hello) and 4 commits' \
32 + 'add_line_into_file "1: Hello World" hello &&
33 + add_line_into_file "2: A new day for git" hello &&
34 + add_line_into_file "3: Another new day for git" hello &&
35 + add_line_into_file "4: Ciao for now" hello &&
36 + HASH1=$(git rev-list HEAD | tail -1) &&
37 + HASH3=$(git rev-list HEAD | head -2 | tail -1) &&
38 + HASH4=$(git rev-list HEAD | head -1)'
39 +
40 +# We want to automatically find the commit that
41 +# introduced "Another" into hello.
42 +test_expect_success \
43 + 'git bisect run simple case' \
44 + 'echo "#!/bin/sh" > test_script.sh &&
45 + echo "grep Another hello > /dev/null" >> test_script.sh &&
46 + echo "test \$? -ne 0" >> test_script.sh &&
47 + chmod +x test_script.sh &&
48 + git bisect start &&
49 + git bisect good $HASH1 &&
50 + git bisect bad $HASH4 &&
51 + git bisect run ./test_script.sh > my_bisect_log.txt &&
52 + grep "$HASH3 is first bad commit" my_bisect_log.txt'
53 +
54 +#
55 +#
56 +test_done
57 +