1 #include "clar_libgit2.h"
2 #include "submodule_helpers.h"
3 #include "git2/sys/repository.h"
4 #include "repository.h"
7 static git_repository
*g_repo
= NULL
;
9 void test_submodule_lookup__initialize(void)
11 g_repo
= setup_fixture_submod2();
14 void test_submodule_lookup__cleanup(void)
16 cl_git_sandbox_cleanup();
19 void test_submodule_lookup__simple_lookup(void)
21 assert_submodule_exists(g_repo
, "sm_unchanged");
23 /* lookup pending change in .gitmodules that is not in HEAD */
24 assert_submodule_exists(g_repo
, "sm_added_and_uncommited");
26 /* lookup pending change in .gitmodules that is not in HEAD nor index */
27 assert_submodule_exists(g_repo
, "sm_gitmodules_only");
29 /* lookup git repo subdir that is not added as submodule */
30 refute_submodule_exists(g_repo
, "not-submodule", GIT_EEXISTS
);
32 /* lookup existing directory that is not a submodule */
33 refute_submodule_exists(g_repo
, "just_a_dir", GIT_ENOTFOUND
);
35 /* lookup existing file that is not a submodule */
36 refute_submodule_exists(g_repo
, "just_a_file", GIT_ENOTFOUND
);
38 /* lookup non-existent item */
39 refute_submodule_exists(g_repo
, "no_such_file", GIT_ENOTFOUND
);
41 /* lookup a submodule by path with a trailing slash */
42 assert_submodule_exists(g_repo
, "sm_added_and_uncommited/");
45 void test_submodule_lookup__accessors(void)
48 const char *oid
= "480095882d281ed676fe5b863569520e54a7d5c0";
50 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "sm_unchanged"));
51 cl_assert(git_submodule_owner(sm
) == g_repo
);
52 cl_assert_equal_s("sm_unchanged", git_submodule_name(sm
));
53 cl_assert(git__suffixcmp(git_submodule_path(sm
), "sm_unchanged") == 0);
54 cl_assert(git__suffixcmp(git_submodule_url(sm
), "/submod2_target") == 0);
56 cl_assert(git_oid_streq(git_submodule_index_id(sm
), oid
) == 0);
57 cl_assert(git_oid_streq(git_submodule_head_id(sm
), oid
) == 0);
58 cl_assert(git_oid_streq(git_submodule_wd_id(sm
), oid
) == 0);
60 cl_assert(git_submodule_ignore(sm
) == GIT_SUBMODULE_IGNORE_NONE
);
61 cl_assert(git_submodule_update_strategy(sm
) == GIT_SUBMODULE_UPDATE_CHECKOUT
);
63 git_submodule_free(sm
);
66 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "sm_changed_head"));
67 cl_assert_equal_s("sm_changed_head", git_submodule_name(sm
));
69 cl_assert(git_oid_streq(git_submodule_index_id(sm
), oid
) == 0);
70 cl_assert(git_oid_streq(git_submodule_head_id(sm
), oid
) == 0);
71 cl_assert(git_oid_streq(git_submodule_wd_id(sm
),
72 "3d9386c507f6b093471a3e324085657a3c2b4247") == 0);
74 git_submodule_free(sm
);
77 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "sm_added_and_uncommited"));
78 cl_assert_equal_s("sm_added_and_uncommited", git_submodule_name(sm
));
80 cl_assert(git_oid_streq(git_submodule_index_id(sm
), oid
) == 0);
81 cl_assert(git_submodule_head_id(sm
) == NULL
);
82 cl_assert(git_oid_streq(git_submodule_wd_id(sm
), oid
) == 0);
84 git_submodule_free(sm
);
87 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "sm_missing_commits"));
88 cl_assert_equal_s("sm_missing_commits", git_submodule_name(sm
));
90 cl_assert(git_oid_streq(git_submodule_index_id(sm
), oid
) == 0);
91 cl_assert(git_oid_streq(git_submodule_head_id(sm
), oid
) == 0);
92 cl_assert(git_oid_streq(git_submodule_wd_id(sm
),
93 "5e4963595a9774b90524d35a807169049de8ccad") == 0);
95 git_submodule_free(sm
);
102 static int sm_lookup_cb(git_submodule
*sm
, const char *name
, void *payload
)
104 sm_lookup_data
*data
= payload
;
106 cl_assert_equal_s(git_submodule_name(sm
), name
);
110 void test_submodule_lookup__foreach(void)
115 memset(&data
, 0, sizeof(data
));
116 cl_git_pass(git_submodule_foreach(g_repo
, sm_lookup_cb
, &data
));
117 cl_assert_equal_i(8, data
.count
);
119 memset(&data
, 0, sizeof(data
));
121 /* Change the path for a submodule so it doesn't match the name */
122 cl_git_pass(git_config_open_ondisk(&cfg
, "submod2/.gitmodules"));
124 cl_git_pass(git_config_set_string(cfg
, "submodule.smchangedindex.path", "sm_changed_index"));
125 cl_git_pass(git_config_set_string(cfg
, "submodule.smchangedindex.url", "../submod2_target"));
126 cl_git_pass(git_config_delete_entry(cfg
, "submodule.sm_changed_index.path"));
127 cl_git_pass(git_config_delete_entry(cfg
, "submodule.sm_changed_index.url"));
129 git_config_free(cfg
);
131 cl_git_pass(git_submodule_foreach(g_repo
, sm_lookup_cb
, &data
));
132 cl_assert_equal_i(8, data
.count
);
135 static int foreach_cb(git_submodule
*sm
, const char *name
, void *payload
)
143 void test_submodule_lookup__duplicated_path(void)
145 cl_git_rewritefile("submod2/.gitmodules",
146 "[submodule \"sm1\"]\n"
147 " path = duplicated-path\n"
149 "[submodule \"sm2\"]\n"
150 " path = duplicated-path\n"
153 cl_git_fail(git_submodule_foreach(g_repo
, foreach_cb
, NULL
));
156 void test_submodule_lookup__lookup_even_with_unborn_head(void)
160 /* put us on an unborn branch */
161 cl_git_pass(git_reference_symbolic_create(
162 &head
, g_repo
, "HEAD", "refs/heads/garbage", 1, NULL
));
163 git_reference_free(head
);
165 test_submodule_lookup__simple_lookup(); /* baseline should still pass */
168 void test_submodule_lookup__lookup_even_with_missing_index(void)
172 /* give the repo an empty index */
173 cl_git_pass(git_index_new(&idx
));
174 git_repository_set_index(g_repo
, idx
);
177 test_submodule_lookup__simple_lookup(); /* baseline should still pass */
180 void test_submodule_lookup__backslashes(void)
184 git_repository
*subrepo
;
185 git_buf buf
= GIT_BUF_INIT
;
186 const char *backslashed_path
= "..\\submod2_target";
188 cl_git_pass(git_config_open_ondisk(&cfg
, "submod2/.gitmodules"));
189 cl_git_pass(git_config_set_string(cfg
, "submodule.sm_unchanged.url", backslashed_path
));
190 git_config_free(cfg
);
192 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "sm_unchanged"));
193 cl_assert_equal_s(backslashed_path
, git_submodule_url(sm
));
194 cl_git_pass(git_submodule_open(&subrepo
, sm
));
196 cl_git_pass(git_submodule_resolve_url(&buf
, g_repo
, backslashed_path
));
198 git_buf_dispose(&buf
);
199 git_submodule_free(sm
);
200 git_repository_free(subrepo
);
203 static void baseline_tests(void)
205 /* small baseline that should work even if we change the index or make
206 * commits from the index
208 assert_submodule_exists(g_repo
, "sm_unchanged");
209 assert_submodule_exists(g_repo
, "sm_gitmodules_only");
210 refute_submodule_exists(g_repo
, "not-submodule", GIT_EEXISTS
);
213 static void add_submodule_with_commit(const char *name
)
216 git_repository
*smrepo
;
218 git_buf p
= GIT_BUF_INIT
;
220 cl_git_pass(git_submodule_add_setup(&sm
, g_repo
,
221 "https://github.com/libgit2/libgit2.git", name
, 1));
223 assert_submodule_exists(g_repo
, name
);
225 cl_git_pass(git_submodule_open(&smrepo
, sm
));
226 cl_git_pass(git_repository_index(&idx
, smrepo
));
228 cl_git_pass(git_buf_joinpath(&p
, git_repository_workdir(smrepo
), "file"));
229 cl_git_mkfile(p
.ptr
, "new file");
232 cl_git_pass(git_index_add_bypath(idx
, "file"));
233 cl_git_pass(git_index_write(idx
));
236 cl_repo_commit_from_index(NULL
, smrepo
, NULL
, 0, "initial commit");
237 git_repository_free(smrepo
);
239 cl_git_pass(git_submodule_add_finalize(sm
));
241 git_submodule_free(sm
);
244 void test_submodule_lookup__just_added(void)
247 git_buf snap1
= GIT_BUF_INIT
, snap2
= GIT_BUF_INIT
;
248 git_reference
*original_head
= NULL
;
250 refute_submodule_exists(g_repo
, "sm_just_added", GIT_ENOTFOUND
);
251 refute_submodule_exists(g_repo
, "sm_just_added_2", GIT_ENOTFOUND
);
252 refute_submodule_exists(g_repo
, "sm_just_added_idx", GIT_ENOTFOUND
);
253 refute_submodule_exists(g_repo
, "sm_just_added_head", GIT_ENOTFOUND
);
254 refute_submodule_exists(g_repo
, "mismatch_name", GIT_ENOTFOUND
);
255 refute_submodule_exists(g_repo
, "mismatch_path", GIT_ENOTFOUND
);
258 cl_git_pass(git_futils_readbuffer(&snap1
, "submod2/.gitmodules"));
259 cl_git_pass(git_repository_head(&original_head
, g_repo
));
261 cl_git_pass(git_submodule_add_setup(&sm
, g_repo
,
262 "https://github.com/libgit2/libgit2.git", "sm_just_added", 1));
263 git_submodule_free(sm
);
264 assert_submodule_exists(g_repo
, "sm_just_added");
266 cl_git_pass(git_submodule_add_setup(&sm
, g_repo
,
267 "https://github.com/libgit2/libgit2.git", "sm_just_added_2", 1));
268 assert_submodule_exists(g_repo
, "sm_just_added_2");
269 cl_git_fail(git_submodule_add_finalize(sm
)); /* fails if no HEAD */
270 git_submodule_free(sm
);
272 add_submodule_with_commit("sm_just_added_head");
273 cl_repo_commit_from_index(NULL
, g_repo
, NULL
, 0, "commit new sm to head");
274 assert_submodule_exists(g_repo
, "sm_just_added_head");
276 add_submodule_with_commit("sm_just_added_idx");
277 assert_submodule_exists(g_repo
, "sm_just_added_idx");
279 cl_git_pass(git_futils_readbuffer(&snap2
, "submod2/.gitmodules"));
282 "submod2/.gitmodules",
283 "\n[submodule \"mismatch_name\"]\n"
284 "\tpath = mismatch_path\n"
285 "\turl = https://example.com/example.git\n\n");
287 assert_submodule_exists(g_repo
, "mismatch_name");
288 assert_submodule_exists(g_repo
, "mismatch_path");
289 assert_submodule_exists(g_repo
, "sm_just_added");
290 assert_submodule_exists(g_repo
, "sm_just_added_2");
291 assert_submodule_exists(g_repo
, "sm_just_added_idx");
292 assert_submodule_exists(g_repo
, "sm_just_added_head");
295 cl_git_rewritefile("submod2/.gitmodules", snap2
.ptr
);
296 git_buf_dispose(&snap2
);
298 refute_submodule_exists(g_repo
, "mismatch_name", GIT_ENOTFOUND
);
299 refute_submodule_exists(g_repo
, "mismatch_path", GIT_ENOTFOUND
);
300 assert_submodule_exists(g_repo
, "sm_just_added");
301 assert_submodule_exists(g_repo
, "sm_just_added_2");
302 assert_submodule_exists(g_repo
, "sm_just_added_idx");
303 assert_submodule_exists(g_repo
, "sm_just_added_head");
306 cl_git_rewritefile("submod2/.gitmodules", snap1
.ptr
);
307 git_buf_dispose(&snap1
);
309 refute_submodule_exists(g_repo
, "mismatch_name", GIT_ENOTFOUND
);
310 refute_submodule_exists(g_repo
, "mismatch_path", GIT_ENOTFOUND
);
311 /* note error code change, because add_setup made a repo in the workdir */
312 refute_submodule_exists(g_repo
, "sm_just_added", GIT_EEXISTS
);
313 refute_submodule_exists(g_repo
, "sm_just_added_2", GIT_EEXISTS
);
314 /* these still exist in index and head respectively */
315 assert_submodule_exists(g_repo
, "sm_just_added_idx");
316 assert_submodule_exists(g_repo
, "sm_just_added_head");
321 cl_git_pass(git_repository_index(&idx
, g_repo
));
322 cl_git_pass(git_index_remove_bypath(idx
, "sm_just_added_idx"));
323 cl_git_pass(git_index_remove_bypath(idx
, "sm_just_added_head"));
324 cl_git_pass(git_index_write(idx
));
328 refute_submodule_exists(g_repo
, "sm_just_added_idx", GIT_EEXISTS
);
329 assert_submodule_exists(g_repo
, "sm_just_added_head");
332 cl_git_pass(git_reference_create(NULL
, g_repo
, "refs/heads/master", git_reference_target(original_head
), 1, "move head back"));
333 git_reference_free(original_head
);
336 refute_submodule_exists(g_repo
, "sm_just_added_head", GIT_EEXISTS
);
339 /* Test_App and Test_App2 are fairly similar names, make sure we load the right one */
340 void test_submodule_lookup__prefix_name(void)
344 cl_git_rewritefile("submod2/.gitmodules",
345 "[submodule \"Test_App\"]\n"
347 " url = ../Test_App\n"
348 "[submodule \"Test_App2\"]\n"
349 " path = Test_App2\n"
350 " url = ../Test_App\n");
352 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "Test_App"));
353 cl_assert_equal_s("Test_App", git_submodule_name(sm
));
355 git_submodule_free(sm
);
357 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "Test_App2"));
358 cl_assert_equal_s("Test_App2", git_submodule_name(sm
));
360 git_submodule_free(sm
);
363 void test_submodule_lookup__renamed(void)
365 const char *newpath
= "sm_actually_changed";
369 cl_git_pass(git_repository_index__weakptr(&idx
, g_repo
));
371 /* We're replicating 'git mv sm_unchanged sm_actually_changed' in this test */
373 cl_git_pass(p_rename("submod2/sm_unchanged", "submod2/sm_actually_changed"));
375 /* Change the path in .gitmodules and stage it*/
379 cl_git_pass(git_config_open_ondisk(&cfg
, "submod2/.gitmodules"));
380 cl_git_pass(git_config_set_string(cfg
, "submodule.sm_unchanged.path", newpath
));
381 git_config_free(cfg
);
383 cl_git_pass(git_index_add_bypath(idx
, ".gitmodules"));
386 /* Change the worktree info in the submodule's config */
390 cl_git_pass(git_config_open_ondisk(&cfg
, "submod2/.git/modules/sm_unchanged/config"));
391 cl_git_pass(git_config_set_string(cfg
, "core.worktree", "../../../sm_actually_changed"));
392 git_config_free(cfg
);
395 /* Rename the entry in the index */
397 const git_index_entry
*e
;
398 git_index_entry entry
= {{ 0 }};
400 e
= git_index_get_bypath(idx
, "sm_unchanged", 0);
402 cl_assert_equal_i(GIT_FILEMODE_COMMIT
, e
->mode
);
404 entry
.path
= newpath
;
405 entry
.mode
= GIT_FILEMODE_COMMIT
;
406 git_oid_cpy(&entry
.id
, &e
->id
);
408 cl_git_pass(git_index_remove(idx
, "sm_unchanged", 0));
409 cl_git_pass(git_index_add(idx
, &entry
));
410 cl_git_pass(git_index_write(idx
));
413 memset(&data
, 0, sizeof(data
));
414 cl_git_pass(git_submodule_foreach(g_repo
, sm_lookup_cb
, &data
));
415 cl_assert_equal_i(8, data
.count
);
418 void test_submodule_lookup__cached(void)
422 /* See that the simple tests still pass. */
424 git_repository_submodule_cache_all(g_repo
);
425 test_submodule_lookup__simple_lookup();
426 git_repository_submodule_cache_clear(g_repo
);
427 test_submodule_lookup__simple_lookup();
429 /* Check that subsequent calls return different objects when cached. */
430 git_repository_submodule_cache_all(g_repo
);
431 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "sm_unchanged"));
432 cl_git_pass(git_submodule_lookup(&sm2
, g_repo
, "sm_unchanged"));
433 cl_assert_equal_p(sm
, sm2
);
434 git_submodule_free(sm2
);
436 /* and that we get new objects again after clearing the cache. */
437 git_repository_submodule_cache_clear(g_repo
);
438 cl_git_pass(git_submodule_lookup(&sm2
, g_repo
, "sm_unchanged"));
439 cl_assert(sm
!= sm2
);
440 git_submodule_free(sm
);
441 git_submodule_free(sm2
);
444 void test_submodule_lookup__lookup_in_bare_repository_fails(void)
448 cl_git_sandbox_cleanup();
449 g_repo
= cl_git_sandbox_init("submodules.git");
451 cl_git_fail(git_submodule_lookup(&sm
, g_repo
, "nonexisting"));
454 void test_submodule_lookup__foreach_in_bare_repository_fails(void)
456 cl_git_sandbox_cleanup();
457 g_repo
= cl_git_sandbox_init("submodules.git");
459 cl_git_fail(git_submodule_foreach(g_repo
, foreach_cb
, NULL
));
462 void test_submodule_lookup__fail_invalid_gitmodules(void)
466 memset(&data
, 0, sizeof(data
));
468 cl_git_rewritefile("submod2/.gitmodules",
469 "[submodule \"Test_App\"\n"
471 " url = ../Test_App\n");
473 cl_git_fail(git_submodule_lookup(&sm
, g_repo
, "Test_App"));
475 cl_git_fail(git_submodule_foreach(g_repo
, sm_lookup_cb
, &data
));