revparse: don't allow an empty string Asking the library for "" used to give HEAD, but that's trying to impose a default at the wrong layer. Make it fail.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
diff --git a/src/revparse.c b/src/revparse.c
index 6cfea0c..650d7a9 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -705,7 +705,7 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec
*out = NULL;
- do {
+ while (spec[pos]) {
switch (spec[pos]) {
case '^':
if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
@@ -813,9 +813,9 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec
pos++;
identifier_len++;
}
- } while (spec[pos]);
+ }
- if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, true)) < 0)
+ if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
goto cleanup;
*out = base_rev;
diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c
index 05a9565..02acb88 100644
--- a/tests-clar/refs/revparse.c
+++ b/tests-clar/refs/revparse.c
@@ -64,6 +64,8 @@ void test_refs_revparse__invalid_reference_name(void)
cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't make sense"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't make sense^1"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't make sense~2"));
+ cl_git_fail(git_revparse_single(&g_obj, g_repo, ""));
+
}
void test_refs_revparse__shas(void)
@@ -74,7 +76,6 @@ void test_refs_revparse__shas(void)
void test_refs_revparse__head(void)
{
- test_object("", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("HEAD^0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("HEAD~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");