branch: add test for -m renaming multiple config sections
[git.git] / t / lib-submodule-update.sh
blob58bd4aeb2c52c67831a1dced2ef6b09755d417d3
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)
19 # ----O
20 # / ^
21 # / remove_sub1
22 # /
23 # add_sub1 /-------O---------O--------O modify_sub1_recursively
24 # | / ^ add_nested_sub
25 # | / modify_sub1
26 # v/
27 # O------O-----------O---------O
28 # ^ \ ^ replace_directory_with_sub1
29 # | \ replace_sub1_with_directory
30 # no_submodule \
31 # --------O---------O
32 # \ ^ replace_file_with_sub1
33 # \ replace_sub1_with_file
34 # \
35 # ----O---------O
36 # ^ valid_sub1
37 # invalid_sub1
40 create_lib_submodule_repo () {
41 git init submodule_update_sub1 &&
43 cd submodule_update_sub1 &&
44 echo "expect" >>.gitignore &&
45 echo "actual" >>.gitignore &&
46 echo "x" >file1 &&
47 echo "y" >file2 &&
48 git add .gitignore file1 file2 &&
49 git commit -m "Base inside first submodule" &&
50 git branch "no_submodule"
51 ) &&
52 git init submodule_update_sub2 &&
54 cd submodule_update_sub2
55 echo "expect" >>.gitignore &&
56 echo "actual" >>.gitignore &&
57 echo "x" >file1 &&
58 echo "y" >file2 &&
59 git add .gitignore file1 file2 &&
60 git commit -m "nested submodule base" &&
61 git branch "no_submodule"
62 ) &&
63 git init submodule_update_repo &&
65 cd submodule_update_repo &&
66 echo "expect" >>.gitignore &&
67 echo "actual" >>.gitignore &&
68 echo "x" >file1 &&
69 echo "y" >file2 &&
70 git add .gitignore file1 file2 &&
71 git commit -m "Base" &&
72 git branch "no_submodule" &&
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 &&
83 git revert HEAD &&
85 git checkout -b modify_sub1 add_sub1 &&
86 git submodule update &&
88 cd sub1 &&
89 git fetch &&
90 git checkout -b "modifications" &&
91 echo "z" >file2 &&
92 echo "x" >file3 &&
93 git add file2 file3 &&
94 git commit -m "modified file2 and added file3" &&
95 git push origin modifications
96 ) &&
97 git add sub1 &&
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" &&
104 git add sub1 &&
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" &&
114 git add sub1 &&
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 &&
124 rm sub1/.git* &&
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 &&
130 git revert HEAD &&
132 git checkout -b replace_sub1_with_file add_sub1 &&
133 git rm sub1 &&
134 echo "content" >sub1 &&
135 git add sub1 &&
136 git commit -m "Replace sub1 with file" &&
138 git checkout -b replace_file_with_sub1 &&
139 git revert HEAD &&
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 &&
145 git revert HEAD &&
147 git checkout master
151 # Helper function to replace gitfile with .git directory
152 replace_gitfile_with_git_dir () {
154 cd "$1" &&
155 git_dir="$(git rev-parse --git-dir)" &&
156 rm -f .git &&
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
177 ) &&
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" &&
188 if test -f sub1/.git
189 then
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.
198 prolog () {
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 &&
212 rm -rf sub1 &&
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}")
219 then
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
229 then
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
233 fi &&
234 if ! test -d submodule_update/.git/modules/sub1/modules/sub2
235 then
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
239 fi &&
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"
260 then
261 cd "$2"
262 shift; shift;
264 if test $# != 2
265 then
266 echo "test_submodule_content needs two arguments"
267 return 1
268 fi &&
269 submodule="$1" &&
270 commit="$2" &&
271 test -d "$submodule"/ &&
272 if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
273 then
274 echo "Submodule $submodule is not populated"
275 return 1
276 fi &&
277 sha1=$(git rev-parse --verify "$commit:$submodule") &&
278 if test -z "$sha1"
279 then
280 echo "Couldn't retrieve SHA-1 of $submodule for $commit"
281 return 1
282 fi &&
284 cd "$submodule" &&
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
294 # - New 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 () {
312 command="$1"
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
316 then
317 # Restoring stash fails to restore submodule index entry
318 RESULT="failure"
319 else
320 RESULT="success"
322 test_expect_$RESULT "$command: added submodule creates empty directory" '
323 prolog &&
324 reset_work_tree_to no_submodule &&
326 cd submodule_update &&
327 git branch -t add_sub1 origin/add_sub1 &&
328 $command 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" '
337 prolog &&
338 reset_work_tree_to no_submodule &&
340 cd submodule_update &&
341 mkdir sub1 &&
342 git branch -t add_sub1 origin/add_sub1 &&
343 $command 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" '
352 prolog &&
353 reset_work_tree_to no_submodule &&
355 cd submodule_update &&
356 git branch -t add_sub1 origin/add_sub1 &&
357 >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
364 # directory ...
365 test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
366 prolog &&
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
379 # submodule.
380 if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
381 then
382 # Non fast-forward merges fail with "Directory sub1 doesn't
383 # exist. sub1" because the empty submodule directory is not
384 # created
385 RESULT="failure"
386 else
387 RESULT="success"
389 test_expect_$RESULT "$command: replace directory with submodule" '
390 prolog &&
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
406 then
407 RESULT="failure"
408 else
409 RESULT="success"
411 test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
412 prolog &&
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" '
424 prolog &&
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
439 then
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.
443 RESULT="failure"
444 else
445 RESULT="success"
447 test_expect_$RESULT "$command: replace submodule with a directory must fail" '
448 prolog &&
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" '
460 prolog &&
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" '
475 prolog &&
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" '
488 prolog &&
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
504 then
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."
508 RESULT="failure"
509 else
510 RESULT="success"
512 test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
513 prolog &&
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" '
529 prolog &&
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" '
544 prolog &&
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 () {
562 command="$1"
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" '
566 prolog &&
567 reset_work_tree_to no_submodule &&
569 cd submodule_update &&
570 git branch -t add_sub1 origin/add_sub1 &&
571 $command 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" '
580 prolog &&
581 reset_work_tree_to no_submodule &&
583 cd submodule_update &&
584 git branch -t add_sub1 origin/add_sub1 &&
585 mkdir sub1 &&
586 $command 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" '
595 prolog &&
596 reset_work_tree_to no_submodule &&
598 cd submodule_update &&
599 git branch -t add_sub1 origin/add_sub1 &&
600 >sub1 &&
601 $command 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
607 # directory ...
608 test_expect_success "$command: replace tracked file with submodule creates empty directory" '
609 prolog &&
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
622 # submodule.
623 test_expect_success "$command: replace directory with submodule" '
624 prolog &&
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" '
640 prolog &&
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" '
652 prolog &&
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" '
667 prolog &&
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" '
679 prolog &&
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" '
694 prolog &&
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" '
707 prolog &&
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" '
723 prolog &&
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" '
738 prolog &&
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" '
753 prolog &&
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
771 # in the submodule)
772 # - Updated submodule
773 # - New 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
780 # New test cases
781 # - Removing a submodule with a git directory absorbs the submodules
782 # git directory first into the superproject.
784 test_submodule_switch_recursing () {
785 command="$1"
786 RESULTDS=success
787 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
788 then
789 RESULTDS=failure
791 RESULTOI=success
792 if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
793 then
794 RESULTOI=failure
796 ######################### Appearing submodule #########################
797 # Switching to a commit letting a submodule appear checks it out ...
798 test_expect_success "$command: added submodule is checked out" '
799 prolog &&
800 reset_work_tree_to_interested no_submodule &&
802 cd submodule_update &&
803 git branch -t add_sub1 origin/add_sub1 &&
804 $command add_sub1 &&
805 test_superproject_content origin/add_sub1 &&
806 test_submodule_content sub1 origin/add_sub1
809 # ... ignoring an empty existing directory ...
810 test_expect_success "$command: added submodule is checked out in empty dir" '
811 prolog &&
812 reset_work_tree_to_interested no_submodule &&
814 cd submodule_update &&
815 mkdir sub1 &&
816 git branch -t add_sub1 origin/add_sub1 &&
817 $command add_sub1 &&
818 test_superproject_content origin/add_sub1 &&
819 test_submodule_content sub1 origin/add_sub1
822 # ... unless there is an untracked file in its place.
823 test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
824 prolog &&
825 reset_work_tree_to_interested no_submodule &&
827 cd submodule_update &&
828 git branch -t add_sub1 origin/add_sub1 &&
829 : >sub1 &&
830 test_must_fail $command add_sub1 &&
831 test_superproject_content origin/no_submodule &&
832 test_must_be_empty sub1
835 # ... but an ignored file is fine.
836 test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
837 test_when_finished "rm submodule_update/.git/info/exclude" &&
838 prolog &&
839 reset_work_tree_to_interested no_submodule &&
841 cd submodule_update &&
842 git branch -t add_sub1 origin/add_sub1 &&
843 : >sub1 &&
844 echo sub1 >.git/info/exclude
845 $command add_sub1 &&
846 test_superproject_content origin/add_sub1 &&
847 test_submodule_content sub1 origin/add_sub1
850 # Replacing a tracked file with a submodule produces a checked out submodule
851 test_expect_success "$command: replace tracked file with submodule checks out submodule" '
852 prolog &&
853 reset_work_tree_to_interested replace_sub1_with_file &&
855 cd submodule_update &&
856 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
857 $command replace_file_with_sub1 &&
858 test_superproject_content origin/replace_file_with_sub1 &&
859 test_submodule_content sub1 origin/replace_file_with_sub1
862 # ... as does removing a directory with tracked files with a submodule.
863 test_expect_success "$command: replace directory with submodule" '
864 prolog &&
865 reset_work_tree_to_interested replace_sub1_with_directory &&
867 cd submodule_update &&
868 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
869 $command replace_directory_with_sub1 &&
870 test_superproject_content origin/replace_directory_with_sub1 &&
871 test_submodule_content sub1 origin/replace_directory_with_sub1
875 ######################## Disappearing submodule #######################
876 # Removing a submodule removes its work tree ...
877 test_expect_success "$command: removed submodule removes submodules working tree" '
878 prolog &&
879 reset_work_tree_to_interested add_sub1 &&
881 cd submodule_update &&
882 git branch -t remove_sub1 origin/remove_sub1 &&
883 $command remove_sub1 &&
884 test_superproject_content origin/remove_sub1 &&
885 ! test -e sub1
888 # ... absorbing a .git directory along the way.
889 test_expect_success "$command: removed submodule absorbs submodules .git directory" '
890 prolog &&
891 reset_work_tree_to_interested add_sub1 &&
893 cd submodule_update &&
894 git branch -t remove_sub1 origin/remove_sub1 &&
895 replace_gitfile_with_git_dir sub1 &&
896 rm -rf .git/modules &&
897 $command remove_sub1 &&
898 test_superproject_content origin/remove_sub1 &&
899 ! test -e sub1 &&
900 test_git_directory_exists sub1
903 # Replacing a submodule with files in a directory must succeeds
904 # when the submodule is clean
905 test_expect_$RESULTDS "$command: replace submodule with a directory" '
906 prolog &&
907 reset_work_tree_to_interested add_sub1 &&
909 cd submodule_update &&
910 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
911 $command replace_sub1_with_directory &&
912 test_superproject_content origin/replace_sub1_with_directory &&
913 test_submodule_content sub1 origin/replace_sub1_with_directory
916 # ... absorbing a .git directory.
917 test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
918 prolog &&
919 reset_work_tree_to_interested add_sub1 &&
921 cd submodule_update &&
922 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
923 replace_gitfile_with_git_dir sub1 &&
924 rm -rf .git/modules &&
925 $command replace_sub1_with_directory &&
926 test_superproject_content origin/replace_sub1_with_directory &&
927 test_git_directory_exists sub1
931 # Replacing it with a file ...
932 test_expect_success "$command: replace submodule with a file" '
933 prolog &&
934 reset_work_tree_to_interested add_sub1 &&
936 cd submodule_update &&
937 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
938 $command replace_sub1_with_file &&
939 test_superproject_content origin/replace_sub1_with_file &&
940 test -f sub1
944 # ... must check its local work tree for untracked files
945 test_expect_$RESULTDS "$command: replace submodule with a file must fail with untracked files" '
946 prolog &&
947 reset_work_tree_to_interested add_sub1 &&
949 cd submodule_update &&
950 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
951 : >sub1/untrackedfile &&
952 test_must_fail $command replace_sub1_with_file &&
953 test_superproject_content origin/add_sub1 &&
954 test_submodule_content sub1 origin/add_sub1
958 # ... and ignored files are ignored
959 test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
960 test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
961 prolog &&
962 reset_work_tree_to_interested add_sub1 &&
964 cd submodule_update &&
965 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
966 : >sub1/ignored &&
967 $command replace_sub1_with_file &&
968 test_superproject_content origin/replace_sub1_with_file &&
969 test -f sub1
973 ########################## Modified submodule #########################
974 # Updating a submodule sha1 updates the submodule's work tree
975 test_expect_success "$command: modified submodule updates submodule work tree" '
976 prolog &&
977 reset_work_tree_to_interested add_sub1 &&
979 cd submodule_update &&
980 git branch -t modify_sub1 origin/modify_sub1 &&
981 $command modify_sub1 &&
982 test_superproject_content origin/modify_sub1 &&
983 test_submodule_content sub1 origin/modify_sub1
987 # Updating a submodule to an invalid sha1 doesn't update the
988 # superproject nor the submodule's work tree.
989 test_expect_success "$command: updating to a missing submodule commit fails" '
990 prolog &&
991 reset_work_tree_to_interested add_sub1 &&
993 cd submodule_update &&
994 git branch -t invalid_sub1 origin/invalid_sub1 &&
995 test_must_fail $command invalid_sub1 &&
996 test_superproject_content origin/add_sub1 &&
997 test_submodule_content sub1 origin/add_sub1
1001 # recursing deeper than one level doesn't work yet.
1002 test_expect_success "$command: modified submodule updates submodule recursively" '
1003 prolog &&
1004 reset_work_tree_to_interested add_nested_sub &&
1006 cd submodule_update &&
1007 git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
1008 $command modify_sub1_recursively &&
1009 test_superproject_content origin/modify_sub1_recursively &&
1010 test_submodule_content sub1 origin/modify_sub1_recursively &&
1011 test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
1016 # Test that submodule contents are updated when switching between commits
1017 # that change a submodule, but throwing away local changes in
1018 # the superproject as well as the submodule is allowed.
1019 test_submodule_forced_switch_recursing () {
1020 command="$1"
1021 RESULT=success
1022 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
1023 then
1024 RESULT=failure
1026 ######################### Appearing submodule #########################
1027 # Switching to a commit letting a submodule appear creates empty dir ...
1028 test_expect_success "$command: added submodule is checked out" '
1029 prolog &&
1030 reset_work_tree_to_interested no_submodule &&
1032 cd submodule_update &&
1033 git branch -t add_sub1 origin/add_sub1 &&
1034 $command add_sub1 &&
1035 test_superproject_content origin/add_sub1 &&
1036 test_submodule_content sub1 origin/add_sub1
1039 # ... and doesn't care if it already exists ...
1040 test_expect_success "$command: added submodule ignores empty directory" '
1041 prolog &&
1042 reset_work_tree_to_interested no_submodule &&
1044 cd submodule_update &&
1045 git branch -t add_sub1 origin/add_sub1 &&
1046 mkdir sub1 &&
1047 $command add_sub1 &&
1048 test_superproject_content origin/add_sub1 &&
1049 test_submodule_content sub1 origin/add_sub1
1052 # ... not caring about an untracked file either
1053 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
1054 prolog &&
1055 reset_work_tree_to_interested no_submodule &&
1057 cd submodule_update &&
1058 git branch -t add_sub1 origin/add_sub1 &&
1059 >sub1 &&
1060 $command add_sub1 &&
1061 test_superproject_content origin/add_sub1 &&
1062 test_submodule_content sub1 origin/add_sub1
1065 # Replacing a tracked file with a submodule checks out the submodule
1066 test_expect_success "$command: replace tracked file with submodule populates the submodule" '
1067 prolog &&
1068 reset_work_tree_to_interested replace_sub1_with_file &&
1070 cd submodule_update &&
1071 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
1072 $command replace_file_with_sub1 &&
1073 test_superproject_content origin/replace_file_with_sub1 &&
1074 test_submodule_content sub1 origin/replace_file_with_sub1
1077 # ... as does removing a directory with tracked files with a
1078 # submodule.
1079 test_expect_success "$command: replace directory with submodule" '
1080 prolog &&
1081 reset_work_tree_to_interested replace_sub1_with_directory &&
1083 cd submodule_update &&
1084 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
1085 $command replace_directory_with_sub1 &&
1086 test_superproject_content origin/replace_directory_with_sub1 &&
1087 test_submodule_content sub1 origin/replace_directory_with_sub1
1091 ######################## Disappearing submodule #######################
1092 # Removing a submodule doesn't remove its work tree ...
1093 test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
1094 prolog &&
1095 reset_work_tree_to_interested add_sub1 &&
1097 cd submodule_update &&
1098 git branch -t remove_sub1 origin/remove_sub1 &&
1099 $command remove_sub1 &&
1100 test_superproject_content origin/remove_sub1 &&
1101 ! test -e sub1
1104 # ... especially when it contains a .git directory.
1105 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
1106 prolog &&
1107 reset_work_tree_to_interested add_sub1 &&
1109 cd submodule_update &&
1110 git branch -t remove_sub1 origin/remove_sub1 &&
1111 replace_gitfile_with_git_dir sub1 &&
1112 rm -rf .git/modules/sub1 &&
1113 $command remove_sub1 &&
1114 test_superproject_content origin/remove_sub1 &&
1115 test_git_directory_exists sub1 &&
1116 ! test -e sub1
1119 # Replacing a submodule with files in a directory ...
1120 test_expect_success "$command: replace submodule with a directory" '
1121 prolog &&
1122 reset_work_tree_to_interested add_sub1 &&
1124 cd submodule_update &&
1125 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1126 $command replace_sub1_with_directory &&
1127 test_superproject_content origin/replace_sub1_with_directory
1130 # ... absorbing a .git directory.
1131 test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
1132 prolog &&
1133 reset_work_tree_to_interested add_sub1 &&
1135 cd submodule_update &&
1136 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1137 replace_gitfile_with_git_dir sub1 &&
1138 rm -rf .git/modules/sub1 &&
1139 $command replace_sub1_with_directory &&
1140 test_superproject_content origin/replace_sub1_with_directory &&
1141 test_submodule_content sub1 origin/modify_sub1
1142 test_git_directory_exists sub1
1145 # Replacing it with a file
1146 test_expect_success "$command: replace submodule with a file" '
1147 prolog &&
1148 reset_work_tree_to_interested add_sub1 &&
1150 cd submodule_update &&
1151 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1152 $command replace_sub1_with_file &&
1153 test_superproject_content origin/replace_sub1_with_file
1157 # ... even if the submodule contains ignored files
1158 test_expect_success "$command: replace submodule with a file ignoring ignored files" '
1159 prolog &&
1160 reset_work_tree_to_interested add_sub1 &&
1162 cd submodule_update &&
1163 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1164 : >sub1/expect &&
1165 $command replace_sub1_with_file &&
1166 test_superproject_content origin/replace_sub1_with_file
1170 # ... but stops for untracked files that would be lost
1171 test_expect_$RESULT "$command: replace submodule with a file stops for untracked files" '
1172 prolog &&
1173 reset_work_tree_to_interested add_sub1 &&
1175 cd submodule_update &&
1176 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1177 : >sub1/untracked_file &&
1178 test_must_fail $command replace_sub1_with_file &&
1179 test_superproject_content origin/add_sub1 &&
1180 test -f sub1/untracked_file
1184 ########################## Modified submodule #########################
1185 # Updating a submodule sha1 updates the submodule's work tree
1186 test_expect_success "$command: modified submodule updates submodule work tree" '
1187 prolog &&
1188 reset_work_tree_to_interested add_sub1 &&
1190 cd submodule_update &&
1191 git branch -t modify_sub1 origin/modify_sub1 &&
1192 $command modify_sub1 &&
1193 test_superproject_content origin/modify_sub1 &&
1194 test_submodule_content sub1 origin/modify_sub1
1197 # Updating a submodule to an invalid sha1 doesn't update the
1198 # submodule's work tree, subsequent update will fail
1199 test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
1200 prolog &&
1201 reset_work_tree_to_interested add_sub1 &&
1203 cd submodule_update &&
1204 git branch -t invalid_sub1 origin/invalid_sub1 &&
1205 test_must_fail $command invalid_sub1 &&
1206 test_superproject_content origin/add_sub1 &&
1207 test_submodule_content sub1 origin/add_sub1
1210 # Updating a submodule from an invalid sha1 updates
1211 test_expect_success "$command: modified submodule does update submodule work tree from invalid commit" '
1212 prolog &&
1213 reset_work_tree_to_interested invalid_sub1 &&
1215 cd submodule_update &&
1216 git branch -t valid_sub1 origin/valid_sub1 &&
1217 $command valid_sub1 &&
1218 test_superproject_content origin/valid_sub1 &&
1219 test_submodule_content sub1 origin/valid_sub1
1223 # Old versions of Git were buggy writing the .git link file
1224 # (e.g. before f8eaa0ba98b and then moving the superproject repo
1225 # whose submodules contained absolute paths)
1226 test_expect_success "$command: updating submodules fixes .git links" '
1227 prolog &&
1228 reset_work_tree_to_interested add_sub1 &&
1230 cd submodule_update &&
1231 git branch -t modify_sub1 origin/modify_sub1 &&
1232 echo "gitdir: bogus/path" >sub1/.git &&
1233 $command modify_sub1 &&
1234 test_superproject_content origin/modify_sub1 &&
1235 test_submodule_content sub1 origin/modify_sub1