]> granicus.if.org Git - git/commitdiff
show-ref: detect dangling refs under --verify as well
authorVladimir Panteleev <git@thecybershadow.net>
Mon, 23 Jan 2017 18:00:58 +0000 (18:00 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Jan 2017 20:06:29 +0000 (12:06 -0800)
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 <git@thecybershadow.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/show-ref.c
t/t1403-show-ref.sh

index ab8e0dc4142d8f2525641c3bf3f211c142578d9d..107d05fe0e9bdddf8103d369dd7044cda297d7f6 100644 (file)
@@ -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;
index c6872bd96f03c6e08d55373be9695a17e5bb75ca..30354fd26c0cf6b6e2a6c01bc2bcbef48168e562 100755 (executable)
@@ -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