init.c 3.53 KB
Newer Older
1 2 3 4
#include "clar_libgit2.h"
#include "posix.h"
#include "path.h"
#include "submodule_helpers.h"
5
#include "futils.h"
6 7 8 9 10 11 12 13 14 15 16 17

static git_repository *g_repo = NULL;

void test_submodule_init__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

void test_submodule_init__absolute_url(void)
{
	git_submodule *sm;
	git_config *cfg;
18
	git_str absolute_url = GIT_STR_INIT;
19 20 21 22
	const char *config_url;

	g_repo = setup_fixture_submodule_simple();

23
	cl_assert(git_fs_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
24
	cl_git_pass(git_str_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
25 26

	/* write the absolute url to the .gitmodules file*/
27 28 29
	cl_git_pass(git_submodule_set_url(g_repo, "testrepo", absolute_url.ptr));

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
30 31 32 33 34 35 36

	/* verify that the .gitmodules is set with an absolute path*/
	cl_assert_equal_s(absolute_url.ptr, git_submodule_url(sm));

	/* init and verify that absolute path is written to .git/config */
	cl_git_pass(git_submodule_init(sm, false));

37
	cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
38

39
	cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url"));
40 41
	cl_assert_equal_s(absolute_url.ptr, config_url);

42
	git_str_dispose(&absolute_url);
43
	git_config_free(cfg);
44
	git_submodule_free(sm);
45 46 47 48 49 50
}

void test_submodule_init__relative_url(void)
{
	git_submodule *sm;
	git_config *cfg;
51
	git_str absolute_url = GIT_STR_INIT;
52 53 54 55
	const char *config_url;

	g_repo = setup_fixture_submodule_simple();

56
	cl_assert(git_fs_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
57
	cl_git_pass(git_str_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
58 59 60 61 62 63 64 65 66

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));

	/* verify that the .gitmodules is set with an absolute path*/
	cl_assert_equal_s("../testrepo.git", git_submodule_url(sm));

	/* init and verify that absolute path is written to .git/config */
	cl_git_pass(git_submodule_init(sm, false));

67
	cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
68

69
	cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url"));
70 71
	cl_assert_equal_s(absolute_url.ptr, config_url);

72
	git_str_dispose(&absolute_url);
73
	git_config_free(cfg);
74
	git_submodule_free(sm);
75
}
76 77 78 79 80

void test_submodule_init__relative_url_detached_head(void)
{
	git_submodule *sm;
	git_config *cfg;
81
	git_str absolute_url = GIT_STR_INIT;
82 83 84 85 86 87 88 89
	const char *config_url;
	git_reference *head_ref = NULL;
	git_object *head_commit = NULL;

	g_repo = setup_fixture_submodule_simple();

	/* Put the parent repository into a detached head state. */
	cl_git_pass(git_repository_head(&head_ref, g_repo));
90
	cl_git_pass(git_reference_peel(&head_commit, head_ref, GIT_OBJECT_COMMIT));
91

92
	cl_git_pass(git_repository_set_head_detached(g_repo, git_commit_id((git_commit *)head_commit)));
93

94
	cl_assert(git_fs_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
95
	cl_git_pass(git_str_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
96 97 98 99 100 101 102 103 104

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));

	/* verify that the .gitmodules is set with an absolute path*/
	cl_assert_equal_s("../testrepo.git", git_submodule_url(sm));

	/* init and verify that absolute path is written to .git/config */
	cl_git_pass(git_submodule_init(sm, false));

105
	cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
106

107
	cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url"));
108 109
	cl_assert_equal_s(absolute_url.ptr, config_url);

110
	git_str_dispose(&absolute_url);
111
	git_config_free(cfg);
Carlos Martín Nieto committed
112
	git_object_free(head_commit);
113 114
	git_reference_free(head_ref);
	git_submodule_free(sm);
115
}