add.c 5.04 KB
Newer Older
1 2 3 4
#include "clar_libgit2.h"
#include "posix.h"
#include "path.h"
#include "submodule_helpers.h"
5
#include "config/config_helpers.h"
6
#include "fileops.h"
7
#include "repository.h"
8 9

static git_repository *g_repo = NULL;
10
static const char *valid_blob_id = "fa49b077972391ad58037050f2a75f74e3671e92";
11 12 13 14 15 16

void test_submodule_add__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

Russell Belfer committed
17 18 19 20 21 22
static void assert_submodule_url(const char* name, const char *url)
{
	git_buf key = GIT_BUF_INIT;


	cl_git_pass(git_buf_printf(&key, "submodule.%s.url", name));
23
	assert_config_entry_value(g_repo, git_buf_cstr(&key), url);
Russell Belfer committed
24 25 26 27

	git_buf_free(&key);
}

28 29 30
void test_submodule_add__url_absolute(void)
{
	git_submodule *sm;
31 32
	git_repository *repo;
	git_buf dot_git_content = GIT_BUF_INIT;
33

Russell Belfer committed
34 35
	g_repo = setup_fixture_submod2();

36
	/* re-add existing submodule */
Russell Belfer committed
37
	cl_git_fail_with(
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
		GIT_EEXISTS,
		git_submodule_add_setup(NULL, g_repo, "whatever", "sm_unchanged", 1));

	/* add a submodule using a gitlink */

	cl_git_pass(
		git_submodule_add_setup(&sm, g_repo, "https://github.com/libgit2/libgit2.git", "sm_libgit2", 1)
		);
	git_submodule_free(sm);

	cl_assert(git_path_isfile("submod2/" "sm_libgit2" "/.git"));

	cl_assert(git_path_isdir("submod2/.git/modules"));
	cl_assert(git_path_isdir("submod2/.git/modules/" "sm_libgit2"));
	cl_assert(git_path_isfile("submod2/.git/modules/" "sm_libgit2" "/HEAD"));
	assert_submodule_url("sm_libgit2", "https://github.com/libgit2/libgit2.git");

55 56 57
	cl_git_pass(git_repository_open(&repo, "submod2/" "sm_libgit2"));

	/* Verify worktree path is relative */
58
	assert_config_entry_value(repo, "core.worktree", "../../../sm_libgit2/");
59 60 61 62 63 64 65 66

	/* Verify gitdir path is relative */
	cl_git_pass(git_futils_readbuffer(&dot_git_content, "submod2/" "sm_libgit2" "/.git"));
	cl_assert_equal_s("gitdir: ../.git/modules/sm_libgit2/", dot_git_content.ptr);

	git_repository_free(repo);
	git_buf_free(&dot_git_content);

67 68 69 70 71 72 73 74 75 76 77 78 79
	/* add a submodule not using a gitlink */

	cl_git_pass(
		git_submodule_add_setup(&sm, g_repo, "https://github.com/libgit2/libgit2.git", "sm_libgit2b", 0)
		);
	git_submodule_free(sm);

	cl_assert(git_path_isdir("submod2/" "sm_libgit2b" "/.git"));
	cl_assert(git_path_isfile("submod2/" "sm_libgit2b" "/.git/HEAD"));
	cl_assert(!git_path_exists("submod2/.git/modules/" "sm_libgit2b"));
	assert_submodule_url("sm_libgit2b", "https://github.com/libgit2/libgit2.git");
}

Russell Belfer committed
80 81
void test_submodule_add__url_relative(void)
{
82 83
	git_submodule *sm;
	git_remote *remote;
84
	git_strarray problems = {0};
Russell Belfer committed
85

86 87
	/* default remote url is https://github.com/libgit2/false.git */
	g_repo = cl_git_sandbox_init("testrepo2");
Russell Belfer committed
88 89

	/* make sure we don't default to origin - rename origin -> test_remote */
90
	cl_git_pass(git_remote_rename(&problems, g_repo, "origin", "test_remote"));
91 92
	cl_assert_equal_i(0, problems.count);
	git_strarray_free(&problems);
93
	cl_git_fail(git_remote_lookup(&remote, g_repo, "origin"));
94 95 96 97 98

	cl_git_pass(
		git_submodule_add_setup(&sm, g_repo, "../TestGitRepository", "TestGitRepository", 1)
		);
	git_submodule_free(sm);
Russell Belfer committed
99

100 101 102
	assert_submodule_url("TestGitRepository", "https://github.com/libgit2/TestGitRepository");
}

Russell Belfer committed
103 104
void test_submodule_add__url_relative_to_origin(void)
{
105
	git_submodule *sm;
Russell Belfer committed
106

107 108 109 110 111 112 113
	/* default remote url is https://github.com/libgit2/false.git */
	g_repo = cl_git_sandbox_init("testrepo2");

	cl_git_pass(
		git_submodule_add_setup(&sm, g_repo, "../TestGitRepository", "TestGitRepository", 1)
		);
	git_submodule_free(sm);
Russell Belfer committed
114

115 116 117
	assert_submodule_url("TestGitRepository", "https://github.com/libgit2/TestGitRepository");
}

Russell Belfer committed
118 119
void test_submodule_add__url_relative_to_workdir(void)
{
120 121 122 123 124 125 126 127 128 129
	git_submodule *sm;

	/* In this repo, HEAD (master) has no remote tracking branc h*/
	g_repo = cl_git_sandbox_init("testrepo");

	cl_git_pass(
		git_submodule_add_setup(&sm, g_repo, "./", "TestGitRepository", 1)
		);
	git_submodule_free(sm);

Russell Belfer committed
130
	assert_submodule_url("TestGitRepository", git_repository_workdir(g_repo));
131
}
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
static void test_add_entry(
	git_index *index,
	const char *idstr,
	const char *path,
	git_filemode_t mode)
{
	git_index_entry entry = {{0}};

	cl_git_pass(git_oid_fromstr(&entry.id, idstr));

	entry.path = path;
	entry.mode = mode;

	cl_git_pass(git_index_add(index, &entry));
}

149 150 151 152 153 154 155 156
void test_submodule_add__path_exists_in_index(void)
{
	git_index *index;
	git_submodule *sm;
	git_buf filename = GIT_BUF_INIT;

	g_repo = cl_git_sandbox_init("testrepo");

157
	cl_git_pass(git_buf_joinpath(&filename, "subdirectory", "test.txt"));
158

159
	cl_git_pass(git_repository_index__weakptr(&index, g_repo));
160

161
	test_add_entry(index, valid_blob_id, filename.ptr, GIT_FILEMODE_BLOB);
162

163
	cl_git_fail_with(git_submodule_add_setup(&sm, g_repo, "./", "subdirectory", 1), GIT_EEXISTS);
164

165
	git_submodule_free(sm);
166 167 168 169 170 171 172 173 174 175 176
	git_buf_free(&filename);
}

void test_submodule_add__file_exists_in_index(void)
{
	git_index *index;
	git_submodule *sm;
	git_buf name = GIT_BUF_INIT;

	g_repo = cl_git_sandbox_init("testrepo");

177
	cl_git_pass(git_repository_index__weakptr(&index, g_repo));
178

179
	test_add_entry(index, valid_blob_id, "subdirectory", GIT_FILEMODE_BLOB);
180

181
	cl_git_fail_with(git_submodule_add_setup(&sm, g_repo, "./", "subdirectory", 1), GIT_EEXISTS);
182

183
	git_submodule_free(sm);
184 185
	git_buf_free(&name);
}