1 # Create a submodule layout used for all tests below.
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)
23 # add_sub1 /-------O---------O--------O modify_sub1_recursively
24 # | / ^ add_nested_sub
27 # O------O-----------O---------O
28 # ^ \ ^ replace_directory_with_sub1
29 # | \ replace_sub1_with_directory
32 # \ ^ replace_file_with_sub1
33 # \ replace_sub1_with_file
40 create_lib_submodule_repo () {
41 git init submodule_update_sub1 &&
43 cd submodule_update_sub1 &&
44 echo "expect" >>.gitignore &&
45 echo "actual" >>.gitignore &&
48 git add .gitignore file1 file2 &&
49 git commit -m "Base inside first submodule" &&
50 git branch "no_submodule"
52 git init submodule_update_sub2 &&
54 cd submodule_update_sub2
55 echo "expect" >>.gitignore &&
56 echo "actual" >>.gitignore &&
59 git add .gitignore file1 file2 &&
60 git commit -m "nested submodule base" &&
61 git branch "no_submodule"
63 git init submodule_update_repo &&
65 cd submodule_update_repo &&
66 echo "expect" >>.gitignore &&
67 echo "actual" >>.gitignore &&
70 git add .gitignore file1 file2 &&
71 git commit -m "Base" &&
72 git branch "no_submodule" &&
74 git checkout -b "add_sub1" &&
75 git submodule add ../submodule_update_sub1 sub1 &&
76 git submodule add ../submodule_update_sub1 uninitialized_sub &&
77 git config -f .gitmodules submodule.sub1.ignore all &&
78 git config submodule.sub1.ignore all &&
79 git add .gitmodules &&
80 git commit -m "Add sub1" &&
82 git checkout -b remove_sub1 add_sub1 &&
85 git checkout -b modify_sub1 add_sub1 &&
86 git submodule update &&
90 git checkout -b "modifications" &&
93 git add file2 file3 &&
94 git commit -m "modified file2 and added file3" &&
95 git push origin modifications
98 git commit -m "Modify sub1" &&
100 git checkout -b add_nested_sub modify_sub1 &&
101 git -C sub1 checkout -b "add_nested_sub" &&
102 git -C sub1 submodule add --branch no_submodule ../submodule_update_sub2 sub2 &&
103 git -C sub1 commit -a -m "add a nested submodule" &&
105 git commit -a -m "update submodule, that updates a nested submodule" &&
106 git checkout -b modify_sub1_recursively &&
107 git -C sub1 checkout -b modify_sub1_recursively &&
108 git -C sub1/sub2 checkout -b modify_sub1_recursively &&
109 echo change >sub1/sub2/file3 &&
110 git -C sub1/sub2 add file3 &&
111 git -C sub1/sub2 commit -m "make a change in nested sub" &&
112 git -C sub1 add sub2 &&
113 git -C sub1 commit -m "update nested sub" &&
115 git commit -m "update sub1, that updates nested sub" &&
116 git -C sub1 push origin modify_sub1_recursively &&
117 git -C sub1/sub2 push origin modify_sub1_recursively &&
118 git -C sub1 submodule deinit -f --all &&
120 git checkout -b replace_sub1_with_directory add_sub1 &&
121 git submodule update &&
122 git -C sub1 checkout modifications &&
123 git rm --cached sub1 &&
125 git config -f .gitmodules --remove-section "submodule.sub1" &&
126 git add .gitmodules sub1/* &&
127 git commit -m "Replace sub1 with directory" &&
129 git checkout -b replace_directory_with_sub1 &&
132 git checkout -b replace_sub1_with_file add_sub1 &&
134 echo "content" >sub1 &&
136 git commit -m "Replace sub1 with file" &&
138 git checkout -b replace_file_with_sub1 &&
141 git checkout -b invalid_sub1 add_sub1 &&
142 git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 sub1 &&
143 git commit -m "Invalid sub1 commit" &&
144 git checkout -b valid_sub1 &&
151 # Helper function to replace gitfile with .git directory
152 replace_gitfile_with_git_dir () {
155 git_dir="$(git rev-parse --git-dir)" &&
157 cp -R "$git_dir" .git &&
158 GIT_WORK_TREE=. git config --unset core.worktree
162 # Test that the .git directory in the submodule is unchanged (except for the
163 # core.worktree setting, which appears only in $GIT_DIR/modules/$1/config).
164 # Call this function before test_submodule_content as the latter might
165 # write the index file leading to false positive index differences.
167 # Note that this only supports submodules at the root level of the
168 # superproject, with the default name, i.e. same as its path.
169 test_git_directory_is_unchanged () {
171 cd ".git/modules/$1" &&
172 # does core.worktree point at the right place?
173 test "$(git config core.worktree)" = "../../../$1" &&
174 # remove it temporarily before comparing, as
175 # "$1/.git/config" lacks it...
176 git config --unset core.worktree
178 diff -r ".git/modules/$1" "$1/.git" &&
180 # ... and then restore.
181 cd ".git/modules/$1" &&
182 git config core.worktree "../../../$1"
186 test_git_directory_exists() {
187 test -e ".git/modules/$1" &&
190 # does core.worktree point at the right place?
191 test "$(git -C .git/modules/$1 config core.worktree)" = "../../../$1"
195 # Helper function to be executed at the start of every test below, it sets up
196 # the submodule repo if it doesn't exist and configures the most problematic
197 # settings for diff.ignoreSubmodules.
199 (test -d submodule_update_repo || create_lib_submodule_repo) &&
200 test_config_global diff.ignoreSubmodules all &&
201 test_config diff.ignoreSubmodules all
204 # Helper function to bring work tree back into the state given by the
205 # commit. This includes trying to populate sub1 accordingly if it exists and
206 # should be updated to an existing commit.
207 reset_work_tree_to () {
208 rm -rf submodule_update &&
209 git clone submodule_update_repo submodule_update &&
211 cd submodule_update &&
213 git checkout -f "$1" &&
214 git status -u -s >actual &&
215 test_must_be_empty actual &&
216 hash=$(git rev-parse --revs-only HEAD:sub1) &&
217 if test -n "$hash" &&
218 test $(cd "../submodule_update_sub1" && git rev-parse --verify "$hash^{commit}")
220 git submodule update --init --recursive "sub1"
225 reset_work_tree_to_interested () {
226 reset_work_tree_to $1 &&
227 # make the submodule git dirs available
228 if ! test -d submodule_update/.git/modules/sub1
230 mkdir -p submodule_update/.git/modules &&
231 cp -r submodule_update_repo/.git/modules/sub1 submodule_update/.git/modules/sub1
232 GIT_WORK_TREE=. git -C submodule_update/.git/modules/sub1 config --unset core.worktree
234 if ! test -d submodule_update/.git/modules/sub1/modules/sub2
236 mkdir -p submodule_update/.git/modules/sub1/modules &&
237 cp -r submodule_update_repo/.git/modules/sub1/modules/sub2 submodule_update/.git/modules/sub1/modules/sub2
238 # core.worktree is unset for sub2 as it is not checked out
240 # indicate we are interested in the submodule:
241 git -C submodule_update config submodule.sub1.url "bogus" &&
242 # sub1 might not be checked out, so use the git dir
243 git -C submodule_update/.git/modules/sub1 config submodule.sub2.url "bogus"
246 # Test that the superproject contains the content according to commit "$1"
247 # (the work tree must match the index for everything but submodules but the
248 # index must exactly match the given commit including any submodule SHA-1s).
249 test_superproject_content () {
250 git diff-index --cached "$1" >actual &&
251 test_must_be_empty actual &&
252 git diff-files --ignore-submodules >actual &&
253 test_must_be_empty actual
256 # Test that the given submodule at path "$1" contains the content according
257 # to the submodule commit recorded in the superproject's commit "$2"
258 test_submodule_content () {
259 if test x"$1" = "x-C"
266 echo "test_submodule_content needs two arguments"
271 test -d "$submodule"/ &&
272 if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
274 echo "Submodule $submodule is not populated"
277 sha1=$(git rev-parse --verify "$commit:$submodule") &&
280 echo "Couldn't retrieve SHA-1 of $submodule for $commit"
285 git status -u -s >actual &&
286 test_must_be_empty actual &&
287 git diff "$sha1" >actual &&
288 test_must_be_empty actual
292 # Test that the following transitions are correctly handled:
293 # - Updated submodule
295 # - Removed submodule
296 # - Directory containing tracked files replaced by submodule
297 # - Submodule replaced by tracked files in directory
298 # - Submodule replaced by tracked file with the same name
299 # - tracked file replaced by submodule
301 # The default is that submodule contents aren't changed until "git submodule
302 # update" is run. And even then that command doesn't delete the work tree of
303 # a removed submodule.
305 # Removing a submodule containing a .git directory must fail even when forced
306 # to protect the history!
309 # Internal function; use test_submodule_switch() or
310 # test_submodule_forced_switch() instead.
311 test_submodule_switch_common() {
313 ######################### Appearing submodule #########################
314 # Switching to a commit letting a submodule appear creates empty dir ...
315 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
317 # Restoring stash fails to restore submodule index entry
322 test_expect_$RESULT "$command: added submodule creates empty directory" '
324 reset_work_tree_to no_submodule &&
326 cd submodule_update &&
327 git branch -t add_sub1 origin/add_sub1 &&
329 test_superproject_content origin/add_sub1 &&
330 test_dir_is_empty sub1 &&
331 git submodule update --init --recursive &&
332 test_submodule_content sub1 origin/add_sub1
335 # ... and doesn't care if it already exists.
336 test_expect_$RESULT "$command: added submodule leaves existing empty directory alone" '
338 reset_work_tree_to no_submodule &&
340 cd submodule_update &&
342 git branch -t add_sub1 origin/add_sub1 &&
344 test_superproject_content origin/add_sub1 &&
345 test_dir_is_empty sub1 &&
346 git submodule update --init --recursive &&
347 test_submodule_content sub1 origin/add_sub1
350 # Replacing a tracked file with a submodule produces an empty
352 test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
354 reset_work_tree_to replace_sub1_with_file &&
356 cd submodule_update &&
357 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
358 $command replace_file_with_sub1 &&
359 test_superproject_content origin/replace_file_with_sub1 &&
360 test_dir_is_empty sub1 &&
361 git submodule update --init --recursive &&
362 test_submodule_content sub1 origin/replace_file_with_sub1
365 # ... as does removing a directory with tracked files with a
367 if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
369 # Non fast-forward merges fail with "Directory sub1 doesn't
370 # exist. sub1" because the empty submodule directory is not
376 test_expect_$RESULT "$command: replace directory with submodule" '
378 reset_work_tree_to replace_sub1_with_directory &&
380 cd submodule_update &&
381 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
382 $command replace_directory_with_sub1 &&
383 test_superproject_content origin/replace_directory_with_sub1 &&
384 test_dir_is_empty sub1 &&
385 git submodule update --init --recursive &&
386 test_submodule_content sub1 origin/replace_directory_with_sub1
390 ######################## Disappearing submodule #######################
391 # Removing a submodule doesn't remove its work tree ...
392 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
398 test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
400 reset_work_tree_to add_sub1 &&
402 cd submodule_update &&
403 git branch -t remove_sub1 origin/remove_sub1 &&
404 $command remove_sub1 &&
405 test_superproject_content origin/remove_sub1 &&
406 test_submodule_content sub1 origin/add_sub1
409 # ... especially when it contains a .git directory.
410 test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
412 reset_work_tree_to add_sub1 &&
414 cd submodule_update &&
415 git branch -t remove_sub1 origin/remove_sub1 &&
416 replace_gitfile_with_git_dir sub1 &&
417 $command remove_sub1 &&
418 test_superproject_content origin/remove_sub1 &&
419 test_git_directory_is_unchanged sub1 &&
420 test_submodule_content sub1 origin/add_sub1
423 # Replacing a submodule with files in a directory must fail as the
424 # submodule work tree isn't removed ...
425 if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
427 # Non fast-forward merges attempt to merge the former
428 # submodule files with the newly checked out ones in the
429 # directory of the same name while it shouldn't.
431 elif test "$KNOWN_FAILURE_FORCED_SWITCH_TESTS" = 1
433 # All existing tests that use test_submodule_forced_switch()
439 test_expect_$RESULT "$command: replace submodule with a directory must fail" '
441 reset_work_tree_to add_sub1 &&
443 cd submodule_update &&
444 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
445 test_must_fail $command replace_sub1_with_directory &&
446 test_superproject_content origin/add_sub1 &&
447 test_submodule_content sub1 origin/add_sub1
450 # ... especially when it contains a .git directory.
451 test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
453 reset_work_tree_to add_sub1 &&
455 cd submodule_update &&
456 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
457 replace_gitfile_with_git_dir sub1 &&
458 test_must_fail $command replace_sub1_with_directory &&
459 test_superproject_content origin/add_sub1 &&
460 test_git_directory_is_unchanged sub1 &&
461 test_submodule_content sub1 origin/add_sub1
464 # Replacing it with a file must fail as it could throw away any local
465 # work tree changes ...
466 test_expect_failure "$command: replace submodule with a file must fail" '
468 reset_work_tree_to add_sub1 &&
470 cd submodule_update &&
471 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
472 test_must_fail $command replace_sub1_with_file &&
473 test_superproject_content origin/add_sub1 &&
474 test_submodule_content sub1 origin/add_sub1
477 # ... or even destroy unpushed parts of submodule history if that
478 # still uses a .git directory.
479 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
481 reset_work_tree_to add_sub1 &&
483 cd submodule_update &&
484 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
485 replace_gitfile_with_git_dir sub1 &&
486 test_must_fail $command replace_sub1_with_file &&
487 test_superproject_content origin/add_sub1 &&
488 test_git_directory_is_unchanged sub1 &&
489 test_submodule_content sub1 origin/add_sub1
493 ########################## Modified submodule #########################
494 # Updating a submodule sha1 doesn't update the submodule's work tree
495 if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
497 # When cherry picking a SHA-1 update for an ignored submodule
498 # the commit incorrectly fails with "The previous cherry-pick
499 # is now empty, possibly due to conflict resolution."
504 test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
506 reset_work_tree_to add_sub1 &&
508 cd submodule_update &&
509 git branch -t modify_sub1 origin/modify_sub1 &&
510 $command modify_sub1 &&
511 test_superproject_content origin/modify_sub1 &&
512 test_submodule_content sub1 origin/add_sub1 &&
513 git submodule update &&
514 test_submodule_content sub1 origin/modify_sub1
517 # Updating a submodule to an invalid sha1 doesn't update the
518 # submodule's work tree, subsequent update will fail
519 test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
521 reset_work_tree_to add_sub1 &&
523 cd submodule_update &&
524 git branch -t invalid_sub1 origin/invalid_sub1 &&
525 $command invalid_sub1 &&
526 test_superproject_content origin/invalid_sub1 &&
527 test_submodule_content sub1 origin/add_sub1 &&
528 test_must_fail git submodule update &&
529 test_submodule_content sub1 origin/add_sub1
532 # Updating a submodule from an invalid sha1 doesn't update the
533 # submodule's work tree, subsequent update will succeed
534 test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
536 reset_work_tree_to invalid_sub1 &&
538 cd submodule_update &&
539 git branch -t valid_sub1 origin/valid_sub1 &&
540 $command valid_sub1 &&
541 test_superproject_content origin/valid_sub1 &&
542 test_dir_is_empty sub1 &&
543 git submodule update --init --recursive &&
544 test_submodule_content sub1 origin/valid_sub1
549 # Declares and invokes several tests that, in various situations, checks that
550 # the provided transition function:
551 # - succeeds in updating the worktree and index of a superproject to a target
552 # commit, or fails atomically (depending on the test situation)
553 # - if succeeds, the contents of submodule directories are unchanged
554 # - if succeeds, once "git submodule update" is invoked, the contents of
555 # submodule directories are updated
557 # If the command under test is known to not work with submodules in certain
558 # conditions, set the appropriate KNOWN_FAILURE_* variable used in the tests
565 # # Do something here that updates the worktree and index to match target,
566 # # but not any submodule directories.
568 # test_submodule_switch "my_func"
569 test_submodule_switch () {
571 test_submodule_switch_common "$command"
573 # An empty directory does not prevent the creation of a submodule of
574 # the same name, but a file does.
575 test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
577 reset_work_tree_to no_submodule &&
579 cd submodule_update &&
580 git branch -t add_sub1 origin/add_sub1 &&
582 test_must_fail $command add_sub1 &&
583 test_superproject_content origin/no_submodule &&
584 test_must_be_empty sub1
589 # Same as test_submodule_switch(), except that throwing away local changes in
590 # the superproject is allowed.
591 test_submodule_forced_switch () {
593 KNOWN_FAILURE_FORCED_SWITCH_TESTS=1
594 test_submodule_switch_common "$command"
596 # When forced, a file in the superproject does not prevent creating a
597 # submodule of the same name.
598 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
600 reset_work_tree_to no_submodule &&
602 cd submodule_update &&
603 git branch -t add_sub1 origin/add_sub1 &&
606 test_superproject_content origin/add_sub1 &&
607 test_dir_is_empty sub1
612 # Test that submodule contents are correctly updated when switching
613 # between commits that change a submodule.
614 # Test that the following transitions are correctly handled:
615 # (These tests are also above in the case where we expect no change
617 # - Updated submodule
619 # - Removed submodule
620 # - Directory containing tracked files replaced by submodule
621 # - Submodule replaced by tracked files in directory
622 # - Submodule replaced by tracked file with the same name
623 # - tracked file replaced by submodule
626 # - Removing a submodule with a git directory absorbs the submodules
627 # git directory first into the superproject.
629 # Internal function; use test_submodule_switch_recursing_with_args() or
630 # test_submodule_forced_switch_recursing_with_args() instead.
631 test_submodule_recursing_with_args_common() {
634 ######################### Appearing submodule #########################
635 # Switching to a commit letting a submodule appear checks it out ...
636 test_expect_success "$command: added submodule is checked out" '
638 reset_work_tree_to_interested no_submodule &&
640 cd submodule_update &&
641 git branch -t add_sub1 origin/add_sub1 &&
643 test_superproject_content origin/add_sub1 &&
644 test_submodule_content sub1 origin/add_sub1
647 # ... ignoring an empty existing directory.
648 test_expect_success "$command: added submodule is checked out in empty dir" '
650 reset_work_tree_to_interested no_submodule &&
652 cd submodule_update &&
654 git branch -t add_sub1 origin/add_sub1 &&
656 test_superproject_content origin/add_sub1 &&
657 test_submodule_content sub1 origin/add_sub1
660 test_expect_success "$command: submodule branch is not changed, detach HEAD instead" '
662 reset_work_tree_to_interested add_sub1 &&
664 cd submodule_update &&
665 git -C sub1 checkout -b keep_branch &&
666 git -C sub1 rev-parse HEAD >expect &&
667 git branch -t modify_sub1 origin/modify_sub1 &&
668 $command modify_sub1 &&
669 test_superproject_content origin/modify_sub1 &&
670 test_submodule_content sub1 origin/modify_sub1 &&
671 git -C sub1 rev-parse keep_branch >actual &&
672 test_cmp expect actual &&
673 test_must_fail git -C sub1 symbolic-ref HEAD
677 # Replacing a tracked file with a submodule produces a checked out submodule
678 test_expect_success "$command: replace tracked file with submodule checks out submodule" '
680 reset_work_tree_to_interested replace_sub1_with_file &&
682 cd submodule_update &&
683 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
684 $command replace_file_with_sub1 &&
685 test_superproject_content origin/replace_file_with_sub1 &&
686 test_submodule_content sub1 origin/replace_file_with_sub1
689 # ... as does removing a directory with tracked files with a submodule.
690 test_expect_success "$command: replace directory with submodule" '
692 reset_work_tree_to_interested replace_sub1_with_directory &&
694 cd submodule_update &&
695 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
696 $command replace_directory_with_sub1 &&
697 test_superproject_content origin/replace_directory_with_sub1 &&
698 test_submodule_content sub1 origin/replace_directory_with_sub1
702 ######################## Disappearing submodule #######################
703 # Removing a submodule removes its work tree ...
704 test_expect_success "$command: removed submodule removes submodules working tree" '
706 reset_work_tree_to_interested add_sub1 &&
708 cd submodule_update &&
709 git branch -t remove_sub1 origin/remove_sub1 &&
710 $command remove_sub1 &&
711 test_superproject_content origin/remove_sub1 &&
713 test_must_fail git config -f .git/modules/sub1/config core.worktree
716 # ... absorbing a .git directory along the way.
717 test_expect_success "$command: removed submodule absorbs submodules .git directory" '
719 reset_work_tree_to_interested add_sub1 &&
721 cd submodule_update &&
722 git branch -t remove_sub1 origin/remove_sub1 &&
723 replace_gitfile_with_git_dir sub1 &&
724 rm -rf .git/modules &&
725 $command remove_sub1 &&
726 test_superproject_content origin/remove_sub1 &&
728 test_git_directory_exists sub1
732 # Replacing it with a file ...
733 test_expect_success "$command: replace submodule with a file" '
735 reset_work_tree_to_interested add_sub1 &&
737 cd submodule_update &&
738 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
739 $command replace_sub1_with_file &&
740 test_superproject_content origin/replace_sub1_with_file &&
745 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
749 # ... must check its local work tree for untracked files
750 test_expect_$RESULTDS "$command: replace submodule with a file must fail with untracked files" '
752 reset_work_tree_to_interested add_sub1 &&
754 cd submodule_update &&
755 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
756 : >sub1/untrackedfile &&
757 test_must_fail $command replace_sub1_with_file &&
758 test_superproject_content origin/add_sub1 &&
759 test_submodule_content sub1 origin/add_sub1 &&
760 test -f sub1/untracked_file
764 ########################## Modified submodule #########################
765 # Updating a submodule sha1 updates the submodule's work tree
766 test_expect_success "$command: modified submodule updates submodule work tree" '
768 reset_work_tree_to_interested add_sub1 &&
770 cd submodule_update &&
771 git branch -t modify_sub1 origin/modify_sub1 &&
772 $command modify_sub1 &&
773 test_superproject_content origin/modify_sub1 &&
774 test_submodule_content sub1 origin/modify_sub1
777 # Updating a submodule to an invalid sha1 doesn't update the
778 # superproject nor the submodule's work tree.
779 test_expect_success "$command: updating to a missing submodule commit fails" '
781 reset_work_tree_to_interested add_sub1 &&
783 cd submodule_update &&
784 git branch -t invalid_sub1 origin/invalid_sub1 &&
785 test_must_fail $command invalid_sub1 2>err &&
786 test_i18ngrep sub1 err &&
787 test_superproject_content origin/add_sub1 &&
788 test_submodule_content sub1 origin/add_sub1
793 # Declares and invokes several tests that, in various situations, checks that
794 # the provided Git command, when invoked with --recurse-submodules:
795 # - succeeds in updating the worktree and index of a superproject to a target
796 # commit, or fails atomically (depending on the test situation)
797 # - if succeeds, the contents of submodule directories are updated
799 # Specify the Git command so that "git $GIT_COMMAND --recurse-submodules"
802 # If the command under test is known to not work with submodules in certain
803 # conditions, set the appropriate KNOWN_FAILURE_* variable used in the tests
808 # test_submodule_switch_recursing_with_args "$GIT_COMMAND"
809 test_submodule_switch_recursing_with_args () {
811 command="git $cmd_args --recurse-submodules"
812 test_submodule_recursing_with_args_common "$command"
815 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
820 if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
824 # Switching to a commit letting a submodule appear cannot override an
826 test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
828 reset_work_tree_to_interested no_submodule &&
830 cd submodule_update &&
831 git branch -t add_sub1 origin/add_sub1 &&
833 test_must_fail $command add_sub1 &&
834 test_superproject_content origin/no_submodule &&
835 test_must_be_empty sub1
838 # ... but an ignored file is fine.
839 test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
840 test_when_finished "rm submodule_update/.git/info/exclude" &&
842 reset_work_tree_to_interested no_submodule &&
844 cd submodule_update &&
845 git branch -t add_sub1 origin/add_sub1 &&
847 echo sub1 >.git/info/exclude &&
849 test_superproject_content origin/add_sub1 &&
850 test_submodule_content sub1 origin/add_sub1
854 # Replacing a submodule with files in a directory must succeeds
855 # when the submodule is clean
856 test_expect_$RESULTDS "$command: replace submodule with a directory" '
858 reset_work_tree_to_interested add_sub1 &&
860 cd submodule_update &&
861 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
862 $command replace_sub1_with_directory &&
863 test_superproject_content origin/replace_sub1_with_directory &&
864 test_submodule_content sub1 origin/replace_sub1_with_directory
867 # ... absorbing a .git directory.
868 test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
870 reset_work_tree_to_interested add_sub1 &&
872 cd submodule_update &&
873 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
874 replace_gitfile_with_git_dir sub1 &&
875 rm -rf .git/modules &&
876 $command replace_sub1_with_directory &&
877 test_superproject_content origin/replace_sub1_with_directory &&
878 test_git_directory_exists sub1
882 # ... and ignored files are ignored
883 test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
884 test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
886 reset_work_tree_to_interested add_sub1 &&
888 cd submodule_update &&
889 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
890 echo ignored >.git/modules/sub1/info/exclude &&
892 $command replace_sub1_with_file &&
893 test_superproject_content origin/replace_sub1_with_file &&
898 test_expect_success "git -c submodule.recurse=true $cmd_args: modified submodule updates submodule work tree" '
900 reset_work_tree_to_interested add_sub1 &&
902 cd submodule_update &&
903 git branch -t modify_sub1 origin/modify_sub1 &&
904 git -c submodule.recurse=true $cmd_args modify_sub1 &&
905 test_superproject_content origin/modify_sub1 &&
906 test_submodule_content sub1 origin/modify_sub1
910 # recursing deeper than one level doesn't work yet.
911 test_expect_success "$command: modified submodule updates submodule recursively" '
913 reset_work_tree_to_interested add_nested_sub &&
915 cd submodule_update &&
916 git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
917 $command modify_sub1_recursively &&
918 test_superproject_content origin/modify_sub1_recursively &&
919 test_submodule_content sub1 origin/modify_sub1_recursively &&
920 test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
925 # Same as test_submodule_switch_recursing_with_args(), except that throwing
926 # away local changes in the superproject is allowed.
927 test_submodule_forced_switch_recursing_with_args () {
929 command="git $cmd_args --recurse-submodules"
930 test_submodule_recursing_with_args_common "$command"
933 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
937 # Switching to a commit letting a submodule appear does not care about
939 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
941 reset_work_tree_to_interested no_submodule &&
943 cd submodule_update &&
944 git branch -t add_sub1 origin/add_sub1 &&
947 test_superproject_content origin/add_sub1 &&
948 test_submodule_content sub1 origin/add_sub1
952 # Replacing a submodule with files in a directory ...
953 test_expect_success "$command: replace submodule with a directory" '
955 reset_work_tree_to_interested add_sub1 &&
957 cd submodule_update &&
958 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
959 $command replace_sub1_with_directory &&
960 test_superproject_content origin/replace_sub1_with_directory
963 # ... absorbing a .git directory.
964 test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
966 reset_work_tree_to_interested add_sub1 &&
968 cd submodule_update &&
969 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
970 replace_gitfile_with_git_dir sub1 &&
971 rm -rf .git/modules/sub1 &&
972 $command replace_sub1_with_directory &&
973 test_superproject_content origin/replace_sub1_with_directory &&
974 test_git_directory_exists sub1
978 # ... even if the submodule contains ignored files
979 test_expect_success "$command: replace submodule with a file ignoring ignored files" '
981 reset_work_tree_to_interested add_sub1 &&
983 cd submodule_update &&
984 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
986 $command replace_sub1_with_file &&
987 test_superproject_content origin/replace_sub1_with_file
991 # Updating a submodule from an invalid sha1 updates
992 test_expect_success "$command: modified submodule does update submodule work tree from invalid commit" '
994 reset_work_tree_to_interested invalid_sub1 &&
996 cd submodule_update &&
997 git branch -t valid_sub1 origin/valid_sub1 &&
998 $command valid_sub1 &&
999 test_superproject_content origin/valid_sub1 &&
1000 test_submodule_content sub1 origin/valid_sub1
1004 # Old versions of Git were buggy writing the .git link file
1005 # (e.g. before f8eaa0ba98b and then moving the superproject repo
1006 # whose submodules contained absolute paths)
1007 test_expect_success "$command: updating submodules fixes .git links" '
1009 reset_work_tree_to_interested add_sub1 &&
1011 cd submodule_update &&
1012 git branch -t modify_sub1 origin/modify_sub1 &&
1013 echo "gitdir: bogus/path" >sub1/.git &&
1014 $command modify_sub1 &&
1015 test_superproject_content origin/modify_sub1 &&
1016 test_submodule_content sub1 origin/modify_sub1
1020 test_expect_success "$command: changed submodule worktree is reset" '
1022 reset_work_tree_to_interested add_sub1 &&
1024 cd submodule_update &&
1027 git -C sub1 add new_file &&
1029 test_path_is_file sub1/file1 &&
1030 test_path_is_missing sub1/new_file