threads / patch / 44926

v2, 4 partsshow-ref: Inline show_one

Subject: [PATCH v2 4/4] show-ref: Inline show_one

## tl;dr

7 messages between Jan 21, 2017 and Jan 22, 2017. Diffs are folded; open one to read it.

replies: 6people: 2as markdown or json

Vladimir Panteleev· Jan 21, 2017, 01:08 UTC · lore

[PATCH v2] show-ref: Allow -d, --head to work with --verify

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

Vladimir Panteleev· Jan 21, 2017, 01:08 UTC · re: Vladimir Panteleev · lore

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(-)
Show changes to builtin/show-ref.c +5 −10
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
Vladimir Panteleev· Jan 21, 2017, 01:08 UTC · re: Vladimir Panteleev · lore

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

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(-)
Show changes to builtin/show-ref.c +1 −4
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
Junio C Hamano· Jan 22, 2017, 22:47 UTC · re: Vladimir Panteleev · lore

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

Vladimir Panteleev <git@thecybershadow.net> writes:
Show 36 quoted lines
> 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.
Junio C Hamano· Jan 22, 2017, 22:55 UTC · re: Junio C Hamano · lore

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

Junio C Hamano <gitster@pobox.com> writes:
Show 16 quoted lines
> 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 ...

Show 6 quoted lines
> 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.

Vladimir Panteleev· Jan 21, 2017, 01:08 UTC · re: Vladimir Panteleev · lore

[PATCH v2 1/4] show-ref: Allow --head to work with --verify

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

builtin/show-ref.c, t/t1403-show-ref.sh

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
Vladimir Panteleev· Jan 21, 2017, 01:08 UTC · re: Vladimir Panteleev · lore

[PATCH v2 2/4] show-ref: Allow -d to work with --verify

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

builtin/show-ref.c, t/t1403-show-ref.sh

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

← back to recent threads