git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] bisect: warn if given dubious tag or branch as rev

From
Christian Couder <chriscool@tuxfamily.org>
Date
Apr 14, 2008, 03:41 UTC
Message-ID
<20080414054151.52e55e7f.chriscool@tuxfamily.org>

This patch refactors rev parsing code in a new "bisect_parse_rev" function, and adds warnings in case a rev with a name that is the same as one of the bisect subcommands is given where a revision is expected.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 git-bisect.sh               |   47 ++++++++++++++++++++++++++++++------------
 t/t6030-bisect-porcelain.sh |   21 +++++++++++++++++++
 2 files changed, 54 insertions(+), 14 deletions(-)
diff --git a/git-bisect.sh b/git-bisect.sh
index 6b43461..a090b97 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -61,6 +61,31 @@ bisect_autostart() {
 	}
 }
 
+bisect_parse_rev() {
+	rev="$1"
+	dont_die_on_wrong_rev="$2"
+	name="$rev^{commit}"
+	dubious_rev_name=''
+
+	case "$rev" in
+		HEAD)
+			name='HEAD' ;;
+		help|start|bad|good|skip|next|reset|visualize|replay|log|run)
+			dubious_rev_name='yes' ;;
+	esac
+
+	sha=$(git rev-parse --verify "$name" 2>/dev/null)
+	status=$?
+
+	test $status -eq 0 && test -n "$dubious_rev_name" &&
+		echo >&2 "A tag or branch named '$rev' exists and will be used." \
+			"This may be a syntax error, please check."
+
+	test -n "$dont_die_on_wrong_rev" && return $status
+
+	test $status -eq 0 || die "Bad rev input: $rev"
+}
+
 bisect_start() {
 	#
 	# Verify HEAD. If we were bisecting before this, reset to the
@@ -68,7 +93,7 @@ bisect_start() {
 	#
 	head=$(GIT_DIR="$GIT_DIR" git symbolic-ref HEAD) ||
 	head=$(GIT_DIR="$GIT_DIR" git rev-parse --verify HEAD) ||
-	die "Bad HEAD - I need a HEAD"
+		die "Bad HEAD - I need a HEAD"
 	start_head=''
 	case "$head" in
 	refs/heads/bisect)
@@ -98,9 +123,9 @@ bisect_start() {
 	#
 	# Check for one bad and then some good revisions.
 	#
-	has_double_dash=0
+	dont_die_on_wrong_rev='yes'
 	for arg; do
-	    case "$arg" in --) has_double_dash=1; break ;; esac
+	    case "$arg" in --) dont_die_on_wrong_rev=''; break ;; esac
 	done
 	orig_args=$(sq "$@")
 	bad_seen=0
@@ -113,16 +138,12 @@ bisect_start() {
 		break
 		;;
 	    *)
-		rev=$(git rev-parse --verify "$arg^{commit}" 2>/dev/null) || {
-		    test $has_double_dash -eq 1 &&
-		        die "'$arg' does not appear to be a valid revision"
-		    break
-		}
+		bisect_parse_rev "$arg" "$dont_die_on_wrong_rev" || break
 		case $bad_seen in
 		0) state='bad' ; bad_seen=1 ;;
 		*) state='good' ;;
 		esac
-		eval="$eval bisect_write '$state' '$rev' 'nolog'; "
+		eval="$eval bisect_write '$state' '$sha' 'nolog'; "
 		shift
 		;;
 	    esac
@@ -156,16 +177,14 @@ bisect_state() {
 	0,*)
 		die "Please call 'bisect_state' with at least one argument." ;;
 	1,bad|1,good|1,skip)
-		rev=$(git rev-parse --verify HEAD) ||
-			die "Bad rev input: HEAD"
-		bisect_write "$state" "$rev" ;;
+		bisect_parse_rev HEAD
+		bisect_write "$state" "$sha" ;;
 	2,bad|*,good|*,skip)
 		shift
 		eval=''
 		for rev in "$@"
 		do
-			sha=$(git rev-parse --verify "$rev^{commit}") ||
-				die "Bad rev input: $rev"
+			bisect_parse_rev "$rev"
 			eval="$eval bisect_write '$state' '$sha'; "
 		done
 		eval "$eval" ;;
diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
index 5e3e544..dc95117 100755
--- a/t/t6030-bisect-porcelain.sh
+++ b/t/t6030-bisect-porcelain.sh
@@ -89,6 +89,27 @@ test_expect_success 'bisect fails if given any junk instead of revs' '
 	git bisect bad $HASH4
 '
 
+test_expect_success 'bisect warns if given dubious tags or branches' '
+	git branch bad $HASH4 &&
+	git bisect start bad -- 2>start.output &&
+	grep "bad" start.output &&
+	grep "may be a syntax error" start.output &&
+	git tag good $HASH1 &&
+	git bisect start $HASH4 $HASH2 good -- 2>start.output &&
+	grep "good" start.output &&
+	grep "may be a syntax error" start.output &&
+	git bisect start &&
+	git bisect good $HASH2 bad 2>good.output &&
+	grep "bad" good.output &&
+	grep "may be a syntax error" good.output &&
+	git bisect start &&
+	git bisect bad bad 2>bad.output &&
+	grep "bad" bad.output &&
+	grep "may be a syntax error" bad.output &&
+	git branch -D bad &&
+	git tag -d good
+'
+
 test_expect_success 'bisect reset: back in the master branch' '
 	git bisect reset &&
 	echo "* master" > branch.expect &&
-- 
1.5.5.50.ge6e82.dirty
Next: Junio C Hamano
Message 1 of 3 in “bisect: warn if given dubious tag or branch as rev”
  1. 2/2 bisect: warn if given dubious tag or branch as revChristian Couder, Apr 14, 2008
  2. Junio C HamanoApr 14, 2008
  3. Christian CouderApr 15, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.