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