From ec7c51bc6bda0a836b4955962becedbb702bbc43 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Mon, 23 Jan 2017 18:00:55 +0000 Subject: [PATCH 1/6] show-ref: accept HEAD with --verify 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. Signed-off-by: Vladimir Panteleev Signed-off-by: Junio C Hamano --- builtin/show-ref.c | 2 +- t/t1403-show-ref.sh | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/builtin/show-ref.c b/builtin/show-ref.c index 6d4e669002..0e53e3da4f 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -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); diff --git a/t/t1403-show-ref.sh b/t/t1403-show-ref.sh index 7e10bcfe39..5932beada1 100755 --- a/t/t1403-show-ref.sh +++ b/t/t1403-show-ref.sh @@ -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 From f1627040b9305dc38994f799c172393909b2c70c Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Mon, 23 Jan 2017 18:00:56 +0000 Subject: [PATCH 2/6] 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. Signed-off-by: Vladimir Panteleev Signed-off-by: Junio C Hamano --- builtin/show-ref.c | 23 ++++++++++++----------- t/t1403-show-ref.sh | 9 +++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/builtin/show-ref.c b/builtin/show-ref.c index 0e53e3da4f..a72a626b18 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -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 @@ match: 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; } diff --git a/t/t1403-show-ref.sh b/t/t1403-show-ref.sh index 5932beada1..c6872bd96f 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 ' From 14144d3b53b913b48970aa0947accfe34e745c0f Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Mon, 23 Jan 2017 18:00:57 +0000 Subject: [PATCH 3/6] show-ref: move --quiet handling into show_one() 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. Signed-off-by: Vladimir Panteleev Signed-off-by: Junio C Hamano --- builtin/show-ref.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/builtin/show-ref.c b/builtin/show-ref.c index a72a626b18..ab8e0dc414 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -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 @@ match: 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); From d01b8203ec24732e5e8829db44a8150468f2fb7d Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Mon, 23 Jan 2017 18:00:58 +0000 Subject: [PATCH 4/6] show-ref: detect dangling refs under --verify as well Move detection of dangling refs into show_one(), so that they are detected when --verify is present as well as when it is absent. Signed-off-by: Vladimir Panteleev Signed-off-by: Junio C Hamano --- builtin/show-ref.c | 16 ++++++++-------- t/t1403-show-ref.sh | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/builtin/show-ref.c b/builtin/show-ref.c index ab8e0dc414..107d05fe0e 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -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; diff --git a/t/t1403-show-ref.sh b/t/t1403-show-ref.sh index c6872bd96f..30354fd26c 100755 --- a/t/t1403-show-ref.sh +++ b/t/t1403-show-ref.sh @@ -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 From 02bdc9d9f6ef6ae2f304121a0cf169f7c50f2370 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Mon, 23 Jan 2017 18:00:59 +0000 Subject: [PATCH 5/6] show-ref: remove dead `if (verify)' check As show_ref() is only ever called on the path where --verify is not specified, `verify' can never possibly be true here. Signed-off-by: Vladimir Panteleev Signed-off-by: Junio C Hamano --- builtin/show-ref.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/builtin/show-ref.c b/builtin/show-ref.c index 107d05fe0e..2dfcb5634c 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -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; } From 0d583ff02d1a6551863acf3bf87e6f19eef8c5ab Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 23 Jan 2017 18:51:56 -0800 Subject: [PATCH 6/6] show-ref: remove a stale comment When cf0adba788 ("Store peeled refs in packed-refs file.", 2006-11-19) made the command to die with a message on error even when --quiet is passed, it left the comment to say it changed the semantics. But that kind of information belongs to the log message, not in-code comment. Besides, the behaviour after the change has been the established one for the past 10 years ;-) Signed-off-by: Junio C Hamano --- builtin/show-ref.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/builtin/show-ref.c b/builtin/show-ref.c index 2dfcb5634c..013d241abc 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -22,10 +22,6 @@ 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));