Merge pull request #629 from nulltoken/issue/index-refcount Index 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
diff --git a/src/repository.c b/src/repository.c
index 18881ec..f81ece0 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -595,6 +595,7 @@ void git_repository_set_index(git_repository *repo, git_index *index)
repo->_index = index;
GIT_REFCOUNT_OWN(repo->_index, repo);
+ GIT_REFCOUNT_INC(index);
}
static int check_repositoryformatversion(git_repository *repo)
diff --git a/tests-clar/repo/open.c b/tests-clar/repo/open.c
index 2450de0..6968c44 100644
--- a/tests-clar/repo/open.c
+++ b/tests-clar/repo/open.c
@@ -234,7 +234,6 @@ void test_repo_open__win32_path(void)
#ifdef GIT_WIN32
git_repository *repo = cl_git_sandbox_init("empty_standard_repo"), *repo2;
git_buf winpath = GIT_BUF_INIT;
- char *src, *tgt;
static const char *repo_path = "empty_standard_repo/.git/";
static const char *repo_wd = "empty_standard_repo/";
diff --git a/tests-clar/repo/setters.c b/tests-clar/repo/setters.c
index 721eaaf..0c3b28d 100644
--- a/tests-clar/repo/setters.c
+++ b/tests-clar/repo/setters.c
@@ -1,6 +1,7 @@
#include "clar_libgit2.h"
#include "buffer.h"
#include "posix.h"
+#include "util.h"
static git_repository *repo;
@@ -35,3 +36,24 @@ void test_repo_setters__setting_a_workdir_prettifies_its_path(void)
cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
}
+
+void test_repo_setters__setting_a_new_index_on_a_repo_which_has_already_loaded_one_properly_honors_the_refcount(void)
+{
+ git_index *new_index;
+
+ cl_git_pass(git_index_open(&new_index, "./my-index"));
+ cl_assert(((git_refcount *)new_index)->refcount == 1);
+
+ git_repository_set_index(repo, new_index);
+ cl_assert(((git_refcount *)new_index)->refcount == 2);
+
+ git_repository_free(repo);
+ cl_assert(((git_refcount *)new_index)->refcount == 1);
+
+ git_index_free(new_index);
+
+ /*
+ * Ensure the cleanup method won't try to free the repo as it's already been taken care of
+ */
+ repo = NULL;
+}