1
0
mirror of https://github.com/git/git synced 2024-07-07 19:39:27 +00:00

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>
This commit is contained in:
Phillip Wood 2024-02-07 16:44:35 +00:00 committed by Junio C Hamano
parent 235986be82
commit 1dbe401563
2 changed files with 9 additions and 1 deletions

View File

@ -172,7 +172,7 @@ static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
while (*refs) {
struct object_id oid;
if ((starts_with(*refs, "refs/") || !strcmp(*refs, "HEAD")) &&
if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) &&
!read_ref(*refs, &oid)) {
show_one(show_one_opts, *refs, &oid);
}

View File

@ -174,6 +174,14 @@ test_expect_success 'show-ref --verify HEAD' '
test_must_be_empty actual
'
test_expect_success 'show-ref --verify pseudorefs' '
git update-ref CHERRY_PICK_HEAD HEAD $ZERO_OID &&
test_when_finished "git update-ref -d CHERRY_PICK_HEAD" &&
git show-ref -s --verify HEAD >actual &&
git show-ref -s --verify CHERRY_PICK_HEAD >expect &&
test_cmp actual expect
'
test_expect_success 'show-ref --verify with dangling ref' '
sha1_file() {
echo "$*" | sed "s#..#.git/objects/&/#"