git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] show-ref: Allow -d, --head to work with --verify
@ 2017-01-21  1:08 Vladimir Panteleev
  2017-01-21  1:08 ` [PATCH v2 1/4] show-ref: Allow " Vladimir Panteleev
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Vladimir Panteleev @ 2017-01-21  1:08 UTC (permalink / raw)
  To: git

This is the second take for "[PATCH] show-ref: Allow --head to work
with --verify". Thanks to Junio for his extensive feedback.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/4] show-ref: Allow --head to work with --verify
  2017-01-21  1:08 [PATCH v2] show-ref: Allow -d, --head to work with --verify Vladimir Panteleev
@ 2017-01-21  1:08 ` Vladimir Panteleev
  2017-01-21  1:08 ` [PATCH v2 2/4] show-ref: Allow -d " Vladimir Panteleev
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Vladimir Panteleev @ 2017-01-21  1:08 UTC (permalink / raw)
  To: git; +Cc: Vladimir Panteleev

Previously, when --verify was specified, show-ref would use a separate
code path which ignored --head. Thus, "git show-ref HEAD" used with
"--verify" (because the user is not interested in seeing
refs/remotes/origin/HEAD), and used with "--head" (because the user
does not want HEAD to be filtered out), i.e. "git show-ref --head
--verify HEAD", did not work as expected.

Instead of insisting that the input begins with "refs/", allow "HEAD"
when "--head" is given in the codepath that handles "--verify", so
that all valid full refnames including HEAD are passed to the same
output machinery.

Signed-off-by: Vladimir Panteleev <git@thecybershadow.net>
---
 builtin/show-ref.c  |  3 ++-
 t/t1403-show-ref.sh | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/builtin/show-ref.c b/builtin/show-ref.c
index 6d4e66900..945a483e3 100644
--- a/builtin/show-ref.c
+++ b/builtin/show-ref.c
@@ -202,7 +202,8 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
 		while (*pattern) {
 			struct object_id oid;
 
-			if (starts_with(*pattern, "refs/") &&
+			if ((starts_with(*pattern, "refs/") ||
+			     (show_head && !strcmp(*pattern, "HEAD"))) &&
 			    !read_ref(*pattern, oid.hash)) {
 				if (!quiet)
 					show_one(*pattern, &oid);
diff --git a/t/t1403-show-ref.sh b/t/t1403-show-ref.sh
index 7e10bcfe3..2fb5dc879 100755
--- a/t/t1403-show-ref.sh
+++ b/t/t1403-show-ref.sh
@@ -164,4 +164,18 @@ test_expect_success 'show-ref --heads, --tags, --head, pattern' '
 	test_cmp expect actual
 '
 
+test_expect_success 'show-ref --verify --head' '
+	echo $(git rev-parse HEAD) HEAD >expect &&
+	git show-ref --verify --head HEAD >actual &&
+	test_cmp expect actual &&
+
+	>expect &&
+
+	git show-ref --verify --head -q HEAD >actual &&
+	test_cmp expect actual &&
+
+	test_must_fail git show-ref --verify --head -q A >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/4] show-ref: Allow -d to work with --verify
  2017-01-21  1:08 [PATCH v2] show-ref: Allow -d, --head to work with --verify Vladimir Panteleev
  2017-01-21  1:08 ` [PATCH v2 1/4] show-ref: Allow " Vladimir Panteleev
@ 2017-01-21  1:08 ` Vladimir Panteleev
  2017-01-21  1:08 ` [PATCH v2 3/4] show-ref: Optimize show_ref a bit Vladimir Panteleev
  2017-01-21  1:08 ` [PATCH v2 4/4] show-ref: Inline show_one Vladimir Panteleev
  3 siblings, 0 replies; 7+ messages in thread
From: Vladimir Panteleev @ 2017-01-21  1:08 UTC (permalink / raw)
  To: git; +Cc: Vladimir Panteleev

Use the same output machinery when --verify is absent or present,
which allows tag dereferencing (-d) to work with --verify. This is
useful when the user wishes to avoid the costly iteration of refs.

Signed-off-by: Vladimir Panteleev <git@thecybershadow.net>
---
 builtin/show-ref.c  | 3 +--
 t/t1403-show-ref.sh | 9 +++++++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/builtin/show-ref.c b/builtin/show-ref.c
index 945a483e3..bcdc1a95e 100644
--- a/builtin/show-ref.c
+++ b/builtin/show-ref.c
@@ -205,8 +205,7 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
 			if ((starts_with(*pattern, "refs/") ||
 			     (show_head && !strcmp(*pattern, "HEAD"))) &&
 			    !read_ref(*pattern, oid.hash)) {
-				if (!quiet)
-					show_one(*pattern, &oid);
+				show_ref(*pattern, &oid, 0, NULL);
 			}
 			else if (!quiet)
 				die("'%s' - not a valid ref", *pattern);
diff --git a/t/t1403-show-ref.sh b/t/t1403-show-ref.sh
index 2fb5dc879..5c540e67f 100755
--- a/t/t1403-show-ref.sh
+++ b/t/t1403-show-ref.sh
@@ -97,6 +97,9 @@ test_expect_success 'show-ref -d' '
 	git show-ref -d refs/tags/A refs/tags/C >actual &&
 	test_cmp expect actual &&
 
+	git show-ref --verify -d refs/tags/A refs/tags/C >actual &&
+	test_cmp expect actual &&
+
 	echo $(git rev-parse refs/heads/master) refs/heads/master >expect &&
 	git show-ref -d master >actual &&
 	test_cmp expect actual &&
@@ -116,6 +119,12 @@ test_expect_success 'show-ref -d' '
 	test_cmp expect actual &&
 
 	test_must_fail git show-ref -d --verify heads/master >actual &&
+	test_cmp expect actual &&
+
+	test_must_fail git show-ref --verify -d A C >actual &&
+	test_cmp expect actual &&
+
+	test_must_fail git show-ref --verify -d tags/A tags/C >actual &&
 	test_cmp expect actual
 
 '
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/4] show-ref: Optimize show_ref a bit
  2017-01-21  1:08 [PATCH v2] show-ref: Allow -d, --head to work with --verify Vladimir Panteleev
  2017-01-21  1:08 ` [PATCH v2 1/4] show-ref: Allow " Vladimir Panteleev
  2017-01-21  1:08 ` [PATCH v2 2/4] show-ref: Allow -d " Vladimir Panteleev
@ 2017-01-21  1:08 ` Vladimir Panteleev
  2017-01-22 22:47   ` Junio C Hamano
  2017-01-21  1:08 ` [PATCH v2 4/4] show-ref: Inline show_one Vladimir Panteleev
  3 siblings, 1 reply; 7+ messages in thread
From: Vladimir Panteleev @ 2017-01-21  1:08 UTC (permalink / raw)
  To: git; +Cc: Vladimir Panteleev

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 <git@thecybershadow.net>
---
 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;
 		}
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 4/4] show-ref: Inline show_one
  2017-01-21  1:08 [PATCH v2] show-ref: Allow -d, --head to work with --verify Vladimir Panteleev
                   ` (2 preceding siblings ...)
  2017-01-21  1:08 ` [PATCH v2 3/4] show-ref: Optimize show_ref a bit Vladimir Panteleev
@ 2017-01-21  1:08 ` Vladimir Panteleev
  3 siblings, 0 replies; 7+ messages in thread
From: Vladimir Panteleev @ 2017-01-21  1:08 UTC (permalink / raw)
  To: git; +Cc: Vladimir Panteleev

As the small function is now only called from a single place, there is
no point in keeping it as a separate function any longer.

Signed-off-by: Vladimir Panteleev <git@thecybershadow.net>
---
 builtin/show-ref.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/builtin/show-ref.c b/builtin/show-ref.c
index 3cf344d47..ec44164d8 100644
--- a/builtin/show-ref.c
+++ b/builtin/show-ref.c
@@ -17,15 +17,6 @@ static int deref_tags, show_head, tags_only, heads_only, found_match, verify,
 static const char **pattern;
 static const char *exclude_existing_arg;
 
-static void show_one(const char *refname, const struct object_id *oid)
-{
-	const char *hex = find_unique_abbrev(oid->hash, abbrev);
-	if (hash_only)
-		printf("%s\n", hex);
-	else
-		printf("%s %s\n", hex, refname);
-}
-
 static int show_ref(const char *refname, const struct object_id *oid,
 		    int flag, void *cbdata)
 {
@@ -74,7 +65,11 @@ static int show_ref(const char *refname, const struct object_id *oid,
 	if (quiet)
 		return 0;
 
-	show_one(refname, oid);
+	hex = find_unique_abbrev(oid->hash, abbrev);
+	if (hash_only)
+		printf("%s\n", hex);
+	else
+		printf("%s %s\n", hex, refname);
 
 	if (!deref_tags)
 		return 0;
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 3/4] show-ref: Optimize show_ref a bit
  2017-01-21  1:08 ` [PATCH v2 3/4] show-ref: Optimize show_ref a bit Vladimir Panteleev
@ 2017-01-22 22:47   ` Junio C Hamano
  2017-01-22 22:55     ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2017-01-22 22:47 UTC (permalink / raw)
  To: Vladimir Panteleev; +Cc: git

Vladimir Panteleev <git@thecybershadow.net> 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 <git@thecybershadow.net>
> ---
>  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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 3/4] show-ref: Optimize show_ref a bit
  2017-01-22 22:47   ` Junio C Hamano
@ 2017-01-22 22:55     ` Junio C Hamano
  0 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2017-01-22 22:55 UTC (permalink / raw)
  To: Vladimir Panteleev; +Cc: git

Junio C Hamano <gitster@pobox.com> writes:

> 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.

... also "error out if the named object did not exist" can be part
of this, which means ...

> 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.

... "if (!has_sha1_file(oid->hash)) die()" in show_ref() would
probably want to be part of this update.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-01-22 22:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-21  1:08 [PATCH v2] show-ref: Allow -d, --head to work with --verify Vladimir Panteleev
2017-01-21  1:08 ` [PATCH v2 1/4] show-ref: Allow " Vladimir Panteleev
2017-01-21  1:08 ` [PATCH v2 2/4] show-ref: Allow -d " Vladimir Panteleev
2017-01-21  1:08 ` [PATCH v2 3/4] show-ref: Optimize show_ref a bit Vladimir Panteleev
2017-01-22 22:47   ` Junio C Hamano
2017-01-22 22:55     ` Junio C Hamano
2017-01-21  1:08 ` [PATCH v2 4/4] show-ref: Inline show_one Vladimir Panteleev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).