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 # - Submodule updated to invalid commit (add_sub1 => invalid_sub1)
8 # - Submodule updated from invalid commit (invalid_sub1 => valid_sub1)
9 # - Submodule replaced by tracked files in directory (add_sub1 =>
10 # replace_sub1_with_directory)
11 # - Directory containing tracked files replaced by submodule
12 # (replace_sub1_with_directory => replace_directory_with_sub1)
13 # - Submodule replaced by tracked file with the same name (add_sub1 =>
14 # replace_sub1_with_file)
15 # - Tracked file replaced by submodule (replace_sub1_with_file =>
16 # replace_file_with_sub1)
19 # / ^ replace_directory_with_sub1
20 # / replace_sub1_with_directory
28 # | | \ ^ replace_file_with_sub1
29 # | | \ replace_sub1_with_file
30 # | add_sub1 --O-----O
31 # no_submodule ^ valid_sub1
34 create_lib_submodule_repo () {
35 git init submodule_update_repo &&
37 cd submodule_update_repo &&
38 echo "expect" >>.gitignore &&
39 echo "actual" >>.gitignore &&
42 git add .gitignore file1 file2 &&
43 git commit -m "Base" &&
44 git branch "no_submodule" &&
46 git checkout -b "add_sub1" &&
47 git submodule add ./. sub1 &&
48 git config -f .gitmodules submodule.sub1.ignore all &&
49 git config submodule.sub1.ignore all &&
50 git add .gitmodules &&
51 git commit -m "Add sub1" &&
52 git checkout -b remove_sub1 &&
55 git checkout -b "modify_sub1" "add_sub1" &&
56 git submodule update &&
60 git checkout -b "modifications" &&
63 git add file2 file3 &&
64 git commit -m "modified file2 and added file3" &&
65 git push origin modifications
68 git commit -m "Modify sub1" &&
70 git checkout -b "replace_sub1_with_directory" "add_sub1" &&
71 git submodule update &&
74 git checkout modifications
76 git rm --cached sub1 &&
78 git config -f .gitmodules --remove-section "submodule.sub1" &&
79 git add .gitmodules sub1/* &&
80 git commit -m "Replace sub1 with directory" &&
81 git checkout -b replace_directory_with_sub1 &&
84 git checkout -b "replace_sub1_with_file" "add_sub1" &&
86 echo "content" >sub1 &&
88 git commit -m "Replace sub1 with file" &&
89 git checkout -b replace_file_with_sub1 &&
92 git checkout -b "invalid_sub1" "add_sub1" &&
93 git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 sub1 &&
94 git commit -m "Invalid sub1 commit" &&
95 git checkout -b valid_sub1 &&
101 # Helper function to replace gitfile with .git directory
102 replace_gitfile_with_git_dir () {
105 git_dir="$(git rev-parse --git-dir)" &&
107 cp -R "$git_dir" .git &&
108 GIT_WORK_TREE=. git config --unset core.worktree
112 # Test that the .git directory in the submodule is unchanged (except for the
113 # core.worktree setting, which appears only in $GIT_DIR/modules/$1/config).
114 # Call this function before test_submodule_content as the latter might
115 # write the index file leading to false positive index differences.
117 # Note that this only supports submodules at the root level of the
118 # superproject, with the default name, i.e. same as its path.
119 test_git_directory_is_unchanged () {
121 cd ".git/modules/$1" &&
122 # does core.worktree point at the right place?
123 test "$(git config core.worktree)" = "../../../$1" &&
124 # remove it temporarily before comparing, as
125 # "$1/.git/config" lacks it...
126 git config --unset core.worktree
128 diff -r ".git/modules/$1" "$1/.git" &&
130 # ... and then restore.
131 cd ".git/modules/$1" &&
132 git config core.worktree "../../../$1"
136 # Helper function to be executed at the start of every test below, it sets up
137 # the submodule repo if it doesn't exist and configures the most problematic
138 # settings for diff.ignoreSubmodules.
140 (test -d submodule_update_repo || create_lib_submodule_repo) &&
141 test_config_global diff.ignoreSubmodules all &&
142 test_config diff.ignoreSubmodules all
145 # Helper function to bring work tree back into the state given by the
146 # commit. This includes trying to populate sub1 accordingly if it exists and
147 # should be updated to an existing commit.
148 reset_work_tree_to () {
149 rm -rf submodule_update &&
150 git clone submodule_update_repo submodule_update &&
152 cd submodule_update &&
154 git checkout -f "$1" &&
155 git status -u -s >actual &&
156 test_must_be_empty actual &&
157 sha1=$(git rev-parse --revs-only HEAD:sub1) &&
158 if test -n "$sha1" &&
159 test $(cd "sub1" && git rev-parse --verify "$sha1^{commit}")
161 git submodule update --init --recursive "sub1"
166 # Test that the superproject contains the content according to commit "$1"
167 # (the work tree must match the index for everything but submodules but the
168 # index must exactly match the given commit including any submodule SHA-1s).
169 test_superproject_content () {
170 git diff-index --cached "$1" >actual &&
171 test_must_be_empty actual &&
172 git diff-files --ignore-submodules >actual &&
173 test_must_be_empty actual
176 # Test that the given submodule at path "$1" contains the content according
177 # to the submodule commit recorded in the superproject's commit "$2"
178 test_submodule_content () {
181 echo "test_submodule_content needs two arguments"
186 test -d "$submodule"/ &&
187 if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
189 echo "Submodule $submodule is not populated"
192 sha1=$(git rev-parse --verify "$commit:$submodule") &&
195 echo "Couldn't retrieve SHA-1 of $submodule for $commit"
200 git status -u -s >actual &&
201 test_must_be_empty actual &&
202 git diff "$sha1" >actual &&
203 test_must_be_empty actual
207 # Test that the following transitions are correctly handled:
208 # - Updated submodule
210 # - Removed submodule
211 # - Directory containing tracked files replaced by submodule
212 # - Submodule replaced by tracked files in directory
213 # - Submodule replaced by tracked file with the same name
214 # - tracked file replaced by submodule
216 # The default is that submodule contents aren't changed until "git submodule
217 # update" is run. And even then that command doesn't delete the work tree of
218 # a removed submodule.
220 # Removing a submodule containing a .git directory must fail even when forced
221 # to protect the history!
224 # Test that submodule contents are currently not updated when switching
225 # between commits that change a submodule.
226 test_submodule_switch () {
228 ######################### Appearing submodule #########################
229 # Switching to a commit letting a submodule appear creates empty dir ...
230 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
232 # Restoring stash fails to restore submodule index entry
237 test_expect_$RESULT "$command: added submodule creates empty directory" '
239 reset_work_tree_to no_submodule &&
241 cd submodule_update &&
242 git branch -t add_sub1 origin/add_sub1 &&
244 test_superproject_content origin/add_sub1 &&
245 test_dir_is_empty sub1 &&
246 git submodule update --init --recursive &&
247 test_submodule_content sub1 origin/add_sub1
250 # ... and doesn't care if it already exists ...
251 test_expect_$RESULT "$command: added submodule leaves existing empty directory alone" '
253 reset_work_tree_to no_submodule &&
255 cd submodule_update &&
257 git branch -t add_sub1 origin/add_sub1 &&
259 test_superproject_content origin/add_sub1 &&
260 test_dir_is_empty sub1 &&
261 git submodule update --init --recursive &&
262 test_submodule_content sub1 origin/add_sub1
265 # ... unless there is an untracked file in its place.
266 test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
268 reset_work_tree_to no_submodule &&
270 cd submodule_update &&
271 git branch -t add_sub1 origin/add_sub1 &&
273 test_must_fail $command add_sub1 &&
274 test_superproject_content origin/no_submodule &&
275 test_must_be_empty sub1
278 # Replacing a tracked file with a submodule produces an empty
280 test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
282 reset_work_tree_to replace_sub1_with_file &&
284 cd submodule_update &&
285 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
286 $command replace_file_with_sub1 &&
287 test_superproject_content origin/replace_file_with_sub1 &&
288 test_dir_is_empty sub1 &&
289 git submodule update --init --recursive &&
290 test_submodule_content sub1 origin/replace_file_with_sub1
293 # ... as does removing a directory with tracked files with a
295 if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
297 # Non fast-forward merges fail with "Directory sub1 doesn't
298 # exist. sub1" because the empty submodule directory is not
304 test_expect_$RESULT "$command: replace directory with submodule" '
306 reset_work_tree_to replace_sub1_with_directory &&
308 cd submodule_update &&
309 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
310 $command replace_directory_with_sub1 &&
311 test_superproject_content origin/replace_directory_with_sub1 &&
312 test_dir_is_empty sub1 &&
313 git submodule update --init --recursive &&
314 test_submodule_content sub1 origin/replace_directory_with_sub1
318 ######################## Disappearing submodule #######################
319 # Removing a submodule doesn't remove its work tree ...
320 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
326 test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
328 reset_work_tree_to add_sub1 &&
330 cd submodule_update &&
331 git branch -t remove_sub1 origin/remove_sub1 &&
332 $command remove_sub1 &&
333 test_superproject_content origin/remove_sub1 &&
334 test_submodule_content sub1 origin/add_sub1
337 # ... especially when it contains a .git directory.
338 test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
340 reset_work_tree_to add_sub1 &&
342 cd submodule_update &&
343 git branch -t remove_sub1 origin/remove_sub1 &&
344 replace_gitfile_with_git_dir sub1 &&
345 $command remove_sub1 &&
346 test_superproject_content origin/remove_sub1 &&
347 test_git_directory_is_unchanged sub1 &&
348 test_submodule_content sub1 origin/add_sub1
351 # Replacing a submodule with files in a directory must fail as the
352 # submodule work tree isn't removed ...
353 if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
355 # Non fast-forward merges attempt to merge the former
356 # submodule files with the newly checked out ones in the
357 # directory of the same name while it shouldn't.
362 test_expect_$RESULT "$command: replace submodule with a directory must fail" '
364 reset_work_tree_to add_sub1 &&
366 cd submodule_update &&
367 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
368 test_must_fail $command replace_sub1_with_directory &&
369 test_superproject_content origin/add_sub1 &&
370 test_submodule_content sub1 origin/add_sub1
373 # ... especially when it contains a .git directory.
374 test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
376 reset_work_tree_to add_sub1 &&
378 cd submodule_update &&
379 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
380 replace_gitfile_with_git_dir sub1 &&
381 test_must_fail $command replace_sub1_with_directory &&
382 test_superproject_content origin/add_sub1 &&
383 test_git_directory_is_unchanged sub1 &&
384 test_submodule_content sub1 origin/add_sub1
387 # Replacing it with a file must fail as it could throw away any local
388 # work tree changes ...
389 test_expect_failure "$command: replace submodule with a file must fail" '
391 reset_work_tree_to add_sub1 &&
393 cd submodule_update &&
394 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
395 test_must_fail $command replace_sub1_with_file &&
396 test_superproject_content origin/add_sub1 &&
397 test_submodule_content sub1 origin/add_sub1
400 # ... or even destroy unpushed parts of submodule history if that
401 # still uses a .git directory.
402 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
404 reset_work_tree_to add_sub1 &&
406 cd submodule_update &&
407 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
408 replace_gitfile_with_git_dir sub1 &&
409 test_must_fail $command replace_sub1_with_file &&
410 test_superproject_content origin/add_sub1 &&
411 test_git_directory_is_unchanged sub1 &&
412 test_submodule_content sub1 origin/add_sub1
416 ########################## Modified submodule #########################
417 # Updating a submodule sha1 doesn't update the submodule's work tree
418 if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
420 # When cherry picking a SHA-1 update for an ignored submodule
421 # the commit incorrectly fails with "The previous cherry-pick
422 # is now empty, possibly due to conflict resolution."
427 test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
429 reset_work_tree_to add_sub1 &&
431 cd submodule_update &&
432 git branch -t modify_sub1 origin/modify_sub1 &&
433 $command modify_sub1 &&
434 test_superproject_content origin/modify_sub1 &&
435 test_submodule_content sub1 origin/add_sub1 &&
436 git submodule update &&
437 test_submodule_content sub1 origin/modify_sub1
441 # Updating a submodule to an invalid sha1 doesn't update the
442 # submodule's work tree, subsequent update will fail
443 test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
445 reset_work_tree_to add_sub1 &&
447 cd submodule_update &&
448 git branch -t invalid_sub1 origin/invalid_sub1 &&
449 $command invalid_sub1 &&
450 test_superproject_content origin/invalid_sub1 &&
451 test_submodule_content sub1 origin/add_sub1 &&
452 test_must_fail git submodule update &&
453 test_submodule_content sub1 origin/add_sub1
456 # Updating a submodule from an invalid sha1 doesn't update the
457 # submodule's work tree, subsequent update will succeed
458 test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
460 reset_work_tree_to invalid_sub1 &&
462 cd submodule_update &&
463 git branch -t valid_sub1 origin/valid_sub1 &&
464 $command valid_sub1 &&
465 test_superproject_content origin/valid_sub1 &&
466 test_dir_is_empty sub1 &&
467 git submodule update --init --recursive &&
468 test_submodule_content sub1 origin/valid_sub1
473 # Test that submodule contents are currently not updated when switching
474 # between commits that change a submodule, but throwing away local changes in
475 # the superproject is allowed.
476 test_submodule_forced_switch () {
478 ######################### Appearing submodule #########################
479 # Switching to a commit letting a submodule appear creates empty dir ...
480 test_expect_success "$command: added submodule creates empty directory" '
482 reset_work_tree_to no_submodule &&
484 cd submodule_update &&
485 git branch -t add_sub1 origin/add_sub1 &&
487 test_superproject_content origin/add_sub1 &&
488 test_dir_is_empty sub1 &&
489 git submodule update --init --recursive &&
490 test_submodule_content sub1 origin/add_sub1
493 # ... and doesn't care if it already exists ...
494 test_expect_success "$command: added submodule leaves existing empty directory alone" '
496 reset_work_tree_to no_submodule &&
498 cd submodule_update &&
499 git branch -t add_sub1 origin/add_sub1 &&
502 test_superproject_content origin/add_sub1 &&
503 test_dir_is_empty sub1 &&
504 git submodule update --init --recursive &&
505 test_submodule_content sub1 origin/add_sub1
508 # ... unless there is an untracked file in its place.
509 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
511 reset_work_tree_to no_submodule &&
513 cd submodule_update &&
514 git branch -t add_sub1 origin/add_sub1 &&
517 test_superproject_content origin/add_sub1 &&
518 test_dir_is_empty sub1
521 # Replacing a tracked file with a submodule produces an empty
523 test_expect_success "$command: replace tracked file with submodule creates empty directory" '
525 reset_work_tree_to replace_sub1_with_file &&
527 cd submodule_update &&
528 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
529 $command replace_file_with_sub1 &&
530 test_superproject_content origin/replace_file_with_sub1 &&
531 test_dir_is_empty sub1 &&
532 git submodule update --init --recursive &&
533 test_submodule_content sub1 origin/replace_file_with_sub1
536 # ... as does removing a directory with tracked files with a
538 test_expect_success "$command: replace directory with submodule" '
540 reset_work_tree_to replace_sub1_with_directory &&
542 cd submodule_update &&
543 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
544 $command replace_directory_with_sub1 &&
545 test_superproject_content origin/replace_directory_with_sub1 &&
546 test_dir_is_empty sub1 &&
547 git submodule update --init --recursive &&
548 test_submodule_content sub1 origin/replace_directory_with_sub1
552 ######################## Disappearing submodule #######################
553 # Removing a submodule doesn't remove its work tree ...
554 test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
556 reset_work_tree_to add_sub1 &&
558 cd submodule_update &&
559 git branch -t remove_sub1 origin/remove_sub1 &&
560 $command remove_sub1 &&
561 test_superproject_content origin/remove_sub1 &&
562 test_submodule_content sub1 origin/add_sub1
565 # ... especially when it contains a .git directory.
566 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
568 reset_work_tree_to add_sub1 &&
570 cd submodule_update &&
571 git branch -t remove_sub1 origin/remove_sub1 &&
572 replace_gitfile_with_git_dir sub1 &&
573 $command remove_sub1 &&
574 test_superproject_content origin/remove_sub1 &&
575 test_git_directory_is_unchanged sub1 &&
576 test_submodule_content sub1 origin/add_sub1
579 # Replacing a submodule with files in a directory must fail as the
580 # submodule work tree isn't removed ...
581 test_expect_failure "$command: replace submodule with a directory must fail" '
583 reset_work_tree_to add_sub1 &&
585 cd submodule_update &&
586 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
587 test_must_fail $command replace_sub1_with_directory &&
588 test_superproject_content origin/add_sub1 &&
589 test_submodule_content sub1 origin/add_sub1
592 # ... especially when it contains a .git directory.
593 test_expect_failure "$command: replace submodule containing a .git directory with a directory must fail" '
595 reset_work_tree_to add_sub1 &&
597 cd submodule_update &&
598 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
599 replace_gitfile_with_git_dir sub1 &&
600 test_must_fail $command replace_sub1_with_directory &&
601 test_superproject_content origin/add_sub1 &&
602 test_git_directory_is_unchanged sub1 &&
603 test_submodule_content sub1 origin/add_sub1
606 # Replacing it with a file must fail as it could throw away any local
607 # work tree changes ...
608 test_expect_failure "$command: replace submodule with a file must fail" '
610 reset_work_tree_to add_sub1 &&
612 cd submodule_update &&
613 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
614 test_must_fail $command replace_sub1_with_file &&
615 test_superproject_content origin/add_sub1 &&
616 test_submodule_content sub1 origin/add_sub1
619 # ... or even destroy unpushed parts of submodule history if that
620 # still uses a .git directory.
621 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
623 reset_work_tree_to add_sub1 &&
625 cd submodule_update &&
626 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
627 replace_gitfile_with_git_dir sub1 &&
628 test_must_fail $command replace_sub1_with_file &&
629 test_superproject_content origin/add_sub1 &&
630 test_git_directory_is_unchanged sub1 &&
631 test_submodule_content sub1 origin/add_sub1
635 ########################## Modified submodule #########################
636 # Updating a submodule sha1 doesn't update the submodule's work tree
637 test_expect_success "$command: modified submodule does not update submodule work tree" '
639 reset_work_tree_to add_sub1 &&
641 cd submodule_update &&
642 git branch -t modify_sub1 origin/modify_sub1 &&
643 $command modify_sub1 &&
644 test_superproject_content origin/modify_sub1 &&
645 test_submodule_content sub1 origin/add_sub1 &&
646 git submodule update &&
647 test_submodule_content sub1 origin/modify_sub1
650 # Updating a submodule to an invalid sha1 doesn't update the
651 # submodule's work tree, subsequent update will fail
652 test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
654 reset_work_tree_to add_sub1 &&
656 cd submodule_update &&
657 git branch -t invalid_sub1 origin/invalid_sub1 &&
658 $command invalid_sub1 &&
659 test_superproject_content origin/invalid_sub1 &&
660 test_submodule_content sub1 origin/add_sub1 &&
661 test_must_fail git submodule update &&
662 test_submodule_content sub1 origin/add_sub1
665 # Updating a submodule from an invalid sha1 doesn't update the
666 # submodule's work tree, subsequent update will succeed
667 test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
669 reset_work_tree_to invalid_sub1 &&
671 cd submodule_update &&
672 git branch -t valid_sub1 origin/valid_sub1 &&
673 $command valid_sub1 &&
674 test_superproject_content origin/valid_sub1 &&
675 test_dir_is_empty sub1 &&
676 git submodule update --init --recursive &&
677 test_submodule_content sub1 origin/valid_sub1