Merge branch 'rs/wt-status-cleanup'
[alt-git.git] / t / lib-submodule-update.sh
blob2d26f86800906ab1783edf10e7196adadd5f4af7
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_with_args () {
785 cmd_args="$1"
786 command="git $cmd_args --recurse-submodules"
787 RESULTDS=success
788 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
789 then
790 RESULTDS=failure
792 RESULTOI=success
793 if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
794 then
795 RESULTOI=failure
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" '
800 prolog &&
801 reset_work_tree_to_interested no_submodule &&
803 cd submodule_update &&
804 git branch -t add_sub1 origin/add_sub1 &&
805 $command 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" '
812 prolog &&
813 reset_work_tree_to_interested no_submodule &&
815 cd submodule_update &&
816 mkdir sub1 &&
817 git branch -t add_sub1 origin/add_sub1 &&
818 $command 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" '
825 prolog &&
826 reset_work_tree_to_interested no_submodule &&
828 cd submodule_update &&
829 git branch -t add_sub1 origin/add_sub1 &&
830 : >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" &&
839 prolog &&
840 reset_work_tree_to_interested no_submodule &&
842 cd submodule_update &&
843 git branch -t add_sub1 origin/add_sub1 &&
844 : >sub1 &&
845 echo sub1 >.git/info/exclude
846 $command add_sub1 &&
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" '
853 prolog &&
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" '
865 prolog &&
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" '
879 prolog &&
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 &&
886 ! test -e sub1
889 # ... absorbing a .git directory along the way.
890 test_expect_success "$command: removed submodule absorbs submodules .git directory" '
891 prolog &&
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 &&
900 ! test -e 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" '
907 prolog &&
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" '
919 prolog &&
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" '
934 prolog &&
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 &&
941 test -f sub1
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" '
947 prolog &&
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" &&
962 prolog &&
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 &&
967 : >sub1/ignored &&
968 $command replace_sub1_with_file &&
969 test_superproject_content origin/replace_sub1_with_file &&
970 test -f sub1
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" '
977 prolog &&
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" '
989 prolog &&
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" '
1003 prolog &&
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" '
1016 prolog &&
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 () {
1033 cmd_args="$1"
1034 command="git $cmd_args --recurse-submodules"
1035 RESULT=success
1036 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
1037 then
1038 RESULT=failure
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" '
1043 prolog &&
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" '
1055 prolog &&
1056 reset_work_tree_to_interested no_submodule &&
1058 cd submodule_update &&
1059 git branch -t add_sub1 origin/add_sub1 &&
1060 mkdir 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" '
1068 prolog &&
1069 reset_work_tree_to_interested no_submodule &&
1071 cd submodule_update &&
1072 git branch -t add_sub1 origin/add_sub1 &&
1073 >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" '
1081 prolog &&
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
1092 # submodule.
1093 test_expect_success "$command: replace directory with submodule" '
1094 prolog &&
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" '
1108 prolog &&
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 &&
1115 ! test -e sub1
1118 # ... especially when it contains a .git directory.
1119 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
1120 prolog &&
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 &&
1130 ! test -e sub1
1133 # Replacing a submodule with files in a directory ...
1134 test_expect_success "$command: replace submodule with a directory" '
1135 prolog &&
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" '
1146 prolog &&
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" '
1161 prolog &&
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" '
1173 prolog &&
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 &&
1178 : >sub1/expect &&
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" '
1186 prolog &&
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" '
1201 prolog &&
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" '
1214 prolog &&
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" '
1226 prolog &&
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" '
1241 prolog &&
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