fix(refs): attach refdb owner to loose pseudorefs

Pseudoref lookup now reads `FETCH_HEAD` directly as a loose ref,
but callers still expect looked-up references to know their owning
repository.

Attach the repo's refdb after the loose pseudoref is read so
owner-dependent ops such as peeling can safely resolve objects.
This commit is contained in:
Weihang Lo
2026-04-21 23:03:18 -04:00
parent c0a5341635
commit 735bb2e2f5
2 changed files with 19 additions and 3 deletions

View File

@@ -236,8 +236,21 @@ int git_reference_lookup_resolved(
* contain additional data that doesn't even follow the normal ref
* format. So we look these up as "loose" refs directly.
*/
if (git_reference__is_pseudoref(name))
return git_reference__lookup_loose(ref_out, repo->gitdir, name, repo->oid_type);
if (git_reference__is_pseudoref(name)) {
if ((error = git_reference__lookup_loose(ref_out, repo->gitdir, name, repo->oid_type)) < 0)
return error;
if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0) {
git_reference_free(*ref_out);
*ref_out = NULL;
return error;
}
GIT_REFCOUNT_INC(refdb);
(*ref_out)->db = refdb;
return 0;
}
if ((error = reference_normalize_for_repo(normalized, repo, name, true)) < 0 ||
(error = git_repository_refdb__weakptr(&refdb, repo)) < 0 ||

View File

@@ -14,6 +14,7 @@ void test_refs_pseudoref__lookup_fetch_head_after_fetch(void)
git_repository *dst;
git_remote *remote;
git_reference *ref;
git_object *commit;
git_str url = GIT_STR_INIT;
cl_git_sandbox_init("testrepo.git");
@@ -28,8 +29,10 @@ void test_refs_pseudoref__lookup_fetch_head_after_fetch(void)
cl_git_pass(git_reference_lookup(&ref, dst, "FETCH_HEAD"));
cl_assert_equal_i(GIT_REFERENCE_DIRECT, git_reference_type(ref));
cl_assert(ref->db == NULL);
cl_assert(ref->db != NULL);
cl_git_pass(git_reference_peel(&commit, ref, GIT_OBJECT_COMMIT));
git_object_free(commit);
git_reference_free(ref);
git_repository_free(dst);
}