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 <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Feb 7, 2024 at 16:44 UTC 1dbe40156307763255d96653cc853ef4ff841920
2 files changed +9 -1
builtin/show-ref.c
+1 -1
@@ -172,7 +172,7 @@ static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
172 while (*refs) {
173 struct object_id oid;
174
175 - if ((starts_with(*refs, "refs/") || !strcmp(*refs, "HEAD")) &&
175 + if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) &&
176 !read_ref(*refs, &oid)) {
177 show_one(show_one_opts, *refs, &oid);
178 }
t/t1403-show-ref.sh
+8
@@ -174,6 +174,14 @@ test_expect_success 'show-ref --verify HEAD' '
174 test_must_be_empty actual
175 '
176
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 +
185 test_expect_success 'show-ref --verify with dangling ref' '
186 sha1_file() {
187 echo "$*" | sed "s#..#.git/objects/&/#"