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 sub
1/* &&
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 test_expect_success
"$command: added submodule creates empty directory" '
232 reset_work_tree_to no_submodule &&
234 cd submodule_update &&
235 git branch -t add_sub1 origin/add_sub1 &&
237 test_superproject_content origin/add_sub1 &&
238 test_dir_is_empty sub1 &&
239 git submodule update --init --recursive &&
240 test_submodule_content sub1 origin/add_sub1
243 # ... and doesn't care if it already exists ...
244 test_expect_success
"$command: added submodule leaves existing empty directory alone" '
246 reset_work_tree_to no_submodule &&
248 cd submodule_update &&
250 git branch -t add_sub1 origin/add_sub1 &&
252 test_superproject_content origin/add_sub1 &&
253 test_dir_is_empty sub1 &&
254 git submodule update --init --recursive &&
255 test_submodule_content sub1 origin/add_sub1
258 # ... unless there is an untracked file in its place.
259 test_expect_success
"$command: added submodule doesn't remove untracked unignored file with same name" '
261 reset_work_tree_to no_submodule &&
263 cd submodule_update &&
264 git branch -t add_sub1 origin/add_sub1 &&
266 test_must_fail $command add_sub1 &&
267 test_superproject_content origin/no_submodule &&
268 test_must_be_empty sub1
271 # Replacing a tracked file with a submodule produces an empty
273 test_expect_success
"$command: replace tracked file with submodule creates empty directory" '
275 reset_work_tree_to replace_sub1_with_file &&
277 cd submodule_update &&
278 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
279 $command replace_file_with_sub1 &&
280 test_superproject_content origin/replace_file_with_sub1 &&
281 test_dir_is_empty sub1 &&
282 git submodule update --init --recursive &&
283 test_submodule_content sub1 origin/replace_file_with_sub1
286 # ... as does removing a directory with tracked files with a
288 test_expect_success
"$command: replace directory with submodule" '
290 reset_work_tree_to replace_sub1_with_directory &&
292 cd submodule_update &&
293 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
294 $command replace_directory_with_sub1 &&
295 test_superproject_content origin/replace_directory_with_sub1 &&
296 test_dir_is_empty sub1 &&
297 git submodule update --init --recursive &&
298 test_submodule_content sub1 origin/replace_directory_with_sub1
302 ######################## Disappearing submodule #######################
303 # Removing a submodule doesn't remove its work tree ...
304 test_expect_success
"$command: removed submodule leaves submodule directory and its contents in place" '
306 reset_work_tree_to add_sub1 &&
308 cd submodule_update &&
309 git branch -t remove_sub1 origin/remove_sub1 &&
310 $command remove_sub1 &&
311 test_superproject_content origin/remove_sub1 &&
312 test_submodule_content sub1 origin/add_sub1
315 # ... especially when it contains a .git directory.
316 test_expect_success
"$command: removed submodule leaves submodule containing a .git directory alone" '
318 reset_work_tree_to add_sub1 &&
320 cd submodule_update &&
321 git branch -t remove_sub1 origin/remove_sub1 &&
322 replace_gitfile_with_git_dir sub1 &&
323 $command remove_sub1 &&
324 test_superproject_content origin/remove_sub1 &&
325 test_git_directory_is_unchanged sub1 &&
326 test_submodule_content sub1 origin/add_sub1
329 # Replacing a submodule with files in a directory must fail as the
330 # submodule work tree isn't removed ...
331 test_expect_success
"$command: replace submodule with a directory must fail" '
333 reset_work_tree_to add_sub1 &&
335 cd submodule_update &&
336 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
337 test_must_fail $command replace_sub1_with_directory &&
338 test_superproject_content origin/add_sub1 &&
339 test_submodule_content sub1 origin/add_sub1
342 # ... especially when it contains a .git directory.
343 test_expect_success
"$command: replace submodule containing a .git directory with a directory must fail" '
345 reset_work_tree_to add_sub1 &&
347 cd submodule_update &&
348 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
349 replace_gitfile_with_git_dir sub1 &&
350 test_must_fail $command replace_sub1_with_directory &&
351 test_superproject_content origin/add_sub1 &&
352 test_git_directory_is_unchanged sub1 &&
353 test_submodule_content sub1 origin/add_sub1
356 # Replacing it with a file must fail as it could throw away any local
357 # work tree changes ...
358 test_expect_failure
"$command: replace submodule with a file must fail" '
360 reset_work_tree_to add_sub1 &&
362 cd submodule_update &&
363 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
364 test_must_fail $command replace_sub1_with_file &&
365 test_superproject_content origin/add_sub1 &&
366 test_submodule_content sub1 origin/add_sub1
369 # ... or even destroy unpushed parts of submodule history if that
370 # still uses a .git directory.
371 test_expect_failure
"$command: replace submodule containing a .git directory with a file must fail" '
373 reset_work_tree_to add_sub1 &&
375 cd submodule_update &&
376 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
377 replace_gitfile_with_git_dir sub1 &&
378 test_must_fail $command replace_sub1_with_file &&
379 test_superproject_content origin/add_sub1 &&
380 test_git_directory_is_unchanged sub1 &&
381 test_submodule_content sub1 origin/add_sub1
385 ########################## Modified submodule #########################
386 # Updating a submodule sha1 doesn't update the submodule's work tree
387 test_expect_success
"$command: modified submodule does not update submodule work tree" '
389 reset_work_tree_to add_sub1 &&
391 cd submodule_update &&
392 git branch -t modify_sub1 origin/modify_sub1 &&
393 $command modify_sub1 &&
394 test_superproject_content origin/modify_sub1 &&
395 test_submodule_content sub1 origin/add_sub1 &&
396 git submodule update &&
397 test_submodule_content sub1 origin/modify_sub1
401 # Updating a submodule to an invalid sha1 doesn't update the
402 # submodule's work tree, subsequent update will fail
403 test_expect_success
"$command: modified submodule does not update submodule work tree to invalid commit" '
405 reset_work_tree_to add_sub1 &&
407 cd submodule_update &&
408 git branch -t invalid_sub1 origin/invalid_sub1 &&
409 $command invalid_sub1 &&
410 test_superproject_content origin/invalid_sub1 &&
411 test_submodule_content sub1 origin/add_sub1 &&
412 test_must_fail git submodule update &&
413 test_submodule_content sub1 origin/add_sub1
416 # Updating a submodule from an invalid sha1 doesn't update the
417 # submodule's work tree, subsequent update will succeed
418 test_expect_success
"$command: modified submodule does not update submodule work tree from invalid commit" '
420 reset_work_tree_to invalid_sub1 &&
422 cd submodule_update &&
423 git branch -t valid_sub1 origin/valid_sub1 &&
424 $command valid_sub1 &&
425 test_superproject_content origin/valid_sub1 &&
426 test_dir_is_empty sub1 &&
427 git submodule update --init --recursive &&
428 test_submodule_content sub1 origin/valid_sub1
433 # Test that submodule contents are currently not updated when switching
434 # between commits that change a submodule, but throwing away local changes in
435 # the superproject is allowed.
436 test_submodule_forced_switch
() {
438 ######################### Appearing submodule #########################
439 # Switching to a commit letting a submodule appear creates empty dir ...
440 test_expect_success
"$command: added submodule creates empty directory" '
442 reset_work_tree_to no_submodule &&
444 cd submodule_update &&
445 git branch -t add_sub1 origin/add_sub1 &&
447 test_superproject_content origin/add_sub1 &&
448 test_dir_is_empty sub1 &&
449 git submodule update --init --recursive &&
450 test_submodule_content sub1 origin/add_sub1
453 # ... and doesn't care if it already exists ...
454 test_expect_success
"$command: added submodule leaves existing empty directory alone" '
456 reset_work_tree_to no_submodule &&
458 cd submodule_update &&
459 git branch -t add_sub1 origin/add_sub1 &&
462 test_superproject_content origin/add_sub1 &&
463 test_dir_is_empty sub1 &&
464 git submodule update --init --recursive &&
465 test_submodule_content sub1 origin/add_sub1
468 # ... unless there is an untracked file in its place.
469 test_expect_success
"$command: added submodule does remove untracked unignored file with same name when forced" '
471 reset_work_tree_to no_submodule &&
473 cd submodule_update &&
474 git branch -t add_sub1 origin/add_sub1 &&
477 test_superproject_content origin/add_sub1 &&
478 test_dir_is_empty sub1
481 # Replacing a tracked file with a submodule produces an empty
483 test_expect_success
"$command: replace tracked file with submodule creates empty directory" '
485 reset_work_tree_to replace_sub1_with_file &&
487 cd submodule_update &&
488 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
489 $command replace_file_with_sub1 &&
490 test_superproject_content origin/replace_file_with_sub1 &&
491 test_dir_is_empty sub1 &&
492 git submodule update --init --recursive &&
493 test_submodule_content sub1 origin/replace_file_with_sub1
496 # ... as does removing a directory with tracked files with a
498 test_expect_success
"$command: replace directory with submodule" '
500 reset_work_tree_to replace_sub1_with_directory &&
502 cd submodule_update &&
503 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
504 $command replace_directory_with_sub1 &&
505 test_superproject_content origin/replace_directory_with_sub1 &&
506 test_dir_is_empty sub1 &&
507 git submodule update --init --recursive &&
508 test_submodule_content sub1 origin/replace_directory_with_sub1
512 ######################## Disappearing submodule #######################
513 # Removing a submodule doesn't remove its work tree ...
514 test_expect_success
"$command: removed submodule leaves submodule directory and its contents in place" '
516 reset_work_tree_to add_sub1 &&
518 cd submodule_update &&
519 git branch -t remove_sub1 origin/remove_sub1 &&
520 $command remove_sub1 &&
521 test_superproject_content origin/remove_sub1 &&
522 test_submodule_content sub1 origin/add_sub1
525 # ... especially when it contains a .git directory.
526 test_expect_success
"$command: removed submodule leaves submodule containing a .git directory alone" '
528 reset_work_tree_to add_sub1 &&
530 cd submodule_update &&
531 git branch -t remove_sub1 origin/remove_sub1 &&
532 replace_gitfile_with_git_dir sub1 &&
533 $command remove_sub1 &&
534 test_superproject_content origin/remove_sub1 &&
535 test_git_directory_is_unchanged sub1 &&
536 test_submodule_content sub1 origin/add_sub1
539 # Replacing a submodule with files in a directory must fail as the
540 # submodule work tree isn't removed ...
541 test_expect_failure
"$command: replace submodule with a directory must fail" '
543 reset_work_tree_to add_sub1 &&
545 cd submodule_update &&
546 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
547 test_must_fail $command replace_sub1_with_directory &&
548 test_superproject_content origin/add_sub1 &&
549 test_submodule_content sub1 origin/add_sub1
552 # ... especially when it contains a .git directory.
553 test_expect_failure
"$command: replace submodule containing a .git directory with a directory must fail" '
555 reset_work_tree_to add_sub1 &&
557 cd submodule_update &&
558 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
559 replace_gitfile_with_git_dir sub1 &&
560 test_must_fail $command replace_sub1_with_directory &&
561 test_superproject_content origin/add_sub1 &&
562 test_git_directory_is_unchanged sub1 &&
563 test_submodule_content sub1 origin/add_sub1
566 # Replacing it with a file must fail as it could throw away any local
567 # work tree changes ...
568 test_expect_failure
"$command: replace submodule with a file must fail" '
570 reset_work_tree_to add_sub1 &&
572 cd submodule_update &&
573 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
574 test_must_fail $command replace_sub1_with_file &&
575 test_superproject_content origin/add_sub1 &&
576 test_submodule_content sub1 origin/add_sub1
579 # ... or even destroy unpushed parts of submodule history if that
580 # still uses a .git directory.
581 test_expect_failure
"$command: replace submodule containing a .git directory with a file must fail" '
583 reset_work_tree_to add_sub1 &&
585 cd submodule_update &&
586 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
587 replace_gitfile_with_git_dir sub1 &&
588 test_must_fail $command replace_sub1_with_file &&
589 test_superproject_content origin/add_sub1 &&
590 test_git_directory_is_unchanged sub1 &&
591 test_submodule_content sub1 origin/add_sub1
595 ########################## Modified submodule #########################
596 # Updating a submodule sha1 doesn't update the submodule's work tree
597 test_expect_success
"$command: modified submodule does not update submodule work tree" '
599 reset_work_tree_to add_sub1 &&
601 cd submodule_update &&
602 git branch -t modify_sub1 origin/modify_sub1 &&
603 $command modify_sub1 &&
604 test_superproject_content origin/modify_sub1 &&
605 test_submodule_content sub1 origin/add_sub1 &&
606 git submodule update &&
607 test_submodule_content sub1 origin/modify_sub1
610 # Updating a submodule to an invalid sha1 doesn't update the
611 # submodule's work tree, subsequent update will fail
612 test_expect_success
"$command: modified submodule does not update submodule work tree to invalid commit" '
614 reset_work_tree_to add_sub1 &&
616 cd submodule_update &&
617 git branch -t invalid_sub1 origin/invalid_sub1 &&
618 $command invalid_sub1 &&
619 test_superproject_content origin/invalid_sub1 &&
620 test_submodule_content sub1 origin/add_sub1 &&
621 test_must_fail git submodule update &&
622 test_submodule_content sub1 origin/add_sub1
625 # Updating a submodule from an invalid sha1 doesn't update the
626 # submodule's work tree, subsequent update will succeed
627 test_expect_success
"$command: modified submodule does not update submodule work tree from invalid commit" '
629 reset_work_tree_to invalid_sub1 &&
631 cd submodule_update &&
632 git branch -t valid_sub1 origin/valid_sub1 &&
633 $command valid_sub1 &&
634 test_superproject_content origin/valid_sub1 &&
635 test_dir_is_empty sub1 &&
636 git submodule update --init --recursive &&
637 test_submodule_content sub1 origin/valid_sub1