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 GIT_WORK_TREE=. git -C submodule_update/.git/modules/sub1/modules/sub2 config --unset core.worktree
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 # Test that submodule contents are currently not updated when switching
310 # between commits that change a submodule.
311 test_submodule_switch () {
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 # ... unless there is an untracked file in its place.
351 test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
353 reset_work_tree_to no_submodule &&
355 cd submodule_update &&
356 git branch -t add_sub1 origin/add_sub1 &&
358 test_must_fail $command add_sub1 &&
359 test_superproject_content origin/no_submodule &&
360 test_must_be_empty sub1
363 # Replacing a tracked file with a submodule produces an empty
365 test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
367 reset_work_tree_to replace_sub1_with_file &&
369 cd submodule_update &&
370 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
371 $command replace_file_with_sub1 &&
372 test_superproject_content origin/replace_file_with_sub1 &&
373 test_dir_is_empty sub1 &&
374 git submodule update --init --recursive &&
375 test_submodule_content sub1 origin/replace_file_with_sub1
378 # ... as does removing a directory with tracked files with a
380 if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
382 # Non fast-forward merges fail with "Directory sub1 doesn't
383 # exist. sub1" because the empty submodule directory is not
389 test_expect_$RESULT "$command: replace directory with submodule" '
391 reset_work_tree_to replace_sub1_with_directory &&
393 cd submodule_update &&
394 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
395 $command replace_directory_with_sub1 &&
396 test_superproject_content origin/replace_directory_with_sub1 &&
397 test_dir_is_empty sub1 &&
398 git submodule update --init --recursive &&
399 test_submodule_content sub1 origin/replace_directory_with_sub1
403 ######################## Disappearing submodule #######################
404 # Removing a submodule doesn't remove its work tree ...
405 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
411 test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
413 reset_work_tree_to add_sub1 &&
415 cd submodule_update &&
416 git branch -t remove_sub1 origin/remove_sub1 &&
417 $command remove_sub1 &&
418 test_superproject_content origin/remove_sub1 &&
419 test_submodule_content sub1 origin/add_sub1
422 # ... especially when it contains a .git directory.
423 test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
425 reset_work_tree_to add_sub1 &&
427 cd submodule_update &&
428 git branch -t remove_sub1 origin/remove_sub1 &&
429 replace_gitfile_with_git_dir sub1 &&
430 $command remove_sub1 &&
431 test_superproject_content origin/remove_sub1 &&
432 test_git_directory_is_unchanged sub1 &&
433 test_submodule_content sub1 origin/add_sub1
436 # Replacing a submodule with files in a directory must fail as the
437 # submodule work tree isn't removed ...
438 if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
440 # Non fast-forward merges attempt to merge the former
441 # submodule files with the newly checked out ones in the
442 # directory of the same name while it shouldn't.
447 test_expect_$RESULT "$command: replace submodule with a directory must fail" '
449 reset_work_tree_to add_sub1 &&
451 cd submodule_update &&
452 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
453 test_must_fail $command replace_sub1_with_directory &&
454 test_superproject_content origin/add_sub1 &&
455 test_submodule_content sub1 origin/add_sub1
458 # ... especially when it contains a .git directory.
459 test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
461 reset_work_tree_to add_sub1 &&
463 cd submodule_update &&
464 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
465 replace_gitfile_with_git_dir sub1 &&
466 test_must_fail $command replace_sub1_with_directory &&
467 test_superproject_content origin/add_sub1 &&
468 test_git_directory_is_unchanged sub1 &&
469 test_submodule_content sub1 origin/add_sub1
472 # Replacing it with a file must fail as it could throw away any local
473 # work tree changes ...
474 test_expect_failure "$command: replace submodule with a file must fail" '
476 reset_work_tree_to add_sub1 &&
478 cd submodule_update &&
479 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
480 test_must_fail $command replace_sub1_with_file &&
481 test_superproject_content origin/add_sub1 &&
482 test_submodule_content sub1 origin/add_sub1
485 # ... or even destroy unpushed parts of submodule history if that
486 # still uses a .git directory.
487 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
489 reset_work_tree_to add_sub1 &&
491 cd submodule_update &&
492 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
493 replace_gitfile_with_git_dir sub1 &&
494 test_must_fail $command replace_sub1_with_file &&
495 test_superproject_content origin/add_sub1 &&
496 test_git_directory_is_unchanged sub1 &&
497 test_submodule_content sub1 origin/add_sub1
501 ########################## Modified submodule #########################
502 # Updating a submodule sha1 doesn't update the submodule's work tree
503 if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
505 # When cherry picking a SHA-1 update for an ignored submodule
506 # the commit incorrectly fails with "The previous cherry-pick
507 # is now empty, possibly due to conflict resolution."
512 test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
514 reset_work_tree_to add_sub1 &&
516 cd submodule_update &&
517 git branch -t modify_sub1 origin/modify_sub1 &&
518 $command modify_sub1 &&
519 test_superproject_content origin/modify_sub1 &&
520 test_submodule_content sub1 origin/add_sub1 &&
521 git submodule update &&
522 test_submodule_content sub1 origin/modify_sub1
526 # Updating a submodule to an invalid sha1 doesn't update the
527 # submodule's work tree, subsequent update will fail
528 test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
530 reset_work_tree_to add_sub1 &&
532 cd submodule_update &&
533 git branch -t invalid_sub1 origin/invalid_sub1 &&
534 $command invalid_sub1 &&
535 test_superproject_content origin/invalid_sub1 &&
536 test_submodule_content sub1 origin/add_sub1 &&
537 test_must_fail git submodule update &&
538 test_submodule_content sub1 origin/add_sub1
541 # Updating a submodule from an invalid sha1 doesn't update the
542 # submodule's work tree, subsequent update will succeed
543 test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
545 reset_work_tree_to invalid_sub1 &&
547 cd submodule_update &&
548 git branch -t valid_sub1 origin/valid_sub1 &&
549 $command valid_sub1 &&
550 test_superproject_content origin/valid_sub1 &&
551 test_dir_is_empty sub1 &&
552 git submodule update --init --recursive &&
553 test_submodule_content sub1 origin/valid_sub1
558 # Test that submodule contents are currently not updated when switching
559 # between commits that change a submodule, but throwing away local changes in
560 # the superproject is allowed.
561 test_submodule_forced_switch () {
563 ######################### Appearing submodule #########################
564 # Switching to a commit letting a submodule appear creates empty dir ...
565 test_expect_success "$command: added submodule creates empty directory" '
567 reset_work_tree_to no_submodule &&
569 cd submodule_update &&
570 git branch -t add_sub1 origin/add_sub1 &&
572 test_superproject_content origin/add_sub1 &&
573 test_dir_is_empty sub1 &&
574 git submodule update --init --recursive &&
575 test_submodule_content sub1 origin/add_sub1
578 # ... and doesn't care if it already exists ...
579 test_expect_success "$command: added submodule leaves existing empty directory alone" '
581 reset_work_tree_to no_submodule &&
583 cd submodule_update &&
584 git branch -t add_sub1 origin/add_sub1 &&
587 test_superproject_content origin/add_sub1 &&
588 test_dir_is_empty sub1 &&
589 git submodule update --init --recursive &&
590 test_submodule_content sub1 origin/add_sub1
593 # ... unless there is an untracked file in its place.
594 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
596 reset_work_tree_to no_submodule &&
598 cd submodule_update &&
599 git branch -t add_sub1 origin/add_sub1 &&
602 test_superproject_content origin/add_sub1 &&
603 test_dir_is_empty sub1
606 # Replacing a tracked file with a submodule produces an empty
608 test_expect_success "$command: replace tracked file with submodule creates empty directory" '
610 reset_work_tree_to replace_sub1_with_file &&
612 cd submodule_update &&
613 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
614 $command replace_file_with_sub1 &&
615 test_superproject_content origin/replace_file_with_sub1 &&
616 test_dir_is_empty sub1 &&
617 git submodule update --init --recursive &&
618 test_submodule_content sub1 origin/replace_file_with_sub1
621 # ... as does removing a directory with tracked files with a
623 test_expect_success "$command: replace directory with submodule" '
625 reset_work_tree_to replace_sub1_with_directory &&
627 cd submodule_update &&
628 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
629 $command replace_directory_with_sub1 &&
630 test_superproject_content origin/replace_directory_with_sub1 &&
631 test_dir_is_empty sub1 &&
632 git submodule update --init --recursive &&
633 test_submodule_content sub1 origin/replace_directory_with_sub1
637 ######################## Disappearing submodule #######################
638 # Removing a submodule doesn't remove its work tree ...
639 test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
641 reset_work_tree_to add_sub1 &&
643 cd submodule_update &&
644 git branch -t remove_sub1 origin/remove_sub1 &&
645 $command remove_sub1 &&
646 test_superproject_content origin/remove_sub1 &&
647 test_submodule_content sub1 origin/add_sub1
650 # ... especially when it contains a .git directory.
651 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
653 reset_work_tree_to add_sub1 &&
655 cd submodule_update &&
656 git branch -t remove_sub1 origin/remove_sub1 &&
657 replace_gitfile_with_git_dir sub1 &&
658 $command remove_sub1 &&
659 test_superproject_content origin/remove_sub1 &&
660 test_git_directory_is_unchanged sub1 &&
661 test_submodule_content sub1 origin/add_sub1
664 # Replacing a submodule with files in a directory must fail as the
665 # submodule work tree isn't removed ...
666 test_expect_failure "$command: replace submodule with a directory must fail" '
668 reset_work_tree_to add_sub1 &&
670 cd submodule_update &&
671 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
672 test_must_fail $command replace_sub1_with_directory &&
673 test_superproject_content origin/add_sub1 &&
674 test_submodule_content sub1 origin/add_sub1
677 # ... especially when it contains a .git directory.
678 test_expect_failure "$command: replace submodule containing a .git directory with a directory must fail" '
680 reset_work_tree_to add_sub1 &&
682 cd submodule_update &&
683 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
684 replace_gitfile_with_git_dir sub1 &&
685 test_must_fail $command replace_sub1_with_directory &&
686 test_superproject_content origin/add_sub1 &&
687 test_git_directory_is_unchanged sub1 &&
688 test_submodule_content sub1 origin/add_sub1
691 # Replacing it with a file must fail as it could throw away any local
692 # work tree changes ...
693 test_expect_failure "$command: replace submodule with a file must fail" '
695 reset_work_tree_to add_sub1 &&
697 cd submodule_update &&
698 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
699 test_must_fail $command replace_sub1_with_file &&
700 test_superproject_content origin/add_sub1 &&
701 test_submodule_content sub1 origin/add_sub1
704 # ... or even destroy unpushed parts of submodule history if that
705 # still uses a .git directory.
706 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
708 reset_work_tree_to add_sub1 &&
710 cd submodule_update &&
711 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
712 replace_gitfile_with_git_dir sub1 &&
713 test_must_fail $command replace_sub1_with_file &&
714 test_superproject_content origin/add_sub1 &&
715 test_git_directory_is_unchanged sub1 &&
716 test_submodule_content sub1 origin/add_sub1
720 ########################## Modified submodule #########################
721 # Updating a submodule sha1 doesn't update the submodule's work tree
722 test_expect_success "$command: modified submodule does not update submodule work tree" '
724 reset_work_tree_to add_sub1 &&
726 cd submodule_update &&
727 git branch -t modify_sub1 origin/modify_sub1 &&
728 $command modify_sub1 &&
729 test_superproject_content origin/modify_sub1 &&
730 test_submodule_content sub1 origin/add_sub1 &&
731 git submodule update &&
732 test_submodule_content sub1 origin/modify_sub1
735 # Updating a submodule to an invalid sha1 doesn't update the
736 # submodule's work tree, subsequent update will fail
737 test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
739 reset_work_tree_to add_sub1 &&
741 cd submodule_update &&
742 git branch -t invalid_sub1 origin/invalid_sub1 &&
743 $command invalid_sub1 &&
744 test_superproject_content origin/invalid_sub1 &&
745 test_submodule_content sub1 origin/add_sub1 &&
746 test_must_fail git submodule update &&
747 test_submodule_content sub1 origin/add_sub1
750 # Updating a submodule from an invalid sha1 doesn't update the
751 # submodule's work tree, subsequent update will succeed
752 test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
754 reset_work_tree_to invalid_sub1 &&
756 cd submodule_update &&
757 git branch -t valid_sub1 origin/valid_sub1 &&
758 $command valid_sub1 &&
759 test_superproject_content origin/valid_sub1 &&
760 test_dir_is_empty sub1 &&
761 git submodule update --init --recursive &&
762 test_submodule_content sub1 origin/valid_sub1
767 # Test that submodule contents are correctly updated when switching
768 # between commits that change a submodule.
769 # Test that the following transitions are correctly handled:
770 # (These tests are also above in the case where we expect no change
772 # - Updated submodule
774 # - Removed submodule
775 # - Directory containing tracked files replaced by submodule
776 # - Submodule replaced by tracked files in directory
777 # - Submodule replaced by tracked file with the same name
778 # - tracked file replaced by submodule
781 # - Removing a submodule with a git directory absorbs the submodules
782 # git directory first into the superproject.
784 test_submodule_switch_recursing_with_args () {
786 command="git $cmd_args --recurse-submodules"
788 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
793 if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
797 ######################### Appearing submodule #########################
798 # Switching to a commit letting a submodule appear checks it out ...
799 test_expect_success "$command: added submodule is checked out" '
801 reset_work_tree_to_interested no_submodule &&
803 cd submodule_update &&
804 git branch -t add_sub1 origin/add_sub1 &&
806 test_superproject_content origin/add_sub1 &&
807 test_submodule_content sub1 origin/add_sub1
810 # ... ignoring an empty existing directory ...
811 test_expect_success "$command: added submodule is checked out in empty dir" '
813 reset_work_tree_to_interested no_submodule &&
815 cd submodule_update &&
817 git branch -t add_sub1 origin/add_sub1 &&
819 test_superproject_content origin/add_sub1 &&
820 test_submodule_content sub1 origin/add_sub1
823 # ... unless there is an untracked file in its place.
824 test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
826 reset_work_tree_to_interested no_submodule &&
828 cd submodule_update &&
829 git branch -t add_sub1 origin/add_sub1 &&
831 test_must_fail $command add_sub1 &&
832 test_superproject_content origin/no_submodule &&
833 test_must_be_empty sub1
836 # ... but an ignored file is fine.
837 test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
838 test_when_finished "rm submodule_update/.git/info/exclude" &&
840 reset_work_tree_to_interested no_submodule &&
842 cd submodule_update &&
843 git branch -t add_sub1 origin/add_sub1 &&
845 echo sub1 >.git/info/exclude
847 test_superproject_content origin/add_sub1 &&
848 test_submodule_content sub1 origin/add_sub1
851 # Replacing a tracked file with a submodule produces a checked out submodule
852 test_expect_success "$command: replace tracked file with submodule checks out submodule" '
854 reset_work_tree_to_interested replace_sub1_with_file &&
856 cd submodule_update &&
857 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
858 $command replace_file_with_sub1 &&
859 test_superproject_content origin/replace_file_with_sub1 &&
860 test_submodule_content sub1 origin/replace_file_with_sub1
863 # ... as does removing a directory with tracked files with a submodule.
864 test_expect_success "$command: replace directory with submodule" '
866 reset_work_tree_to_interested replace_sub1_with_directory &&
868 cd submodule_update &&
869 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
870 $command replace_directory_with_sub1 &&
871 test_superproject_content origin/replace_directory_with_sub1 &&
872 test_submodule_content sub1 origin/replace_directory_with_sub1
876 ######################## Disappearing submodule #######################
877 # Removing a submodule removes its work tree ...
878 test_expect_success "$command: removed submodule removes submodules working tree" '
880 reset_work_tree_to_interested add_sub1 &&
882 cd submodule_update &&
883 git branch -t remove_sub1 origin/remove_sub1 &&
884 $command remove_sub1 &&
885 test_superproject_content origin/remove_sub1 &&
889 # ... absorbing a .git directory along the way.
890 test_expect_success "$command: removed submodule absorbs submodules .git directory" '
892 reset_work_tree_to_interested add_sub1 &&
894 cd submodule_update &&
895 git branch -t remove_sub1 origin/remove_sub1 &&
896 replace_gitfile_with_git_dir sub1 &&
897 rm -rf .git/modules &&
898 $command remove_sub1 &&
899 test_superproject_content origin/remove_sub1 &&
901 test_git_directory_exists sub1
904 # Replacing a submodule with files in a directory must succeeds
905 # when the submodule is clean
906 test_expect_$RESULTDS "$command: replace submodule with a directory" '
908 reset_work_tree_to_interested add_sub1 &&
910 cd submodule_update &&
911 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
912 $command replace_sub1_with_directory &&
913 test_superproject_content origin/replace_sub1_with_directory &&
914 test_submodule_content sub1 origin/replace_sub1_with_directory
917 # ... absorbing a .git directory.
918 test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
920 reset_work_tree_to_interested add_sub1 &&
922 cd submodule_update &&
923 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
924 replace_gitfile_with_git_dir sub1 &&
925 rm -rf .git/modules &&
926 $command replace_sub1_with_directory &&
927 test_superproject_content origin/replace_sub1_with_directory &&
928 test_git_directory_exists sub1
932 # Replacing it with a file ...
933 test_expect_success "$command: replace submodule with a file" '
935 reset_work_tree_to_interested add_sub1 &&
937 cd submodule_update &&
938 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
939 $command replace_sub1_with_file &&
940 test_superproject_content origin/replace_sub1_with_file &&
945 # ... must check its local work tree for untracked files
946 test_expect_$RESULTDS "$command: replace submodule with a file must fail with untracked files" '
948 reset_work_tree_to_interested add_sub1 &&
950 cd submodule_update &&
951 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
952 : >sub1/untrackedfile &&
953 test_must_fail $command replace_sub1_with_file &&
954 test_superproject_content origin/add_sub1 &&
955 test_submodule_content sub1 origin/add_sub1
959 # ... and ignored files are ignored
960 test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
961 test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
963 reset_work_tree_to_interested add_sub1 &&
965 cd submodule_update &&
966 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
968 $command replace_sub1_with_file &&
969 test_superproject_content origin/replace_sub1_with_file &&
974 ########################## Modified submodule #########################
975 # Updating a submodule sha1 updates the submodule's work tree
976 test_expect_success "$command: modified submodule updates submodule work tree" '
978 reset_work_tree_to_interested add_sub1 &&
980 cd submodule_update &&
981 git branch -t modify_sub1 origin/modify_sub1 &&
982 $command modify_sub1 &&
983 test_superproject_content origin/modify_sub1 &&
984 test_submodule_content sub1 origin/modify_sub1
988 test_expect_success "git -c submodule.recurse=true $cmd_args: modified submodule updates submodule work tree" '
990 reset_work_tree_to_interested add_sub1 &&
992 cd submodule_update &&
993 git branch -t modify_sub1 origin/modify_sub1 &&
994 git -c submodule.recurse=true $cmd_args modify_sub1 &&
995 test_superproject_content origin/modify_sub1 &&
996 test_submodule_content sub1 origin/modify_sub1
1000 # Updating a submodule to an invalid sha1 doesn't update the
1001 # superproject nor the submodule's work tree.
1002 test_expect_success "$command: updating to a missing submodule commit fails" '
1004 reset_work_tree_to_interested add_sub1 &&
1006 cd submodule_update &&
1007 git branch -t invalid_sub1 origin/invalid_sub1 &&
1008 test_must_fail $command invalid_sub1 &&
1009 test_superproject_content origin/add_sub1 &&
1010 test_submodule_content sub1 origin/add_sub1
1014 # recursing deeper than one level doesn't work yet.
1015 test_expect_success "$command: modified submodule updates submodule recursively" '
1017 reset_work_tree_to_interested add_nested_sub &&
1019 cd submodule_update &&
1020 git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
1021 $command modify_sub1_recursively &&
1022 test_superproject_content origin/modify_sub1_recursively &&
1023 test_submodule_content sub1 origin/modify_sub1_recursively &&
1024 test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
1029 # Test that submodule contents are updated when switching between commits
1030 # that change a submodule, but throwing away local changes in
1031 # the superproject as well as the submodule is allowed.
1032 test_submodule_forced_switch_recursing_with_args () {
1034 command="git $cmd_args --recurse-submodules"
1036 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
1040 ######################### Appearing submodule #########################
1041 # Switching to a commit letting a submodule appear creates empty dir ...
1042 test_expect_success "$command: added submodule is checked out" '
1044 reset_work_tree_to_interested no_submodule &&
1046 cd submodule_update &&
1047 git branch -t add_sub1 origin/add_sub1 &&
1048 $command add_sub1 &&
1049 test_superproject_content origin/add_sub1 &&
1050 test_submodule_content sub1 origin/add_sub1
1053 # ... and doesn't care if it already exists ...
1054 test_expect_success "$command: added submodule ignores empty directory" '
1056 reset_work_tree_to_interested no_submodule &&
1058 cd submodule_update &&
1059 git branch -t add_sub1 origin/add_sub1 &&
1061 $command add_sub1 &&
1062 test_superproject_content origin/add_sub1 &&
1063 test_submodule_content sub1 origin/add_sub1
1066 # ... not caring about an untracked file either
1067 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
1069 reset_work_tree_to_interested no_submodule &&
1071 cd submodule_update &&
1072 git branch -t add_sub1 origin/add_sub1 &&
1074 $command add_sub1 &&
1075 test_superproject_content origin/add_sub1 &&
1076 test_submodule_content sub1 origin/add_sub1
1079 # Replacing a tracked file with a submodule checks out the submodule
1080 test_expect_success "$command: replace tracked file with submodule populates the submodule" '
1082 reset_work_tree_to_interested replace_sub1_with_file &&
1084 cd submodule_update &&
1085 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
1086 $command replace_file_with_sub1 &&
1087 test_superproject_content origin/replace_file_with_sub1 &&
1088 test_submodule_content sub1 origin/replace_file_with_sub1
1091 # ... as does removing a directory with tracked files with a
1093 test_expect_success "$command: replace directory with submodule" '
1095 reset_work_tree_to_interested replace_sub1_with_directory &&
1097 cd submodule_update &&
1098 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
1099 $command replace_directory_with_sub1 &&
1100 test_superproject_content origin/replace_directory_with_sub1 &&
1101 test_submodule_content sub1 origin/replace_directory_with_sub1
1105 ######################## Disappearing submodule #######################
1106 # Removing a submodule doesn't remove its work tree ...
1107 test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
1109 reset_work_tree_to_interested add_sub1 &&
1111 cd submodule_update &&
1112 git branch -t remove_sub1 origin/remove_sub1 &&
1113 $command remove_sub1 &&
1114 test_superproject_content origin/remove_sub1 &&
1118 # ... especially when it contains a .git directory.
1119 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
1121 reset_work_tree_to_interested add_sub1 &&
1123 cd submodule_update &&
1124 git branch -t remove_sub1 origin/remove_sub1 &&
1125 replace_gitfile_with_git_dir sub1 &&
1126 rm -rf .git/modules/sub1 &&
1127 $command remove_sub1 &&
1128 test_superproject_content origin/remove_sub1 &&
1129 test_git_directory_exists sub1 &&
1133 # Replacing a submodule with files in a directory ...
1134 test_expect_success "$command: replace submodule with a directory" '
1136 reset_work_tree_to_interested add_sub1 &&
1138 cd submodule_update &&
1139 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1140 $command replace_sub1_with_directory &&
1141 test_superproject_content origin/replace_sub1_with_directory
1144 # ... absorbing a .git directory.
1145 test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
1147 reset_work_tree_to_interested add_sub1 &&
1149 cd submodule_update &&
1150 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1151 replace_gitfile_with_git_dir sub1 &&
1152 rm -rf .git/modules/sub1 &&
1153 $command replace_sub1_with_directory &&
1154 test_superproject_content origin/replace_sub1_with_directory &&
1155 test_submodule_content sub1 origin/modify_sub1
1156 test_git_directory_exists sub1
1159 # Replacing it with a file
1160 test_expect_success "$command: replace submodule with a file" '
1162 reset_work_tree_to_interested add_sub1 &&
1164 cd submodule_update &&
1165 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1166 $command replace_sub1_with_file &&
1167 test_superproject_content origin/replace_sub1_with_file
1171 # ... even if the submodule contains ignored files
1172 test_expect_success "$command: replace submodule with a file ignoring ignored files" '
1174 reset_work_tree_to_interested add_sub1 &&
1176 cd submodule_update &&
1177 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1179 $command replace_sub1_with_file &&
1180 test_superproject_content origin/replace_sub1_with_file
1184 # ... but stops for untracked files that would be lost
1185 test_expect_$RESULT "$command: replace submodule with a file stops for untracked files" '
1187 reset_work_tree_to_interested add_sub1 &&
1189 cd submodule_update &&
1190 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1191 : >sub1/untracked_file &&
1192 test_must_fail $command replace_sub1_with_file &&
1193 test_superproject_content origin/add_sub1 &&
1194 test -f sub1/untracked_file
1198 ########################## Modified submodule #########################
1199 # Updating a submodule sha1 updates the submodule's work tree
1200 test_expect_success "$command: modified submodule updates submodule work tree" '
1202 reset_work_tree_to_interested add_sub1 &&
1204 cd submodule_update &&
1205 git branch -t modify_sub1 origin/modify_sub1 &&
1206 $command modify_sub1 &&
1207 test_superproject_content origin/modify_sub1 &&
1208 test_submodule_content sub1 origin/modify_sub1
1211 # Updating a submodule to an invalid sha1 doesn't update the
1212 # submodule's work tree, subsequent update will fail
1213 test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
1215 reset_work_tree_to_interested add_sub1 &&
1217 cd submodule_update &&
1218 git branch -t invalid_sub1 origin/invalid_sub1 &&
1219 test_must_fail $command invalid_sub1 &&
1220 test_superproject_content origin/add_sub1 &&
1221 test_submodule_content sub1 origin/add_sub1
1224 # Updating a submodule from an invalid sha1 updates
1225 test_expect_success "$command: modified submodule does update submodule work tree from invalid commit" '
1227 reset_work_tree_to_interested invalid_sub1 &&
1229 cd submodule_update &&
1230 git branch -t valid_sub1 origin/valid_sub1 &&
1231 $command valid_sub1 &&
1232 test_superproject_content origin/valid_sub1 &&
1233 test_submodule_content sub1 origin/valid_sub1
1237 # Old versions of Git were buggy writing the .git link file
1238 # (e.g. before f8eaa0ba98b and then moving the superproject repo
1239 # whose submodules contained absolute paths)
1240 test_expect_success "$command: updating submodules fixes .git links" '
1242 reset_work_tree_to_interested add_sub1 &&
1244 cd submodule_update &&
1245 git branch -t modify_sub1 origin/modify_sub1 &&
1246 echo "gitdir: bogus/path" >sub1/.git &&
1247 $command modify_sub1 &&
1248 test_superproject_content origin/modify_sub1 &&
1249 test_submodule_content sub1 origin/modify_sub1