Subject: Re: [PATCH v2 3/4] show-ref: Optimize show_ref a bit

From: Junio C Hamano <[email-masked]>
Date: Sun, 22 Jan 2017 14:47:47 -0800

Vladimir Panteleev <[email-masked]> writes:

> The inner `if (verify)' check was not being used before the preceding
> commit, as show_ref was never being called from a code path where
> verify was non-zero.
>
> Adding a `!verify' check to the outer if skips an unnecessary string
> comparison when verify is non-zero, and show_ref is already called
> with a reference exactly matching pattern.
>
> Signed-off-by: Vladimir Panteleev <[email-masked]>
> ---
>  builtin/show-ref.c | 5 +----
>  1 file changed, 1 insertion(+), 4 deletions(-)
>
> diff --git a/builtin/show-ref.c b/builtin/show-ref.c
> index bcdc1a95e..3cf344d47 100644
> --- a/builtin/show-ref.c
> +++ b/builtin/show-ref.c
> @@ -43,7 +43,7 @@ static int show_ref(const char *refname, const struct object_id *oid,
>  		if (!match)
>  			return 0;
>  	}
> -	if (pattern) {
> +	if (pattern && !verify) {
>  		int reflen = strlen(refname);
>  		const char **p = pattern, *m;
>  		while ((m = *p++) != NULL) {
> @@ -54,9 +54,6 @@ static int show_ref(const char *refname, const struct object_id *oid,
>  				continue;
>  			if (len == reflen)
>  				goto match;
> -			/* "--verify" requires an exact match */
> -			if (verify)
> -				continue;
>  			if (refname[reflen - len - 1] == '/')
>  				goto match;
>  		}

Having to do this change probably is an indication that the division
of labour between show_ref() and show_one() up to this step needs to
be rethought.

Conceptually, "git show-ref" works in two ways:

 * When in --verify mode, the end user gives which refnames to
   consider showing.

 * Otherwise the end user gives pattern and the command infers which
   refnames to consider showing using the pattern.

And for the refnames that are considered for showing, we may do
various things, like -d to deref and --quiet to be silent.  We want
this actual "output" step to be the same between two modes of
operation.

So a better division of labour would be:

 * Make show_ref() about "using pattern, enumerate what refs to
   show" and call show_one().

 * Update show_one() and teach _it_ to handle quiet and deref_tags.

 * From cmd_show_ref(), in --verify mode, make sure the ref exists
   and call show_one(), because we do not do the "using pattern,
   enumerate what refs to show" at all.

And from that point of view, I think 4/4 is going in the wrong
direction.

I also think that "git show-ref --verify HEAD" should work; it is OK
that the command accepts "--head" in that case, but when in --verify
mode, the end user gives which refnames to consider showing, and
HEAD given from the command line is a signal enough that that
psuedo-ref is what the end user wants to be shown.  "--head" is
about not filtering in "enumerate the ones that match the given
patterns" mode, and it feels unnecessary to require it in "--verify"
mode.

Thanks.