Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refs: preserve the owning refdb when duping reference #4618

Merged
merged 2 commits into from Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/refs.c
Expand Up @@ -115,6 +115,9 @@ int git_reference_dup(git_reference **dest, git_reference *source)

GITERR_CHECK_ALLOC(*dest);

(*dest)->db = source->db;
GIT_REFCOUNT_INC((*dest)->db);

return 0;
}

Expand Down
4 changes: 4 additions & 0 deletions src/refs.h
Expand Up @@ -116,6 +116,10 @@ int git_reference_lookup_resolved(
* with the given name pointing to the reference pointed to by
* the file. If it is not a symbolic reference, it will return
* the resolved reference.
*
* Note that because the refdb is not involved for symbolic references, they
* won't be owned, hence you should either not make the returned reference
* 'externally visible', or perform the lookup before returning it to the user.
*/
int git_reference__read_head(
git_reference **out,
Expand Down
2 changes: 2 additions & 0 deletions tests/refs/dup.c
Expand Up @@ -21,6 +21,7 @@ void test_refs_dup__direct(void)
cl_git_pass(git_reference_dup(&b, a));

cl_assert(git_reference_cmp(a, b) == 0);
cl_assert(git_reference_owner(b) == g_repo);

git_reference_free(b);
git_reference_free(a);
Expand All @@ -34,6 +35,7 @@ void test_refs_dup__symbolic(void)
cl_git_pass(git_reference_dup(&b, a));

cl_assert(git_reference_cmp(a, b) == 0);
cl_assert(git_reference_owner(b) == g_repo);

git_reference_free(b);
git_reference_free(a);
Expand Down
1 change: 1 addition & 0 deletions tests/worktree/repository.c
Expand Up @@ -27,6 +27,7 @@ void test_worktree_repository__head(void)
cl_git_pass(git_reference_lookup(&ref, fixture.repo, "refs/heads/testrepo-worktree"));
cl_git_pass(git_repository_head_for_worktree(&head, fixture.repo, "testrepo-worktree"));
cl_assert(git_reference_cmp(ref, head) == 0);
cl_assert(git_reference_owner(ref) == fixture.repo);

git_reference_free(ref);
git_reference_free(head);
Expand Down