setters.c 2.96 KB
Newer Older
1
#include "clar_libgit2.h"
2 3
#include "git2/sys/repository.h"

4
#include "posix.h"
5
#include "util.h"
6
#include "path.h"
7
#include "futils.h"
8 9 10 11 12 13 14

static git_repository *repo;

void test_repo_setters__initialize(void)
{
	cl_fixture_sandbox("testrepo.git");
	cl_git_pass(git_repository_open(&repo, "testrepo.git"));
15
	cl_must_pass(p_mkdir("new_workdir", 0777));
16 17 18 19 20
}

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

23
	cl_fixture_cleanup("testrepo.git");
24
	cl_fixture_cleanup("new_workdir");
25 26 27 28 29 30 31
}

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);
32
	cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false));
33 34 35 36 37 38 39

	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)
{
40
	cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false));
41

42 43 44 45 46 47
	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;
48
	git_buf buf = GIT_BUF_INIT;
49
	git_str content = GIT_STR_INIT;
50 51 52

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

53
	cl_assert(git_fs_path_isfile("./new_workdir/.git"));
54 55

	cl_git_pass(git_futils_readbuffer(&content, "./new_workdir/.git"));
56 57 58
	cl_assert(git__prefixcmp(git_str_cstr(&content), "gitdir: ") == 0);
	cl_assert(git__suffixcmp(git_str_cstr(&content), "testrepo.git/") == 0);
	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 69 70 71 72

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"));
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 82

	git_index_free(new_index);

83
	/*
84 85 86 87
	 * Ensure the cleanup method won't try to free the repo as it's already been taken care of
	 */
	repo = NULL;
}
88 89 90 91 92 93

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"));
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 103

	git_odb_free(new_odb);

104
	/*
105 106 107 108
	 * Ensure the cleanup method won't try to free the repo as it's already been taken care of
	 */
	repo = NULL;
}