]> git.proxmox.com Git - libgit2.git/blob - tests/libgit2/submodule/lookup.c
New upstream version 1.5.0+ds
[libgit2.git] / tests / libgit2 / submodule / lookup.c
1 #include "clar_libgit2.h"
2 #include "submodule_helpers.h"
3 #include "git2/sys/repository.h"
4 #include "repository.h"
5 #include "futils.h"
6
7 static git_repository *g_repo = NULL;
8
9 void test_submodule_lookup__initialize(void)
10 {
11 g_repo = setup_fixture_submod2();
12 }
13
14 void test_submodule_lookup__cleanup(void)
15 {
16 cl_git_sandbox_cleanup();
17 }
18
19 void test_submodule_lookup__simple_lookup(void)
20 {
21 assert_submodule_exists(g_repo, "sm_unchanged");
22
23 /* lookup pending change in .gitmodules that is not in HEAD */
24 assert_submodule_exists(g_repo, "sm_added_and_uncommited");
25
26 /* lookup pending change in .gitmodules that is not in HEAD nor index */
27 assert_submodule_exists(g_repo, "sm_gitmodules_only");
28
29 /* lookup git repo subdir that is not added as submodule */
30 refute_submodule_exists(g_repo, "not-submodule", GIT_EEXISTS);
31
32 /* lookup existing directory that is not a submodule */
33 refute_submodule_exists(g_repo, "just_a_dir", GIT_ENOTFOUND);
34
35 /* lookup existing file that is not a submodule */
36 refute_submodule_exists(g_repo, "just_a_file", GIT_ENOTFOUND);
37
38 /* lookup non-existent item */
39 refute_submodule_exists(g_repo, "no_such_file", GIT_ENOTFOUND);
40
41 /* lookup a submodule by path with a trailing slash */
42 assert_submodule_exists(g_repo, "sm_added_and_uncommited/");
43 }
44
45 void test_submodule_lookup__can_be_dupped(void)
46 {
47 git_submodule *sm;
48 git_submodule *sm_duplicate;
49 const char *oid = "480095882d281ed676fe5b863569520e54a7d5c0";
50
51 /* Check original */
52 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
53 cl_assert(git_submodule_owner(sm) == g_repo);
54 cl_assert_equal_s("sm_unchanged", git_submodule_name(sm));
55 cl_assert(git__suffixcmp(git_submodule_path(sm), "sm_unchanged") == 0);
56 cl_assert(git__suffixcmp(git_submodule_url(sm), "/submod2_target") == 0);
57
58 cl_assert(git_oid_streq(git_submodule_index_id(sm), oid) == 0);
59 cl_assert(git_oid_streq(git_submodule_head_id(sm), oid) == 0);
60 cl_assert(git_oid_streq(git_submodule_wd_id(sm), oid) == 0);
61
62 cl_assert(git_submodule_ignore(sm) == GIT_SUBMODULE_IGNORE_NONE);
63 cl_assert(git_submodule_update_strategy(sm) == GIT_SUBMODULE_UPDATE_CHECKOUT);
64
65 /* Duplicate and free original */
66 cl_assert(git_submodule_dup(&sm_duplicate, sm) == 0);
67 git_submodule_free(sm);
68
69 /* Check duplicate */
70 cl_assert(git_submodule_owner(sm_duplicate) == g_repo);
71 cl_assert_equal_s("sm_unchanged", git_submodule_name(sm_duplicate));
72 cl_assert(git__suffixcmp(git_submodule_path(sm_duplicate), "sm_unchanged") == 0);
73 cl_assert(git__suffixcmp(git_submodule_url(sm_duplicate), "/submod2_target") == 0);
74
75 cl_assert(git_oid_streq(git_submodule_index_id(sm_duplicate), oid) == 0);
76 cl_assert(git_oid_streq(git_submodule_head_id(sm_duplicate), oid) == 0);
77 cl_assert(git_oid_streq(git_submodule_wd_id(sm_duplicate), oid) == 0);
78
79 cl_assert(git_submodule_ignore(sm_duplicate) == GIT_SUBMODULE_IGNORE_NONE);
80 cl_assert(git_submodule_update_strategy(sm_duplicate) == GIT_SUBMODULE_UPDATE_CHECKOUT);
81
82 git_submodule_free(sm_duplicate);
83 }
84
85 void test_submodule_lookup__accessors(void)
86 {
87 git_submodule *sm;
88 const char *oid = "480095882d281ed676fe5b863569520e54a7d5c0";
89
90 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
91 cl_assert(git_submodule_owner(sm) == g_repo);
92 cl_assert_equal_s("sm_unchanged", git_submodule_name(sm));
93 cl_assert(git__suffixcmp(git_submodule_path(sm), "sm_unchanged") == 0);
94 cl_assert(git__suffixcmp(git_submodule_url(sm), "/submod2_target") == 0);
95
96 cl_assert(git_oid_streq(git_submodule_index_id(sm), oid) == 0);
97 cl_assert(git_oid_streq(git_submodule_head_id(sm), oid) == 0);
98 cl_assert(git_oid_streq(git_submodule_wd_id(sm), oid) == 0);
99
100 cl_assert(git_submodule_ignore(sm) == GIT_SUBMODULE_IGNORE_NONE);
101 cl_assert(git_submodule_update_strategy(sm) == GIT_SUBMODULE_UPDATE_CHECKOUT);
102
103 git_submodule_free(sm);
104
105
106 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
107 cl_assert_equal_s("sm_changed_head", git_submodule_name(sm));
108
109 cl_assert(git_oid_streq(git_submodule_index_id(sm), oid) == 0);
110 cl_assert(git_oid_streq(git_submodule_head_id(sm), oid) == 0);
111 cl_assert(git_oid_streq(git_submodule_wd_id(sm),
112 "3d9386c507f6b093471a3e324085657a3c2b4247") == 0);
113
114 git_submodule_free(sm);
115
116
117 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited"));
118 cl_assert_equal_s("sm_added_and_uncommited", git_submodule_name(sm));
119
120 cl_assert(git_oid_streq(git_submodule_index_id(sm), oid) == 0);
121 cl_assert(git_submodule_head_id(sm) == NULL);
122 cl_assert(git_oid_streq(git_submodule_wd_id(sm), oid) == 0);
123
124 git_submodule_free(sm);
125
126
127 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits"));
128 cl_assert_equal_s("sm_missing_commits", git_submodule_name(sm));
129
130 cl_assert(git_oid_streq(git_submodule_index_id(sm), oid) == 0);
131 cl_assert(git_oid_streq(git_submodule_head_id(sm), oid) == 0);
132 cl_assert(git_oid_streq(git_submodule_wd_id(sm),
133 "5e4963595a9774b90524d35a807169049de8ccad") == 0);
134
135 git_submodule_free(sm);
136 }
137
138 typedef struct {
139 int count;
140 } sm_lookup_data;
141
142 static int sm_lookup_cb(git_submodule *sm, const char *name, void *payload)
143 {
144 sm_lookup_data *data = payload;
145 data->count += 1;
146 cl_assert_equal_s(git_submodule_name(sm), name);
147 return 0;
148 }
149
150 void test_submodule_lookup__foreach(void)
151 {
152 git_config *cfg;
153 sm_lookup_data data;
154
155 memset(&data, 0, sizeof(data));
156 cl_git_pass(git_submodule_foreach(g_repo, sm_lookup_cb, &data));
157 cl_assert_equal_i(8, data.count);
158
159 memset(&data, 0, sizeof(data));
160
161 /* Change the path for a submodule so it doesn't match the name */
162 cl_git_pass(git_config_open_ondisk(&cfg, "submod2/.gitmodules"));
163
164 cl_git_pass(git_config_set_string(cfg, "submodule.smchangedindex.path", "sm_changed_index"));
165 cl_git_pass(git_config_set_string(cfg, "submodule.smchangedindex.url", "../submod2_target"));
166 cl_git_pass(git_config_delete_entry(cfg, "submodule.sm_changed_index.path"));
167 cl_git_pass(git_config_delete_entry(cfg, "submodule.sm_changed_index.url"));
168
169 git_config_free(cfg);
170
171 cl_git_pass(git_submodule_foreach(g_repo, sm_lookup_cb, &data));
172 cl_assert_equal_i(8, data.count);
173 }
174
175 static int foreach_cb(git_submodule *sm, const char *name, void *payload)
176 {
177 GIT_UNUSED(sm);
178 GIT_UNUSED(name);
179 GIT_UNUSED(payload);
180 return 0;
181 }
182
183 void test_submodule_lookup__duplicated_path(void)
184 {
185 cl_git_rewritefile("submod2/.gitmodules",
186 "[submodule \"sm1\"]\n"
187 " path = duplicated-path\n"
188 " url = sm1\n"
189 "[submodule \"sm2\"]\n"
190 " path = duplicated-path\n"
191 " url = sm2\n");
192
193 cl_git_fail(git_submodule_foreach(g_repo, foreach_cb, NULL));
194 }
195
196 void test_submodule_lookup__lookup_even_with_unborn_head(void)
197 {
198 git_reference *head;
199
200 /* put us on an unborn branch */
201 cl_git_pass(git_reference_symbolic_create(
202 &head, g_repo, "HEAD", "refs/heads/garbage", 1, NULL));
203 git_reference_free(head);
204
205 test_submodule_lookup__simple_lookup(); /* baseline should still pass */
206 }
207
208 void test_submodule_lookup__lookup_even_with_missing_index(void)
209 {
210 git_index *idx;
211
212 /* give the repo an empty index */
213 cl_git_pass(git_index_new(&idx));
214 git_repository_set_index(g_repo, idx);
215 git_index_free(idx);
216
217 test_submodule_lookup__simple_lookup(); /* baseline should still pass */
218 }
219
220 void test_submodule_lookup__backslashes(void)
221 {
222 git_config *cfg;
223 git_submodule *sm;
224 git_repository *subrepo;
225 git_buf buf = GIT_BUF_INIT;
226 const char *backslashed_path = "..\\submod2_target";
227
228 cl_git_pass(git_config_open_ondisk(&cfg, "submod2/.gitmodules"));
229 cl_git_pass(git_config_set_string(cfg, "submodule.sm_unchanged.url", backslashed_path));
230 git_config_free(cfg);
231
232 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
233 cl_assert_equal_s(backslashed_path, git_submodule_url(sm));
234 cl_git_pass(git_submodule_open(&subrepo, sm));
235
236 cl_git_pass(git_submodule_resolve_url(&buf, g_repo, backslashed_path));
237
238 git_buf_dispose(&buf);
239 git_submodule_free(sm);
240 git_repository_free(subrepo);
241 }
242
243 static void baseline_tests(void)
244 {
245 /* small baseline that should work even if we change the index or make
246 * commits from the index
247 */
248 assert_submodule_exists(g_repo, "sm_unchanged");
249 assert_submodule_exists(g_repo, "sm_gitmodules_only");
250 refute_submodule_exists(g_repo, "not-submodule", GIT_EEXISTS);
251 }
252
253 static void add_submodule_with_commit(const char *name)
254 {
255 git_submodule *sm;
256 git_repository *smrepo;
257 git_index *idx;
258 git_str p = GIT_STR_INIT;
259
260 cl_git_pass(git_submodule_add_setup(&sm, g_repo,
261 "https://github.com/libgit2/libgit2.git", name, 1));
262
263 assert_submodule_exists(g_repo, name);
264
265 cl_git_pass(git_submodule_open(&smrepo, sm));
266 cl_git_pass(git_repository_index(&idx, smrepo));
267
268 cl_git_pass(git_str_joinpath(&p, git_repository_workdir(smrepo), "file"));
269 cl_git_mkfile(p.ptr, "new file");
270 git_str_dispose(&p);
271
272 cl_git_pass(git_index_add_bypath(idx, "file"));
273 cl_git_pass(git_index_write(idx));
274 git_index_free(idx);
275
276 cl_repo_commit_from_index(NULL, smrepo, NULL, 0, "initial commit");
277 git_repository_free(smrepo);
278
279 cl_git_pass(git_submodule_add_finalize(sm));
280
281 git_submodule_free(sm);
282 }
283
284 void test_submodule_lookup__just_added(void)
285 {
286 git_submodule *sm;
287 git_str snap1 = GIT_STR_INIT, snap2 = GIT_STR_INIT;
288 git_reference *original_head = NULL;
289
290 refute_submodule_exists(g_repo, "sm_just_added", GIT_ENOTFOUND);
291 refute_submodule_exists(g_repo, "sm_just_added_2", GIT_ENOTFOUND);
292 refute_submodule_exists(g_repo, "sm_just_added_idx", GIT_ENOTFOUND);
293 refute_submodule_exists(g_repo, "sm_just_added_head", GIT_ENOTFOUND);
294 refute_submodule_exists(g_repo, "mismatch_name", GIT_ENOTFOUND);
295 refute_submodule_exists(g_repo, "mismatch_path", GIT_ENOTFOUND);
296 baseline_tests();
297
298 cl_git_pass(git_futils_readbuffer(&snap1, "submod2/.gitmodules"));
299 cl_git_pass(git_repository_head(&original_head, g_repo));
300
301 cl_git_pass(git_submodule_add_setup(&sm, g_repo,
302 "https://github.com/libgit2/libgit2.git", "sm_just_added", 1));
303 git_submodule_free(sm);
304 assert_submodule_exists(g_repo, "sm_just_added");
305
306 cl_git_pass(git_submodule_add_setup(&sm, g_repo,
307 "https://github.com/libgit2/libgit2.git", "sm_just_added_2", 1));
308 assert_submodule_exists(g_repo, "sm_just_added_2");
309 cl_git_fail(git_submodule_add_finalize(sm)); /* fails if no HEAD */
310 git_submodule_free(sm);
311
312 add_submodule_with_commit("sm_just_added_head");
313 cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "commit new sm to head");
314 assert_submodule_exists(g_repo, "sm_just_added_head");
315
316 add_submodule_with_commit("sm_just_added_idx");
317 assert_submodule_exists(g_repo, "sm_just_added_idx");
318
319 cl_git_pass(git_futils_readbuffer(&snap2, "submod2/.gitmodules"));
320
321 cl_git_append2file(
322 "submod2/.gitmodules",
323 "\n[submodule \"mismatch_name\"]\n"
324 "\tpath = mismatch_path\n"
325 "\turl = https://example.com/example.git\n\n");
326
327 assert_submodule_exists(g_repo, "mismatch_name");
328 assert_submodule_exists(g_repo, "mismatch_path");
329 assert_submodule_exists(g_repo, "sm_just_added");
330 assert_submodule_exists(g_repo, "sm_just_added_2");
331 assert_submodule_exists(g_repo, "sm_just_added_idx");
332 assert_submodule_exists(g_repo, "sm_just_added_head");
333 baseline_tests();
334
335 cl_git_rewritefile("submod2/.gitmodules", snap2.ptr);
336 git_str_dispose(&snap2);
337
338 refute_submodule_exists(g_repo, "mismatch_name", GIT_ENOTFOUND);
339 refute_submodule_exists(g_repo, "mismatch_path", GIT_ENOTFOUND);
340 assert_submodule_exists(g_repo, "sm_just_added");
341 assert_submodule_exists(g_repo, "sm_just_added_2");
342 assert_submodule_exists(g_repo, "sm_just_added_idx");
343 assert_submodule_exists(g_repo, "sm_just_added_head");
344 baseline_tests();
345
346 cl_git_rewritefile("submod2/.gitmodules", snap1.ptr);
347 git_str_dispose(&snap1);
348
349 refute_submodule_exists(g_repo, "mismatch_name", GIT_ENOTFOUND);
350 refute_submodule_exists(g_repo, "mismatch_path", GIT_ENOTFOUND);
351 /* note error code change, because add_setup made a repo in the workdir */
352 refute_submodule_exists(g_repo, "sm_just_added", GIT_EEXISTS);
353 refute_submodule_exists(g_repo, "sm_just_added_2", GIT_EEXISTS);
354 /* these still exist in index and head respectively */
355 assert_submodule_exists(g_repo, "sm_just_added_idx");
356 assert_submodule_exists(g_repo, "sm_just_added_head");
357 baseline_tests();
358
359 {
360 git_index *idx;
361 cl_git_pass(git_repository_index(&idx, g_repo));
362 cl_git_pass(git_index_remove_bypath(idx, "sm_just_added_idx"));
363 cl_git_pass(git_index_remove_bypath(idx, "sm_just_added_head"));
364 cl_git_pass(git_index_write(idx));
365 git_index_free(idx);
366 }
367
368 refute_submodule_exists(g_repo, "sm_just_added_idx", GIT_EEXISTS);
369 assert_submodule_exists(g_repo, "sm_just_added_head");
370
371 {
372 cl_git_pass(git_reference_create(NULL, g_repo, "refs/heads/master", git_reference_target(original_head), 1, "move head back"));
373 git_reference_free(original_head);
374 }
375
376 refute_submodule_exists(g_repo, "sm_just_added_head", GIT_EEXISTS);
377 }
378
379 /* Test_App and Test_App2 are fairly similar names, make sure we load the right one */
380 void test_submodule_lookup__prefix_name(void)
381 {
382 git_submodule *sm;
383
384 cl_git_rewritefile("submod2/.gitmodules",
385 "[submodule \"Test_App\"]\n"
386 " path = Test_App\n"
387 " url = ../Test_App\n"
388 "[submodule \"Test_App2\"]\n"
389 " path = Test_App2\n"
390 " url = ../Test_App\n");
391
392 cl_git_pass(git_submodule_lookup(&sm, g_repo, "Test_App"));
393 cl_assert_equal_s("Test_App", git_submodule_name(sm));
394
395 git_submodule_free(sm);
396
397 cl_git_pass(git_submodule_lookup(&sm, g_repo, "Test_App2"));
398 cl_assert_equal_s("Test_App2", git_submodule_name(sm));
399
400 git_submodule_free(sm);
401 }
402
403 void test_submodule_lookup__renamed(void)
404 {
405 const char *newpath = "sm_actually_changed";
406 git_index *idx;
407 sm_lookup_data data;
408
409 cl_git_pass(git_repository_index__weakptr(&idx, g_repo));
410
411 /* We're replicating 'git mv sm_unchanged sm_actually_changed' in this test */
412
413 cl_git_pass(p_rename("submod2/sm_unchanged", "submod2/sm_actually_changed"));
414
415 /* Change the path in .gitmodules and stage it*/
416 {
417 git_config *cfg;
418
419 cl_git_pass(git_config_open_ondisk(&cfg, "submod2/.gitmodules"));
420 cl_git_pass(git_config_set_string(cfg, "submodule.sm_unchanged.path", newpath));
421 git_config_free(cfg);
422
423 cl_git_pass(git_index_add_bypath(idx, ".gitmodules"));
424 }
425
426 /* Change the worktree info in the submodule's config */
427 {
428 git_config *cfg;
429
430 cl_git_pass(git_config_open_ondisk(&cfg, "submod2/.git/modules/sm_unchanged/config"));
431 cl_git_pass(git_config_set_string(cfg, "core.worktree", "../../../sm_actually_changed"));
432 git_config_free(cfg);
433 }
434
435 /* Rename the entry in the index */
436 {
437 const git_index_entry *e;
438 git_index_entry entry = {{ 0 }};
439
440 e = git_index_get_bypath(idx, "sm_unchanged", 0);
441 cl_assert(e);
442 cl_assert_equal_i(GIT_FILEMODE_COMMIT, e->mode);
443
444 entry.path = newpath;
445 entry.mode = GIT_FILEMODE_COMMIT;
446 git_oid_cpy(&entry.id, &e->id);
447
448 cl_git_pass(git_index_remove(idx, "sm_unchanged", 0));
449 cl_git_pass(git_index_add(idx, &entry));
450 cl_git_pass(git_index_write(idx));
451 }
452
453 memset(&data, 0, sizeof(data));
454 cl_git_pass(git_submodule_foreach(g_repo, sm_lookup_cb, &data));
455 cl_assert_equal_i(8, data.count);
456 }
457
458 void test_submodule_lookup__cached(void)
459 {
460 git_submodule *sm;
461 git_submodule *sm2;
462 /* See that the simple tests still pass. */
463
464 git_repository_submodule_cache_all(g_repo);
465 test_submodule_lookup__simple_lookup();
466 git_repository_submodule_cache_clear(g_repo);
467 test_submodule_lookup__simple_lookup();
468
469 /* Check that subsequent calls return different objects when cached. */
470 git_repository_submodule_cache_all(g_repo);
471 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged"));
472 cl_git_pass(git_submodule_lookup(&sm2, g_repo, "sm_unchanged"));
473 cl_assert_equal_p(sm, sm2);
474 git_submodule_free(sm2);
475
476 /* and that we get new objects again after clearing the cache. */
477 git_repository_submodule_cache_clear(g_repo);
478 cl_git_pass(git_submodule_lookup(&sm2, g_repo, "sm_unchanged"));
479 cl_assert(sm != sm2);
480 git_submodule_free(sm);
481 git_submodule_free(sm2);
482 }
483
484 void test_submodule_lookup__lookup_in_bare_repository_fails(void)
485 {
486 git_submodule *sm;
487
488 cl_git_sandbox_cleanup();
489 g_repo = cl_git_sandbox_init("submodules.git");
490
491 cl_git_fail(git_submodule_lookup(&sm, g_repo, "nonexisting"));
492 }
493
494 void test_submodule_lookup__foreach_in_bare_repository_fails(void)
495 {
496 cl_git_sandbox_cleanup();
497 g_repo = cl_git_sandbox_init("submodules.git");
498
499 cl_git_fail(git_submodule_foreach(g_repo, foreach_cb, NULL));
500 }
501
502 void test_submodule_lookup__fail_invalid_gitmodules(void)
503 {
504 git_submodule *sm;
505 sm_lookup_data data;
506 memset(&data, 0, sizeof(data));
507
508 cl_git_rewritefile("submod2/.gitmodules",
509 "[submodule \"Test_App\"\n"
510 " path = Test_App\n"
511 " url = ../Test_App\n");
512
513 cl_git_fail(git_submodule_lookup(&sm, g_repo, "Test_App"));
514
515 cl_git_fail(git_submodule_foreach(g_repo, sm_lookup_cb, &data));
516 }