]> git.ipfire.org Git - thirdparty/git.git/blob - t/lib-submodule-update.sh
Sync with 2.32.4
[thirdparty/git.git] / t / lib-submodule-update.sh
1 # Create a submodule layout used for all tests below.
2 #
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)
18 #
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
38 #
39
40 create_lib_submodule_repo () {
41 git init submodule_update_sub1 &&
42 (
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 &&
53 (
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 &&
64 (
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" &&
74
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" &&
82
83 git checkout -b remove_sub1 add_sub1 &&
84 git revert HEAD &&
85
86 git checkout -b modify_sub1 add_sub1 &&
87 git submodule update &&
88 (
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" &&
100
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 &&
120
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" &&
129
130 git checkout -b replace_directory_with_sub1 &&
131 git revert HEAD &&
132
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" &&
138
139 git checkout -b replace_file_with_sub1 &&
140 git revert HEAD &&
141
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 &&
147
148 git checkout "$branch"
149 )
150 }
151
152 # Helper function to replace gitfile with .git directory
153 replace_gitfile_with_git_dir () {
154 (
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
160 )
161 }
162
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.
167 #
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 () {
171 (
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" &&
180 (
181 # ... and then restore.
182 cd ".git/modules/$1" &&
183 git config core.worktree "../../../$1"
184 )
185 }
186
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"
193 fi
194 }
195
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
204 }
205
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 submodule_update_repo submodule_update &&
212 (
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"
223 fi
224 )
225 }
226
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"
246 }
247
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
256 }
257
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;
265 fi
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 &&
285 (
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
291 )
292 }
293
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
302 #
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.
306 #
307 # The first argument of the callback function will be the name of the submodule.
308 #
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.
313 #
314
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 &&
324 (
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
332 )
333 '
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"
341 fi
342 test_expect_$RESULT "$command: added submodule leaves existing empty directory alone" '
343 prolog &&
344 reset_work_tree_to no_submodule &&
345 (
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
354 )
355 '
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 &&
361 (
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
369 )
370 '
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"
381 fi
382 test_expect_$RESULT "$command: replace directory with submodule" '
383 prolog &&
384 reset_work_tree_to replace_sub1_with_directory &&
385 (
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
393 )
394 '
395
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"
403 fi
404 test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
405 prolog &&
406 reset_work_tree_to add_sub1 &&
407 (
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
413 )
414 '
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 &&
419 (
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
427 )
428 '
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"
444 fi
445 test_expect_$RESULT "$command: replace submodule with a directory must fail" '
446 prolog &&
447 reset_work_tree_to add_sub1 &&
448 (
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
454 )
455 '
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 &&
460 (
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
468 )
469 '
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 &&
475 (
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
481 )
482 '
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 &&
488 (
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
496 )
497 '
498
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"
509 fi
510 test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
511 prolog &&
512 reset_work_tree_to add_sub1 &&
513 (
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
521 )
522 '
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 &&
528 (
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
536 )
537 '
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 &&
543 (
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
551 )
552 '
553 }
554
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
562 #
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.
566 #
567 # The first argument of the callback function will be the name of the submodule.
568 #
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.
573 #
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.
577 #
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...
586 # }
587 # test_submodule_switch_func "my_func"
588 test_submodule_switch_func () {
589 command="$1"
590 test_submodule_switch_common "$command"
591
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 &&
597 (
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
604 )
605 '
606 }
607
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
611 }
612
613 git_test_func () {
614 may_only_be_test_must_fail "$2" &&
615 $2 git $gitcmd "$1"
616 }
617
618 test_submodule_switch () {
619 gitcmd="$1"
620 test_submodule_switch_func "git_test_func"
621 }
622
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"
630
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 &&
636 (
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
643 )
644 '
645 }
646
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
659 #
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
665
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"
670
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 &&
676 (
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
682 )
683 '
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 &&
688 (
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
695 )
696 '
697
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 &&
702 (
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
708 )
709 '
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 &&
714 (
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
720 )
721 '
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 &&
726 (
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
733 )
734 '
735
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 &&
741 (
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
748 )
749 '
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 &&
754 (
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
763 )
764 '
765
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 &&
770 (
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
776 )
777 '
778 RESULTDS=success
779 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
780 then
781 RESULTDS=failure
782 fi
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 &&
787 (
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
795 )
796 '
797
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 &&
802 (
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
810 )
811 '
812
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 &&
818 (
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
824 )
825 '
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 &&
831 (
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
838 )
839 '
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 &&
844 (
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
855 )
856 '
857 }
858
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
864 #
865 # Specify the Git command so that "git $GIT_COMMAND --recurse-submodules"
866 # works.
867 #
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.
871 #
872 # Use as follows:
873 #
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"
879
880 RESULTDS=success
881 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
882 then
883 RESULTDS=failure
884 fi
885 RESULTOI=success
886 if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
887 then
888 RESULTOI=failure
889 fi
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 &&
895 (
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
902 )
903 '
904 # ... but an ignored file is fine.
905 test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
906 test_when_finished "rm submodule_update/.git/info/exclude" &&
907 prolog &&
908 reset_work_tree_to_interested no_submodule &&
909 (
910 cd submodule_update &&
911 git branch -t add_sub1 origin/add_sub1 &&
912 : >sub1 &&
913 echo sub1 >.git/info/exclude &&
914 $command add_sub1 &&
915 test_superproject_content origin/add_sub1 &&
916 test_submodule_content sub1 origin/add_sub1
917 )
918 '
919
920 # Replacing a submodule with files in a directory must succeeds
921 # when the submodule is clean
922 test_expect_$RESULTDS "$command: replace submodule with a directory" '
923 prolog &&
924 reset_work_tree_to_interested add_sub1 &&
925 (
926 cd submodule_update &&
927 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
928 $command replace_sub1_with_directory &&
929 test_superproject_content origin/replace_sub1_with_directory &&
930 test_submodule_content sub1 origin/replace_sub1_with_directory
931 )
932 '
933 # ... absorbing a .git directory.
934 test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
935 prolog &&
936 reset_work_tree_to_interested add_sub1 &&
937 (
938 cd submodule_update &&
939 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
940 replace_gitfile_with_git_dir sub1 &&
941 rm -rf .git/modules &&
942 $command replace_sub1_with_directory &&
943 test_superproject_content origin/replace_sub1_with_directory &&
944 test_git_directory_exists sub1
945 )
946 '
947
948 # ... and ignored files are ignored
949 test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
950 test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
951 prolog &&
952 reset_work_tree_to_interested add_sub1 &&
953 (
954 cd submodule_update &&
955 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
956 echo ignored >.git/modules/sub1/info/exclude &&
957 : >sub1/ignored &&
958 $command replace_sub1_with_file &&
959 test_superproject_content origin/replace_sub1_with_file &&
960 test -f sub1
961 )
962 '
963
964 test_expect_success "git -c submodule.recurse=true $cmd_args: modified submodule updates submodule work tree" '
965 prolog &&
966 reset_work_tree_to_interested add_sub1 &&
967 (
968 cd submodule_update &&
969 git branch -t modify_sub1 origin/modify_sub1 &&
970 git -c submodule.recurse=true $cmd_args modify_sub1 &&
971 test_superproject_content origin/modify_sub1 &&
972 test_submodule_content sub1 origin/modify_sub1
973 )
974 '
975
976 test_expect_success "$command: modified submodule updates submodule recursively" '
977 prolog &&
978 reset_work_tree_to_interested add_nested_sub &&
979 (
980 cd submodule_update &&
981 git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
982 $command modify_sub1_recursively &&
983 test_superproject_content origin/modify_sub1_recursively &&
984 test_submodule_content sub1 origin/modify_sub1_recursively &&
985 test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
986 )
987 '
988 }
989
990 # Same as test_submodule_switch_recursing_with_args(), except that throwing
991 # away local changes in the superproject is allowed.
992 test_submodule_forced_switch_recursing_with_args () {
993 cmd_args="$1"
994 command="git $cmd_args"
995 test_submodule_recursing_with_args_common "$command"
996
997 RESULT=success
998 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
999 then
1000 RESULT=failure
1001 fi
1002 # Switching to a commit letting a submodule appear does not care about
1003 # an untracked file.
1004 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
1005 prolog &&
1006 reset_work_tree_to_interested no_submodule &&
1007 (
1008 cd submodule_update &&
1009 git branch -t add_sub1 origin/add_sub1 &&
1010 >sub1 &&
1011 $command add_sub1 &&
1012 test_superproject_content origin/add_sub1 &&
1013 test_submodule_content sub1 origin/add_sub1
1014 )
1015 '
1016
1017 # Replacing a submodule with files in a directory ...
1018 test_expect_success "$command: replace submodule with a directory" '
1019 prolog &&
1020 reset_work_tree_to_interested add_sub1 &&
1021 (
1022 cd submodule_update &&
1023 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1024 $command replace_sub1_with_directory &&
1025 test_superproject_content origin/replace_sub1_with_directory
1026 )
1027 '
1028 # ... absorbing a .git directory.
1029 test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
1030 prolog &&
1031 reset_work_tree_to_interested add_sub1 &&
1032 (
1033 cd submodule_update &&
1034 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1035 replace_gitfile_with_git_dir sub1 &&
1036 rm -rf .git/modules/sub1 &&
1037 $command replace_sub1_with_directory &&
1038 test_superproject_content origin/replace_sub1_with_directory &&
1039 test_git_directory_exists sub1
1040 )
1041 '
1042
1043 # ... even if the submodule contains ignored files
1044 test_expect_success "$command: replace submodule with a file ignoring ignored files" '
1045 prolog &&
1046 reset_work_tree_to_interested add_sub1 &&
1047 (
1048 cd submodule_update &&
1049 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1050 : >sub1/expect &&
1051 $command replace_sub1_with_file &&
1052 test_superproject_content origin/replace_sub1_with_file
1053 )
1054 '
1055
1056 # Updating a submodule from an invalid sha1 updates
1057 test_expect_success "$command: modified submodule does update submodule work tree from invalid commit" '
1058 prolog &&
1059 reset_work_tree_to_interested invalid_sub1 &&
1060 (
1061 cd submodule_update &&
1062 git branch -t valid_sub1 origin/valid_sub1 &&
1063 $command valid_sub1 &&
1064 test_superproject_content origin/valid_sub1 &&
1065 test_submodule_content sub1 origin/valid_sub1
1066 )
1067 '
1068
1069 # Old versions of Git were buggy writing the .git link file
1070 # (e.g. before f8eaa0ba98b and then moving the superproject repo
1071 # whose submodules contained absolute paths)
1072 test_expect_success "$command: updating submodules fixes .git links" '
1073 prolog &&
1074 reset_work_tree_to_interested add_sub1 &&
1075 (
1076 cd submodule_update &&
1077 git branch -t modify_sub1 origin/modify_sub1 &&
1078 echo "gitdir: bogus/path" >sub1/.git &&
1079 $command modify_sub1 &&
1080 test_superproject_content origin/modify_sub1 &&
1081 test_submodule_content sub1 origin/modify_sub1
1082 )
1083 '
1084
1085 test_expect_success "$command: changed submodule worktree is reset" '
1086 prolog &&
1087 reset_work_tree_to_interested add_sub1 &&
1088 (
1089 cd submodule_update &&
1090 rm sub1/file1 &&
1091 : >sub1/new_file &&
1092 git -C sub1 add new_file &&
1093 $command HEAD &&
1094 test_path_is_file sub1/file1 &&
1095 test_path_is_missing sub1/new_file
1096 )
1097 '
1098 }