Merge branch 'vp/show-ref-verify-head'
authorJunio C Hamano <gitster@pobox.com>
Tue, 31 Jan 2017 21:14:59 +0000 (13:14 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 31 Jan 2017 21:14:59 +0000 (13:14 -0800)
"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.

* vp/show-ref-verify-head:
show-ref: remove a stale comment
show-ref: remove dead `if (verify)' check
show-ref: detect dangling refs under --verify as well
show-ref: move --quiet handling into show_one()
show-ref: allow -d to work with --verify
show-ref: accept HEAD with --verify

builtin/show-ref.c
t/t1403-show-ref.sh
index 6d4e669002b1e58ebf40af5543221d809ffd0427..013d241abc09be76c318dbc25d5fb3b40ade7b3a 100644 (file)
@@ -19,19 +19,34 @@ 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;
+
+       if (!has_sha1_file(oid->hash))
+               die("git show-ref: bad ref %s (%s)", refname,
+                   oid_to_hex(oid));
+
+       if (quiet)
+               return;
+
+       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;
 
@@ -54,9 +69,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;
                }
@@ -66,26 +78,8 @@ 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));
-
-       if (quiet)
-               return 0;
-
        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;
 }
 
@@ -202,10 +196,9 @@ 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);
+                               show_one(*pattern, &oid);
                        }
                        else if (!quiet)
                                die("'%s' - not a valid ref", *pattern);
index 7e10bcfe395609cff5edc1f8f6ce95fcf69cf5f6..30354fd26c0cf6b6e2a6c01bc2bcbef48168e562 100755 (executable)
@@ -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
 
 '
@@ -164,4 +173,37 @@ 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_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