1 #include "clar_libgit2.h"
2 #include "posix.h"
3 #include "path.h"
4 #include "submodule_helpers.h"
5 #include "futils.h"
6 
7 static git_repository *g_repo = NULL;
8 
test_submodule_init__cleanup(void)9 void test_submodule_init__cleanup(void)
10 {
11 	cl_git_sandbox_cleanup();
12 }
13 
test_submodule_init__absolute_url(void)14 void test_submodule_init__absolute_url(void)
15 {
16 	git_submodule *sm;
17 	git_config *cfg;
18 	git_buf absolute_url = GIT_BUF_INIT;
19 	const char *config_url;
20 
21 	g_repo = setup_fixture_submodule_simple();
22 
23 	cl_assert(git_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
24 	cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
25 
26 	/* write the absolute url to the .gitmodules file*/
27 	cl_git_pass(git_submodule_set_url(g_repo, "testrepo", absolute_url.ptr));
28 
29 	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
30 
31 	/* verify that the .gitmodules is set with an absolute path*/
32 	cl_assert_equal_s(absolute_url.ptr, git_submodule_url(sm));
33 
34 	/* init and verify that absolute path is written to .git/config */
35 	cl_git_pass(git_submodule_init(sm, false));
36 
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 	cl_assert_equal_s(absolute_url.ptr, config_url);
41 
42 	git_buf_dispose(&absolute_url);
43 	git_config_free(cfg);
44 	git_submodule_free(sm);
45 }
46 
test_submodule_init__relative_url(void)47 void test_submodule_init__relative_url(void)
48 {
49 	git_submodule *sm;
50 	git_config *cfg;
51 	git_buf absolute_url = GIT_BUF_INIT;
52 	const char *config_url;
53 
54 	g_repo = setup_fixture_submodule_simple();
55 
56 	cl_assert(git_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
57 	cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
58 
59 	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
60 
61 	/* verify that the .gitmodules is set with an absolute path*/
62 	cl_assert_equal_s("../testrepo.git", git_submodule_url(sm));
63 
64 	/* init and verify that absolute path is written to .git/config */
65 	cl_git_pass(git_submodule_init(sm, false));
66 
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 	cl_assert_equal_s(absolute_url.ptr, config_url);
71 
72 	git_buf_dispose(&absolute_url);
73 	git_config_free(cfg);
74 	git_submodule_free(sm);
75 }
76 
test_submodule_init__relative_url_detached_head(void)77 void test_submodule_init__relative_url_detached_head(void)
78 {
79 	git_submodule *sm;
80 	git_config *cfg;
81 	git_buf absolute_url = GIT_BUF_INIT;
82 	const char *config_url;
83 	git_reference *head_ref = NULL;
84 	git_object *head_commit = NULL;
85 
86 	g_repo = setup_fixture_submodule_simple();
87 
88 	/* Put the parent repository into a detached head state. */
89 	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_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
95 	cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
96 
97 	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
98 
99 	/* verify that the .gitmodules is set with an absolute path*/
100 	cl_assert_equal_s("../testrepo.git", git_submodule_url(sm));
101 
102 	/* init and verify that absolute path is written to .git/config */
103 	cl_git_pass(git_submodule_init(sm, false));
104 
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 	cl_assert_equal_s(absolute_url.ptr, config_url);
109 
110 	git_buf_dispose(&absolute_url);
111 	git_config_free(cfg);
112 	git_object_free(head_commit);
113 	git_reference_free(head_ref);
114 	git_submodule_free(sm);
115 }
116