Skip to contentSkip to conversation

show-ref: Allow -d, --head to work with --verify

PATCH In review Vladimir Panteleev 2 participants 8 messages Jan 23, 2017 – Jan 24, 2017

lore
Share & tools
lore
#3 lore raw

PATCH v3 3/5 show-ref: Move --quiet handling into show_one

In reply to Vladimir Panteleev

Do the same with --quiet as was done with -d, to remove the need to perform this check at show_one's call site from the --verify branch.

---
 builtin/show-ref.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
builtin/show-ref.c +4 -5
index a72a626b1..ab8e0dc41 100644
@@ -22,6 +22,9 @@ static void show_one(const char *refname, const struct object_id *oid)
 	const char *hex;
 	struct object_id peeled;
 
+	if (quiet)
+		return;
+
 	hex = find_unique_abbrev(oid->hash, abbrev);
 	if (hash_only)
 		printf("%s\n", hex);
@@ -82,9 +85,6 @@ static int show_ref(const char *refname, const struct object_id *oid,
 		die("git show-ref: bad ref %s (%s)", refname,
 		    oid_to_hex(oid));
 
-	if (quiet)
-		return 0;
-
 	show_one(refname, oid);
 
 	return 0;
@@ -205,8 +205,7 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
 
 			if ((starts_with(*pattern, "refs/") || !strcmp(*pattern, "HEAD")) &&
 			    !read_ref(*pattern, oid.hash)) {
-				if (!quiet)
-					show_one(*pattern, &oid);
+				show_one(*pattern, &oid);
 			}
 			else if (!quiet)
 				die("'%s' - not a valid ref", *pattern);
Signature · 1 line
2.11.0
#6 lore raw

PATCH v3 5/5 show-ref: Remove dead `if (verify)' check

In reply to Vladimir Panteleev

As show_ref is only ever called on the path where --verify is not specified, `verify' can never possibly be true here.

---
 builtin/show-ref.c | 3 ---
 1 file changed, 3 deletions(-)
builtin/show-ref.c +0 -3
index 107d05fe0..2dfcb5634 100644
@@ -73,9 +73,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;
 		}
Signature · 1 line
2.11.0
#1 lore raw

PATCH v3 1/5 show-ref: Accept HEAD with --verify

In reply to Vladimir Panteleev

Previously, when --verify was specified, show-ref would use a separate code path which did not handle HEAD and treated it as an invalid ref. Thus, "git show-ref --verify HEAD" (where "--verify" is used because the user is not interested in seeing refs/remotes/origin/HEAD) did not work as expected.

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

---
 builtin/show-ref.c  |  2 +-
 t/t1403-show-ref.sh | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
builtin/show-ref.c +1 -1
index 6d4e66900..0e53e3da4 100644
@@ -202,7 +202,7 @@ 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/") || !strcmp(*pattern, "HEAD")) &&
 			    !read_ref(*pattern, oid.hash)) {
 				if (!quiet)
 					show_one(*pattern, &oid);
t/t1403-show-ref.sh +11 -0
index 7e10bcfe3..5932beada 100755
@@ -164,4 +164,15 @@ 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 >actual &&
+	test_cmp expect actual &&
+
+	>expect &&
+
+	git show-ref --verify -q HEAD >actual &&
+	test_cmp expect actual
+'
+
 test_done
Signature · 1 line
2.11.0
#0 lore raw

v3 cover show-ref: Allow -d, --head to work with --verify

Third iteration, according to Junio's comments. This time we keep show_ref and show_one separate, accept HEAD with --verify even without --head, and add tests for dangling ref validation with --verify.

#2 lore raw

PATCH v3 2/5 show-ref: Allow -d to work with --verify

Move handling of -d into show_one, so that it takes effect when --verify is present as well as when it is absent. This is useful when the user wishes to avoid the costly iteration of refs.

---
 builtin/show-ref.c  | 23 ++++++++++++-----------
 t/t1403-show-ref.sh |  9 +++++++++
 2 files changed, 21 insertions(+), 11 deletions(-)
builtin/show-ref.c +12 -11
index 0e53e3da4..a72a626b1 100644
@@ -19,19 +19,27 @@ 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);
+	const char *hex;
+	struct object_id peeled;
+
+	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;
+
+	if (!peel_ref(refname, peeled.hash)) {
+		hex = find_unique_abbrev(peeled.hash, abbrev);
+		printf("%s %s^{}\n", hex, refname);
+	}
 }
 
 static int show_ref(const char *refname, const struct object_id *oid,
 		    int flag, void *cbdata)
 {
-	const char *hex;
-	struct object_id peeled;
-
 	if (show_head && !strcmp(refname, "HEAD"))
 		goto match;
 
@@ -79,13 +87,6 @@ static int show_ref(const char *refname, const struct object_id *oid,
 
 	show_one(refname, oid);
 
-	if (!deref_tags)
-		return 0;
-
-	if (!peel_ref(refname, peeled.hash)) {
-		hex = find_unique_abbrev(peeled.hash, abbrev);
-		printf("%s %s^{}\n", hex, refname);
-	}
 	return 0;
 }
 
t/t1403-show-ref.sh +9 -0
index 5932beada..c6872bd96 100755
@@ -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
 
 '
Signature · 1 line
2.11.0
#4 lore raw

PATCH v3 4/5 show-ref: Detect dangling refs under --verify as well

In reply to Vladimir Panteleev

Move detection of dangling refs into show_one, so that they are detected when --verify is present as well as when it is absent.

---
 builtin/show-ref.c  | 16 ++++++++--------
 t/t1403-show-ref.sh | 22 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 8 deletions(-)
builtin/show-ref.c +8 -8
index ab8e0dc41..107d05fe0 100644
@@ -22,6 +22,14 @@ static void show_one(const char *refname, const struct object_id *oid)
 	const char *hex;
 	struct object_id peeled;
 
+	/* This changes the semantics slightly that even under quiet we
+	 * detect and return error if the repository is corrupt and
+	 * ref points at a nonexistent object.
+	 */
+	if (!has_sha1_file(oid->hash))
+		die("git show-ref: bad ref %s (%s)", refname,
+		    oid_to_hex(oid));
+
 	if (quiet)
 		return;
 
@@ -77,14 +85,6 @@ static int show_ref(const char *refname, const struct object_id *oid,
 match:
 	found_match++;
 
-	/* This changes the semantics slightly that even under quiet we
-	 * detect and return error if the repository is corrupt and
-	 * ref points at a nonexistent object.
-	 */
-	if (!has_sha1_file(oid->hash))
-		die("git show-ref: bad ref %s (%s)", refname,
-		    oid_to_hex(oid));
-
 	show_one(refname, oid);
 
 	return 0;
t/t1403-show-ref.sh +22 -0
index c6872bd96..30354fd26 100755
@@ -184,4 +184,26 @@ test_expect_success 'show-ref --verify HEAD' '
 	test_cmp expect actual
 '
 
+test_expect_success 'show-ref --verify with dangling ref' '
+	sha1_file() {
+		echo "$*" | sed "s#..#.git/objects/&/#"
+	} &&
+
+	remove_object() {
+		file=$(sha1_file "$*") &&
+		test -e "$file" &&
+		rm -f "$file"
+	} &&
+
+	test_when_finished "rm -rf dangling" &&
+	(
+		git init dangling &&
+		cd dangling &&
+		test_commit dangling &&
+		sha=$(git rev-parse refs/tags/dangling) &&
+		remove_object $sha &&
+		test_must_fail git show-ref --verify refs/tags/dangling
+	)
+'
+
 test_done
Signature · 1 line
2.11.0
#7 lore raw
In reply to Vladimir Panteleev
Quoted 3 lines · Vladimir Panteleev

Third iteration, according to Junio's comments. This time we keep show_ref and show_one separate, accept HEAD with --verify even without --head, and add tests for dangling ref validation with --verify.

I am no longer a neutral judge but to me the resulting code looks a lot more naturally structured than the original.

Thanks. Will queue.

#5 lore raw

Re: [PATCH v3 4/5] show-ref: Detect dangling refs under --verify as well

In reply to Vladimir Panteleev
Quoted 14 lines · Vladimir Panteleev

Move detection of dangling refs into show_one, so that they are detected when --verify is present as well as when it is absent.

---
 builtin/show-ref.c  | 16 ++++++++--------
 t/t1403-show-ref.sh | 22 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 8 deletions(-)
builtin/show-ref.c +0 -0
index ab8e0dc41..107d05fe0 100644

builtin/show-ref.c static void show_one(const char *refname, const struct object_id *oid) · line 28

Quoted 7 lines
builtin/show-ref.c +4 -0
@@ -22,6 +22,14 @@ static void show_one(const char *refname, const struct object_id *oid)
 	const char *hex;
 	struct object_id peeled;
 
+	/* This changes the semantics slightly that even under quiet we
+	 * detect and return error if the repository is corrupt and
+	 * ref points at a nonexistent object.
+	 */

This is my fault from more than 10 years ago, but I think the comment shouldn't have been here (or at its original location). It talks about the behaviour change relative to the previous version when the comment was added, i.e. cf0adba788 ("Store peeled refs in packed-refs file.", 2006-11-19).

I'll remove it after the series settles.