]> git.proxmox.com Git - libgit2.git/blob - tests/submodule/add.c
New upstream version 1.3.0+dfsg.1
[libgit2.git] / tests / submodule / add.c
1 #include "clar_libgit2.h"
2 #include "posix.h"
3 #include "path.h"
4 #include "submodule_helpers.h"
5 #include "config/config_helpers.h"
6 #include "futils.h"
7 #include "repository.h"
8 #include "git2/sys/commit.h"
9
10 static git_repository *g_repo = NULL;
11 static const char *valid_blob_id = "fa49b077972391ad58037050f2a75f74e3671e92";
12
13 void test_submodule_add__cleanup(void)
14 {
15 cl_git_sandbox_cleanup();
16 }
17
18 static void assert_submodule_url(const char* name, const char *url)
19 {
20 git_buf key = GIT_BUF_INIT;
21
22
23 cl_git_pass(git_buf_printf(&key, "submodule.%s.url", name));
24 assert_config_entry_value(g_repo, git_buf_cstr(&key), url);
25
26 git_buf_dispose(&key);
27 }
28
29 void test_submodule_add__url_absolute(void)
30 {
31 git_submodule *sm;
32 git_repository *repo;
33 git_buf dot_git_content = GIT_BUF_INIT;
34
35 g_repo = setup_fixture_submod2();
36
37 /* re-add existing submodule */
38 cl_git_fail_with(
39 GIT_EEXISTS,
40 git_submodule_add_setup(NULL, g_repo, "whatever", "sm_unchanged", 1));
41
42 /* add a submodule using a gitlink */
43
44 cl_git_pass(
45 git_submodule_add_setup(&sm, g_repo, "https://github.com/libgit2/libgit2.git", "sm_libgit2", 1)
46 );
47 git_submodule_free(sm);
48
49 cl_assert(git_path_isfile("submod2/" "sm_libgit2" "/.git"));
50
51 cl_assert(git_path_isdir("submod2/.git/modules"));
52 cl_assert(git_path_isdir("submod2/.git/modules/" "sm_libgit2"));
53 cl_assert(git_path_isfile("submod2/.git/modules/" "sm_libgit2" "/HEAD"));
54 assert_submodule_url("sm_libgit2", "https://github.com/libgit2/libgit2.git");
55
56 cl_git_pass(git_repository_open(&repo, "submod2/" "sm_libgit2"));
57
58 /* Verify worktree path is relative */
59 assert_config_entry_value(repo, "core.worktree", "../../../sm_libgit2/");
60
61 /* Verify gitdir path is relative */
62 cl_git_pass(git_futils_readbuffer(&dot_git_content, "submod2/" "sm_libgit2" "/.git"));
63 cl_assert_equal_s("gitdir: ../.git/modules/sm_libgit2/", dot_git_content.ptr);
64
65 git_repository_free(repo);
66 git_buf_dispose(&dot_git_content);
67
68 /* add a submodule not using a gitlink */
69
70 cl_git_pass(
71 git_submodule_add_setup(&sm, g_repo, "https://github.com/libgit2/libgit2.git", "sm_libgit2b", 0)
72 );
73 git_submodule_free(sm);
74
75 cl_assert(git_path_isdir("submod2/" "sm_libgit2b" "/.git"));
76 cl_assert(git_path_isfile("submod2/" "sm_libgit2b" "/.git/HEAD"));
77 cl_assert(!git_path_exists("submod2/.git/modules/" "sm_libgit2b"));
78 assert_submodule_url("sm_libgit2b", "https://github.com/libgit2/libgit2.git");
79 }
80
81 void test_submodule_add__url_relative(void)
82 {
83 git_submodule *sm;
84 git_remote *remote;
85 git_strarray problems = {0};
86
87 /* default remote url is https://github.com/libgit2/false.git */
88 g_repo = cl_git_sandbox_init("testrepo2");
89
90 /* make sure we don't default to origin - rename origin -> test_remote */
91 cl_git_pass(git_remote_rename(&problems, g_repo, "origin", "test_remote"));
92 cl_assert_equal_i(0, problems.count);
93 git_strarray_dispose(&problems);
94 cl_git_fail(git_remote_lookup(&remote, g_repo, "origin"));
95
96 cl_git_pass(
97 git_submodule_add_setup(&sm, g_repo, "../TestGitRepository", "TestGitRepository", 1)
98 );
99 git_submodule_free(sm);
100
101 assert_submodule_url("TestGitRepository", "https://github.com/libgit2/TestGitRepository");
102 }
103
104 void test_submodule_add__url_relative_to_origin(void)
105 {
106 git_submodule *sm;
107
108 /* default remote url is https://github.com/libgit2/false.git */
109 g_repo = cl_git_sandbox_init("testrepo2");
110
111 cl_git_pass(
112 git_submodule_add_setup(&sm, g_repo, "../TestGitRepository", "TestGitRepository", 1)
113 );
114 git_submodule_free(sm);
115
116 assert_submodule_url("TestGitRepository", "https://github.com/libgit2/TestGitRepository");
117 }
118
119 void test_submodule_add__url_relative_to_workdir(void)
120 {
121 git_submodule *sm;
122
123 /* In this repo, HEAD (master) has no remote tracking branc h*/
124 g_repo = cl_git_sandbox_init("testrepo");
125
126 cl_git_pass(
127 git_submodule_add_setup(&sm, g_repo, "./", "TestGitRepository", 1)
128 );
129 git_submodule_free(sm);
130
131 assert_submodule_url("TestGitRepository", git_repository_workdir(g_repo));
132 }
133
134 static void test_add_entry(
135 git_index *index,
136 const char *idstr,
137 const char *path,
138 git_filemode_t mode)
139 {
140 git_index_entry entry = {{0}};
141
142 cl_git_pass(git_oid_fromstr(&entry.id, idstr));
143
144 entry.path = path;
145 entry.mode = mode;
146
147 cl_git_pass(git_index_add(index, &entry));
148 }
149
150 void test_submodule_add__path_exists_in_index(void)
151 {
152 git_index *index;
153 git_submodule *sm;
154 git_buf filename = GIT_BUF_INIT;
155
156 g_repo = cl_git_sandbox_init("testrepo");
157
158 cl_git_pass(git_buf_joinpath(&filename, "subdirectory", "test.txt"));
159
160 cl_git_pass(git_repository_index__weakptr(&index, g_repo));
161
162 test_add_entry(index, valid_blob_id, filename.ptr, GIT_FILEMODE_BLOB);
163
164 cl_git_fail_with(git_submodule_add_setup(&sm, g_repo, "./", "subdirectory", 1), GIT_EEXISTS);
165
166 git_submodule_free(sm);
167 git_buf_dispose(&filename);
168 }
169
170 void test_submodule_add__file_exists_in_index(void)
171 {
172 git_index *index;
173 git_submodule *sm;
174 git_buf name = GIT_BUF_INIT;
175
176 g_repo = cl_git_sandbox_init("testrepo");
177
178 cl_git_pass(git_repository_index__weakptr(&index, g_repo));
179
180 test_add_entry(index, valid_blob_id, "subdirectory", GIT_FILEMODE_BLOB);
181
182 cl_git_fail_with(git_submodule_add_setup(&sm, g_repo, "./", "subdirectory", 1), GIT_EEXISTS);
183
184 git_submodule_free(sm);
185 git_buf_dispose(&name);
186 }
187
188 void test_submodule_add__submodule_clone(void)
189 {
190 git_oid tree_id, commit_id;
191 git_signature *sig;
192 git_submodule *sm;
193 git_index *index;
194
195 g_repo = cl_git_sandbox_init("empty_standard_repo");
196
197 /* Create the submodule structure, clone into it and finalize */
198 cl_git_pass(git_submodule_add_setup(&sm, g_repo, cl_fixture("testrepo.git"), "testrepo-add", true));
199 cl_git_pass(git_submodule_clone(NULL, sm, NULL));
200 cl_git_pass(git_submodule_add_finalize(sm));
201
202 /* Create the submodule commit */
203 cl_git_pass(git_repository_index(&index, g_repo));
204 cl_git_pass(git_index_write_tree(&tree_id, index));
205 cl_git_pass(git_signature_now(&sig, "Submoduler", "submoduler@local"));
206 cl_git_pass(git_commit_create_from_ids(&commit_id, g_repo, "HEAD", sig, sig, NULL, "A submodule\n",
207 &tree_id, 0, NULL));
208
209 assert_submodule_exists(g_repo, "testrepo-add");
210
211 git_signature_free(sig);
212 git_submodule_free(sm);
213 git_index_free(index);
214 }
215
216 void test_submodule_add__submodule_clone_into_nonempty_dir_succeeds(void)
217 {
218 git_submodule *sm;
219
220 g_repo = cl_git_sandbox_init("empty_standard_repo");
221
222 cl_git_pass(p_mkdir("empty_standard_repo/sm", 0777));
223 cl_git_mkfile("empty_standard_repo/sm/foobar", "");
224
225 /* Create the submodule structure, clone into it and finalize */
226 cl_git_pass(git_submodule_add_setup(&sm, g_repo, cl_fixture("testrepo.git"), "sm", true));
227 cl_git_pass(git_submodule_clone(NULL, sm, NULL));
228 cl_git_pass(git_submodule_add_finalize(sm));
229
230 cl_assert(git_path_exists("empty_standard_repo/sm/foobar"));
231
232 assert_submodule_exists(g_repo, "sm");
233
234 git_submodule_free(sm);
235 }
236
237 void test_submodule_add__submodule_clone_twice_fails(void)
238 {
239 git_submodule *sm;
240
241 g_repo = cl_git_sandbox_init("empty_standard_repo");
242
243 /* Create the submodule structure, clone into it and finalize */
244 cl_git_pass(git_submodule_add_setup(&sm, g_repo, cl_fixture("testrepo.git"), "sm", true));
245 cl_git_pass(git_submodule_clone(NULL, sm, NULL));
246 cl_git_pass(git_submodule_add_finalize(sm));
247
248 cl_git_fail(git_submodule_clone(NULL, sm, NULL));
249
250 git_submodule_free(sm);
251 }