From 60f219e80be22a733fb4321e5aed189fde984dbe Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Wed, 8 May 2024 17:05:19 +0200 Subject: [PATCH] Revparse: Correctly accept ref with '@' at the end Signed-off-by: Sven Strickroth --- src/libgit2/revparse.c | 8 +------- tests/libgit2/refs/revparse.c | 31 +++++++++++++++++++------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/libgit2/revparse.c b/src/libgit2/revparse.c index 082376287..9083e7a3c 100644 --- a/src/libgit2/revparse.c +++ b/src/libgit2/revparse.c @@ -816,13 +816,7 @@ static int revparse( if (temp_object != NULL) base_rev = temp_object; break; - } else if (spec[pos+1] == '\0') { - if (pos) { - git_error_set(GIT_ERROR_REFERENCE, "invalid revspec"); - error = GIT_EINVALIDSPEC; - goto cleanup; - } - + } else if (spec[pos + 1] == '\0' && !pos) { spec = "HEAD"; identifier_len = 4; parsed = true; diff --git a/tests/libgit2/refs/revparse.c b/tests/libgit2/refs/revparse.c index 3fe078117..9bc3bd10e 100644 --- a/tests/libgit2/refs/revparse.c +++ b/tests/libgit2/refs/revparse.c @@ -747,6 +747,25 @@ void test_refs_revparse__try_to_retrieve_branch_before_abbrev_sha(void) cl_git_sandbox_cleanup(); } +void test_refs_revparse__at_at_end_of_refname(void) +{ + git_repository *repo; + git_reference *branch; + git_object *target; + + repo = cl_git_sandbox_init("testrepo.git"); + + cl_git_pass(git_revparse_single(&target, repo, "HEAD")); + cl_git_pass(git_branch_create(&branch, repo, "master@", (git_commit *)target, 0)); + git_object_free(target); + + test_id_inrepo("master@", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE, repo); + + cl_git_fail_with(GIT_ENOTFOUND, git_revparse_single(&target, repo, "foo@")); + + git_reference_free(branch); + cl_git_sandbox_cleanup(); +} void test_refs_revparse__range(void) { @@ -889,15 +908,3 @@ void test_refs_revparse__parses_at_head(void) test_id("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE); test_id("@", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE); } - -void test_refs_revparse__rejects_bogus_at(void) -{ - git_repository *repo; - git_object *target; - - repo = cl_git_sandbox_init("testrepo.git"); - - cl_git_fail_with(GIT_EINVALIDSPEC, git_revparse_single(&target, repo, "foo@")); - - cl_git_sandbox_cleanup(); -}