]> git.proxmox.com Git - libgit2.git/blame - tests/repo/setters.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / repo / setters.c
CommitLineData
b78fb64d 1#include "clar_libgit2.h"
1384b688
RB
2#include "git2/sys/repository.h"
3
b78fb64d 4#include "buffer.h"
1de77cd3 5#include "posix.h"
c1aefb35 6#include "util.h"
991a56c7 7#include "path.h"
22a2d3d5 8#include "futils.h"
b78fb64d 9
10static git_repository *repo;
11
12void test_repo_setters__initialize(void)
13{
14 cl_fixture_sandbox("testrepo.git");
15 cl_git_pass(git_repository_open(&repo, "testrepo.git"));
1de77cd3 16 cl_must_pass(p_mkdir("new_workdir", 0777));
b78fb64d 17}
18
19void test_repo_setters__cleanup(void)
20{
21 git_repository_free(repo);
9094d30b
SC
22 repo = NULL;
23
b78fb64d 24 cl_fixture_cleanup("testrepo.git");
991a56c7 25 cl_fixture_cleanup("new_workdir");
b78fb64d 26}
27
28void 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);
991a56c7 33 cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false));
b78fb64d 34
35 cl_assert(git_repository_workdir(repo) != NULL);
36 cl_assert(git_repository_is_bare(repo) == 0);
37}
38
39void test_repo_setters__setting_a_workdir_prettifies_its_path(void)
40{
991a56c7 41 cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false));
b78fb64d 42
991a56c7
RB
43 cl_assert(git__suffixcmp(git_repository_workdir(repo), "new_workdir/") == 0);
44}
45
46void test_repo_setters__setting_a_workdir_creates_a_gitlink(void)
47{
48 git_config *cfg;
9a97f49e 49 git_buf buf = GIT_BUF_INIT;
991a56c7
RB
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);
ac3d33df 59 git_buf_dispose(&content);
991a56c7
RB
60
61 cl_git_pass(git_repository_config(&cfg, repo));
9a97f49e
CMN
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
ac3d33df 65 git_buf_dispose(&buf);
991a56c7 66 git_config_free(cfg);
b78fb64d 67}
c1aefb35 68
69void 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"));
05b17964 74 cl_assert(((git_refcount *)new_index)->refcount.val == 1);
c1aefb35 75
76 git_repository_set_index(repo, new_index);
05b17964 77 cl_assert(((git_refcount *)new_index)->refcount.val == 2);
c1aefb35 78
79 git_repository_free(repo);
05b17964 80 cl_assert(((git_refcount *)new_index)->refcount.val == 1);
c1aefb35 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}
baf861a5 89
90void 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"));
05b17964 95 cl_assert(((git_refcount *)new_odb)->refcount.val == 1);
baf861a5 96
97 git_repository_set_odb(repo, new_odb);
05b17964 98 cl_assert(((git_refcount *)new_odb)->refcount.val == 2);
baf861a5 99
100 git_repository_free(repo);
05b17964 101 cl_assert(((git_refcount *)new_odb)->refcount.val == 1);
baf861a5 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}