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

Do not register fake repository as owner of ODB #6443

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 2 additions & 1 deletion include/git2/sys/repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ GIT_EXTERN(int) git_repository_set_config(git_repository *repo, git_config *conf
*
* @param repo A repository object
* @param odb An ODB object
* @param set_owner Register repository as owner of ODB
* @return 0 on success, or an error code
*/
GIT_EXTERN(int) git_repository_set_odb(git_repository *repo, git_odb *odb);
GIT_EXTERN(int) git_repository_set_odb(git_repository *repo, git_odb *odb, bool set_owner);

/**
* Set the Reference Database Backend for this repository
Expand Down
16 changes: 9 additions & 7 deletions src/libgit2/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ git_str git_repository__reserved_names_posix[] = {
};
size_t git_repository__reserved_names_posix_len = 1;

static void set_odb(git_repository *repo, git_odb *odb)
static void set_odb(git_repository *repo, git_odb *odb, bool set_owner)
{
if (odb) {
GIT_REFCOUNT_OWN(odb, repo);
if (set_owner) {
GIT_REFCOUNT_OWN(odb, repo);
}
GIT_REFCOUNT_INC(odb);
}

Expand Down Expand Up @@ -153,7 +155,7 @@ int git_repository__cleanup(git_repository *repo)

set_config(repo, NULL);
set_index(repo, NULL);
set_odb(repo, NULL);
set_odb(repo, NULL, /*set_owner=*/true);
set_refdb(repo, NULL);

return 0;
Expand Down Expand Up @@ -880,7 +882,7 @@ static int _git_repository_open_ext_from_env(
goto error;

if (odb)
git_repository_set_odb(repo, odb);
git_repository_set_odb(repo, odb, /*set_owner=*/true);

error = git__getenv(&alts_buf, "GIT_ALTERNATE_OBJECT_DIRECTORIES");
if (error == GIT_ENOTFOUND) {
Expand Down Expand Up @@ -1098,7 +1100,7 @@ int git_repository_wrap_odb(git_repository **repo_out, git_odb *odb)
repo = repository_alloc();
GIT_ERROR_CHECK_ALLOC(repo);

git_repository_set_odb(repo, odb);
git_repository_set_odb(repo, odb, /*set_owner=*/false);
*repo_out = repo;

return 0;
Expand Down Expand Up @@ -1305,12 +1307,12 @@ int git_repository_odb(git_odb **out, git_repository *repo)
return 0;
}

int git_repository_set_odb(git_repository *repo, git_odb *odb)
int git_repository_set_odb(git_repository *repo, git_odb *odb, bool set_owner)
{
GIT_ASSERT_ARG(repo);
GIT_ASSERT_ARG(odb);

set_odb(repo, odb);
set_odb(repo, odb, set_owner);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/libgit2/odb/backend/nobackend.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void test_odb_backend_nobackend__initialize(void)
cl_git_pass(git_refdb_new(&refdb, _repo));

git_repository_set_config(_repo, config);
git_repository_set_odb(_repo, odb);
git_repository_set_odb(_repo, odb, /*set_owner=*/true);
git_repository_set_refdb(_repo, refdb);

/* The set increases the refcount and we don't want them anymore */
Expand Down
23 changes: 22 additions & 1 deletion tests/libgit2/repo/setters.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,28 @@ void test_repo_setters__setting_a_new_odb_on_a_repo_which_already_loaded_one_pro
cl_git_pass(git_odb__open(&new_odb, "./testrepo.git/objects", NULL));
cl_assert(((git_refcount *)new_odb)->refcount.val == 1);

git_repository_set_odb(repo, new_odb);
git_repository_set_odb(repo, new_odb, /*set_owner=*/true);
cl_assert(((git_refcount *)new_odb)->refcount.val == 2);

git_repository_free(repo);
cl_assert(((git_refcount *)new_odb)->refcount.val == 1);

git_odb_free(new_odb);

/*
* Ensure the cleanup method won't try to free the repo as it's already been taken care of
*/
repo = NULL;
}

void test_repo_setters__setting_a_new_odb_on_a_non_owner_repo_which_already_loaded_one_properly_honors_the_refcount(void)
{
git_odb *new_odb;

cl_git_pass(git_odb__open(&new_odb, "./testrepo.git/objects", NULL));
cl_assert(((git_refcount *)new_odb)->refcount.val == 1);

git_repository_set_odb(repo, new_odb, /*set_owner=*/false);
cl_assert(((git_refcount *)new_odb)->refcount.val == 2);

git_repository_free(repo);
Expand Down