]> git.proxmox.com Git - libgit2.git/blob - tests/status/submodules.c
38a39471ac24e3a3649c71d36ab82b26011bace5
[libgit2.git] / tests / status / submodules.c
1 #include "clar_libgit2.h"
2 #include "futils.h"
3 #include "status_helpers.h"
4 #include "../submodule/submodule_helpers.h"
5
6 static git_repository *g_repo = NULL;
7
8 void test_status_submodules__initialize(void)
9 {
10 }
11
12 void test_status_submodules__cleanup(void)
13 {
14 }
15
16 void test_status_submodules__api(void)
17 {
18 git_submodule *sm;
19
20 g_repo = setup_fixture_submodules();
21
22 cl_assert(git_submodule_lookup(NULL, g_repo, "nonexistent") == GIT_ENOTFOUND);
23
24 cl_assert(git_submodule_lookup(NULL, g_repo, "modified") == GIT_ENOTFOUND);
25
26 cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
27 cl_assert(sm != NULL);
28 cl_assert_equal_s("testrepo", git_submodule_name(sm));
29 cl_assert_equal_s("testrepo", git_submodule_path(sm));
30 git_submodule_free(sm);
31 }
32
33 void test_status_submodules__0(void)
34 {
35 int counts = 0;
36
37 g_repo = setup_fixture_submodules();
38
39 cl_assert(git_path_isdir("submodules/.git"));
40 cl_assert(git_path_isdir("submodules/testrepo/.git"));
41 cl_assert(git_path_isfile("submodules/.gitmodules"));
42
43 cl_git_pass(
44 git_status_foreach(g_repo, cb_status__count, &counts)
45 );
46
47 cl_assert_equal_i(6, counts);
48 }
49
50 static const char *expected_files[] = {
51 ".gitmodules",
52 "added",
53 "deleted",
54 "ignored",
55 "modified",
56 "untracked"
57 };
58
59 static unsigned int expected_status[] = {
60 GIT_STATUS_WT_MODIFIED,
61 GIT_STATUS_INDEX_NEW,
62 GIT_STATUS_INDEX_DELETED,
63 GIT_STATUS_IGNORED,
64 GIT_STATUS_WT_MODIFIED,
65 GIT_STATUS_WT_NEW
66 };
67
68 static int cb_status__match(const char *p, unsigned int s, void *payload)
69 {
70 status_entry_counts *counts = payload;
71 int idx = counts->entry_count++;
72
73 clar__assert_equal(
74 counts->file, counts->func, counts->line,
75 "Status path mismatch", 1,
76 "%s", counts->expected_paths[idx], p);
77
78 clar__assert_equal(
79 counts->file, counts->func, counts->line,
80 "Status code mismatch", 1,
81 "%o", counts->expected_statuses[idx], s);
82
83 return 0;
84 }
85
86 void test_status_submodules__1(void)
87 {
88 status_entry_counts counts;
89
90 g_repo = setup_fixture_submodules();
91
92 cl_assert(git_path_isdir("submodules/.git"));
93 cl_assert(git_path_isdir("submodules/testrepo/.git"));
94 cl_assert(git_path_isfile("submodules/.gitmodules"));
95
96 status_counts_init(counts, expected_files, expected_status);
97
98 cl_git_pass( git_status_foreach(g_repo, cb_status__match, &counts) );
99
100 cl_assert_equal_i(6, counts.entry_count);
101 }
102
103 void test_status_submodules__single_file(void)
104 {
105 unsigned int status = 0;
106 g_repo = setup_fixture_submodules();
107 cl_git_pass( git_status_file(&status, g_repo, "testrepo") );
108 cl_assert(!status);
109 }
110
111 void test_status_submodules__moved_head(void)
112 {
113 git_submodule *sm;
114 git_repository *smrepo;
115 git_oid oid;
116 git_status_options opts = GIT_STATUS_OPTIONS_INIT;
117 status_entry_counts counts;
118 static const char *expected_files_with_sub[] = {
119 ".gitmodules",
120 "added",
121 "deleted",
122 "ignored",
123 "modified",
124 "testrepo",
125 "untracked"
126 };
127 static unsigned int expected_status_with_sub[] = {
128 GIT_STATUS_WT_MODIFIED,
129 GIT_STATUS_INDEX_NEW,
130 GIT_STATUS_INDEX_DELETED,
131 GIT_STATUS_IGNORED,
132 GIT_STATUS_WT_MODIFIED,
133 GIT_STATUS_WT_MODIFIED,
134 GIT_STATUS_WT_NEW
135 };
136
137 g_repo = setup_fixture_submodules();
138
139 cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
140 cl_git_pass(git_submodule_open(&smrepo, sm));
141 git_submodule_free(sm);
142
143 /* move submodule HEAD to c47800c7266a2be04c571c04d5a6614691ea99bd */
144 cl_git_pass(
145 git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
146 cl_git_pass(git_repository_set_head_detached(smrepo, &oid));
147
148 /* first do a normal status, which should now include the submodule */
149
150 opts.flags = GIT_STATUS_OPT_DEFAULTS;
151
152 status_counts_init(
153 counts, expected_files_with_sub, expected_status_with_sub);
154 cl_git_pass(
155 git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
156 cl_assert_equal_i(7, counts.entry_count);
157
158 /* try again with EXCLUDE_SUBMODULES which should skip it */
159
160 opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
161
162 status_counts_init(counts, expected_files, expected_status);
163 cl_git_pass(
164 git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
165 cl_assert_equal_i(6, counts.entry_count);
166
167 git_repository_free(smrepo);
168 }
169
170 void test_status_submodules__dirty_workdir_only(void)
171 {
172 git_status_options opts = GIT_STATUS_OPTIONS_INIT;
173 status_entry_counts counts;
174 static const char *expected_files_with_sub[] = {
175 ".gitmodules",
176 "added",
177 "deleted",
178 "ignored",
179 "modified",
180 "testrepo",
181 "untracked"
182 };
183 static unsigned int expected_status_with_sub[] = {
184 GIT_STATUS_WT_MODIFIED,
185 GIT_STATUS_INDEX_NEW,
186 GIT_STATUS_INDEX_DELETED,
187 GIT_STATUS_IGNORED,
188 GIT_STATUS_WT_MODIFIED,
189 GIT_STATUS_WT_MODIFIED,
190 GIT_STATUS_WT_NEW
191 };
192
193 g_repo = setup_fixture_submodules();
194
195 cl_git_rewritefile("submodules/testrepo/README", "heyheyhey");
196 cl_git_mkfile("submodules/testrepo/all_new.txt", "never seen before");
197
198 /* first do a normal status, which should now include the submodule */
199
200 opts.flags = GIT_STATUS_OPT_DEFAULTS;
201
202 status_counts_init(
203 counts, expected_files_with_sub, expected_status_with_sub);
204 cl_git_pass(
205 git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
206 cl_assert_equal_i(7, counts.entry_count);
207
208 /* try again with EXCLUDE_SUBMODULES which should skip it */
209
210 opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
211
212 status_counts_init(counts, expected_files, expected_status);
213 cl_git_pass(
214 git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
215 cl_assert_equal_i(6, counts.entry_count);
216 }
217
218 void test_status_submodules__uninitialized(void)
219 {
220 git_repository *cloned_repo;
221 git_status_list *statuslist;
222
223 g_repo = cl_git_sandbox_init("submod2");
224
225 cl_git_pass(git_clone(&cloned_repo, "submod2", "submod2-clone", NULL));
226
227 cl_git_pass(git_status_list_new(&statuslist, cloned_repo, NULL));
228 cl_assert_equal_i(0, git_status_list_entrycount(statuslist));
229
230 git_status_list_free(statuslist);
231 git_repository_free(cloned_repo);
232 cl_git_sandbox_cleanup();
233 }
234
235 void test_status_submodules__contained_untracked_repo(void)
236 {
237 git_status_options opts = GIT_STATUS_OPTIONS_INIT;
238 status_entry_counts counts;
239 git_repository *contained;
240 static const char *expected_files_not_ignored[] = {
241 ".gitmodules",
242 "added",
243 "deleted",
244 "modified",
245 "untracked"
246 };
247 static unsigned int expected_status_not_ignored[] = {
248 GIT_STATUS_WT_MODIFIED,
249 GIT_STATUS_INDEX_NEW,
250 GIT_STATUS_INDEX_DELETED,
251 GIT_STATUS_WT_MODIFIED,
252 GIT_STATUS_WT_NEW,
253 };
254 static const char *expected_files_with_untracked[] = {
255 ".gitmodules",
256 "added",
257 "deleted",
258 "dir/file.md",
259 "modified",
260 "untracked"
261 };
262 static const char *expected_files_with_untracked_dir[] = {
263 ".gitmodules",
264 "added",
265 "deleted",
266 "dir/",
267 "modified",
268 "untracked"
269 };
270 static unsigned int expected_status_with_untracked[] = {
271 GIT_STATUS_WT_MODIFIED,
272 GIT_STATUS_INDEX_NEW,
273 GIT_STATUS_INDEX_DELETED,
274 GIT_STATUS_WT_NEW,
275 GIT_STATUS_WT_MODIFIED,
276 GIT_STATUS_WT_NEW
277 };
278
279 g_repo = setup_fixture_submodules();
280
281 /* skip empty directory */
282
283 cl_must_pass(p_mkdir("submodules/dir", 0777));
284 opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED;
285
286 status_counts_init(
287 counts, expected_files_not_ignored, expected_status_not_ignored);
288 cl_git_pass(git_status_foreach_ext(
289 g_repo, &opts, cb_status__match, &counts));
290 cl_assert_equal_i(5, counts.entry_count);
291
292 /* still skipping because empty == ignored */
293
294 opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
295 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
296
297 status_counts_init(
298 counts, expected_files_not_ignored, expected_status_not_ignored);
299 cl_git_pass(git_status_foreach_ext(
300 g_repo, &opts, cb_status__match, &counts));
301 cl_assert_equal_i(5, counts.entry_count);
302
303 /* find non-ignored contents of directory */
304
305 cl_git_mkfile("submodules/dir/file.md", "hello");
306 opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
307 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
308
309 status_counts_init(
310 counts, expected_files_with_untracked, expected_status_with_untracked);
311 cl_git_pass(git_status_foreach_ext(
312 g_repo, &opts, cb_status__match, &counts));
313 cl_assert_equal_i(6, counts.entry_count);
314
315 /* but skip if all content is ignored */
316
317 cl_git_append2file("submodules/.git/info/exclude", "\n*.md\n\n");
318 opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
319 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
320
321 status_counts_init(
322 counts, expected_files_not_ignored, expected_status_not_ignored);
323 cl_git_pass(git_status_foreach_ext(
324 g_repo, &opts, cb_status__match, &counts));
325 cl_assert_equal_i(5, counts.entry_count);
326
327 /* same is true if it contains a git link */
328
329 cl_git_mkfile("submodules/dir/.git", "gitlink: ../.git");
330 opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
331 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
332
333 status_counts_init(
334 counts, expected_files_not_ignored, expected_status_not_ignored);
335 cl_git_pass(git_status_foreach_ext(
336 g_repo, &opts, cb_status__match, &counts));
337 cl_assert_equal_i(5, counts.entry_count);
338
339 /* but if it contains tracked files, it should just show up as a
340 * directory and exclude the files in it
341 */
342
343 cl_git_mkfile("submodules/dir/another_file", "hello");
344 opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
345 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
346
347 status_counts_init(
348 counts, expected_files_with_untracked_dir,
349 expected_status_with_untracked);
350 cl_git_pass(git_status_foreach_ext(
351 g_repo, &opts, cb_status__match, &counts));
352 cl_assert_equal_i(6, counts.entry_count);
353
354 /* that applies to a git repo with a .git directory too */
355
356 cl_must_pass(p_unlink("submodules/dir/.git"));
357 cl_git_pass(git_repository_init(&contained, "submodules/dir", false));
358 git_repository_free(contained);
359 opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
360 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
361
362 status_counts_init(
363 counts, expected_files_with_untracked_dir,
364 expected_status_with_untracked);
365 cl_git_pass(git_status_foreach_ext(
366 g_repo, &opts, cb_status__match, &counts));
367 cl_assert_equal_i(6, counts.entry_count);
368
369 /* same result even if we don't recurse into subdirectories */
370
371 opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED;
372
373 status_counts_init(
374 counts, expected_files_with_untracked_dir,
375 expected_status_with_untracked);
376 cl_git_pass(git_status_foreach_ext(
377 g_repo, &opts, cb_status__match, &counts));
378 cl_assert_equal_i(6, counts.entry_count);
379
380 /* and if we remove the untracked file, it goes back to ignored */
381
382 cl_must_pass(p_unlink("submodules/dir/another_file"));
383
384 status_counts_init(
385 counts, expected_files_not_ignored, expected_status_not_ignored);
386 cl_git_pass(git_status_foreach_ext(
387 g_repo, &opts, cb_status__match, &counts));
388 cl_assert_equal_i(5, counts.entry_count);
389 }
390
391 void test_status_submodules__broken_stuff_that_git_allows(void)
392 {
393 git_status_options opts = GIT_STATUS_OPTIONS_INIT;
394 status_entry_counts counts;
395 git_repository *contained;
396 static const char *expected_files_with_broken[] = {
397 ".gitmodules",
398 "added",
399 "broken/tracked",
400 "deleted",
401 "ignored",
402 "modified",
403 "untracked"
404 };
405 static unsigned int expected_status_with_broken[] = {
406 GIT_STATUS_WT_MODIFIED,
407 GIT_STATUS_INDEX_NEW,
408 GIT_STATUS_INDEX_NEW,
409 GIT_STATUS_INDEX_DELETED,
410 GIT_STATUS_IGNORED,
411 GIT_STATUS_WT_MODIFIED,
412 GIT_STATUS_WT_NEW,
413 };
414
415 g_repo = setup_fixture_submodules();
416
417 opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
418 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
419 GIT_STATUS_OPT_INCLUDE_IGNORED;
420
421 /* make a directory and stick a tracked item into the index */
422 {
423 git_index *idx;
424 cl_must_pass(p_mkdir("submodules/broken", 0777));
425 cl_git_mkfile("submodules/broken/tracked", "tracked content");
426 cl_git_pass(git_repository_index(&idx, g_repo));
427 cl_git_pass(git_index_add_bypath(idx, "broken/tracked"));
428 cl_git_pass(git_index_write(idx));
429 git_index_free(idx);
430 }
431
432 status_counts_init(
433 counts, expected_files_with_broken, expected_status_with_broken);
434 cl_git_pass(git_status_foreach_ext(
435 g_repo, &opts, cb_status__match, &counts));
436 cl_assert_equal_i(7, counts.entry_count);
437
438 /* directory with tracked items that looks a little bit like a repo */
439
440 cl_must_pass(p_mkdir("submodules/broken/.git", 0777));
441 cl_must_pass(p_mkdir("submodules/broken/.git/info", 0777));
442 cl_git_mkfile("submodules/broken/.git/info/exclude", "# bogus");
443
444 status_counts_init(
445 counts, expected_files_with_broken, expected_status_with_broken);
446 cl_git_pass(git_status_foreach_ext(
447 g_repo, &opts, cb_status__match, &counts));
448 cl_assert_equal_i(7, counts.entry_count);
449
450 /* directory with tracked items that is a repo */
451
452 cl_git_pass(git_futils_rmdir_r(
453 "submodules/broken/.git", NULL, GIT_RMDIR_REMOVE_FILES));
454 cl_git_pass(git_repository_init(&contained, "submodules/broken", false));
455 git_repository_free(contained);
456
457 status_counts_init(
458 counts, expected_files_with_broken, expected_status_with_broken);
459 cl_git_pass(git_status_foreach_ext(
460 g_repo, &opts, cb_status__match, &counts));
461 cl_assert_equal_i(7, counts.entry_count);
462
463 /* directory with tracked items that claims to be a submodule but is not */
464
465 cl_git_pass(git_futils_rmdir_r(
466 "submodules/broken/.git", NULL, GIT_RMDIR_REMOVE_FILES));
467 cl_git_append2file("submodules/.gitmodules",
468 "\n[submodule \"broken\"]\n"
469 "\tpath = broken\n"
470 "\turl = https://github.com/not/used\n\n");
471
472 status_counts_init(
473 counts, expected_files_with_broken, expected_status_with_broken);
474 cl_git_pass(git_status_foreach_ext(
475 g_repo, &opts, cb_status__match, &counts));
476 cl_assert_equal_i(7, counts.entry_count);
477 }
478
479 void test_status_submodules__entry_but_dir_tracked(void)
480 {
481 git_repository *repo;
482 git_status_list *status;
483 git_diff *diff;
484 git_index *index;
485 git_tree *tree;
486
487 cl_git_pass(git_repository_init(&repo, "mixed-submodule", 0));
488 cl_git_mkfile("mixed-submodule/.gitmodules", "[submodule \"sub\"]\n path = sub\n url = ../foo\n");
489 cl_git_pass(p_mkdir("mixed-submodule/sub", 0777));
490 cl_git_mkfile("mixed-submodule/sub/file", "");
491
492 /* Create the commit with sub/file as a file, and an entry for sub in the modules list */
493 {
494 git_oid tree_id, commit_id;
495 git_signature *sig;
496 git_reference *ref;
497
498 cl_git_pass(git_repository_index(&index, repo));
499 cl_git_pass(git_index_add_bypath(index, ".gitmodules"));
500 cl_git_pass(git_index_add_bypath(index, "sub/file"));
501 cl_git_pass(git_index_write(index));
502 cl_git_pass(git_index_write_tree(&tree_id, index));
503 cl_git_pass(git_signature_now(&sig, "Sloppy Submoduler", "sloppy@example.com"));
504 cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
505 cl_git_pass(git_commit_create(&commit_id, repo, NULL, sig, sig, NULL, "message", tree, 0, NULL));
506 cl_git_pass(git_reference_create(&ref, repo, "refs/heads/master", &commit_id, 1, "commit: foo"));
507 git_reference_free(ref);
508 git_signature_free(sig);
509 }
510
511 cl_git_pass(git_diff_tree_to_index(&diff, repo, tree, index, NULL));
512 cl_assert_equal_i(0, git_diff_num_deltas(diff));
513 git_diff_free(diff);
514
515 cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, NULL));
516 cl_assert_equal_i(0, git_diff_num_deltas(diff));
517 git_diff_free(diff);
518
519 cl_git_pass(git_status_list_new(&status, repo, NULL));
520 cl_assert_equal_i(0, git_status_list_entrycount(status));
521
522 git_status_list_free(status);
523 git_index_free(index);
524 git_tree_free(tree);
525 git_repository_free(repo);
526 }
527
528 void test_status_submodules__mixed_case(void)
529 {
530 git_status_list *status;
531 git_status_options status_opts = GIT_STATUS_OPTIONS_INIT;
532 const git_status_entry *s;
533 size_t i;
534
535 status_opts.flags =
536 GIT_STATUS_OPT_INCLUDE_UNTRACKED |
537 GIT_STATUS_OPT_INCLUDE_IGNORED |
538 GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
539 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
540 GIT_STATUS_OPT_RECURSE_IGNORED_DIRS |
541 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
542 GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR |
543 GIT_STATUS_OPT_RENAMES_FROM_REWRITES |
544 GIT_STATUS_OPT_INCLUDE_UNREADABLE |
545 GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED;
546
547 g_repo = setup_fixture_submod3();
548
549 cl_git_pass(git_status_list_new(&status, g_repo, &status_opts));
550
551 for (i = 0; i < git_status_list_entrycount(status); i++) {
552 s = git_status_byindex(status, i);
553
554 if (s->head_to_index &&
555 strcmp(s->head_to_index->old_file.path, ".gitmodules") == 0)
556 continue;
557
558 cl_assert_equal_i(0, s->status);
559 }
560
561 git_status_list_free(status);
562 }
563