Merge pull request #637 from nulltoken/issue/odb-refcount Fix git_repository_set_odb() refcount issue
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
diff --git a/src/repository.c b/src/repository.c
index f81ece0..b8b2033 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -552,6 +552,7 @@ void git_repository_set_odb(git_repository *repo, git_odb *odb)
repo->_odb = odb;
GIT_REFCOUNT_OWN(repo->_odb, repo);
+ GIT_REFCOUNT_INC(odb);
}
int git_repository_index__weakptr(git_index **out, git_repository *repo)
diff --git a/tests-clar/repo/getters.c b/tests-clar/repo/getters.c
index a0d4379..966de1f 100644
--- a/tests-clar/repo/getters.c
+++ b/tests-clar/repo/getters.c
@@ -68,3 +68,19 @@ void test_repo_getters__head_orphan(void)
git_reference_free(ref);
git_repository_free(repo);
}
+
+void test_repo_getters__retrieving_the_odb_honors_the_refcount(void)
+{
+ git_odb *odb;
+ git_repository *repo;
+
+ cl_git_pass(git_repository_open(&repo, "testrepo.git"));
+
+ cl_git_pass(git_repository_odb(&odb, repo));
+ cl_assert(((git_refcount *)odb)->refcount == 2);
+
+ git_repository_free(repo);
+ cl_assert(((git_refcount *)odb)->refcount == 1);
+
+ git_odb_free(odb);
+}
diff --git a/tests-clar/repo/setters.c b/tests-clar/repo/setters.c
index 0c3b28d..6242d85 100644
--- a/tests-clar/repo/setters.c
+++ b/tests-clar/repo/setters.c
@@ -57,3 +57,24 @@ void test_repo_setters__setting_a_new_index_on_a_repo_which_has_already_loaded_o
*/
repo = NULL;
}
+
+void test_repo_setters__setting_a_new_odb_on_a_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"));
+ cl_assert(((git_refcount *)new_odb)->refcount == 1);
+
+ git_repository_set_odb(repo, new_odb);
+ cl_assert(((git_refcount *)new_odb)->refcount == 2);
+
+ git_repository_free(repo);
+ cl_assert(((git_refcount *)new_odb)->refcount == 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;
+}