]> git.proxmox.com Git - libgit2.git/blob - tests/repo/setters.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / repo / setters.c
1 #include "clar_libgit2.h"
2 #include "git2/sys/repository.h"
3
4 #include "posix.h"
5 #include "util.h"
6 #include "path.h"
7 #include "futils.h"
8
9 static git_repository *repo;
10
11 void test_repo_setters__initialize(void)
12 {
13 cl_fixture_sandbox("testrepo.git");
14 cl_git_pass(git_repository_open(&repo, "testrepo.git"));
15 cl_must_pass(p_mkdir("new_workdir", 0777));
16 }
17
18 void test_repo_setters__cleanup(void)
19 {
20 git_repository_free(repo);
21 repo = NULL;
22
23 cl_fixture_cleanup("testrepo.git");
24 cl_fixture_cleanup("new_workdir");
25 }
26
27 void test_repo_setters__setting_a_workdir_turns_a_bare_repository_into_a_standard_one(void)
28 {
29 cl_assert(git_repository_is_bare(repo) == 1);
30
31 cl_assert(git_repository_workdir(repo) == NULL);
32 cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false));
33
34 cl_assert(git_repository_workdir(repo) != NULL);
35 cl_assert(git_repository_is_bare(repo) == 0);
36 }
37
38 void test_repo_setters__setting_a_workdir_prettifies_its_path(void)
39 {
40 cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false));
41
42 cl_assert(git__suffixcmp(git_repository_workdir(repo), "new_workdir/") == 0);
43 }
44
45 void test_repo_setters__setting_a_workdir_creates_a_gitlink(void)
46 {
47 git_config *cfg;
48 git_buf buf = GIT_BUF_INIT;
49 git_str content = GIT_STR_INIT;
50
51 cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", true));
52
53 cl_assert(git_fs_path_isfile("./new_workdir/.git"));
54
55 cl_git_pass(git_futils_readbuffer(&content, "./new_workdir/.git"));
56 cl_assert(git__prefixcmp(git_str_cstr(&content), "gitdir: ") == 0);
57 cl_assert(git__suffixcmp(git_str_cstr(&content), "testrepo.git/") == 0);
58 git_str_dispose(&content);
59
60 cl_git_pass(git_repository_config(&cfg, repo));
61 cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.worktree"));
62 cl_assert(git__suffixcmp(buf.ptr, "new_workdir/") == 0);
63
64 git_buf_dispose(&buf);
65 git_config_free(cfg);
66 }
67
68 void test_repo_setters__setting_a_new_index_on_a_repo_which_has_already_loaded_one_properly_honors_the_refcount(void)
69 {
70 git_index *new_index;
71
72 cl_git_pass(git_index_open(&new_index, "./my-index"));
73 cl_assert(((git_refcount *)new_index)->refcount.val == 1);
74
75 git_repository_set_index(repo, new_index);
76 cl_assert(((git_refcount *)new_index)->refcount.val == 2);
77
78 git_repository_free(repo);
79 cl_assert(((git_refcount *)new_index)->refcount.val == 1);
80
81 git_index_free(new_index);
82
83 /*
84 * Ensure the cleanup method won't try to free the repo as it's already been taken care of
85 */
86 repo = NULL;
87 }
88
89 void test_repo_setters__setting_a_new_odb_on_a_repo_which_already_loaded_one_properly_honors_the_refcount(void)
90 {
91 git_odb *new_odb;
92
93 cl_git_pass(git_odb_open(&new_odb, "./testrepo.git/objects"));
94 cl_assert(((git_refcount *)new_odb)->refcount.val == 1);
95
96 git_repository_set_odb(repo, new_odb);
97 cl_assert(((git_refcount *)new_odb)->refcount.val == 2);
98
99 git_repository_free(repo);
100 cl_assert(((git_refcount *)new_odb)->refcount.val == 1);
101
102 git_odb_free(new_odb);
103
104 /*
105 * Ensure the cleanup method won't try to free the repo as it's already been taken care of
106 */
107 repo = NULL;
108 }