add.c 4.05 KB
Newer Older
1 2 3 4
#include "clar_libgit2.h"
#include "posix.h"
#include "path.h"
#include "submodule_helpers.h"
5
#include "fileops.h"
6 7 8 9 10 11 12 13

static git_repository *g_repo = NULL;

void test_submodule_add__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

Russell Belfer committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
static void assert_submodule_url(const char* name, const char *url)
{
	git_config *cfg;
	const char *s;
	git_buf key = GIT_BUF_INIT;

	cl_git_pass(git_repository_config(&cfg, g_repo));

	cl_git_pass(git_buf_printf(&key, "submodule.%s.url", name));
	cl_git_pass(git_config_get_string(&s, cfg, git_buf_cstr(&key)));
	cl_assert_equal_s(s, url);

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

30 31 32
void test_submodule_add__url_absolute(void)
{
	git_submodule *sm;
33 34 35 36
	git_config *cfg;
	git_repository *repo;
	const char *worktree_path;
	git_buf dot_git_content = GIT_BUF_INIT;
37

Russell Belfer committed
38 39
	g_repo = setup_fixture_submod2();

40
	/* re-add existing submodule */
Russell Belfer committed
41
	cl_git_fail_with(
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
		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");

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	cl_git_pass(git_repository_open(&repo, "submod2/" "sm_libgit2"));

	/* Verify worktree path is relative */
	cl_git_pass(git_repository_config(&cfg, repo));
	cl_git_pass(git_config_get_string(&worktree_path, cfg, "core.worktree"));
	cl_assert_equal_s("../../../sm_libgit2/", worktree_path);

	/* 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_config_free(cfg);
	git_repository_free(repo);
	git_buf_free(&dot_git_content);

74 75 76 77 78 79 80 81 82 83 84 85 86
	/* 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
87 88
void test_submodule_add__url_relative(void)
{
89 90
	git_submodule *sm;
	git_remote *remote;
91
	git_strarray problems = {0};
Russell Belfer committed
92

93 94
	/* default remote url is https://github.com/libgit2/false.git */
	g_repo = cl_git_sandbox_init("testrepo2");
Russell Belfer committed
95 96

	/* make sure we don't default to origin - rename origin -> test_remote */
97
	cl_git_pass(git_remote_load(&remote, g_repo, "origin"));
98 99 100
	cl_git_pass(git_remote_rename(&problems, remote, "test_remote"));
	cl_assert_equal_i(0, problems.count);
	git_strarray_free(&problems);
101 102 103 104 105 106 107
	cl_git_fail(git_remote_load(&remote, g_repo, "origin"));
	git_remote_free(remote);

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

109 110 111
	assert_submodule_url("TestGitRepository", "https://github.com/libgit2/TestGitRepository");
}

Russell Belfer committed
112 113
void test_submodule_add__url_relative_to_origin(void)
{
114
	git_submodule *sm;
Russell Belfer committed
115

116 117 118 119 120 121 122
	/* 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
123

124 125 126
	assert_submodule_url("TestGitRepository", "https://github.com/libgit2/TestGitRepository");
}

Russell Belfer committed
127 128
void test_submodule_add__url_relative_to_workdir(void)
{
129 130 131 132 133 134 135 136 137 138
	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
139
	assert_submodule_url("TestGitRepository", git_repository_workdir(g_repo));
140
}