Skip to content

Commit 0a5280a

Browse files
author
Junio C Hamano
committed
git-bisect: allow bisecting with only one bad commit.
This allows you to say: git bisect start git bisect bad $bad git bisect next to start bisection without knowing a good commit. This would have you try a commit that is half-way since the beginning of the history, which is rather wasteful if you already know a good commit, but if you don't (or your history is short enough that you do not care), there is no reason not to allow this. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4f50671 commit 0a5280a

File tree

2 files changed

+59
-45
lines changed

2 files changed

+59
-45
lines changed

git-bisect.sh

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -168,26 +168,40 @@ bisect_write_good() {
168168
}
169169

170170
bisect_next_check() {
171-
next_ok=no
172-
git show-ref -q --verify refs/bisect/bad &&
173-
test -n "$(git for-each-ref "refs/bisect/good-*")" &&
174-
next_ok=yes
175-
176-
case "$next_ok,$1" in
177-
no,) false ;;
178-
no,fail)
179-
THEN=''
180-
test -d "$GIT_DIR/refs/bisect" || {
181-
echo >&2 'You need to start by "git bisect start".'
182-
THEN='then '
183-
}
184-
echo >&2 'You '$THEN'need to give me at least one good' \
185-
'and one bad revisions.'
186-
echo >&2 '(You can use "git bisect bad" and' \
187-
'"git bisect good" for that.)'
188-
exit 1 ;;
171+
missing_good= missing_bad=
172+
git show-ref -q --verify refs/bisect/bad || missing_bad=t
173+
test -n "$(git for-each-ref "refs/bisect/good-*")" || missing_good=t
174+
175+
case "$missing_good,$missing_bad,$1" in
176+
,,*)
177+
: have both good and bad - ok
178+
;;
179+
*,)
180+
# do not have both but not asked to fail - just report.
181+
false
182+
;;
183+
t,,good)
184+
# have bad but not good. we could bisect although
185+
# this is less optimum.
186+
echo >&2 'Warning: bisecting only with a bad commit.'
187+
if test -t 0
188+
then
189+
printf >&2 'Are you sure [Y/n]? '
190+
case "$(read yesno)" in [Nn]*) exit 1 ;; esac
191+
fi
192+
: bisect without good...
193+
;;
189194
*)
190-
true ;;
195+
THEN=''
196+
test -d "$GIT_DIR/refs/bisect" || {
197+
echo >&2 'You need to start by "git bisect start".'
198+
THEN='then '
199+
}
200+
echo >&2 'You '$THEN'need to give me at least one good' \
201+
'and one bad revisions.'
202+
echo >&2 '(You can use "git bisect bad" and' \
203+
'"git bisect good" for that.)'
204+
exit 1 ;;
191205
esac
192206
}
193207

@@ -198,27 +212,32 @@ bisect_auto_next() {
198212
bisect_next() {
199213
case "$#" in 0) ;; *) usage ;; esac
200214
bisect_autostart
201-
bisect_next_check fail
215+
bisect_next_check good
216+
202217
bad=$(git-rev-parse --verify refs/bisect/bad) &&
203-
good=$(git-rev-parse --sq --revs-only --not \
204-
$(cd "$GIT_DIR" && ls refs/bisect/good-*)) &&
205-
rev=$(eval "git-rev-list --bisect $good $bad -- $(cat "$GIT_DIR/BISECT_NAMES")") || exit
206-
if [ -z "$rev" ]; then
207-
echo "$bad was both good and bad"
208-
exit 1
218+
good=$(git for-each-ref --format='^%(objectname)' \
219+
"refs/bisect/good-*" | tr '[\012]' ' ') &&
220+
eval="git-rev-list --bisect-vars $good $bad --" &&
221+
eval="$eval $(cat "$GIT_DIR/BISECT_NAMES")" &&
222+
eval=$(eval "$eval") &&
223+
eval "$eval" || exit
224+
225+
if [ -z "$bisect_rev" ]; then
226+
echo "$bad was both good and bad"
227+
exit 1
209228
fi
210-
if [ "$rev" = "$bad" ]; then
211-
echo "$rev is first bad commit"
212-
git-diff-tree --pretty $rev
213-
exit 0
229+
if [ "$bisect_rev" = "$bad" ]; then
230+
echo "$bisect_rev is first bad commit"
231+
git-diff-tree --pretty $bisect_rev
232+
exit 0
214233
fi
215-
nr=$(eval "git-rev-list $rev $good -- $(cat $GIT_DIR/BISECT_NAMES)" | wc -l) || exit
216-
echo "Bisecting: $nr revisions left to test after this"
217-
echo "$rev" > "$GIT_DIR/refs/heads/new-bisect"
234+
235+
echo "Bisecting: $bisect_nr revisions left to test after this"
236+
echo "$bisect_rev" >"$GIT_DIR/refs/heads/new-bisect"
218237
git checkout -q new-bisect || exit
219238
mv "$GIT_DIR/refs/heads/new-bisect" "$GIT_DIR/refs/heads/bisect" &&
220239
GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD refs/heads/bisect
221-
git-show-branch "$rev"
240+
git-show-branch "$bisect_rev"
222241
}
223242

224243
bisect_visualize() {

t/t6030-bisect-run.sh

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#
55
test_description='Tests git-bisect functionality'
66

7+
exec </dev/null
8+
79
. ./test-lib.sh
810

911
add_line_into_file()
@@ -37,21 +39,14 @@ test_expect_success \
3739
HASH3=$(git rev-list HEAD | head -2 | tail -1) &&
3840
HASH4=$(git rev-list HEAD | head -1)'
3941

40-
test_expect_success 'bisect does not start with only one bad' '
42+
test_expect_success 'bisect starts with only one bad' '
4143
git bisect reset &&
4244
git bisect start &&
43-
git bisect bad $HASH4 || return 1
44-
45-
if git bisect next
46-
then
47-
echo Oops, should have failed.
48-
false
49-
else
50-
:
51-
fi
45+
git bisect bad $HASH4 &&
46+
git bisect next
5247
'
5348

54-
test_expect_success 'bisect does not start with only one good' '
49+
test_expect_success 'bisect starts with only one good' '
5550
git bisect reset &&
5651
git bisect start &&
5752
git bisect good $HASH1 || return 1

0 commit comments

Comments
 (0)