Skip to content

Commit af6dd9d

Browse files
committed
Merge branch 'vp/show-ref-verify-head' into next
"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
2 parents f8a14b4 + 0d583ff commit af6dd9d

File tree

2 files changed

+63
-28
lines changed

2 files changed

+63
-28
lines changed

builtin/show-ref.c

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,34 @@ static const char *exclude_existing_arg;
1919

2020
static void show_one(const char *refname, const struct object_id *oid)
2121
{
22-
const char *hex = find_unique_abbrev(oid->hash, abbrev);
22+
const char *hex;
23+
struct object_id peeled;
24+
25+
if (!has_sha1_file(oid->hash))
26+
die("git show-ref: bad ref %s (%s)", refname,
27+
oid_to_hex(oid));
28+
29+
if (quiet)
30+
return;
31+
32+
hex = find_unique_abbrev(oid->hash, abbrev);
2333
if (hash_only)
2434
printf("%s\n", hex);
2535
else
2636
printf("%s %s\n", hex, refname);
37+
38+
if (!deref_tags)
39+
return;
40+
41+
if (!peel_ref(refname, peeled.hash)) {
42+
hex = find_unique_abbrev(peeled.hash, abbrev);
43+
printf("%s %s^{}\n", hex, refname);
44+
}
2745
}
2846

2947
static int show_ref(const char *refname, const struct object_id *oid,
3048
int flag, void *cbdata)
3149
{
32-
const char *hex;
33-
struct object_id peeled;
34-
3550
if (show_head && !strcmp(refname, "HEAD"))
3651
goto match;
3752

@@ -54,9 +69,6 @@ static int show_ref(const char *refname, const struct object_id *oid,
5469
continue;
5570
if (len == reflen)
5671
goto match;
57-
/* "--verify" requires an exact match */
58-
if (verify)
59-
continue;
6072
if (refname[reflen - len - 1] == '/')
6173
goto match;
6274
}
@@ -66,26 +78,8 @@ static int show_ref(const char *refname, const struct object_id *oid,
6678
match:
6779
found_match++;
6880

69-
/* This changes the semantics slightly that even under quiet we
70-
* detect and return error if the repository is corrupt and
71-
* ref points at a nonexistent object.
72-
*/
73-
if (!has_sha1_file(oid->hash))
74-
die("git show-ref: bad ref %s (%s)", refname,
75-
oid_to_hex(oid));
76-
77-
if (quiet)
78-
return 0;
79-
8081
show_one(refname, oid);
8182

82-
if (!deref_tags)
83-
return 0;
84-
85-
if (!peel_ref(refname, peeled.hash)) {
86-
hex = find_unique_abbrev(peeled.hash, abbrev);
87-
printf("%s %s^{}\n", hex, refname);
88-
}
8983
return 0;
9084
}
9185

@@ -202,10 +196,9 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
202196
while (*pattern) {
203197
struct object_id oid;
204198

205-
if (starts_with(*pattern, "refs/") &&
199+
if ((starts_with(*pattern, "refs/") || !strcmp(*pattern, "HEAD")) &&
206200
!read_ref(*pattern, oid.hash)) {
207-
if (!quiet)
208-
show_one(*pattern, &oid);
201+
show_one(*pattern, &oid);
209202
}
210203
else if (!quiet)
211204
die("'%s' - not a valid ref", *pattern);

t/t1403-show-ref.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ test_expect_success 'show-ref -d' '
9797
git show-ref -d refs/tags/A refs/tags/C >actual &&
9898
test_cmp expect actual &&
9999
100+
git show-ref --verify -d refs/tags/A refs/tags/C >actual &&
101+
test_cmp expect actual &&
102+
100103
echo $(git rev-parse refs/heads/master) refs/heads/master >expect &&
101104
git show-ref -d master >actual &&
102105
test_cmp expect actual &&
@@ -116,6 +119,12 @@ test_expect_success 'show-ref -d' '
116119
test_cmp expect actual &&
117120
118121
test_must_fail git show-ref -d --verify heads/master >actual &&
122+
test_cmp expect actual &&
123+
124+
test_must_fail git show-ref --verify -d A C >actual &&
125+
test_cmp expect actual &&
126+
127+
test_must_fail git show-ref --verify -d tags/A tags/C >actual &&
119128
test_cmp expect actual
120129
121130
'
@@ -164,4 +173,37 @@ test_expect_success 'show-ref --heads, --tags, --head, pattern' '
164173
test_cmp expect actual
165174
'
166175

176+
test_expect_success 'show-ref --verify HEAD' '
177+
echo $(git rev-parse HEAD) HEAD >expect &&
178+
git show-ref --verify HEAD >actual &&
179+
test_cmp expect actual &&
180+
181+
>expect &&
182+
183+
git show-ref --verify -q HEAD >actual &&
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+
167209
test_done

0 commit comments

Comments
 (0)