1# Create a submodule layout used for all tests below.
2#
3# The following use cases are covered:
4# - New submodule (no_submodule => add_sub1)
5# - Removed submodule (add_sub1 => remove_sub1)
6# - Updated submodule (add_sub1 => modify_sub1)
7# - Updated submodule recursively (add_nested_sub => modify_sub1_recursively)
8# - Submodule updated to invalid commit (add_sub1 => invalid_sub1)
9# - Submodule updated from invalid commit (invalid_sub1 => valid_sub1)
10# - Submodule replaced by tracked files in directory (add_sub1 =>
11# replace_sub1_with_directory)
12# - Directory containing tracked files replaced by submodule
13# (replace_sub1_with_directory => replace_directory_with_sub1)
14# - Submodule replaced by tracked file with the same name (add_sub1 =>
15# replace_sub1_with_file)
16# - Tracked file replaced by submodule (replace_sub1_with_file =>
17# replace_file_with_sub1)
18#
19# ----O
20# / ^
21# / remove_sub1
22# /
23# add_sub1 /-------O---------O--------O modify_sub1_recursively
24# | / ^ add_nested_sub
25# | / modify_sub1
26# v/
27# O------O-----------O---------O
28# ^ \ ^ replace_directory_with_sub1
29# | \ replace_sub1_with_directory
30# no_submodule \
31# --------O---------O
32# \ ^ replace_file_with_sub1
33# \ replace_sub1_with_file
34# \
35# ----O---------O
36# ^ valid_sub1
37# invalid_sub1
38#
39
40create_lib_submodule_repo () {
41 git init submodule_update_sub1 &&
42 (
43 cd submodule_update_sub1 &&
44 echo "expect" >>.gitignore &&
45 echo "actual" >>.gitignore &&
46 echo "x" >file1 &&
47 echo "y" >file2 &&
48 git add .gitignore file1 file2 &&
49 git commit -m "Base inside first submodule" &&
50 git branch "no_submodule"
51 ) &&
52 git init submodule_update_sub2 &&
53 (
54 cd submodule_update_sub2
55 echo "expect" >>.gitignore &&
56 echo "actual" >>.gitignore &&
57 echo "x" >file1 &&
58 echo "y" >file2 &&
59 git add .gitignore file1 file2 &&
60 git commit -m "nested submodule base" &&
61 git branch "no_submodule"
62 ) &&
63 git init submodule_update_repo &&
64 (
65 cd submodule_update_repo &&
66 echo "expect" >>.gitignore &&
67 echo "actual" >>.gitignore &&
68 echo "x" >file1 &&
69 echo "y" >file2 &&
70 git add .gitignore file1 file2 &&
71 git commit -m "Base" &&
72 git branch "no_submodule" &&
73
74 git checkout -b "add_sub1" &&
75 git submodule add ../submodule_update_sub1 sub1 &&
76 git config -f .gitmodules submodule.sub1.ignore all &&
77 git config submodule.sub1.ignore all &&
78 git add .gitmodules &&
79 git commit -m "Add sub1" &&
80
81 git checkout -b remove_sub1 add_sub1 &&
82 git revert HEAD &&
83
84 git checkout -b modify_sub1 add_sub1 &&
85 git submodule update &&
86 (
87 cd sub1 &&
88 git fetch &&
89 git checkout -b "modifications" &&
90 echo "z" >file2 &&
91 echo "x" >file3 &&
92 git add file2 file3 &&
93 git commit -m "modified file2 and added file3" &&
94 git push origin modifications
95 ) &&
96 git add sub1 &&
97 git commit -m "Modify sub1" &&
98
99 git checkout -b add_nested_sub modify_sub1 &&
100 git -C sub1 checkout -b "add_nested_sub" &&
101 git -C sub1 submodule add --branch no_submodule ../submodule_update_sub2 sub2 &&
102 git -C sub1 commit -a -m "add a nested submodule" &&
103 git add sub1 &&
104 git commit -a -m "update submodule, that updates a nested submodule" &&
105 git checkout -b modify_sub1_recursively &&
106 git -C sub1 checkout -b modify_sub1_recursively &&
107 git -C sub1/sub2 checkout -b modify_sub1_recursively &&
108 echo change >sub1/sub2/file3 &&
109 git -C sub1/sub2 add file3 &&
110 git -C sub1/sub2 commit -m "make a change in nested sub" &&
111 git -C sub1 add sub2 &&
112 git -C sub1 commit -m "update nested sub" &&
113 git add sub1 &&
114 git commit -m "update sub1, that updates nested sub" &&
115 git -C sub1 push origin modify_sub1_recursively &&
116 git -C sub1/sub2 push origin modify_sub1_recursively &&
117 git -C sub1 submodule deinit -f --all &&
118
119 git checkout -b replace_sub1_with_directory add_sub1 &&
120 git submodule update &&
121 git -C sub1 checkout modifications &&
122 git rm --cached sub1 &&
123 rm sub1/.git* &&
124 git config -f .gitmodules --remove-section "submodule.sub1" &&
125 git add .gitmodules sub1/* &&
126 git commit -m "Replace sub1 with directory" &&
127
128 git checkout -b replace_directory_with_sub1 &&
129 git revert HEAD &&
130
131 git checkout -b replace_sub1_with_file add_sub1 &&
132 git rm sub1 &&
133 echo "content" >sub1 &&
134 git add sub1 &&
135 git commit -m "Replace sub1 with file" &&
136
137 git checkout -b replace_file_with_sub1 &&
138 git revert HEAD &&
139
140 git checkout -b invalid_sub1 add_sub1 &&
141 git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 sub1 &&
142 git commit -m "Invalid sub1 commit" &&
143 git checkout -b valid_sub1 &&
144 git revert HEAD &&
145
146 git checkout master
147 )
148}
149
150# Helper function to replace gitfile with .git directory
151replace_gitfile_with_git_dir () {
152 (
153 cd "$1" &&
154 git_dir="$(git rev-parse --git-dir)" &&
155 rm -f .git &&
156 cp -R "$git_dir" .git &&
157 GIT_WORK_TREE=. git config --unset core.worktree
158 )
159}
160
161# Test that the .git directory in the submodule is unchanged (except for the
162# core.worktree setting, which appears only in $GIT_DIR/modules/$1/config).
163# Call this function before test_submodule_content as the latter might
164# write the index file leading to false positive index differences.
165#
166# Note that this only supports submodules at the root level of the
167# superproject, with the default name, i.e. same as its path.
168test_git_directory_is_unchanged () {
169 (
170 cd ".git/modules/$1" &&
171 # does core.worktree point at the right place?
172 test "$(git config core.worktree)" = "../../../$1" &&
173 # remove it temporarily before comparing, as
174 # "$1/.git/config" lacks it...
175 git config --unset core.worktree
176 ) &&
177 diff -r ".git/modules/$1" "$1/.git" &&
178 (
179 # ... and then restore.
180 cd ".git/modules/$1" &&
181 git config core.worktree "../../../$1"
182 )
183}
184
185test_git_directory_exists() {
186 test -e ".git/modules/$1" &&
187 if test -f sub1/.git
188 then
189 # does core.worktree point at the right place?
190 test "$(git -C .git/modules/$1 config core.worktree)" = "../../../$1"
191 fi
192}
193
194# Helper function to be executed at the start of every test below, it sets up
195# the submodule repo if it doesn't exist and configures the most problematic
196# settings for diff.ignoreSubmodules.
197prolog () {
198 (test -d submodule_update_repo || create_lib_submodule_repo) &&
199 test_config_global diff.ignoreSubmodules all &&
200 test_config diff.ignoreSubmodules all
201}
202
203# Helper function to bring work tree back into the state given by the
204# commit. This includes trying to populate sub1 accordingly if it exists and
205# should be updated to an existing commit.
206reset_work_tree_to () {
207 rm -rf submodule_update &&
208 git clone submodule_update_repo submodule_update &&
209 (
210 cd submodule_update &&
211 rm -rf sub1 &&
212 git checkout -f "$1" &&
213 git status -u -s >actual &&
214 test_must_be_empty actual &&
215 hash=$(git rev-parse --revs-only HEAD:sub1) &&
216 if test -n "$hash" &&
217 test $(cd "../submodule_update_sub1" && git rev-parse --verify "$hash^{commit}")
218 then
219 git submodule update --init --recursive "sub1"
220 fi
221 )
222}
223
224reset_work_tree_to_interested () {
225 reset_work_tree_to $1 &&
226 # make the submodule git dirs available
227 if ! test -d submodule_update/.git/modules/sub1
228 then
229 mkdir -p submodule_update/.git/modules &&
230 cp -r submodule_update_repo/.git/modules/sub1 submodule_update/.git/modules/sub1
231 GIT_WORK_TREE=. git -C submodule_update/.git/modules/sub1 config --unset core.worktree
232 fi &&
233 if ! test -d submodule_update/.git/modules/sub1/modules/sub2
234 then
235 mkdir -p submodule_update/.git/modules/sub1/modules &&
236 cp -r submodule_update_repo/.git/modules/sub1/modules/sub2 submodule_update/.git/modules/sub1/modules/sub2
237 GIT_WORK_TREE=. git -C submodule_update/.git/modules/sub1/modules/sub2 config --unset core.worktree
238 fi &&
239 # indicate we are interested in the submodule:
240 git -C submodule_update config submodule.sub1.url "bogus" &&
241 # sub1 might not be checked out, so use the git dir
242 git -C submodule_update/.git/modules/sub1 config submodule.sub2.url "bogus"
243}
244
245# Test that the superproject contains the content according to commit "$1"
246# (the work tree must match the index for everything but submodules but the
247# index must exactly match the given commit including any submodule SHA-1s).
248test_superproject_content () {
249 git diff-index --cached "$1" >actual &&
250 test_must_be_empty actual &&
251 git diff-files --ignore-submodules >actual &&
252 test_must_be_empty actual
253}
254
255# Test that the given submodule at path "$1" contains the content according
256# to the submodule commit recorded in the superproject's commit "$2"
257test_submodule_content () {
258 if test x"$1" = "x-C"
259 then
260 cd "$2"
261 shift; shift;
262 fi
263 if test $# != 2
264 then
265 echo "test_submodule_content needs two arguments"
266 return 1
267 fi &&
268 submodule="$1" &&
269 commit="$2" &&
270 test -d "$submodule"/ &&
271 if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
272 then
273 echo "Submodule $submodule is not populated"
274 return 1
275 fi &&
276 sha1=$(git rev-parse --verify "$commit:$submodule") &&
277 if test -z "$sha1"
278 then
279 echo "Couldn't retrieve SHA-1 of $submodule for $commit"
280 return 1
281 fi &&
282 (
283 cd "$submodule" &&
284 git status -u -s >actual &&
285 test_must_be_empty actual &&
286 git diff "$sha1" >actual &&
287 test_must_be_empty actual
288 )
289}
290
291# Test that the following transitions are correctly handled:
292# - Updated submodule
293# - New submodule
294# - Removed submodule
295# - Directory containing tracked files replaced by submodule
296# - Submodule replaced by tracked files in directory
297# - Submodule replaced by tracked file with the same name
298# - tracked file replaced by submodule
299#
300# The default is that submodule contents aren't changed until "git submodule
301# update" is run. And even then that command doesn't delete the work tree of
302# a removed submodule.
303#
304# Removing a submodule containing a .git directory must fail even when forced
305# to protect the history!
306#
307
308# Test that submodule contents are currently not updated when switching
309# between commits that change a submodule.
310test_submodule_switch () {
311 command="$1"
312 ######################### Appearing submodule #########################
313 # Switching to a commit letting a submodule appear creates empty dir ...
314 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
315 then
316 # Restoring stash fails to restore submodule index entry
317 RESULT="failure"
318 else
319 RESULT="success"
320 fi
321 test_expect_$RESULT "$command: added submodule creates empty directory" '
322 prolog &&
323 reset_work_tree_to no_submodule &&
324 (
325 cd submodule_update &&
326 git branch -t add_sub1 origin/add_sub1 &&
327 $command add_sub1 &&
328 test_superproject_content origin/add_sub1 &&
329 test_dir_is_empty sub1 &&
330 git submodule update --init --recursive &&
331 test_submodule_content sub1 origin/add_sub1
332 )
333 '
334 # ... and doesn't care if it already exists ...
335 test_expect_$RESULT "$command: added submodule leaves existing empty directory alone" '
336 prolog &&
337 reset_work_tree_to no_submodule &&
338 (
339 cd submodule_update &&
340 mkdir sub1 &&
341 git branch -t add_sub1 origin/add_sub1 &&
342 $command add_sub1 &&
343 test_superproject_content origin/add_sub1 &&
344 test_dir_is_empty sub1 &&
345 git submodule update --init --recursive &&
346 test_submodule_content sub1 origin/add_sub1
347 )
348 '
349 # ... unless there is an untracked file in its place.
350 test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
351 prolog &&
352 reset_work_tree_to no_submodule &&
353 (
354 cd submodule_update &&
355 git branch -t add_sub1 origin/add_sub1 &&
356 >sub1 &&
357 test_must_fail $command add_sub1 &&
358 test_superproject_content origin/no_submodule &&
359 test_must_be_empty sub1
360 )
361 '
362 # Replacing a tracked file with a submodule produces an empty
363 # directory ...
364 test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
365 prolog &&
366 reset_work_tree_to replace_sub1_with_file &&
367 (
368 cd submodule_update &&
369 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
370 $command replace_file_with_sub1 &&
371 test_superproject_content origin/replace_file_with_sub1 &&
372 test_dir_is_empty sub1 &&
373 git submodule update --init --recursive &&
374 test_submodule_content sub1 origin/replace_file_with_sub1
375 )
376 '
377 # ... as does removing a directory with tracked files with a
378 # submodule.
379 if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
380 then
381 # Non fast-forward merges fail with "Directory sub1 doesn't
382 # exist. sub1" because the empty submodule directory is not
383 # created
384 RESULT="failure"
385 else
386 RESULT="success"
387 fi
388 test_expect_$RESULT "$command: replace directory with submodule" '
389 prolog &&
390 reset_work_tree_to replace_sub1_with_directory &&
391 (
392 cd submodule_update &&
393 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
394 $command replace_directory_with_sub1 &&
395 test_superproject_content origin/replace_directory_with_sub1 &&
396 test_dir_is_empty sub1 &&
397 git submodule update --init --recursive &&
398 test_submodule_content sub1 origin/replace_directory_with_sub1
399 )
400 '
401
402 ######################## Disappearing submodule #######################
403 # Removing a submodule doesn't remove its work tree ...
404 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
405 then
406 RESULT="failure"
407 else
408 RESULT="success"
409 fi
410 test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
411 prolog &&
412 reset_work_tree_to add_sub1 &&
413 (
414 cd submodule_update &&
415 git branch -t remove_sub1 origin/remove_sub1 &&
416 $command remove_sub1 &&
417 test_superproject_content origin/remove_sub1 &&
418 test_submodule_content sub1 origin/add_sub1
419 )
420 '
421 # ... especially when it contains a .git directory.
422 test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
423 prolog &&
424 reset_work_tree_to add_sub1 &&
425 (
426 cd submodule_update &&
427 git branch -t remove_sub1 origin/remove_sub1 &&
428 replace_gitfile_with_git_dir sub1 &&
429 $command remove_sub1 &&
430 test_superproject_content origin/remove_sub1 &&
431 test_git_directory_is_unchanged sub1 &&
432 test_submodule_content sub1 origin/add_sub1
433 )
434 '
435 # Replacing a submodule with files in a directory must fail as the
436 # submodule work tree isn't removed ...
437 if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
438 then
439 # Non fast-forward merges attempt to merge the former
440 # submodule files with the newly checked out ones in the
441 # directory of the same name while it shouldn't.
442 RESULT="failure"
443 else
444 RESULT="success"
445 fi
446 test_expect_$RESULT "$command: replace submodule with a directory must fail" '
447 prolog &&
448 reset_work_tree_to add_sub1 &&
449 (
450 cd submodule_update &&
451 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
452 test_must_fail $command replace_sub1_with_directory &&
453 test_superproject_content origin/add_sub1 &&
454 test_submodule_content sub1 origin/add_sub1
455 )
456 '
457 # ... especially when it contains a .git directory.
458 test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
459 prolog &&
460 reset_work_tree_to add_sub1 &&
461 (
462 cd submodule_update &&
463 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
464 replace_gitfile_with_git_dir sub1 &&
465 test_must_fail $command replace_sub1_with_directory &&
466 test_superproject_content origin/add_sub1 &&
467 test_git_directory_is_unchanged sub1 &&
468 test_submodule_content sub1 origin/add_sub1
469 )
470 '
471 # Replacing it with a file must fail as it could throw away any local
472 # work tree changes ...
473 test_expect_failure "$command: replace submodule with a file must fail" '
474 prolog &&
475 reset_work_tree_to add_sub1 &&
476 (
477 cd submodule_update &&
478 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
479 test_must_fail $command replace_sub1_with_file &&
480 test_superproject_content origin/add_sub1 &&
481 test_submodule_content sub1 origin/add_sub1
482 )
483 '
484 # ... or even destroy unpushed parts of submodule history if that
485 # still uses a .git directory.
486 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
487 prolog &&
488 reset_work_tree_to add_sub1 &&
489 (
490 cd submodule_update &&
491 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
492 replace_gitfile_with_git_dir sub1 &&
493 test_must_fail $command replace_sub1_with_file &&
494 test_superproject_content origin/add_sub1 &&
495 test_git_directory_is_unchanged sub1 &&
496 test_submodule_content sub1 origin/add_sub1
497 )
498 '
499
500 ########################## Modified submodule #########################
501 # Updating a submodule sha1 doesn't update the submodule's work tree
502 if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
503 then
504 # When cherry picking a SHA-1 update for an ignored submodule
505 # the commit incorrectly fails with "The previous cherry-pick
506 # is now empty, possibly due to conflict resolution."
507 RESULT="failure"
508 else
509 RESULT="success"
510 fi
511 test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
512 prolog &&
513 reset_work_tree_to add_sub1 &&
514 (
515 cd submodule_update &&
516 git branch -t modify_sub1 origin/modify_sub1 &&
517 $command modify_sub1 &&
518 test_superproject_content origin/modify_sub1 &&
519 test_submodule_content sub1 origin/add_sub1 &&
520 git submodule update &&
521 test_submodule_content sub1 origin/modify_sub1
522 )
523 '
524
525 # Updating a submodule to an invalid sha1 doesn't update the
526 # submodule's work tree, subsequent update will fail
527 test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
528 prolog &&
529 reset_work_tree_to add_sub1 &&
530 (
531 cd submodule_update &&
532 git branch -t invalid_sub1 origin/invalid_sub1 &&
533 $command invalid_sub1 &&
534 test_superproject_content origin/invalid_sub1 &&
535 test_submodule_content sub1 origin/add_sub1 &&
536 test_must_fail git submodule update &&
537 test_submodule_content sub1 origin/add_sub1
538 )
539 '
540 # Updating a submodule from an invalid sha1 doesn't update the
541 # submodule's work tree, subsequent update will succeed
542 test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
543 prolog &&
544 reset_work_tree_to invalid_sub1 &&
545 (
546 cd submodule_update &&
547 git branch -t valid_sub1 origin/valid_sub1 &&
548 $command valid_sub1 &&
549 test_superproject_content origin/valid_sub1 &&
550 test_dir_is_empty sub1 &&
551 git submodule update --init --recursive &&
552 test_submodule_content sub1 origin/valid_sub1
553 )
554 '
555}
556
557# Test that submodule contents are currently not updated when switching
558# between commits that change a submodule, but throwing away local changes in
559# the superproject is allowed.
560test_submodule_forced_switch () {
561 command="$1"
562 ######################### Appearing submodule #########################
563 # Switching to a commit letting a submodule appear creates empty dir ...
564 test_expect_success "$command: added submodule creates empty directory" '
565 prolog &&
566 reset_work_tree_to no_submodule &&
567 (
568 cd submodule_update &&
569 git branch -t add_sub1 origin/add_sub1 &&
570 $command add_sub1 &&
571 test_superproject_content origin/add_sub1 &&
572 test_dir_is_empty sub1 &&
573 git submodule update --init --recursive &&
574 test_submodule_content sub1 origin/add_sub1
575 )
576 '
577 # ... and doesn't care if it already exists ...
578 test_expect_success "$command: added submodule leaves existing empty directory alone" '
579 prolog &&
580 reset_work_tree_to no_submodule &&
581 (
582 cd submodule_update &&
583 git branch -t add_sub1 origin/add_sub1 &&
584 mkdir sub1 &&
585 $command add_sub1 &&
586 test_superproject_content origin/add_sub1 &&
587 test_dir_is_empty sub1 &&
588 git submodule update --init --recursive &&
589 test_submodule_content sub1 origin/add_sub1
590 )
591 '
592 # ... unless there is an untracked file in its place.
593 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
594 prolog &&
595 reset_work_tree_to no_submodule &&
596 (
597 cd submodule_update &&
598 git branch -t add_sub1 origin/add_sub1 &&
599 >sub1 &&
600 $command add_sub1 &&
601 test_superproject_content origin/add_sub1 &&
602 test_dir_is_empty sub1
603 )
604 '
605 # Replacing a tracked file with a submodule produces an empty
606 # directory ...
607 test_expect_success "$command: replace tracked file with submodule creates empty directory" '
608 prolog &&
609 reset_work_tree_to replace_sub1_with_file &&
610 (
611 cd submodule_update &&
612 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
613 $command replace_file_with_sub1 &&
614 test_superproject_content origin/replace_file_with_sub1 &&
615 test_dir_is_empty sub1 &&
616 git submodule update --init --recursive &&
617 test_submodule_content sub1 origin/replace_file_with_sub1
618 )
619 '
620 # ... as does removing a directory with tracked files with a
621 # submodule.
622 test_expect_success "$command: replace directory with submodule" '
623 prolog &&
624 reset_work_tree_to replace_sub1_with_directory &&
625 (
626 cd submodule_update &&
627 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
628 $command replace_directory_with_sub1 &&
629 test_superproject_content origin/replace_directory_with_sub1 &&
630 test_dir_is_empty sub1 &&
631 git submodule update --init --recursive &&
632 test_submodule_content sub1 origin/replace_directory_with_sub1
633 )
634 '
635
636 ######################## Disappearing submodule #######################
637 # Removing a submodule doesn't remove its work tree ...
638 test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
639 prolog &&
640 reset_work_tree_to add_sub1 &&
641 (
642 cd submodule_update &&
643 git branch -t remove_sub1 origin/remove_sub1 &&
644 $command remove_sub1 &&
645 test_superproject_content origin/remove_sub1 &&
646 test_submodule_content sub1 origin/add_sub1
647 )
648 '
649 # ... especially when it contains a .git directory.
650 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
651 prolog &&
652 reset_work_tree_to add_sub1 &&
653 (
654 cd submodule_update &&
655 git branch -t remove_sub1 origin/remove_sub1 &&
656 replace_gitfile_with_git_dir sub1 &&
657 $command remove_sub1 &&
658 test_superproject_content origin/remove_sub1 &&
659 test_git_directory_is_unchanged sub1 &&
660 test_submodule_content sub1 origin/add_sub1
661 )
662 '
663 # Replacing a submodule with files in a directory must fail as the
664 # submodule work tree isn't removed ...
665 test_expect_failure "$command: replace submodule with a directory must fail" '
666 prolog &&
667 reset_work_tree_to add_sub1 &&
668 (
669 cd submodule_update &&
670 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
671 test_must_fail $command replace_sub1_with_directory &&
672 test_superproject_content origin/add_sub1 &&
673 test_submodule_content sub1 origin/add_sub1
674 )
675 '
676 # ... especially when it contains a .git directory.
677 test_expect_failure "$command: replace submodule containing a .git directory with a directory must fail" '
678 prolog &&
679 reset_work_tree_to add_sub1 &&
680 (
681 cd submodule_update &&
682 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
683 replace_gitfile_with_git_dir sub1 &&
684 test_must_fail $command replace_sub1_with_directory &&
685 test_superproject_content origin/add_sub1 &&
686 test_git_directory_is_unchanged sub1 &&
687 test_submodule_content sub1 origin/add_sub1
688 )
689 '
690 # Replacing it with a file must fail as it could throw away any local
691 # work tree changes ...
692 test_expect_failure "$command: replace submodule with a file must fail" '
693 prolog &&
694 reset_work_tree_to add_sub1 &&
695 (
696 cd submodule_update &&
697 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
698 test_must_fail $command replace_sub1_with_file &&
699 test_superproject_content origin/add_sub1 &&
700 test_submodule_content sub1 origin/add_sub1
701 )
702 '
703 # ... or even destroy unpushed parts of submodule history if that
704 # still uses a .git directory.
705 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
706 prolog &&
707 reset_work_tree_to add_sub1 &&
708 (
709 cd submodule_update &&
710 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
711 replace_gitfile_with_git_dir sub1 &&
712 test_must_fail $command replace_sub1_with_file &&
713 test_superproject_content origin/add_sub1 &&
714 test_git_directory_is_unchanged sub1 &&
715 test_submodule_content sub1 origin/add_sub1
716 )
717 '
718
719 ########################## Modified submodule #########################
720 # Updating a submodule sha1 doesn't update the submodule's work tree
721 test_expect_success "$command: modified submodule does not update submodule work tree" '
722 prolog &&
723 reset_work_tree_to add_sub1 &&
724 (
725 cd submodule_update &&
726 git branch -t modify_sub1 origin/modify_sub1 &&
727 $command modify_sub1 &&
728 test_superproject_content origin/modify_sub1 &&
729 test_submodule_content sub1 origin/add_sub1 &&
730 git submodule update &&
731 test_submodule_content sub1 origin/modify_sub1
732 )
733 '
734 # Updating a submodule to an invalid sha1 doesn't update the
735 # submodule's work tree, subsequent update will fail
736 test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
737 prolog &&
738 reset_work_tree_to add_sub1 &&
739 (
740 cd submodule_update &&
741 git branch -t invalid_sub1 origin/invalid_sub1 &&
742 $command invalid_sub1 &&
743 test_superproject_content origin/invalid_sub1 &&
744 test_submodule_content sub1 origin/add_sub1 &&
745 test_must_fail git submodule update &&
746 test_submodule_content sub1 origin/add_sub1
747 )
748 '
749 # Updating a submodule from an invalid sha1 doesn't update the
750 # submodule's work tree, subsequent update will succeed
751 test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
752 prolog &&
753 reset_work_tree_to invalid_sub1 &&
754 (
755 cd submodule_update &&
756 git branch -t valid_sub1 origin/valid_sub1 &&
757 $command valid_sub1 &&
758 test_superproject_content origin/valid_sub1 &&
759 test_dir_is_empty sub1 &&
760 git submodule update --init --recursive &&
761 test_submodule_content sub1 origin/valid_sub1
762 )
763 '
764}
765
766# Test that submodule contents are correctly updated when switching
767# between commits that change a submodule.
768# Test that the following transitions are correctly handled:
769# (These tests are also above in the case where we expect no change
770# in the submodule)
771# - Updated submodule
772# - New submodule
773# - Removed submodule
774# - Directory containing tracked files replaced by submodule
775# - Submodule replaced by tracked files in directory
776# - Submodule replaced by tracked file with the same name
777# - tracked file replaced by submodule
778#
779# New test cases
780# - Removing a submodule with a git directory absorbs the submodules
781# git directory first into the superproject.
782
783test_submodule_switch_recursing () {
784 command="$1"
785 RESULTDS=success
786 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
787 then
788 RESULTDS=failure
789 fi
790 RESULTR=success
791 if test "$KNOWN_FAILURE_SUBMODULE_RECURSIVE_NESTED" = 1
792 then
793 RESULTR=failure
794 fi
795 RESULTOI=success
796 if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
797 then
798 RESULTOI=failure
799 fi
800 ######################### Appearing submodule #########################
801 # Switching to a commit letting a submodule appear checks it out ...
802 test_expect_success "$command: added submodule is checked out" '
803 prolog &&
804 reset_work_tree_to_interested no_submodule &&
805 (
806 cd submodule_update &&
807 git branch -t add_sub1 origin/add_sub1 &&
808 $command add_sub1 &&
809 test_superproject_content origin/add_sub1 &&
810 test_submodule_content sub1 origin/add_sub1
811 )
812 '
813 # ... ignoring an empty existing directory ...
814 test_expect_success "$command: added submodule is checked out in empty dir" '
815 prolog &&
816 reset_work_tree_to_interested no_submodule &&
817 (
818 cd submodule_update &&
819 mkdir sub1 &&
820 git branch -t add_sub1 origin/add_sub1 &&
821 $command add_sub1 &&
822 test_superproject_content origin/add_sub1 &&
823 test_submodule_content sub1 origin/add_sub1
824 )
825 '
826 # ... unless there is an untracked file in its place.
827 test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
828 prolog &&
829 reset_work_tree_to_interested no_submodule &&
830 (
831 cd submodule_update &&
832 git branch -t add_sub1 origin/add_sub1 &&
833 : >sub1 &&
834 test_must_fail $command add_sub1 &&
835 test_superproject_content origin/no_submodule &&
836 test_must_be_empty sub1
837 )
838 '
839 # ... but an ignored file is fine.
840 test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
841 test_when_finished "rm submodule_update/.git/info/exclude" &&
842 prolog &&
843 reset_work_tree_to_interested no_submodule &&
844 (
845 cd submodule_update &&
846 git branch -t add_sub1 origin/add_sub1 &&
847 : >sub1 &&
848 echo sub1 >.git/info/exclude
849 $command add_sub1 &&
850 test_superproject_content origin/add_sub1 &&
851 test_submodule_content sub1 origin/add_sub1
852 )
853 '
854 # Replacing a tracked file with a submodule produces a checked out submodule
855 test_expect_success "$command: replace tracked file with submodule checks out submodule" '
856 prolog &&
857 reset_work_tree_to_interested replace_sub1_with_file &&
858 (
859 cd submodule_update &&
860 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
861 $command replace_file_with_sub1 &&
862 test_superproject_content origin/replace_file_with_sub1 &&
863 test_submodule_content sub1 origin/replace_file_with_sub1
864 )
865 '
866 # ... as does removing a directory with tracked files with a submodule.
867 test_expect_success "$command: replace directory with submodule" '
868 prolog &&
869 reset_work_tree_to_interested replace_sub1_with_directory &&
870 (
871 cd submodule_update &&
872 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
873 $command replace_directory_with_sub1 &&
874 test_superproject_content origin/replace_directory_with_sub1 &&
875 test_submodule_content sub1 origin/replace_directory_with_sub1
876 )
877 '
878
879 ######################## Disappearing submodule #######################
880 # Removing a submodule removes its work tree ...
881 test_expect_success "$command: removed submodule removes submodules working tree" '
882 prolog &&
883 reset_work_tree_to_interested add_sub1 &&
884 (
885 cd submodule_update &&
886 git branch -t remove_sub1 origin/remove_sub1 &&
887 $command remove_sub1 &&
888 test_superproject_content origin/remove_sub1 &&
889 ! test -e sub1
890 )
891 '
892 # ... absorbing a .git directory along the way.
893 test_expect_success "$command: removed submodule absorbs submodules .git directory" '
894 prolog &&
895 reset_work_tree_to_interested add_sub1 &&
896 (
897 cd submodule_update &&
898 git branch -t remove_sub1 origin/remove_sub1 &&
899 replace_gitfile_with_git_dir sub1 &&
900 rm -rf .git/modules &&
901 $command remove_sub1 &&
902 test_superproject_content origin/remove_sub1 &&
903 ! test -e sub1 &&
904 test_git_directory_exists sub1
905 )
906 '
907 # Replacing a submodule with files in a directory must succeeds
908 # when the submodule is clean
909 test_expect_$RESULTDS "$command: replace submodule with a directory" '
910 prolog &&
911 reset_work_tree_to_interested add_sub1 &&
912 (
913 cd submodule_update &&
914 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
915 $command replace_sub1_with_directory &&
916 test_superproject_content origin/replace_sub1_with_directory &&
917 test_submodule_content sub1 origin/replace_sub1_with_directory
918 )
919 '
920 # ... absorbing a .git directory.
921 test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
922 prolog &&
923 reset_work_tree_to_interested add_sub1 &&
924 (
925 cd submodule_update &&
926 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
927 replace_gitfile_with_git_dir sub1 &&
928 rm -rf .git/modules &&
929 $command replace_sub1_with_directory &&
930 test_superproject_content origin/replace_sub1_with_directory &&
931 test_git_directory_exists sub1
932 )
933 '
934
935 # Replacing it with a file ...
936 test_expect_success "$command: replace submodule with a file" '
937 prolog &&
938 reset_work_tree_to_interested add_sub1 &&
939 (
940 cd submodule_update &&
941 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
942 $command replace_sub1_with_file &&
943 test_superproject_content origin/replace_sub1_with_file &&
944 test -f sub1
945 )
946 '
947
948 # ... must check its local work tree for untracked files
949 test_expect_$RESULTDS "$command: replace submodule with a file must fail with untracked files" '
950 prolog &&
951 reset_work_tree_to_interested add_sub1 &&
952 (
953 cd submodule_update &&
954 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
955 : >sub1/untrackedfile &&
956 test_must_fail $command replace_sub1_with_file &&
957 test_superproject_content origin/add_sub1 &&
958 test_submodule_content sub1 origin/add_sub1
959 )
960 '
961
962 # ... and ignored files are ignored
963 test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
964 test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
965 prolog &&
966 reset_work_tree_to_interested add_sub1 &&
967 (
968 cd submodule_update &&
969 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
970 : >sub1/ignored &&
971 $command replace_sub1_with_file &&
972 test_superproject_content origin/replace_sub1_with_file &&
973 test -f sub1
974 )
975 '
976
977 ########################## Modified submodule #########################
978 # Updating a submodule sha1 updates the submodule's work tree
979 test_expect_success "$command: modified submodule updates submodule work tree" '
980 prolog &&
981 reset_work_tree_to_interested add_sub1 &&
982 (
983 cd submodule_update &&
984 git branch -t modify_sub1 origin/modify_sub1 &&
985 $command modify_sub1 &&
986 test_superproject_content origin/modify_sub1 &&
987 test_submodule_content sub1 origin/modify_sub1
988 )
989 '
990
991 # Updating a submodule to an invalid sha1 doesn't update the
992 # superproject nor the submodule's work tree.
993 test_expect_success "$command: updating to a missing submodule commit fails" '
994 prolog &&
995 reset_work_tree_to_interested add_sub1 &&
996 (
997 cd submodule_update &&
998 git branch -t invalid_sub1 origin/invalid_sub1 &&
999 test_must_fail $command invalid_sub1 &&
1000 test_superproject_content origin/add_sub1 &&
1001 test_submodule_content sub1 origin/add_sub1
1002 )
1003 '
1004
1005 # recursing deeper than one level doesn't work yet.
1006 test_expect_$RESULTR "$command: modified submodule updates submodule recursively" '
1007 prolog &&
1008 reset_work_tree_to_interested add_nested_sub &&
1009 (
1010 cd submodule_update &&
1011 git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
1012 $command modify_sub1_recursively &&
1013 test_superproject_content origin/modify_sub1_recursively &&
1014 test_submodule_content sub1 origin/modify_sub1_recursively &&
1015 test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
1016 )
1017 '
1018}
1019
1020# Test that submodule contents are updated when switching between commits
1021# that change a submodule, but throwing away local changes in
1022# the superproject as well as the submodule is allowed.
1023test_submodule_forced_switch_recursing () {
1024 command="$1"
1025 RESULT=success
1026 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
1027 then
1028 RESULT=failure
1029 fi
1030 ######################### Appearing submodule #########################
1031 # Switching to a commit letting a submodule appear creates empty dir ...
1032 test_expect_success "$command: added submodule is checked out" '
1033 prolog &&
1034 reset_work_tree_to_interested no_submodule &&
1035 (
1036 cd submodule_update &&
1037 git branch -t add_sub1 origin/add_sub1 &&
1038 $command add_sub1 &&
1039 test_superproject_content origin/add_sub1 &&
1040 test_submodule_content sub1 origin/add_sub1
1041 )
1042 '
1043 # ... and doesn't care if it already exists ...
1044 test_expect_success "$command: added submodule ignores empty directory" '
1045 prolog &&
1046 reset_work_tree_to_interested no_submodule &&
1047 (
1048 cd submodule_update &&
1049 git branch -t add_sub1 origin/add_sub1 &&
1050 mkdir sub1 &&
1051 $command add_sub1 &&
1052 test_superproject_content origin/add_sub1 &&
1053 test_submodule_content sub1 origin/add_sub1
1054 )
1055 '
1056 # ... not caring about an untracked file either
1057 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
1058 prolog &&
1059 reset_work_tree_to_interested no_submodule &&
1060 (
1061 cd submodule_update &&
1062 git branch -t add_sub1 origin/add_sub1 &&
1063 >sub1 &&
1064 $command add_sub1 &&
1065 test_superproject_content origin/add_sub1 &&
1066 test_submodule_content sub1 origin/add_sub1
1067 )
1068 '
1069 # Replacing a tracked file with a submodule checks out the submodule
1070 test_expect_success "$command: replace tracked file with submodule populates the submodule" '
1071 prolog &&
1072 reset_work_tree_to_interested replace_sub1_with_file &&
1073 (
1074 cd submodule_update &&
1075 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
1076 $command replace_file_with_sub1 &&
1077 test_superproject_content origin/replace_file_with_sub1 &&
1078 test_submodule_content sub1 origin/replace_file_with_sub1
1079 )
1080 '
1081 # ... as does removing a directory with tracked files with a
1082 # submodule.
1083 test_expect_success "$command: replace directory with submodule" '
1084 prolog &&
1085 reset_work_tree_to_interested replace_sub1_with_directory &&
1086 (
1087 cd submodule_update &&
1088 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
1089 $command replace_directory_with_sub1 &&
1090 test_superproject_content origin/replace_directory_with_sub1 &&
1091 test_submodule_content sub1 origin/replace_directory_with_sub1
1092 )
1093 '
1094
1095 ######################## Disappearing submodule #######################
1096 # Removing a submodule doesn't remove its work tree ...
1097 test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
1098 prolog &&
1099 reset_work_tree_to_interested add_sub1 &&
1100 (
1101 cd submodule_update &&
1102 git branch -t remove_sub1 origin/remove_sub1 &&
1103 $command remove_sub1 &&
1104 test_superproject_content origin/remove_sub1 &&
1105 ! test -e sub1
1106 )
1107 '
1108 # ... especially when it contains a .git directory.
1109 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
1110 prolog &&
1111 reset_work_tree_to_interested add_sub1 &&
1112 (
1113 cd submodule_update &&
1114 git branch -t remove_sub1 origin/remove_sub1 &&
1115 replace_gitfile_with_git_dir sub1 &&
1116 rm -rf .git/modules/sub1 &&
1117 $command remove_sub1 &&
1118 test_superproject_content origin/remove_sub1 &&
1119 test_git_directory_exists sub1 &&
1120 ! test -e sub1
1121 )
1122 '
1123 # Replacing a submodule with files in a directory ...
1124 test_expect_success "$command: replace submodule with a directory" '
1125 prolog &&
1126 reset_work_tree_to_interested add_sub1 &&
1127 (
1128 cd submodule_update &&
1129 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1130 $command replace_sub1_with_directory &&
1131 test_superproject_content origin/replace_sub1_with_directory
1132 )
1133 '
1134 # ... absorbing a .git directory.
1135 test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
1136 prolog &&
1137 reset_work_tree_to_interested add_sub1 &&
1138 (
1139 cd submodule_update &&
1140 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1141 replace_gitfile_with_git_dir sub1 &&
1142 rm -rf .git/modules/sub1 &&
1143 $command replace_sub1_with_directory &&
1144 test_superproject_content origin/replace_sub1_with_directory &&
1145 test_submodule_content sub1 origin/modify_sub1
1146 test_git_directory_exists sub1
1147 )
1148 '
1149 # Replacing it with a file
1150 test_expect_success "$command: replace submodule with a file" '
1151 prolog &&
1152 reset_work_tree_to_interested add_sub1 &&
1153 (
1154 cd submodule_update &&
1155 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1156 $command replace_sub1_with_file &&
1157 test_superproject_content origin/replace_sub1_with_file
1158 )
1159 '
1160
1161 # ... even if the submodule contains ignored files
1162 test_expect_success "$command: replace submodule with a file ignoring ignored files" '
1163 prolog &&
1164 reset_work_tree_to_interested add_sub1 &&
1165 (
1166 cd submodule_update &&
1167 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1168 : >sub1/expect &&
1169 $command replace_sub1_with_file &&
1170 test_superproject_content origin/replace_sub1_with_file
1171 )
1172 '
1173
1174 # ... but stops for untracked files that would be lost
1175 test_expect_$RESULT "$command: replace submodule with a file stops for untracked files" '
1176 prolog &&
1177 reset_work_tree_to_interested add_sub1 &&
1178 (
1179 cd submodule_update &&
1180 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1181 : >sub1/untracked_file &&
1182 test_must_fail $command replace_sub1_with_file &&
1183 test_superproject_content origin/add_sub1 &&
1184 test -f sub1/untracked_file
1185 )
1186 '
1187
1188 ########################## Modified submodule #########################
1189 # Updating a submodule sha1 updates the submodule's work tree
1190 test_expect_success "$command: modified submodule updates submodule work tree" '
1191 prolog &&
1192 reset_work_tree_to_interested add_sub1 &&
1193 (
1194 cd submodule_update &&
1195 git branch -t modify_sub1 origin/modify_sub1 &&
1196 $command modify_sub1 &&
1197 test_superproject_content origin/modify_sub1 &&
1198 test_submodule_content sub1 origin/modify_sub1
1199 )
1200 '
1201 # Updating a submodule to an invalid sha1 doesn't update the
1202 # submodule's work tree, subsequent update will fail
1203 test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
1204 prolog &&
1205 reset_work_tree_to_interested add_sub1 &&
1206 (
1207 cd submodule_update &&
1208 git branch -t invalid_sub1 origin/invalid_sub1 &&
1209 test_must_fail $command invalid_sub1 &&
1210 test_superproject_content origin/add_sub1 &&
1211 test_submodule_content sub1 origin/add_sub1
1212 )
1213 '
1214 # Updating a submodule from an invalid sha1 updates
1215 test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
1216 prolog &&
1217 reset_work_tree_to_interested invalid_sub1 &&
1218 (
1219 cd submodule_update &&
1220 git branch -t valid_sub1 origin/valid_sub1 &&
1221 test_must_fail $command valid_sub1 &&
1222 test_superproject_content origin/invalid_sub1
1223 )
1224 '
1225}