revparse: Make revparse_ext() return git_reference from names as well
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
diff --git a/src/revparse.c b/src/revparse.c
index f4c03ad..3e3edb6 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -84,12 +84,16 @@ static int maybe_describe(git_object**out, git_repository *repo, const char *spe
return maybe_abbrev(out, repo, substr+2);
}
-static int revparse_lookup_object(git_object **out, git_repository *repo, const char *spec)
+static int revparse_lookup_object(
+ git_object **object_out,
+ git_reference **reference_out,
+ git_repository *repo,
+ const char *spec)
{
int error;
git_reference *ref;
- error = maybe_sha(out, repo, spec);
+ error = maybe_sha(object_out, repo, spec);
if (!error)
return 0;
@@ -98,22 +102,27 @@ static int revparse_lookup_object(git_object **out, git_repository *repo, const
error = git_reference_dwim(&ref, repo, spec);
if (!error) {
- error = git_object_lookup(out, repo, git_reference_target(ref), GIT_OBJ_ANY);
- git_reference_free(ref);
+
+ error = git_object_lookup(
+ object_out, repo, git_reference_target(ref), GIT_OBJ_ANY);
+
+ if (!error)
+ *reference_out = ref;
+
return error;
}
if (error < 0 && error != GIT_ENOTFOUND)
return error;
- error = maybe_abbrev(out, repo, spec);
+ error = maybe_abbrev(object_out, repo, spec);
if (!error)
return 0;
if (error < 0 && error != GIT_ENOTFOUND)
return error;
- error = maybe_describe(out, repo, spec);
+ error = maybe_describe(object_out, repo, spec);
if (!error)
return 0;
@@ -609,7 +618,7 @@ static int object_from_reference(git_object **object, git_reference *reference)
return error;
}
-static int ensure_base_rev_loaded(git_object **object, git_reference *reference, const char *spec, size_t identifier_len, git_repository *repo, bool allow_empty_identifier)
+static int ensure_base_rev_loaded(git_object **object, git_reference **reference, const char *spec, size_t identifier_len, git_repository *repo, bool allow_empty_identifier)
{
int error;
git_buf identifier = GIT_BUF_INIT;
@@ -617,8 +626,8 @@ static int ensure_base_rev_loaded(git_object **object, git_reference *reference,
if (*object != NULL)
return 0;
- if (reference != NULL)
- return object_from_reference(object, reference);
+ if (*reference != NULL)
+ return object_from_reference(object, *reference);
if (!allow_empty_identifier && identifier_len == 0)
return GIT_EINVALIDSPEC;
@@ -626,7 +635,7 @@ static int ensure_base_rev_loaded(git_object **object, git_reference *reference,
if (git_buf_put(&identifier, spec, identifier_len) < 0)
return -1;
- error = revparse_lookup_object(object, repo, git_buf_cstr(&identifier));
+ error = revparse_lookup_object(object, reference, repo, git_buf_cstr(&identifier));
git_buf_free(&identifier);
return error;
@@ -684,7 +693,7 @@ int revparse__ext(
while (spec[pos]) {
switch (spec[pos]) {
case '^':
- if ((error = ensure_base_rev_loaded(&base_rev, reference, spec, identifier_len, repo, false)) < 0)
+ if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
goto cleanup;
if (spec[pos+1] == '{') {
@@ -719,7 +728,7 @@ int revparse__ext(
if ((error = extract_how_many(&n, spec, &pos)) < 0)
goto cleanup;
- if ((error = ensure_base_rev_loaded(&base_rev, reference, spec, identifier_len, repo, false)) < 0)
+ if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
goto cleanup;
if ((error = handle_linear_syntax(&temp_object, base_rev, n)) < 0)
@@ -738,7 +747,7 @@ int revparse__ext(
goto cleanup;
if (any_left_hand_identifier(base_rev, reference, identifier_len)) {
- 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, true)) < 0)
goto cleanup;
if ((error = handle_colon_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0)
@@ -795,7 +804,7 @@ int revparse__ext(
}
}
- if ((error = ensure_base_rev_loaded(&base_rev, reference, spec, identifier_len, repo, false)) < 0)
+ if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
goto cleanup;
*object_out = base_rev;
diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c
index 1d156f5..69d9274 100644
--- a/tests-clar/refs/revparse.c
+++ b/tests-clar/refs/revparse.c
@@ -732,10 +732,10 @@ void test_refs_revparse__ext_retrieves_both_the_reference_and_its_target(void)
"refs/heads/br2");
}
-void test_refs_revparse__ext_only_retrieve_the_object_target(void)
+void test_refs_revparse__ext_can_expand_short_reference_names(void)
{
test_object_and_ref(
"master",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
- NULL);
+ "refs/heads/master");
}