pack-objects: simplify --filter handling
[git/debian.git] / t / lib-submodule-update.sh
blob2d31fcfda1f338973cfc868b7f01ae925b39d326
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 branch=$(git symbolic-ref --short HEAD) &&
67 echo "expect" >>.gitignore &&
68 echo "actual" >>.gitignore &&
69 echo "x" >file1 &&
70 echo "y" >file2 &&
71 git add .gitignore file1 file2 &&
72 git commit -m "Base" &&
73 git branch "no_submodule" &&
75 git checkout -b "add_sub1" &&
76 git submodule add ../submodule_update_sub1 sub1 &&
77 git submodule add ../submodule_update_sub1 uninitialized_sub &&
78 git config -f .gitmodules submodule.sub1.ignore all &&
79 git config submodule.sub1.ignore all &&
80 git add .gitmodules &&
81 git commit -m "Add sub1" &&
83 git checkout -b remove_sub1 add_sub1 &&
84 git revert HEAD &&
86 git checkout -b modify_sub1 add_sub1 &&
87 git submodule update &&
89 cd sub1 &&
90 git fetch &&
91 git checkout -b "modifications" &&
92 echo "z" >file2 &&
93 echo "x" >file3 &&
94 git add file2 file3 &&
95 git commit -m "modified file2 and added file3" &&
96 git push origin modifications
97 ) &&
98 git add sub1 &&
99 git commit -m "Modify sub1" &&
101 git checkout -b add_nested_sub modify_sub1 &&
102 git -C sub1 checkout -b "add_nested_sub" &&
103 git -C sub1 submodule add --branch no_submodule ../submodule_update_sub2 sub2 &&
104 git -C sub1 commit -a -m "add a nested submodule" &&
105 git add sub1 &&
106 git commit -a -m "update submodule, that updates a nested submodule" &&
107 git checkout -b modify_sub1_recursively &&
108 git -C sub1 checkout -b modify_sub1_recursively &&
109 git -C sub1/sub2 checkout -b modify_sub1_recursively &&
110 echo change >sub1/sub2/file3 &&
111 git -C sub1/sub2 add file3 &&
112 git -C sub1/sub2 commit -m "make a change in nested sub" &&
113 git -C sub1 add sub2 &&
114 git -C sub1 commit -m "update nested sub" &&
115 git add sub1 &&
116 git commit -m "update sub1, that updates nested sub" &&
117 git -C sub1 push origin modify_sub1_recursively &&
118 git -C sub1/sub2 push origin modify_sub1_recursively &&
119 git -C sub1 submodule deinit -f --all &&
121 git checkout -b replace_sub1_with_directory add_sub1 &&
122 git submodule update &&
123 git -C sub1 checkout modifications &&
124 git rm --cached sub1 &&
125 rm sub1/.git* &&
126 git config -f .gitmodules --remove-section "submodule.sub1" &&
127 git add .gitmodules sub1/* &&
128 git commit -m "Replace sub1 with directory" &&
130 git checkout -b replace_directory_with_sub1 &&
131 git revert HEAD &&
133 git checkout -b replace_sub1_with_file add_sub1 &&
134 git rm sub1 &&
135 echo "content" >sub1 &&
136 git add sub1 &&
137 git commit -m "Replace sub1 with file" &&
139 git checkout -b replace_file_with_sub1 &&
140 git revert HEAD &&
142 git checkout -b invalid_sub1 add_sub1 &&
143 git update-index --cacheinfo 160000 $(test_oid numeric) sub1 &&
144 git commit -m "Invalid sub1 commit" &&
145 git checkout -b valid_sub1 &&
146 git revert HEAD &&
148 git checkout "$branch"
152 # Helper function to replace gitfile with .git directory
153 replace_gitfile_with_git_dir () {
155 cd "$1" &&
156 git_dir="$(git rev-parse --git-dir)" &&
157 rm -f .git &&
158 cp -R "$git_dir" .git &&
159 GIT_WORK_TREE=. git config --unset core.worktree
163 # Test that the .git directory in the submodule is unchanged (except for the
164 # core.worktree setting, which appears only in $GIT_DIR/modules/$1/config).
165 # Call this function before test_submodule_content as the latter might
166 # write the index file leading to false positive index differences.
168 # Note that this only supports submodules at the root level of the
169 # superproject, with the default name, i.e. same as its path.
170 test_git_directory_is_unchanged () {
172 cd ".git/modules/$1" &&
173 # does core.worktree point at the right place?
174 test "$(git config core.worktree)" = "../../../$1" &&
175 # remove it temporarily before comparing, as
176 # "$1/.git/config" lacks it...
177 git config --unset core.worktree
178 ) &&
179 diff -r ".git/modules/$1" "$1/.git" &&
181 # ... and then restore.
182 cd ".git/modules/$1" &&
183 git config core.worktree "../../../$1"
187 test_git_directory_exists () {
188 test -e ".git/modules/$1" &&
189 if test -f sub1/.git
190 then
191 # does core.worktree point at the right place?
192 test "$(git -C .git/modules/$1 config core.worktree)" = "../../../$1"
196 # Helper function to be executed at the start of every test below, it sets up
197 # the submodule repo if it doesn't exist and configures the most problematic
198 # settings for diff.ignoreSubmodules.
199 prolog () {
200 test_config_global protocol.file.allow always &&
201 (test -d submodule_update_repo || create_lib_submodule_repo) &&
202 test_config_global diff.ignoreSubmodules all &&
203 test_config diff.ignoreSubmodules all
206 # Helper function to bring work tree back into the state given by the
207 # commit. This includes trying to populate sub1 accordingly if it exists and
208 # should be updated to an existing commit.
209 reset_work_tree_to () {
210 rm -rf submodule_update &&
211 git clone --template= submodule_update_repo submodule_update &&
213 cd submodule_update &&
214 rm -rf sub1 &&
215 git checkout -f "$1" &&
216 git status -u -s >actual &&
217 test_must_be_empty actual &&
218 hash=$(git rev-parse --revs-only HEAD:sub1) &&
219 if test -n "$hash" &&
220 test $(cd "../submodule_update_sub1" && git rev-parse --verify "$hash^{commit}")
221 then
222 git submodule update --init --recursive "sub1"
227 reset_work_tree_to_interested () {
228 reset_work_tree_to $1 &&
229 # make the submodule git dirs available
230 if ! test -d submodule_update/.git/modules/sub1
231 then
232 mkdir -p submodule_update/.git/modules &&
233 cp -r submodule_update_repo/.git/modules/sub1 submodule_update/.git/modules/sub1
234 GIT_WORK_TREE=. git -C submodule_update/.git/modules/sub1 config --unset core.worktree
235 fi &&
236 if ! test -d submodule_update/.git/modules/sub1/modules/sub2
237 then
238 mkdir -p submodule_update/.git/modules/sub1/modules &&
239 cp -r submodule_update_repo/.git/modules/sub1/modules/sub2 submodule_update/.git/modules/sub1/modules/sub2
240 # core.worktree is unset for sub2 as it is not checked out
241 fi &&
242 # indicate we are interested in the submodule:
243 git -C submodule_update config submodule.sub1.url "bogus" &&
244 # sub1 might not be checked out, so use the git dir
245 git -C submodule_update/.git/modules/sub1 config submodule.sub2.url "bogus"
248 # Test that the superproject contains the content according to commit "$1"
249 # (the work tree must match the index for everything but submodules but the
250 # index must exactly match the given commit including any submodule SHA-1s).
251 test_superproject_content () {
252 git diff-index --cached "$1" >actual &&
253 test_must_be_empty actual &&
254 git diff-files --ignore-submodules >actual &&
255 test_must_be_empty actual
258 # Test that the given submodule at path "$1" contains the content according
259 # to the submodule commit recorded in the superproject's commit "$2"
260 test_submodule_content () {
261 if test x"$1" = "x-C"
262 then
263 cd "$2"
264 shift; shift;
266 if test $# != 2
267 then
268 echo "test_submodule_content needs two arguments"
269 return 1
270 fi &&
271 submodule="$1" &&
272 commit="$2" &&
273 test -d "$submodule"/ &&
274 if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
275 then
276 echo "Submodule $submodule is not populated"
277 return 1
278 fi &&
279 sha1=$(git rev-parse --verify "$commit:$submodule") &&
280 if test -z "$sha1"
281 then
282 echo "Couldn't retrieve SHA-1 of $submodule for $commit"
283 return 1
284 fi &&
286 cd "$submodule" &&
287 git status -u -s >actual &&
288 test_must_be_empty actual &&
289 git diff "$sha1" >actual &&
290 test_must_be_empty actual
294 # Test that the following transitions are correctly handled:
295 # - Updated submodule
296 # - New submodule
297 # - Removed submodule
298 # - Directory containing tracked files replaced by submodule
299 # - Submodule replaced by tracked files in directory
300 # - Submodule replaced by tracked file with the same name
301 # - Tracked file replaced by submodule
303 # The default is that submodule contents aren't changed until "git submodule
304 # update" is run. And even then that command doesn't delete the work tree of
305 # a removed submodule.
307 # The first argument of the callback function will be the name of the submodule.
309 # Removing a submodule containing a .git directory must fail even when forced
310 # to protect the history! If we are testing this case, the second argument of
311 # the callback function will be 'test_must_fail', else it will be the empty
312 # string.
315 # Internal function; use test_submodule_switch_func(), test_submodule_switch(),
316 # or test_submodule_forced_switch() instead.
317 test_submodule_switch_common () {
318 command="$1"
319 ######################### Appearing submodule #########################
320 # Switching to a commit letting a submodule appear creates empty dir ...
321 test_expect_success "$command: added submodule creates empty directory" '
322 prolog &&
323 reset_work_tree_to no_submodule &&
325 cd submodule_update &&
326 git branch -t add_sub1 origin/add_sub1 &&
327 $command add_sub1 &&
328 test_superproject_content origin/add_sub1 &&
329 test_dir_is_empty sub1 &&
330 git submodule update --init --recursive &&
331 test_submodule_content sub1 origin/add_sub1
334 # ... and doesn't care if it already exists.
335 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
336 then
337 # Restoring stash fails to restore submodule index entry
338 RESULT="failure"
339 else
340 RESULT="success"
342 test_expect_$RESULT "$command: added submodule leaves existing empty directory alone" '
343 prolog &&
344 reset_work_tree_to no_submodule &&
346 cd submodule_update &&
347 mkdir sub1 &&
348 git branch -t add_sub1 origin/add_sub1 &&
349 $command add_sub1 &&
350 test_superproject_content origin/add_sub1 &&
351 test_dir_is_empty sub1 &&
352 git submodule update --init --recursive &&
353 test_submodule_content sub1 origin/add_sub1
356 # Replacing a tracked file with a submodule produces an empty
357 # directory ...
358 test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
359 prolog &&
360 reset_work_tree_to replace_sub1_with_file &&
362 cd submodule_update &&
363 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
364 $command replace_file_with_sub1 &&
365 test_superproject_content origin/replace_file_with_sub1 &&
366 test_dir_is_empty sub1 &&
367 git submodule update --init --recursive &&
368 test_submodule_content sub1 origin/replace_file_with_sub1
371 # ... as does removing a directory with tracked files with a
372 # submodule.
373 if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
374 then
375 # Non fast-forward merges fail with "Directory sub1 doesn't
376 # exist. sub1" because the empty submodule directory is not
377 # created
378 RESULT="failure"
379 else
380 RESULT="success"
382 test_expect_$RESULT "$command: replace directory with submodule" '
383 prolog &&
384 reset_work_tree_to replace_sub1_with_directory &&
386 cd submodule_update &&
387 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
388 $command replace_directory_with_sub1 &&
389 test_superproject_content origin/replace_directory_with_sub1 &&
390 test_dir_is_empty sub1 &&
391 git submodule update --init --recursive &&
392 test_submodule_content sub1 origin/replace_directory_with_sub1
396 ######################## Disappearing submodule #######################
397 # Removing a submodule doesn't remove its work tree ...
398 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
399 then
400 RESULT="failure"
401 else
402 RESULT="success"
404 test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
405 prolog &&
406 reset_work_tree_to add_sub1 &&
408 cd submodule_update &&
409 git branch -t remove_sub1 origin/remove_sub1 &&
410 $command remove_sub1 &&
411 test_superproject_content origin/remove_sub1 &&
412 test_submodule_content sub1 origin/add_sub1
415 # ... especially when it contains a .git directory.
416 test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
417 prolog &&
418 reset_work_tree_to add_sub1 &&
420 cd submodule_update &&
421 git branch -t remove_sub1 origin/remove_sub1 &&
422 replace_gitfile_with_git_dir sub1 &&
423 $command remove_sub1 &&
424 test_superproject_content origin/remove_sub1 &&
425 test_git_directory_is_unchanged sub1 &&
426 test_submodule_content sub1 origin/add_sub1
429 # Replacing a submodule with files in a directory must fail as the
430 # submodule work tree isn't removed ...
431 if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
432 then
433 # Non fast-forward merges attempt to merge the former
434 # submodule files with the newly checked out ones in the
435 # directory of the same name while it shouldn't.
436 RESULT="failure"
437 elif test "$KNOWN_FAILURE_FORCED_SWITCH_TESTS" = 1
438 then
439 # All existing tests that use test_submodule_forced_switch()
440 # require this.
441 RESULT="failure"
442 else
443 RESULT="success"
445 test_expect_$RESULT "$command: replace submodule with a directory must fail" '
446 prolog &&
447 reset_work_tree_to add_sub1 &&
449 cd submodule_update &&
450 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
451 $command replace_sub1_with_directory test_must_fail &&
452 test_superproject_content origin/add_sub1 &&
453 test_submodule_content sub1 origin/add_sub1
456 # ... especially when it contains a .git directory.
457 test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
458 prolog &&
459 reset_work_tree_to add_sub1 &&
461 cd submodule_update &&
462 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
463 replace_gitfile_with_git_dir sub1 &&
464 $command replace_sub1_with_directory test_must_fail &&
465 test_superproject_content origin/add_sub1 &&
466 test_git_directory_is_unchanged sub1 &&
467 test_submodule_content sub1 origin/add_sub1
470 # Replacing it with a file must fail as it could throw away any local
471 # work tree changes ...
472 test_expect_failure "$command: replace submodule with a file must fail" '
473 prolog &&
474 reset_work_tree_to add_sub1 &&
476 cd submodule_update &&
477 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
478 $command replace_sub1_with_file test_must_fail &&
479 test_superproject_content origin/add_sub1 &&
480 test_submodule_content sub1 origin/add_sub1
483 # ... or even destroy unpushed parts of submodule history if that
484 # still uses a .git directory.
485 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
486 prolog &&
487 reset_work_tree_to add_sub1 &&
489 cd submodule_update &&
490 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
491 replace_gitfile_with_git_dir sub1 &&
492 $command replace_sub1_with_file test_must_fail &&
493 test_superproject_content origin/add_sub1 &&
494 test_git_directory_is_unchanged sub1 &&
495 test_submodule_content sub1 origin/add_sub1
499 ########################## Modified submodule #########################
500 # Updating a submodule sha1 doesn't update the submodule's work tree
501 if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
502 then
503 # When cherry picking a SHA-1 update for an ignored submodule
504 # the commit incorrectly fails with "The previous cherry-pick
505 # is now empty, possibly due to conflict resolution."
506 RESULT="failure"
507 else
508 RESULT="success"
510 test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
511 prolog &&
512 reset_work_tree_to add_sub1 &&
514 cd submodule_update &&
515 git branch -t modify_sub1 origin/modify_sub1 &&
516 $command modify_sub1 &&
517 test_superproject_content origin/modify_sub1 &&
518 test_submodule_content sub1 origin/add_sub1 &&
519 git submodule update &&
520 test_submodule_content sub1 origin/modify_sub1
523 # Updating a submodule to an invalid sha1 doesn't update the
524 # submodule's work tree, subsequent update will fail
525 test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
526 prolog &&
527 reset_work_tree_to add_sub1 &&
529 cd submodule_update &&
530 git branch -t invalid_sub1 origin/invalid_sub1 &&
531 $command invalid_sub1 &&
532 test_superproject_content origin/invalid_sub1 &&
533 test_submodule_content sub1 origin/add_sub1 &&
534 test_must_fail git submodule update &&
535 test_submodule_content sub1 origin/add_sub1
538 # Updating a submodule from an invalid sha1 doesn't update the
539 # submodule's work tree, subsequent update will succeed
540 test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
541 prolog &&
542 reset_work_tree_to invalid_sub1 &&
544 cd submodule_update &&
545 git branch -t valid_sub1 origin/valid_sub1 &&
546 $command valid_sub1 &&
547 test_superproject_content origin/valid_sub1 &&
548 test_dir_is_empty sub1 &&
549 git submodule update --init --recursive &&
550 test_submodule_content sub1 origin/valid_sub1
555 # Declares and invokes several tests that, in various situations, checks that
556 # the provided transition function:
557 # - succeeds in updating the worktree and index of a superproject to a target
558 # commit, or fails atomically (depending on the test situation)
559 # - if succeeds, the contents of submodule directories are unchanged
560 # - if succeeds, once "git submodule update" is invoked, the contents of
561 # submodule directories are updated
563 # If the command under test is known to not work with submodules in certain
564 # conditions, set the appropriate KNOWN_FAILURE_* variable used in the tests
565 # below to 1.
567 # The first argument of the callback function will be the name of the submodule.
569 # Removing a submodule containing a .git directory must fail even when forced
570 # to protect the history! If we are testing this case, the second argument of
571 # the callback function will be 'test_must_fail', else it will be the empty
572 # string.
574 # The following example uses `git some-command` as an example command to be
575 # tested. It updates the worktree and index to match a target, but not any
576 # submodule directories.
578 # my_func () {
579 # ...prepare for `git some-command` to be run...
580 # $2 git some-command "$1" &&
581 # if test -n "$2"
582 # then
583 # return
584 # fi &&
585 # ...check the state after git some-command is run...
587 # test_submodule_switch_func "my_func"
588 test_submodule_switch_func () {
589 command="$1"
590 test_submodule_switch_common "$command"
592 # An empty directory does not prevent the creation of a submodule of
593 # the same name, but a file does.
594 test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
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 test_must_fail &&
602 test_superproject_content origin/no_submodule &&
603 test_must_be_empty sub1
608 # Ensures that the that the arg either contains "test_must_fail" or is empty.
609 may_only_be_test_must_fail () {
610 test -z "$1" || test "$1" = test_must_fail || die
613 git_test_func () {
614 may_only_be_test_must_fail "$2" &&
615 $2 git $gitcmd "$1"
618 test_submodule_switch () {
619 gitcmd="$1"
620 test_submodule_switch_func "git_test_func"
623 # Same as test_submodule_switch(), except that throwing away local changes in
624 # the superproject is allowed.
625 test_submodule_forced_switch () {
626 gitcmd="$1"
627 command="git_test_func"
628 KNOWN_FAILURE_FORCED_SWITCH_TESTS=1
629 test_submodule_switch_common "$command"
631 # When forced, a file in the superproject does not prevent creating a
632 # submodule of the same name.
633 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
634 prolog &&
635 reset_work_tree_to no_submodule &&
637 cd submodule_update &&
638 git branch -t add_sub1 origin/add_sub1 &&
639 >sub1 &&
640 $command add_sub1 &&
641 test_superproject_content origin/add_sub1 &&
642 test_dir_is_empty sub1
647 # Test that submodule contents are correctly updated when switching
648 # between commits that change a submodule.
649 # Test that the following transitions are correctly handled:
650 # (These tests are also above in the case where we expect no change
651 # in the submodule)
652 # - Updated submodule
653 # - New submodule
654 # - Removed submodule
655 # - Directory containing tracked files replaced by submodule
656 # - Submodule replaced by tracked files in directory
657 # - Submodule replaced by tracked file with the same name
658 # - Tracked file replaced by submodule
660 # New test cases
661 # - Removing a submodule with a git directory absorbs the submodules
662 # git directory first into the superproject.
663 # - Switching from no submodule to nested submodules
664 # - Switching from nested submodules to no submodule
666 # Internal function; use test_submodule_switch_recursing_with_args() or
667 # test_submodule_forced_switch_recursing_with_args() instead.
668 test_submodule_recursing_with_args_common () {
669 command="$1 --recurse-submodules"
671 ######################### Appearing submodule #########################
672 # Switching to a commit letting a submodule appear checks it out ...
673 test_expect_success "$command: added submodule is checked out" '
674 prolog &&
675 reset_work_tree_to_interested no_submodule &&
677 cd submodule_update &&
678 git branch -t add_sub1 origin/add_sub1 &&
679 $command add_sub1 &&
680 test_superproject_content origin/add_sub1 &&
681 test_submodule_content sub1 origin/add_sub1
684 # ... ignoring an empty existing directory.
685 test_expect_success "$command: added submodule is checked out in empty dir" '
686 prolog &&
687 reset_work_tree_to_interested no_submodule &&
689 cd submodule_update &&
690 mkdir sub1 &&
691 git branch -t add_sub1 origin/add_sub1 &&
692 $command add_sub1 &&
693 test_superproject_content origin/add_sub1 &&
694 test_submodule_content sub1 origin/add_sub1
698 # Replacing a tracked file with a submodule produces a checked out submodule
699 test_expect_success "$command: replace tracked file with submodule checks out submodule" '
700 prolog &&
701 reset_work_tree_to_interested replace_sub1_with_file &&
703 cd submodule_update &&
704 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
705 $command replace_file_with_sub1 &&
706 test_superproject_content origin/replace_file_with_sub1 &&
707 test_submodule_content sub1 origin/replace_file_with_sub1
710 # ... as does removing a directory with tracked files with a submodule.
711 test_expect_success "$command: replace directory with submodule" '
712 prolog &&
713 reset_work_tree_to_interested replace_sub1_with_directory &&
715 cd submodule_update &&
716 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
717 $command replace_directory_with_sub1 &&
718 test_superproject_content origin/replace_directory_with_sub1 &&
719 test_submodule_content sub1 origin/replace_directory_with_sub1
722 # Switching to a commit with nested submodules recursively checks them out
723 test_expect_success "$command: nested submodules are checked out" '
724 prolog &&
725 reset_work_tree_to_interested no_submodule &&
727 cd submodule_update &&
728 git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
729 $command modify_sub1_recursively &&
730 test_superproject_content origin/modify_sub1_recursively &&
731 test_submodule_content sub1 origin/modify_sub1_recursively &&
732 test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
736 ######################## Disappearing submodule #######################
737 # Removing a submodule removes its work tree ...
738 test_expect_success "$command: removed submodule removes submodules working tree" '
739 prolog &&
740 reset_work_tree_to_interested add_sub1 &&
742 cd submodule_update &&
743 git branch -t remove_sub1 origin/remove_sub1 &&
744 $command remove_sub1 &&
745 test_superproject_content origin/remove_sub1 &&
746 ! test -e sub1 &&
747 test_must_fail git config -f .git/modules/sub1/config core.worktree
750 # ... absorbing a .git directory along the way.
751 test_expect_success "$command: removed submodule absorbs submodules .git directory" '
752 prolog &&
753 reset_work_tree_to_interested add_sub1 &&
755 cd submodule_update &&
756 git branch -t remove_sub1 origin/remove_sub1 &&
757 replace_gitfile_with_git_dir sub1 &&
758 rm -rf .git/modules &&
759 $command remove_sub1 &&
760 test_superproject_content origin/remove_sub1 &&
761 ! test -e sub1 &&
762 test_git_directory_exists sub1
766 # Replacing it with a file ...
767 test_expect_success "$command: replace submodule with a file" '
768 prolog &&
769 reset_work_tree_to_interested add_sub1 &&
771 cd submodule_update &&
772 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
773 $command replace_sub1_with_file &&
774 test_superproject_content origin/replace_sub1_with_file &&
775 test -f sub1
778 RESULTDS=success
779 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
780 then
781 RESULTDS=failure
783 # ... must check its local work tree for untracked files
784 test_expect_$RESULTDS "$command: replace submodule with a file must fail with untracked files" '
785 prolog &&
786 reset_work_tree_to_interested add_sub1 &&
788 cd submodule_update &&
789 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
790 : >sub1/untrackedfile &&
791 test_must_fail $command replace_sub1_with_file &&
792 test_superproject_content origin/add_sub1 &&
793 test_submodule_content sub1 origin/add_sub1 &&
794 test -f sub1/untracked_file
798 # Switching to a commit without nested submodules removes their worktrees
799 test_expect_success "$command: worktrees of nested submodules are removed" '
800 prolog &&
801 reset_work_tree_to_interested add_nested_sub &&
803 cd submodule_update &&
804 git branch -t no_submodule origin/no_submodule &&
805 $command no_submodule &&
806 test_superproject_content origin/no_submodule &&
807 ! test_path_is_dir sub1 &&
808 test_must_fail git config -f .git/modules/sub1/config core.worktree &&
809 test_must_fail git config -f .git/modules/sub1/modules/sub2/config core.worktree
813 ########################## Modified submodule #########################
814 # Updating a submodule sha1 updates the submodule's work tree
815 test_expect_success "$command: modified submodule updates submodule work tree" '
816 prolog &&
817 reset_work_tree_to_interested add_sub1 &&
819 cd submodule_update &&
820 git branch -t modify_sub1 origin/modify_sub1 &&
821 $command modify_sub1 &&
822 test_superproject_content origin/modify_sub1 &&
823 test_submodule_content sub1 origin/modify_sub1
826 # Updating a submodule to an invalid sha1 doesn't update the
827 # superproject nor the submodule's work tree.
828 test_expect_success "$command: updating to a missing submodule commit fails" '
829 prolog &&
830 reset_work_tree_to_interested add_sub1 &&
832 cd submodule_update &&
833 git branch -t invalid_sub1 origin/invalid_sub1 &&
834 test_must_fail $command invalid_sub1 2>err &&
835 test_i18ngrep sub1 err &&
836 test_superproject_content origin/add_sub1 &&
837 test_submodule_content sub1 origin/add_sub1
840 # Updating a submodule does not touch the currently checked out branch in the submodule
841 test_expect_success "$command: submodule branch is not changed, detach HEAD instead" '
842 prolog &&
843 reset_work_tree_to_interested add_sub1 &&
845 cd submodule_update &&
846 git -C sub1 checkout -b keep_branch &&
847 git -C sub1 rev-parse HEAD >expect &&
848 git branch -t modify_sub1 origin/modify_sub1 &&
849 $command modify_sub1 &&
850 test_superproject_content origin/modify_sub1 &&
851 test_submodule_content sub1 origin/modify_sub1 &&
852 git -C sub1 rev-parse keep_branch >actual &&
853 test_cmp expect actual &&
854 test_must_fail git -C sub1 symbolic-ref HEAD
859 # Declares and invokes several tests that, in various situations, checks that
860 # the provided Git command, when invoked with --recurse-submodules:
861 # - succeeds in updating the worktree and index of a superproject to a target
862 # commit, or fails atomically (depending on the test situation)
863 # - if succeeds, the contents of submodule directories are updated
865 # Specify the Git command so that "git $GIT_COMMAND --recurse-submodules"
866 # works.
868 # If the command under test is known to not work with submodules in certain
869 # conditions, set the appropriate KNOWN_FAILURE_* variable used in the tests
870 # below to 1.
872 # Use as follows:
874 # test_submodule_switch_recursing_with_args "$GIT_COMMAND"
875 test_submodule_switch_recursing_with_args () {
876 cmd_args="$1"
877 command="git $cmd_args"
878 test_submodule_recursing_with_args_common "$command"
880 RESULTDS=success
881 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
882 then
883 RESULTDS=failure
885 RESULTOI=success
886 if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
887 then
888 RESULTOI=failure
890 # Switching to a commit letting a submodule appear cannot override an
891 # untracked file.
892 test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
893 prolog &&
894 reset_work_tree_to_interested no_submodule &&
896 cd submodule_update &&
897 git branch -t add_sub1 origin/add_sub1 &&
898 : >sub1 &&
899 test_must_fail $command add_sub1 &&
900 test_superproject_content origin/no_submodule &&
901 test_must_be_empty sub1
904 # ... but an ignored file is fine.
905 test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
906 test_when_finished "rm -rf submodule_update/.git/info" &&
907 prolog &&
908 reset_work_tree_to_interested no_submodule &&
910 cd submodule_update &&
911 git branch -t add_sub1 origin/add_sub1 &&
912 : >sub1 &&
913 mkdir .git/info &&
914 echo sub1 >.git/info/exclude &&
915 $command add_sub1 &&
916 test_superproject_content origin/add_sub1 &&
917 test_submodule_content sub1 origin/add_sub1
921 # Replacing a submodule with files in a directory must succeeds
922 # when the submodule is clean
923 test_expect_$RESULTDS "$command: replace submodule with a directory" '
924 prolog &&
925 reset_work_tree_to_interested add_sub1 &&
927 cd submodule_update &&
928 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
929 $command replace_sub1_with_directory &&
930 test_superproject_content origin/replace_sub1_with_directory &&
931 test_submodule_content sub1 origin/replace_sub1_with_directory
934 # ... absorbing a .git directory.
935 test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
936 prolog &&
937 reset_work_tree_to_interested add_sub1 &&
939 cd submodule_update &&
940 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
941 replace_gitfile_with_git_dir sub1 &&
942 rm -rf .git/modules &&
943 $command replace_sub1_with_directory &&
944 test_superproject_content origin/replace_sub1_with_directory &&
945 test_git_directory_exists sub1
949 # ... and ignored files are ignored
950 test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
951 test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
952 prolog &&
953 reset_work_tree_to_interested add_sub1 &&
955 cd submodule_update &&
956 rm -rf .git/modules/sub1/info &&
957 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
958 mkdir .git/modules/sub1/info &&
959 echo ignored >.git/modules/sub1/info/exclude &&
960 : >sub1/ignored &&
961 $command replace_sub1_with_file &&
962 test_superproject_content origin/replace_sub1_with_file &&
963 test -f sub1
967 test_expect_success "git -c submodule.recurse=true $cmd_args: modified submodule updates submodule work tree" '
968 prolog &&
969 reset_work_tree_to_interested add_sub1 &&
971 cd submodule_update &&
972 git branch -t modify_sub1 origin/modify_sub1 &&
973 git -c submodule.recurse=true $cmd_args modify_sub1 &&
974 test_superproject_content origin/modify_sub1 &&
975 test_submodule_content sub1 origin/modify_sub1
979 test_expect_success "$command: modified submodule updates submodule recursively" '
980 prolog &&
981 reset_work_tree_to_interested add_nested_sub &&
983 cd submodule_update &&
984 git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
985 $command modify_sub1_recursively &&
986 test_superproject_content origin/modify_sub1_recursively &&
987 test_submodule_content sub1 origin/modify_sub1_recursively &&
988 test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
993 # Same as test_submodule_switch_recursing_with_args(), except that throwing
994 # away local changes in the superproject is allowed.
995 test_submodule_forced_switch_recursing_with_args () {
996 cmd_args="$1"
997 command="git $cmd_args"
998 test_submodule_recursing_with_args_common "$command"
1000 RESULT=success
1001 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
1002 then
1003 RESULT=failure
1005 # Switching to a commit letting a submodule appear does not care about
1006 # an untracked file.
1007 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
1008 prolog &&
1009 reset_work_tree_to_interested no_submodule &&
1011 cd submodule_update &&
1012 git branch -t add_sub1 origin/add_sub1 &&
1013 >sub1 &&
1014 $command add_sub1 &&
1015 test_superproject_content origin/add_sub1 &&
1016 test_submodule_content sub1 origin/add_sub1
1020 # Replacing a submodule with files in a directory ...
1021 test_expect_success "$command: replace submodule with a directory" '
1022 prolog &&
1023 reset_work_tree_to_interested add_sub1 &&
1025 cd submodule_update &&
1026 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1027 $command replace_sub1_with_directory &&
1028 test_superproject_content origin/replace_sub1_with_directory
1031 # ... absorbing a .git directory.
1032 test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
1033 prolog &&
1034 reset_work_tree_to_interested add_sub1 &&
1036 cd submodule_update &&
1037 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1038 replace_gitfile_with_git_dir sub1 &&
1039 rm -rf .git/modules/sub1 &&
1040 $command replace_sub1_with_directory &&
1041 test_superproject_content origin/replace_sub1_with_directory &&
1042 test_git_directory_exists sub1
1046 # ... even if the submodule contains ignored files
1047 test_expect_success "$command: replace submodule with a file ignoring ignored files" '
1048 prolog &&
1049 reset_work_tree_to_interested add_sub1 &&
1051 cd submodule_update &&
1052 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1053 : >sub1/expect &&
1054 $command replace_sub1_with_file &&
1055 test_superproject_content origin/replace_sub1_with_file
1059 # Updating a submodule from an invalid sha1 updates
1060 test_expect_success "$command: modified submodule does update submodule work tree from invalid commit" '
1061 prolog &&
1062 reset_work_tree_to_interested invalid_sub1 &&
1064 cd submodule_update &&
1065 git branch -t valid_sub1 origin/valid_sub1 &&
1066 $command valid_sub1 &&
1067 test_superproject_content origin/valid_sub1 &&
1068 test_submodule_content sub1 origin/valid_sub1
1072 # Old versions of Git were buggy writing the .git link file
1073 # (e.g. before f8eaa0ba98b and then moving the superproject repo
1074 # whose submodules contained absolute paths)
1075 test_expect_success "$command: updating submodules fixes .git links" '
1076 prolog &&
1077 reset_work_tree_to_interested add_sub1 &&
1079 cd submodule_update &&
1080 git branch -t modify_sub1 origin/modify_sub1 &&
1081 echo "gitdir: bogus/path" >sub1/.git &&
1082 $command modify_sub1 &&
1083 test_superproject_content origin/modify_sub1 &&
1084 test_submodule_content sub1 origin/modify_sub1
1088 test_expect_success "$command: changed submodule worktree is reset" '
1089 prolog &&
1090 reset_work_tree_to_interested add_sub1 &&
1092 cd submodule_update &&
1093 rm sub1/file1 &&
1094 : >sub1/new_file &&
1095 git -C sub1 add new_file &&
1096 $command HEAD &&
1097 test_path_is_file sub1/file1 &&
1098 test_path_is_missing sub1/new_file