Skip to content

Commit 8cc6a08

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
[PATCH] Making it easier to find which change introduced a bug
This adds a new "git bisect" command. - "git bisect start" start bisection search. - "git bisect bad <rev>" mark some version known-bad (if no arguments, then current HEAD) - "git bisect good <revs>..." mark some versions known-good (if no arguments, then current HEAD) - "git bisect reset <branch>" done with bisection search and go back to your work (if no arguments, then "master"). The way you use it is: git bisect start git bisect bad # Current version is bad git bisect good v2.6.13-rc2 # v2.6.13-rc2 was the last version # tested that was good When you give at least one bad and one good versions, it will bisect the revision tree and say something like: Bisecting: 675 revisions left to test after this and check out the state in the middle. Now, compile that kernel, and boot it. Now, let's say that this booted kernel works fine, then just do git bisect good # this one is good which will now say Bisecting: 337 revisions left to test after this and you continue along, compiling that one, testing it, and depending on whether it is good or bad, you say "git bisect good" or "git bisect bad", and ask for the next bisection. Until you have no more left, and you'll have been left with the first bad kernel rev in "refs/bisect/bad". Oh, and then after you want to reset to the original head, do a git bisect reset to get back to the master branch, instead of being in one of the bisection branches ("git bisect start" will do that for you too, actually: it will reset the bisection state, and before it does that it checks that you're not using some old bisection branch). Not really any harder than doing series of "quilt push" and "quilt pop", now is it? [jc: This patch is a rework based on what Linus posted to the list. The changes are: - The original introduced four separate commands, which was three too many, so I merged them into one with subcommands. - Since the next thing you would want to do after telling it "bad" and "good" is always to bisect, this version does it automatically for you. - I think the termination condition was wrong. The original version checked if the set of revisions reachable from next bisection but not rechable from any of the known good ones is empty, but if the current bisection was a bad one, this would not terminate, so I changed it to terminate it when the set becomes a singleton or empty. - Removed the use of shell array variable. ] Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 9e95049 commit 8cc6a08

File tree

2 files changed

+159
-1
lines changed

2 files changed

+159
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ SCRIPTS=git git-apply-patch-script git-merge-one-file-script git-prune-script \
6262
git-format-patch-script git-sh-setup-script git-push-script \
6363
git-branch-script git-parse-remote git-verify-tag-script \
6464
git-ls-remote-script git-clone-dumb-http git-rename-script \
65-
git-request-pull-script
65+
git-request-pull-script git-bisect-script
6666

6767
PROG= git-update-cache git-diff-files git-init-db git-write-tree \
6868
git-read-tree git-commit-tree git-cat-file git-fsck-cache \

git-bisect-script

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#!/bin/sh
2+
. git-sh-setup-script || dir "Not a git archive"
3+
4+
usage() {
5+
echo >&2 'usage: git bisect [start | bad | good | next | reset]
6+
git bisect start reset bisect state and start bisection.
7+
git bisect bad [<rev>] mark <rev> a known-bad revision.
8+
git bisect good [<rev>...] mark <rev>... known-good revisions.
9+
git bisect next find next bisection to test and check it out.
10+
git bisect reset [<branch>] finish bisection search and go back to branch.'
11+
exit 1
12+
}
13+
14+
bisect_autostart() {
15+
test -d "$GIT_DIR/refs/bisect" || {
16+
echo >&2 'You need to start by "git bisect start"'
17+
if test -t 0
18+
then
19+
echo >&2 -n 'Do you want me to do it for you [Y/n]? '
20+
read yesno
21+
case "$yesno" in
22+
[Nn]*)
23+
exit ;;
24+
esac
25+
bisect_start
26+
else
27+
exit 1
28+
fi
29+
}
30+
}
31+
32+
bisect_start() {
33+
case "$#" in 0) ;; *) usage ;; esac
34+
#
35+
# Verify HEAD. If we were bisecting before this, reset to the
36+
# top-of-line master first!
37+
#
38+
head=$(readlink $GIT_DIR/HEAD) || die "Bad HEAD - I need a symlink"
39+
case "$head" in
40+
refs/heads/bisect*)
41+
git checkout master || exit
42+
;;
43+
refs/heads/*)
44+
;;
45+
*)
46+
die "Bad HEAD - strange symlink"
47+
;;
48+
esac
49+
50+
#
51+
# Get rid of any old bisect state
52+
#
53+
rm -f "$GIT_DIR/refs/heads/bisect"
54+
rm -rf "$GIT_DIR/refs/bisect/"
55+
mkdir "$GIT_DIR/refs/bisect"
56+
}
57+
58+
bisect_bad() {
59+
bisect_autostart
60+
case "$#" in 0 | 1) ;; *) usage ;; esac
61+
rev=$(git-rev-parse --revs-only --verify --default HEAD "$@") || exit
62+
echo "$rev" > "$GIT_DIR/refs/bisect/bad"
63+
bisect_auto_next
64+
}
65+
66+
bisect_good() {
67+
bisect_autostart
68+
case "$#" in
69+
0) revs=$(git-rev-parse --verify HEAD) || exit ;;
70+
*) revs=$(git-rev-parse --revs-only "$@") || exit ;;
71+
esac
72+
for rev in $revs
73+
do
74+
echo "$rev" >"$GIT_DIR/refs/bisect/good-$rev"
75+
done
76+
bisect_auto_next
77+
}
78+
79+
bisect_next_check() {
80+
next_ok=no
81+
test -f "$GIT_DIR/refs/bisect/bad" &&
82+
case "$(cd "$GIT_DIR" && echo refs/bisect/good-*)" in
83+
refs/bisect/good-\*) ;;
84+
*) next_ok=yes ;;
85+
esac
86+
case "$next_ok,$1" in
87+
no,) false ;;
88+
no,fail)
89+
echo >&2 'You need to give me at least one good and one bad revisions.'
90+
exit 1 ;;
91+
*)
92+
true ;;
93+
esac
94+
}
95+
96+
bisect_auto_next() {
97+
bisect_next_check && bisect_next
98+
}
99+
100+
bisect_next() {
101+
case "$#" in 0) ;; *) usage ;; esac
102+
bisect_autostart
103+
bisect_next_check fail
104+
bad=$(git-rev-parse --verify refs/bisect/bad) &&
105+
good=$(git-rev-parse --sq --revs-only --not \
106+
$(cd "$GIT_DIR" && ls refs/bisect/good-*)) &&
107+
rev=$(eval "git-rev-list --bisect $good $bad") || exit
108+
nr=$(eval "git-rev-list $rev $good" | wc -l) || exit
109+
if [ "$nr" -le "1" ]; then
110+
echo "$bad is first bad commit"
111+
git-diff-tree --pretty $bad
112+
exit 0
113+
fi
114+
echo "Bisecting: $nr revisions left to test after this"
115+
echo "$rev" > "$GIT_DIR/refs/heads/new-bisect"
116+
git checkout new-bisect || exit
117+
mv "$GIT_DIR/refs/heads/new-bisect" "$GIT_DIR/refs/heads/bisect" &&
118+
ln -sf refs/heads/bisect "$GIT_DIR/HEAD"
119+
}
120+
121+
bisect_reset() {
122+
case "$#" in
123+
0) branch=master ;;
124+
1) test -f "$GIT_DIR/refs/heads/$1" || {
125+
echo >&2 "$1 does not seem to be a valid branch"
126+
exit 1
127+
}
128+
branch="$1" ;;
129+
*)
130+
usage ;;
131+
esac
132+
git checkout "$branch" &&
133+
rm -fr "$GIT_DIR/refs/bisect"
134+
rm -f "$GIT_DIR/refs/reads/bisect"
135+
}
136+
137+
case "$#" in
138+
0)
139+
usage ;;
140+
*)
141+
cmd="$1"
142+
shift
143+
case "$cmd" in
144+
start)
145+
bisect_start "$@" ;;
146+
bad)
147+
bisect_bad "$@" ;;
148+
good)
149+
bisect_good "$@" ;;
150+
next)
151+
# Not sure we want "next" at the UI level anymore.
152+
bisect_next "$@" ;;
153+
reset)
154+
bisect_reset "$@" ;;
155+
*)
156+
usage ;;
157+
esac
158+
esac

0 commit comments

Comments
 (0)