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 <git@thecybershadow.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Vladimir Panteleev committed Jan 23, 2017 at 18:00 UTC d01b8203ec24732e5e8829db44a8150468f2fb7d
2 files changed +30 -8
builtin/show-ref.c
+8 -8
@@ -22,6 +22,14 @@ static void show_one(const char *refname, const struct object_id *oid)
22 const char *hex;
23 struct object_id peeled;
24
25 + /* This changes the semantics slightly that even under quiet we
26 + * detect and return error if the repository is corrupt and
27 + * ref points at a nonexistent object.
28 + */
29 + if (!has_sha1_file(oid->hash))
30 + die("git show-ref: bad ref %s (%s)", refname,
31 + oid_to_hex(oid));
32 +
33 if (quiet)
34 return;
35
@@ -77,14 +85,6 @@ static int show_ref(const char *refname, const struct object_id *oid,
85 match:
86 found_match++;
87
80 - /* This changes the semantics slightly that even under quiet we
81 - * detect and return error if the repository is corrupt and
82 - * ref points at a nonexistent object.
83 - */
84 - if (!has_sha1_file(oid->hash))
85 - die("git show-ref: bad ref %s (%s)", refname,
86 - oid_to_hex(oid));
87 -
88 show_one(refname, oid);
89
90 return 0;
t/t1403-show-ref.sh
+22
@@ -184,4 +184,26 @@ test_expect_success 'show-ref --verify HEAD' '
184 test_cmp expect actual
185 '
186
187 +test_expect_success 'show-ref --verify with dangling ref' '
188 + sha1_file() {
189 + echo "$*" | sed "s#..#.git/objects/&/#"
190 + } &&
191 +
192 + remove_object() {
193 + file=$(sha1_file "$*") &&
194 + test -e "$file" &&
195 + rm -f "$file"
196 + } &&
197 +
198 + test_when_finished "rm -rf dangling" &&
199 + (
200 + git init dangling &&
201 + cd dangling &&
202 + test_commit dangling &&
203 + sha=$(git rev-parse refs/tags/dangling) &&
204 + remove_object $sha &&
205 + test_must_fail git show-ref --verify refs/tags/dangling
206 + )
207 +'
208 +
209 test_done