summaryrefslogtreecommitdiff
path: root/tests/repo/setters.c
blob: 5a83fdbee4060aef0f085417a9fa85efed14385c (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "clar_libgit2.h"
#include "git2/sys/repository.h"

#include "buffer.h"
#include "posix.h"
#include "util.h"
#include "path.h"
#include "fileops.h"

static git_repository *repo;

void test_repo_setters__initialize(void)
{
	cl_fixture_sandbox("testrepo.git");
	cl_git_pass(git_repository_open(&repo, "testrepo.git"));
	cl_must_pass(p_mkdir("new_workdir", 0777));
}

void test_repo_setters__cleanup(void)
{
	git_repository_free(repo);
	repo = NULL;

	cl_fixture_cleanup("testrepo.git");
	cl_fixture_cleanup("new_workdir");
}

void test_repo_setters__setting_a_workdir_turns_a_bare_repository_into_a_standard_one(void)
{
	cl_assert(git_repository_is_bare(repo) == 1);

	cl_assert(git_repository_workdir(repo) == NULL);
	cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false));

	cl_assert(git_repository_workdir(repo) != NULL);
	cl_assert(git_repository_is_bare(repo) == 0);
}

void test_repo_setters__setting_a_workdir_prettifies_its_path(void)
{
	cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false));

	cl_assert(git__suffixcmp(git_repository_workdir(repo), "new_workdir/") == 0);
}

void test_repo_setters__setting_a_workdir_creates_a_gitlink(void)
{
	git_config *cfg;
	git_buf buf = GIT_BUF_INIT;
	git_buf content = GIT_BUF_INIT;

	cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", true));

	cl_assert(git_path_isfile("./new_workdir/.git"));

	cl_git_pass(git_futils_readbuffer(&content, "./new_workdir/.git"));
	cl_assert(git__prefixcmp(git_buf_cstr(&content), "gitdir: ") == 0);
	cl_assert(git__suffixcmp(git_buf_cstr(&content), "testrepo.git/") == 0);
	git_buf_free(&content);

	cl_git_pass(git_repository_config(&cfg, repo));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.worktree"));
	cl_assert(git__suffixcmp(git_buf_cstr(&buf), "new_workdir/") == 0);

	git_buf_free(&buf);
	git_config_free(cfg);
}

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.val == 1);

	git_repository_set_index(repo, new_index);
	cl_assert(((git_refcount *)new_index)->refcount.val == 2);

	git_repository_free(repo);
	cl_assert(((git_refcount *)new_index)->refcount.val == 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;
}

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.val == 1);

	git_repository_set_odb(repo, new_odb);
	cl_assert(((git_refcount *)new_odb)->refcount.val == 2);

	git_repository_free(repo);
	cl_assert(((git_refcount *)new_odb)->refcount.val == 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;
}