Commit b112b1e9 by Patrick Steinhardt

refs: do not use peeled OID if peeling to a tag

If a reference stored in a packed-refs file does not directly point to a
commit, tree or blob, the packed-refs file will also will include a
fully-peeled OID pointing to the first underlying object of that type.
If we try to peel a reference to an object, we will use that peeled OID
to speed up resolving the object.

As a reference for an annotated tag does not directly point to a commit,
tree or blob but instead to the tag object, the packed-refs file will
have an accomodating fully-peeled OID pointing to the object referenced
by that tag. When we use the fully-peeled OID pointing to the referenced
object when peeling, we obviously cannot peel that to the tag anymore.

Fix this issue by not using the fully-peeled OID whenever we want to
peel to a tag. Note that this does not include the case where we want to
resolve to _any_ object type. Existing code may make use from the fact
that we resolve those to commit objects instead of tag objects, even
though that behaviour is inconsistent between packed and loose
references. Furthermore, some tests of ours make the assumption that we
in fact resolve those references to a commit.
parent c7c5f2c4
......@@ -1360,7 +1360,13 @@ int git_reference_peel(
return peel_error(error, ref, "Cannot resolve reference");
}
if (!git_oid_iszero(&resolved->peel)) {
/*
* If we try to peel an object to a tag, we cannot use
* the fully peeled object, as that will always resolve
* to a commit. So we only want to use the peeled value
* if it is not zero and the target is not a tag.
*/
if (target_type != GIT_OBJ_TAG && !git_oid_iszero(&resolved->peel)) {
error = git_object_lookup(&target,
git_reference_owner(ref), &resolved->peel, GIT_OBJ_ANY);
} else {
......
......@@ -117,3 +117,15 @@ void test_refs_peel__can_peel_fully_peeled_packed_refs(void)
"0df1a5865c8abfc09f1f2182e6a31be550e99f07",
GIT_OBJ_COMMIT);
}
void test_refs_peel__can_peel_fully_peeled_tag_to_tag(void)
{
assert_peel_generic(g_peel_repo,
"refs/tags/tag-inside-tags", GIT_OBJ_TAG,
"c2596aa0151888587ec5c0187f261e63412d9e11",
GIT_OBJ_TAG);
assert_peel_generic(g_peel_repo,
"refs/foo/tag-outside-tags", GIT_OBJ_TAG,
"c2596aa0151888587ec5c0187f261e63412d9e11",
GIT_OBJ_TAG);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment