Skip to content

Commit 1dbe401

Browse files
phillipwoodgitster
authored andcommitted
show-ref --verify: accept pseudorefs
"git show-ref --verify" is useful for scripts that want to look up a fully qualified refname without falling back to the DWIM rules used by "git rev-parse" rules when the ref does not exist. Currently it will only accept "HEAD" or a refname beginning with "refs/". Running git show-ref --verify CHERRY_PICK_HEAD will always result in fatal: 'CHERRY_PICK_HEAD' - not a valid ref even when CHERRY_PICK_HEAD exists. By calling refname_is_safe() instead of comparing the refname to "HEAD" we can accept all one-level refs that contain only uppercase ascii letters and underscores. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 235986b commit 1dbe401

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

builtin/show-ref.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
172172
while (*refs) {
173173
struct object_id oid;
174174

175-
if ((starts_with(*refs, "refs/") || !strcmp(*refs, "HEAD")) &&
175+
if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) &&
176176
!read_ref(*refs, &oid)) {
177177
show_one(show_one_opts, *refs, &oid);
178178
}

t/t1403-show-ref.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ test_expect_success 'show-ref --verify HEAD' '
174174
test_must_be_empty actual
175175
'
176176

177+
test_expect_success 'show-ref --verify pseudorefs' '
178+
git update-ref CHERRY_PICK_HEAD HEAD $ZERO_OID &&
179+
test_when_finished "git update-ref -d CHERRY_PICK_HEAD" &&
180+
git show-ref -s --verify HEAD >actual &&
181+
git show-ref -s --verify CHERRY_PICK_HEAD >expect &&
182+
test_cmp actual expect
183+
'
184+
177185
test_expect_success 'show-ref --verify with dangling ref' '
178186
sha1_file() {
179187
echo "$*" | sed "s#..#.git/objects/&/#"

0 commit comments

Comments
 (0)