threads / patch / 13103

patch, 2 partsbisect: warn if given dubious tag or branch as rev

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

## tl;dr

3 messages between Apr 14, 2008 and Apr 15, 2008. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Christian Couder· Apr 14, 2008, 03:41 UTC · lore

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(-)
Show changes to 2 files +54 −14

git-bisect.sh, t/t6030-bisect-porcelain.sh

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
Junio C Hamano· Apr 14, 2008, 23:33 UTC · re: Christian Couder · lore

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

Christian Couder <chriscool@tuxfamily.org> writes:
Show 31 quoted lines
> 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

I see you listed all the subcommand names here but I somehow doubt this is sensible.

The "bisect good $foo bad $bar" example from Ingo's transcript may suggest that good/bad may incorrectly appear by copying and pasting from an incorrect how-to guide, but on the other hand "bisect bad log" to mark the tip of the "log" branch (or "replay or any other common words) to see where in the development trail on that branch things got broken is a perfectly valid and plausible thing to do. Also when you happen to have found something broken while you are working on a more important issue, you may tentatively do "git tag bad $it", and keep working on that important issue, only to come back later and use that lightweight tag to feed "git bisect" with. In either use case, "you named your ref the same as bisect subcommand name, bad boy" is an unnecessary noise.

Christian Couder· Apr 15, 2008, 07:05 UTC · re: Junio C Hamano · lore

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

Le mardi 15 avril 2008, Junio C Hamano a écrit :
Show 40 quoted lines
> Christian Couder <chriscool@tuxfamily.org> writes:
> > 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
>
> I see you listed all the subcommand names here but I somehow doubt this
> is sensible.
>
> The "bisect good $foo bad $bar" example from Ingo's transcript may
> suggest that good/bad may incorrectly appear by copying and pasting from
> an incorrect how-to guide,

Yes, and there are also some "git bisect start good $foo bad $bar" in his transcript. So perhaps we should warn only on "bad" good" and maybe "skip".

Show 9 quoted lines
> but on the other hand "bisect bad log" to mark 
> the tip of the "log" branch (or "replay or any other common words) to see
> where in the development trail on that branch things got broken is a
> perfectly valid and plausible thing to do.  Also when you happen to have
> found something broken while you are working on a more important issue,
> you may tentatively do "git tag bad $it", and keep working on that
> important issue, only to come back later and use that lightweight tag to
> feed "git bisect" with.  In either use case, "you named your ref the same
> as bisect subcommand name, bad boy" is an unnecessary noise.

I think Ingo uses git and git bisect very often, and still he made mistakes that could have resulted in unwanted behavior with nothing to help understand what happened, if he had tags or branches named "good" or "bad".

So it's not for "bad boys" or forcing good naming, but really preventing mistakes.

In bisect_autostart we also have:
		echo >&2 'You need to start by "git bisect start"'
		if test -t 0
		then
			echo >&2 -n 'Do you want me to do it for you [Y/n]? '
			read yesno
			case "$yesno" in
			[Nn]*)
				exit ;;
			esac
			bisect_start
		else
			exit 1
		fi

to make sure that the users know what happens, and help them do what they want. If you prefer I can add something like the above to make sure that the user knows what he is doing.

Thanks, Christian.

← back to recent threads