test: fix for COLUMNS and bash 5
[git/debian.git] / t / lib-submodule-update.sh
blob4b714e93083f128ba7130b3a6df8da887cfa1184
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 $(test_oid numeric) sub1 &&
143 git commit -m "Invalid sub1 commit" &&
144 git checkout -b valid_sub1 &&
145 git revert HEAD &&
147 git checkout "${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-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 # core.worktree is unset for sub2 as it is not checked out
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 # The first argument of the callback function will be the name of the submodule.
307 # Removing a submodule containing a .git directory must fail even when forced
308 # to protect the history! If we are testing this case, the second argument of
309 # the callback function will be 'test_must_fail', else it will be the empty
310 # string.
313 # Internal function; use test_submodule_switch_func(), test_submodule_switch(),
314 # or test_submodule_forced_switch() instead.
315 test_submodule_switch_common () {
316 command="$1"
317 ######################### Appearing submodule #########################
318 # Switching to a commit letting a submodule appear creates empty dir ...
319 test_expect_success "$command: added submodule creates empty directory" '
320 prolog &&
321 reset_work_tree_to no_submodule &&
323 cd submodule_update &&
324 git branch -t add_sub1 origin/add_sub1 &&
325 $command add_sub1 &&
326 test_superproject_content origin/add_sub1 &&
327 test_dir_is_empty sub1 &&
328 git submodule update --init --recursive &&
329 test_submodule_content sub1 origin/add_sub1
332 # ... and doesn't care if it already exists.
333 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
334 then
335 # Restoring stash fails to restore submodule index entry
336 RESULT="failure"
337 else
338 RESULT="success"
340 test_expect_$RESULT "$command: added submodule leaves existing empty directory alone" '
341 prolog &&
342 reset_work_tree_to no_submodule &&
344 cd submodule_update &&
345 mkdir sub1 &&
346 git branch -t add_sub1 origin/add_sub1 &&
347 $command add_sub1 &&
348 test_superproject_content origin/add_sub1 &&
349 test_dir_is_empty sub1 &&
350 git submodule update --init --recursive &&
351 test_submodule_content sub1 origin/add_sub1
354 # Replacing a tracked file with a submodule produces an empty
355 # directory ...
356 test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
357 prolog &&
358 reset_work_tree_to replace_sub1_with_file &&
360 cd submodule_update &&
361 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
362 $command replace_file_with_sub1 &&
363 test_superproject_content origin/replace_file_with_sub1 &&
364 test_dir_is_empty sub1 &&
365 git submodule update --init --recursive &&
366 test_submodule_content sub1 origin/replace_file_with_sub1
369 # ... as does removing a directory with tracked files with a
370 # submodule.
371 if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
372 then
373 # Non fast-forward merges fail with "Directory sub1 doesn't
374 # exist. sub1" because the empty submodule directory is not
375 # created
376 RESULT="failure"
377 else
378 RESULT="success"
380 test_expect_$RESULT "$command: replace directory with submodule" '
381 prolog &&
382 reset_work_tree_to replace_sub1_with_directory &&
384 cd submodule_update &&
385 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
386 $command replace_directory_with_sub1 &&
387 test_superproject_content origin/replace_directory_with_sub1 &&
388 test_dir_is_empty sub1 &&
389 git submodule update --init --recursive &&
390 test_submodule_content sub1 origin/replace_directory_with_sub1
394 ######################## Disappearing submodule #######################
395 # Removing a submodule doesn't remove its work tree ...
396 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
397 then
398 RESULT="failure"
399 else
400 RESULT="success"
402 test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
403 prolog &&
404 reset_work_tree_to add_sub1 &&
406 cd submodule_update &&
407 git branch -t remove_sub1 origin/remove_sub1 &&
408 $command remove_sub1 &&
409 test_superproject_content origin/remove_sub1 &&
410 test_submodule_content sub1 origin/add_sub1
413 # ... especially when it contains a .git directory.
414 test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
415 prolog &&
416 reset_work_tree_to add_sub1 &&
418 cd submodule_update &&
419 git branch -t remove_sub1 origin/remove_sub1 &&
420 replace_gitfile_with_git_dir sub1 &&
421 $command remove_sub1 &&
422 test_superproject_content origin/remove_sub1 &&
423 test_git_directory_is_unchanged sub1 &&
424 test_submodule_content sub1 origin/add_sub1
427 # Replacing a submodule with files in a directory must fail as the
428 # submodule work tree isn't removed ...
429 if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
430 then
431 # Non fast-forward merges attempt to merge the former
432 # submodule files with the newly checked out ones in the
433 # directory of the same name while it shouldn't.
434 RESULT="failure"
435 elif test "$KNOWN_FAILURE_FORCED_SWITCH_TESTS" = 1
436 then
437 # All existing tests that use test_submodule_forced_switch()
438 # require this.
439 RESULT="failure"
440 else
441 RESULT="success"
443 test_expect_$RESULT "$command: replace submodule with a directory must fail" '
444 prolog &&
445 reset_work_tree_to add_sub1 &&
447 cd submodule_update &&
448 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
449 $command replace_sub1_with_directory test_must_fail &&
450 test_superproject_content origin/add_sub1 &&
451 test_submodule_content sub1 origin/add_sub1
454 # ... especially when it contains a .git directory.
455 test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
456 prolog &&
457 reset_work_tree_to add_sub1 &&
459 cd submodule_update &&
460 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
461 replace_gitfile_with_git_dir sub1 &&
462 $command replace_sub1_with_directory test_must_fail &&
463 test_superproject_content origin/add_sub1 &&
464 test_git_directory_is_unchanged sub1 &&
465 test_submodule_content sub1 origin/add_sub1
468 # Replacing it with a file must fail as it could throw away any local
469 # work tree changes ...
470 test_expect_failure "$command: replace submodule with a file must fail" '
471 prolog &&
472 reset_work_tree_to add_sub1 &&
474 cd submodule_update &&
475 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
476 $command replace_sub1_with_file test_must_fail &&
477 test_superproject_content origin/add_sub1 &&
478 test_submodule_content sub1 origin/add_sub1
481 # ... or even destroy unpushed parts of submodule history if that
482 # still uses a .git directory.
483 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
484 prolog &&
485 reset_work_tree_to add_sub1 &&
487 cd submodule_update &&
488 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
489 replace_gitfile_with_git_dir sub1 &&
490 $command replace_sub1_with_file test_must_fail &&
491 test_superproject_content origin/add_sub1 &&
492 test_git_directory_is_unchanged sub1 &&
493 test_submodule_content sub1 origin/add_sub1
497 ########################## Modified submodule #########################
498 # Updating a submodule sha1 doesn't update the submodule's work tree
499 if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
500 then
501 # When cherry picking a SHA-1 update for an ignored submodule
502 # the commit incorrectly fails with "The previous cherry-pick
503 # is now empty, possibly due to conflict resolution."
504 RESULT="failure"
505 else
506 RESULT="success"
508 test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
509 prolog &&
510 reset_work_tree_to add_sub1 &&
512 cd submodule_update &&
513 git branch -t modify_sub1 origin/modify_sub1 &&
514 $command modify_sub1 &&
515 test_superproject_content origin/modify_sub1 &&
516 test_submodule_content sub1 origin/add_sub1 &&
517 git submodule update &&
518 test_submodule_content sub1 origin/modify_sub1
521 # Updating a submodule to an invalid sha1 doesn't update the
522 # submodule's work tree, subsequent update will fail
523 test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
524 prolog &&
525 reset_work_tree_to add_sub1 &&
527 cd submodule_update &&
528 git branch -t invalid_sub1 origin/invalid_sub1 &&
529 $command invalid_sub1 &&
530 test_superproject_content origin/invalid_sub1 &&
531 test_submodule_content sub1 origin/add_sub1 &&
532 test_must_fail git submodule update &&
533 test_submodule_content sub1 origin/add_sub1
536 # Updating a submodule from an invalid sha1 doesn't update the
537 # submodule's work tree, subsequent update will succeed
538 test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
539 prolog &&
540 reset_work_tree_to invalid_sub1 &&
542 cd submodule_update &&
543 git branch -t valid_sub1 origin/valid_sub1 &&
544 $command valid_sub1 &&
545 test_superproject_content origin/valid_sub1 &&
546 test_dir_is_empty sub1 &&
547 git submodule update --init --recursive &&
548 test_submodule_content sub1 origin/valid_sub1
553 # Declares and invokes several tests that, in various situations, checks that
554 # the provided transition function:
555 # - succeeds in updating the worktree and index of a superproject to a target
556 # commit, or fails atomically (depending on the test situation)
557 # - if succeeds, the contents of submodule directories are unchanged
558 # - if succeeds, once "git submodule update" is invoked, the contents of
559 # submodule directories are updated
561 # If the command under test is known to not work with submodules in certain
562 # conditions, set the appropriate KNOWN_FAILURE_* variable used in the tests
563 # below to 1.
565 # The first argument of the callback function will be the name of the submodule.
567 # Removing a submodule containing a .git directory must fail even when forced
568 # to protect the history! If we are testing this case, the second argument of
569 # the callback function will be 'test_must_fail', else it will be the empty
570 # string.
572 # The following example uses `git some-command` as an example command to be
573 # tested. It updates the worktree and index to match a target, but not any
574 # submodule directories.
576 # my_func () {
577 # ...prepare for `git some-command` to be run...
578 # $2 git some-command "$1" &&
579 # if test -n "$2"
580 # then
581 # return
582 # fi &&
583 # ...check the state after git some-command is run...
585 # test_submodule_switch_func "my_func"
586 test_submodule_switch_func () {
587 command="$1"
588 test_submodule_switch_common "$command"
590 # An empty directory does not prevent the creation of a submodule of
591 # the same name, but a file does.
592 test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
593 prolog &&
594 reset_work_tree_to no_submodule &&
596 cd submodule_update &&
597 git branch -t add_sub1 origin/add_sub1 &&
598 >sub1 &&
599 $command add_sub1 test_must_fail &&
600 test_superproject_content origin/no_submodule &&
601 test_must_be_empty sub1
606 # Ensures that the that the arg either contains "test_must_fail" or is empty.
607 may_only_be_test_must_fail () {
608 test -z "$1" || test "$1" = test_must_fail || die
611 git_test_func () {
612 may_only_be_test_must_fail "$2" &&
613 $2 git $gitcmd "$1"
616 test_submodule_switch () {
617 gitcmd="$1"
618 test_submodule_switch_func "git_test_func"
621 # Same as test_submodule_switch(), except that throwing away local changes in
622 # the superproject is allowed.
623 test_submodule_forced_switch () {
624 gitcmd="$1"
625 command="git_test_func"
626 KNOWN_FAILURE_FORCED_SWITCH_TESTS=1
627 test_submodule_switch_common "$command"
629 # When forced, a file in the superproject does not prevent creating a
630 # submodule of the same name.
631 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
632 prolog &&
633 reset_work_tree_to no_submodule &&
635 cd submodule_update &&
636 git branch -t add_sub1 origin/add_sub1 &&
637 >sub1 &&
638 $command add_sub1 &&
639 test_superproject_content origin/add_sub1 &&
640 test_dir_is_empty sub1
645 # Test that submodule contents are correctly updated when switching
646 # between commits that change a submodule.
647 # Test that the following transitions are correctly handled:
648 # (These tests are also above in the case where we expect no change
649 # in the submodule)
650 # - Updated submodule
651 # - New submodule
652 # - Removed submodule
653 # - Directory containing tracked files replaced by submodule
654 # - Submodule replaced by tracked files in directory
655 # - Submodule replaced by tracked file with the same name
656 # - Tracked file replaced by submodule
658 # New test cases
659 # - Removing a submodule with a git directory absorbs the submodules
660 # git directory first into the superproject.
661 # - Switching from no submodule to nested submodules
662 # - Switching from nested submodules to no submodule
664 # Internal function; use test_submodule_switch_recursing_with_args() or
665 # test_submodule_forced_switch_recursing_with_args() instead.
666 test_submodule_recursing_with_args_common () {
667 command="$1 --recurse-submodules"
669 ######################### Appearing submodule #########################
670 # Switching to a commit letting a submodule appear checks it out ...
671 test_expect_success "$command: added submodule is checked out" '
672 prolog &&
673 reset_work_tree_to_interested no_submodule &&
675 cd submodule_update &&
676 git branch -t add_sub1 origin/add_sub1 &&
677 $command add_sub1 &&
678 test_superproject_content origin/add_sub1 &&
679 test_submodule_content sub1 origin/add_sub1
682 # ... ignoring an empty existing directory.
683 test_expect_success "$command: added submodule is checked out in empty dir" '
684 prolog &&
685 reset_work_tree_to_interested no_submodule &&
687 cd submodule_update &&
688 mkdir sub1 &&
689 git branch -t add_sub1 origin/add_sub1 &&
690 $command add_sub1 &&
691 test_superproject_content origin/add_sub1 &&
692 test_submodule_content sub1 origin/add_sub1
696 # Replacing a tracked file with a submodule produces a checked out submodule
697 test_expect_success "$command: replace tracked file with submodule checks out submodule" '
698 prolog &&
699 reset_work_tree_to_interested replace_sub1_with_file &&
701 cd submodule_update &&
702 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
703 $command replace_file_with_sub1 &&
704 test_superproject_content origin/replace_file_with_sub1 &&
705 test_submodule_content sub1 origin/replace_file_with_sub1
708 # ... as does removing a directory with tracked files with a submodule.
709 test_expect_success "$command: replace directory with submodule" '
710 prolog &&
711 reset_work_tree_to_interested replace_sub1_with_directory &&
713 cd submodule_update &&
714 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
715 $command replace_directory_with_sub1 &&
716 test_superproject_content origin/replace_directory_with_sub1 &&
717 test_submodule_content sub1 origin/replace_directory_with_sub1
720 # Switching to a commit with nested submodules recursively checks them out
721 test_expect_success "$command: nested submodules are checked out" '
722 prolog &&
723 reset_work_tree_to_interested no_submodule &&
725 cd submodule_update &&
726 git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
727 $command modify_sub1_recursively &&
728 test_superproject_content origin/modify_sub1_recursively &&
729 test_submodule_content sub1 origin/modify_sub1_recursively &&
730 test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
734 ######################## Disappearing submodule #######################
735 # Removing a submodule removes its work tree ...
736 test_expect_success "$command: removed submodule removes submodules working tree" '
737 prolog &&
738 reset_work_tree_to_interested add_sub1 &&
740 cd submodule_update &&
741 git branch -t remove_sub1 origin/remove_sub1 &&
742 $command remove_sub1 &&
743 test_superproject_content origin/remove_sub1 &&
744 ! test -e sub1 &&
745 test_must_fail git config -f .git/modules/sub1/config core.worktree
748 # ... absorbing a .git directory along the way.
749 test_expect_success "$command: removed submodule absorbs submodules .git directory" '
750 prolog &&
751 reset_work_tree_to_interested add_sub1 &&
753 cd submodule_update &&
754 git branch -t remove_sub1 origin/remove_sub1 &&
755 replace_gitfile_with_git_dir sub1 &&
756 rm -rf .git/modules &&
757 $command remove_sub1 &&
758 test_superproject_content origin/remove_sub1 &&
759 ! test -e sub1 &&
760 test_git_directory_exists sub1
764 # Replacing it with a file ...
765 test_expect_success "$command: replace submodule with a file" '
766 prolog &&
767 reset_work_tree_to_interested add_sub1 &&
769 cd submodule_update &&
770 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
771 $command replace_sub1_with_file &&
772 test_superproject_content origin/replace_sub1_with_file &&
773 test -f sub1
776 RESULTDS=success
777 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
778 then
779 RESULTDS=failure
781 # ... must check its local work tree for untracked files
782 test_expect_$RESULTDS "$command: replace submodule with a file must fail with untracked files" '
783 prolog &&
784 reset_work_tree_to_interested add_sub1 &&
786 cd submodule_update &&
787 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
788 : >sub1/untrackedfile &&
789 test_must_fail $command replace_sub1_with_file &&
790 test_superproject_content origin/add_sub1 &&
791 test_submodule_content sub1 origin/add_sub1 &&
792 test -f sub1/untracked_file
796 # Switching to a commit without nested submodules removes their worktrees
797 test_expect_success "$command: worktrees of nested submodules are removed" '
798 prolog &&
799 reset_work_tree_to_interested add_nested_sub &&
801 cd submodule_update &&
802 git branch -t no_submodule origin/no_submodule &&
803 $command no_submodule &&
804 test_superproject_content origin/no_submodule &&
805 ! test_path_is_dir sub1 &&
806 test_must_fail git config -f .git/modules/sub1/config core.worktree &&
807 test_must_fail git config -f .git/modules/sub1/modules/sub2/config core.worktree
811 ########################## Modified submodule #########################
812 # Updating a submodule sha1 updates the submodule's work tree
813 test_expect_success "$command: modified submodule updates submodule work tree" '
814 prolog &&
815 reset_work_tree_to_interested add_sub1 &&
817 cd submodule_update &&
818 git branch -t modify_sub1 origin/modify_sub1 &&
819 $command modify_sub1 &&
820 test_superproject_content origin/modify_sub1 &&
821 test_submodule_content sub1 origin/modify_sub1
824 # Updating a submodule to an invalid sha1 doesn't update the
825 # superproject nor the submodule's work tree.
826 test_expect_success "$command: updating to a missing submodule commit fails" '
827 prolog &&
828 reset_work_tree_to_interested add_sub1 &&
830 cd submodule_update &&
831 git branch -t invalid_sub1 origin/invalid_sub1 &&
832 test_must_fail $command invalid_sub1 2>err &&
833 test_i18ngrep sub1 err &&
834 test_superproject_content origin/add_sub1 &&
835 test_submodule_content sub1 origin/add_sub1
838 # Updating a submodule does not touch the currently checked out branch in the submodule
839 test_expect_success "$command: submodule branch is not changed, detach HEAD instead" '
840 prolog &&
841 reset_work_tree_to_interested add_sub1 &&
843 cd submodule_update &&
844 git -C sub1 checkout -b keep_branch &&
845 git -C sub1 rev-parse HEAD >expect &&
846 git branch -t modify_sub1 origin/modify_sub1 &&
847 $command modify_sub1 &&
848 test_superproject_content origin/modify_sub1 &&
849 test_submodule_content sub1 origin/modify_sub1 &&
850 git -C sub1 rev-parse keep_branch >actual &&
851 test_cmp expect actual &&
852 test_must_fail git -C sub1 symbolic-ref HEAD
857 # Declares and invokes several tests that, in various situations, checks that
858 # the provided Git command, when invoked with --recurse-submodules:
859 # - succeeds in updating the worktree and index of a superproject to a target
860 # commit, or fails atomically (depending on the test situation)
861 # - if succeeds, the contents of submodule directories are updated
863 # Specify the Git command so that "git $GIT_COMMAND --recurse-submodules"
864 # works.
866 # If the command under test is known to not work with submodules in certain
867 # conditions, set the appropriate KNOWN_FAILURE_* variable used in the tests
868 # below to 1.
870 # Use as follows:
872 # test_submodule_switch_recursing_with_args "$GIT_COMMAND"
873 test_submodule_switch_recursing_with_args () {
874 cmd_args="$1"
875 command="git $cmd_args"
876 test_submodule_recursing_with_args_common "$command"
878 RESULTDS=success
879 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
880 then
881 RESULTDS=failure
883 RESULTOI=success
884 if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
885 then
886 RESULTOI=failure
888 # Switching to a commit letting a submodule appear cannot override an
889 # untracked file.
890 test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
891 prolog &&
892 reset_work_tree_to_interested no_submodule &&
894 cd submodule_update &&
895 git branch -t add_sub1 origin/add_sub1 &&
896 : >sub1 &&
897 test_must_fail $command add_sub1 &&
898 test_superproject_content origin/no_submodule &&
899 test_must_be_empty sub1
902 # ... but an ignored file is fine.
903 test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
904 test_when_finished "rm submodule_update/.git/info/exclude" &&
905 prolog &&
906 reset_work_tree_to_interested no_submodule &&
908 cd submodule_update &&
909 git branch -t add_sub1 origin/add_sub1 &&
910 : >sub1 &&
911 echo sub1 >.git/info/exclude &&
912 $command add_sub1 &&
913 test_superproject_content origin/add_sub1 &&
914 test_submodule_content sub1 origin/add_sub1
918 # Replacing a submodule with files in a directory must succeeds
919 # when the submodule is clean
920 test_expect_$RESULTDS "$command: replace submodule with a directory" '
921 prolog &&
922 reset_work_tree_to_interested add_sub1 &&
924 cd submodule_update &&
925 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
926 $command replace_sub1_with_directory &&
927 test_superproject_content origin/replace_sub1_with_directory &&
928 test_submodule_content sub1 origin/replace_sub1_with_directory
931 # ... absorbing a .git directory.
932 test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
933 prolog &&
934 reset_work_tree_to_interested add_sub1 &&
936 cd submodule_update &&
937 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
938 replace_gitfile_with_git_dir sub1 &&
939 rm -rf .git/modules &&
940 $command replace_sub1_with_directory &&
941 test_superproject_content origin/replace_sub1_with_directory &&
942 test_git_directory_exists sub1
946 # ... and ignored files are ignored
947 test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
948 test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
949 prolog &&
950 reset_work_tree_to_interested add_sub1 &&
952 cd submodule_update &&
953 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
954 echo ignored >.git/modules/sub1/info/exclude &&
955 : >sub1/ignored &&
956 $command replace_sub1_with_file &&
957 test_superproject_content origin/replace_sub1_with_file &&
958 test -f sub1
962 test_expect_success "git -c submodule.recurse=true $cmd_args: modified submodule updates submodule work tree" '
963 prolog &&
964 reset_work_tree_to_interested add_sub1 &&
966 cd submodule_update &&
967 git branch -t modify_sub1 origin/modify_sub1 &&
968 git -c submodule.recurse=true $cmd_args modify_sub1 &&
969 test_superproject_content origin/modify_sub1 &&
970 test_submodule_content sub1 origin/modify_sub1
974 test_expect_success "$command: modified submodule updates submodule recursively" '
975 prolog &&
976 reset_work_tree_to_interested add_nested_sub &&
978 cd submodule_update &&
979 git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
980 $command modify_sub1_recursively &&
981 test_superproject_content origin/modify_sub1_recursively &&
982 test_submodule_content sub1 origin/modify_sub1_recursively &&
983 test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
988 # Same as test_submodule_switch_recursing_with_args(), except that throwing
989 # away local changes in the superproject is allowed.
990 test_submodule_forced_switch_recursing_with_args () {
991 cmd_args="$1"
992 command="git $cmd_args"
993 test_submodule_recursing_with_args_common "$command"
995 RESULT=success
996 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
997 then
998 RESULT=failure
1000 # Switching to a commit letting a submodule appear does not care about
1001 # an untracked file.
1002 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
1003 prolog &&
1004 reset_work_tree_to_interested no_submodule &&
1006 cd submodule_update &&
1007 git branch -t add_sub1 origin/add_sub1 &&
1008 >sub1 &&
1009 $command add_sub1 &&
1010 test_superproject_content origin/add_sub1 &&
1011 test_submodule_content sub1 origin/add_sub1
1015 # Replacing a submodule with files in a directory ...
1016 test_expect_success "$command: replace submodule with a directory" '
1017 prolog &&
1018 reset_work_tree_to_interested add_sub1 &&
1020 cd submodule_update &&
1021 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1022 $command replace_sub1_with_directory &&
1023 test_superproject_content origin/replace_sub1_with_directory
1026 # ... absorbing a .git directory.
1027 test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
1028 prolog &&
1029 reset_work_tree_to_interested add_sub1 &&
1031 cd submodule_update &&
1032 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1033 replace_gitfile_with_git_dir sub1 &&
1034 rm -rf .git/modules/sub1 &&
1035 $command replace_sub1_with_directory &&
1036 test_superproject_content origin/replace_sub1_with_directory &&
1037 test_git_directory_exists sub1
1041 # ... even if the submodule contains ignored files
1042 test_expect_success "$command: replace submodule with a file ignoring ignored files" '
1043 prolog &&
1044 reset_work_tree_to_interested add_sub1 &&
1046 cd submodule_update &&
1047 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1048 : >sub1/expect &&
1049 $command replace_sub1_with_file &&
1050 test_superproject_content origin/replace_sub1_with_file
1054 # Updating a submodule from an invalid sha1 updates
1055 test_expect_success "$command: modified submodule does update submodule work tree from invalid commit" '
1056 prolog &&
1057 reset_work_tree_to_interested invalid_sub1 &&
1059 cd submodule_update &&
1060 git branch -t valid_sub1 origin/valid_sub1 &&
1061 $command valid_sub1 &&
1062 test_superproject_content origin/valid_sub1 &&
1063 test_submodule_content sub1 origin/valid_sub1
1067 # Old versions of Git were buggy writing the .git link file
1068 # (e.g. before f8eaa0ba98b and then moving the superproject repo
1069 # whose submodules contained absolute paths)
1070 test_expect_success "$command: updating submodules fixes .git links" '
1071 prolog &&
1072 reset_work_tree_to_interested add_sub1 &&
1074 cd submodule_update &&
1075 git branch -t modify_sub1 origin/modify_sub1 &&
1076 echo "gitdir: bogus/path" >sub1/.git &&
1077 $command modify_sub1 &&
1078 test_superproject_content origin/modify_sub1 &&
1079 test_submodule_content sub1 origin/modify_sub1
1083 test_expect_success "$command: changed submodule worktree is reset" '
1084 prolog &&
1085 reset_work_tree_to_interested add_sub1 &&
1087 cd submodule_update &&
1088 rm sub1/file1 &&
1089 : >sub1/new_file &&
1090 git -C sub1 add new_file &&
1091 $command HEAD &&
1092 test_path_is_file sub1/file1 &&
1093 test_path_is_missing sub1/new_file