cherry-pick: add t3512 for submodule updates
[git/git-svn.git] / t / lib-submodule-update.sh
blobd499b2752e6ad4f4da6806f172d1c6ec7094aa57
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 # - Submodule updated to invalid commit (add_sub1 => invalid_sub1)
8 # - Submodule updated from invalid commit (invalid_sub1 => valid_sub1)
9 # - Submodule replaced by tracked files in directory (add_sub1 =>
10 # replace_sub1_with_directory)
11 # - Directory containing tracked files replaced by submodule
12 # (replace_sub1_with_directory => replace_directory_with_sub1)
13 # - Submodule replaced by tracked file with the same name (add_sub1 =>
14 # replace_sub1_with_file)
15 # - Tracked file replaced by submodule (replace_sub1_with_file =>
16 # replace_file_with_sub1)
18 # --O-----O
19 # / ^ replace_directory_with_sub1
20 # / replace_sub1_with_directory
21 # /----O
22 # / ^
23 # / modify_sub1
24 # O------O-------O
25 # ^ ^\ ^
26 # | | \ remove_sub1
27 # | | -----O-----O
28 # | | \ ^ replace_file_with_sub1
29 # | | \ replace_sub1_with_file
30 # | add_sub1 --O-----O
31 # no_submodule ^ valid_sub1
32 # invalid_sub1
34 create_lib_submodule_repo () {
35 git init submodule_update_repo &&
37 cd submodule_update_repo &&
38 echo "expect" >>.gitignore &&
39 echo "actual" >>.gitignore &&
40 echo "x" >file1 &&
41 echo "y" >file2 &&
42 git add .gitignore file1 file2 &&
43 git commit -m "Base" &&
44 git branch "no_submodule" &&
46 git checkout -b "add_sub1" &&
47 git submodule add ./. sub1 &&
48 git config -f .gitmodules submodule.sub1.ignore all &&
49 git config submodule.sub1.ignore all &&
50 git add .gitmodules &&
51 git commit -m "Add sub1" &&
52 git checkout -b remove_sub1 &&
53 git revert HEAD &&
55 git checkout -b "modify_sub1" "add_sub1" &&
56 git submodule update &&
58 cd sub1 &&
59 git fetch &&
60 git checkout -b "modifications" &&
61 echo "z" >file2 &&
62 echo "x" >file3 &&
63 git add file2 file3 &&
64 git commit -m "modified file2 and added file3" &&
65 git push origin modifications
66 ) &&
67 git add sub1 &&
68 git commit -m "Modify sub1" &&
70 git checkout -b "replace_sub1_with_directory" "add_sub1" &&
71 git submodule update &&
73 cd sub1 &&
74 git checkout modifications
75 ) &&
76 git rm --cached sub1 &&
77 rm sub1/.git* &&
78 git config -f .gitmodules --remove-section "submodule.sub1" &&
79 git add .gitmodules sub1/* &&
80 git commit -m "Replace sub1 with directory" &&
81 git checkout -b replace_directory_with_sub1 &&
82 git revert HEAD &&
84 git checkout -b "replace_sub1_with_file" "add_sub1" &&
85 git rm sub1 &&
86 echo "content" >sub1 &&
87 git add sub1 &&
88 git commit -m "Replace sub1 with file" &&
89 git checkout -b replace_file_with_sub1 &&
90 git revert HEAD &&
92 git checkout -b "invalid_sub1" "add_sub1" &&
93 git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 sub1 &&
94 git commit -m "Invalid sub1 commit" &&
95 git checkout -b valid_sub1 &&
96 git revert HEAD &&
97 git checkout master
101 # Helper function to replace gitfile with .git directory
102 replace_gitfile_with_git_dir () {
104 cd "$1" &&
105 git_dir="$(git rev-parse --git-dir)" &&
106 rm -f .git &&
107 cp -R "$git_dir" .git &&
108 GIT_WORK_TREE=. git config --unset core.worktree
112 # Test that the .git directory in the submodule is unchanged (except for the
113 # core.worktree setting, which appears only in $GIT_DIR/modules/$1/config).
114 # Call this function before test_submodule_content as the latter might
115 # write the index file leading to false positive index differences.
117 # Note that this only supports submodules at the root level of the
118 # superproject, with the default name, i.e. same as its path.
119 test_git_directory_is_unchanged () {
121 cd ".git/modules/$1" &&
122 # does core.worktree point at the right place?
123 test "$(git config core.worktree)" = "../../../$1" &&
124 # remove it temporarily before comparing, as
125 # "$1/.git/config" lacks it...
126 git config --unset core.worktree
127 ) &&
128 diff -r ".git/modules/$1" "$1/.git" &&
130 # ... and then restore.
131 cd ".git/modules/$1" &&
132 git config core.worktree "../../../$1"
136 # Helper function to be executed at the start of every test below, it sets up
137 # the submodule repo if it doesn't exist and configures the most problematic
138 # settings for diff.ignoreSubmodules.
139 prolog () {
140 (test -d submodule_update_repo || create_lib_submodule_repo) &&
141 test_config_global diff.ignoreSubmodules all &&
142 test_config diff.ignoreSubmodules all
145 # Helper function to bring work tree back into the state given by the
146 # commit. This includes trying to populate sub1 accordingly if it exists and
147 # should be updated to an existing commit.
148 reset_work_tree_to () {
149 rm -rf submodule_update &&
150 git clone submodule_update_repo submodule_update &&
152 cd submodule_update &&
153 rm -rf sub1 &&
154 git checkout -f "$1" &&
155 git status -u -s >actual &&
156 test_must_be_empty actual &&
157 sha1=$(git rev-parse --revs-only HEAD:sub1) &&
158 if test -n "$sha1" &&
159 test $(cd "sub1" && git rev-parse --verify "$sha1^{commit}")
160 then
161 git submodule update --init --recursive "sub1"
166 # Test that the superproject contains the content according to commit "$1"
167 # (the work tree must match the index for everything but submodules but the
168 # index must exactly match the given commit including any submodule SHA-1s).
169 test_superproject_content () {
170 git diff-index --cached "$1" >actual &&
171 test_must_be_empty actual &&
172 git diff-files --ignore-submodules >actual &&
173 test_must_be_empty actual
176 # Test that the given submodule at path "$1" contains the content according
177 # to the submodule commit recorded in the superproject's commit "$2"
178 test_submodule_content () {
179 if test $# != 2
180 then
181 echo "test_submodule_content needs two arguments"
182 return 1
183 fi &&
184 submodule="$1" &&
185 commit="$2" &&
186 test -d "$submodule"/ &&
187 if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
188 then
189 echo "Submodule $submodule is not populated"
190 return 1
191 fi &&
192 sha1=$(git rev-parse --verify "$commit:$submodule") &&
193 if test -z "$sha1"
194 then
195 echo "Couldn't retrieve SHA-1 of $submodule for $commit"
196 return 1
197 fi &&
199 cd "$submodule" &&
200 git status -u -s >actual &&
201 test_must_be_empty actual &&
202 git diff "$sha1" >actual &&
203 test_must_be_empty actual
207 # Test that the following transitions are correctly handled:
208 # - Updated submodule
209 # - New submodule
210 # - Removed submodule
211 # - Directory containing tracked files replaced by submodule
212 # - Submodule replaced by tracked files in directory
213 # - Submodule replaced by tracked file with the same name
214 # - tracked file replaced by submodule
216 # The default is that submodule contents aren't changed until "git submodule
217 # update" is run. And even then that command doesn't delete the work tree of
218 # a removed submodule.
220 # Removing a submodule containing a .git directory must fail even when forced
221 # to protect the history!
224 # Test that submodule contents are currently not updated when switching
225 # between commits that change a submodule.
226 test_submodule_switch () {
227 command="$1"
228 ######################### Appearing submodule #########################
229 # Switching to a commit letting a submodule appear creates empty dir ...
230 test_expect_success "$command: added submodule creates empty directory" '
231 prolog &&
232 reset_work_tree_to no_submodule &&
234 cd submodule_update &&
235 git branch -t add_sub1 origin/add_sub1 &&
236 $command add_sub1 &&
237 test_superproject_content origin/add_sub1 &&
238 test_dir_is_empty sub1 &&
239 git submodule update --init --recursive &&
240 test_submodule_content sub1 origin/add_sub1
243 # ... and doesn't care if it already exists ...
244 test_expect_success "$command: added submodule leaves existing empty directory alone" '
245 prolog &&
246 reset_work_tree_to no_submodule &&
248 cd submodule_update &&
249 mkdir sub1 &&
250 git branch -t add_sub1 origin/add_sub1 &&
251 $command add_sub1 &&
252 test_superproject_content origin/add_sub1 &&
253 test_dir_is_empty sub1 &&
254 git submodule update --init --recursive &&
255 test_submodule_content sub1 origin/add_sub1
258 # ... unless there is an untracked file in its place.
259 test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
260 prolog &&
261 reset_work_tree_to no_submodule &&
263 cd submodule_update &&
264 git branch -t add_sub1 origin/add_sub1 &&
265 >sub1 &&
266 test_must_fail $command add_sub1 &&
267 test_superproject_content origin/no_submodule &&
268 test_must_be_empty sub1
271 # Replacing a tracked file with a submodule produces an empty
272 # directory ...
273 test_expect_success "$command: replace tracked file with submodule creates empty directory" '
274 prolog &&
275 reset_work_tree_to replace_sub1_with_file &&
277 cd submodule_update &&
278 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
279 $command replace_file_with_sub1 &&
280 test_superproject_content origin/replace_file_with_sub1 &&
281 test_dir_is_empty sub1 &&
282 git submodule update --init --recursive &&
283 test_submodule_content sub1 origin/replace_file_with_sub1
286 # ... as does removing a directory with tracked files with a
287 # submodule.
288 if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
289 then
290 # Non fast-forward merges fail with "Directory sub1 doesn't
291 # exist. sub1" because the empty submodule directory is not
292 # created
293 RESULT="failure"
294 else
295 RESULT="success"
297 test_expect_$RESULT "$command: replace directory with submodule" '
298 prolog &&
299 reset_work_tree_to replace_sub1_with_directory &&
301 cd submodule_update &&
302 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
303 $command replace_directory_with_sub1 &&
304 test_superproject_content origin/replace_directory_with_sub1 &&
305 test_dir_is_empty sub1 &&
306 git submodule update --init --recursive &&
307 test_submodule_content sub1 origin/replace_directory_with_sub1
311 ######################## Disappearing submodule #######################
312 # Removing a submodule doesn't remove its work tree ...
313 test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
314 prolog &&
315 reset_work_tree_to add_sub1 &&
317 cd submodule_update &&
318 git branch -t remove_sub1 origin/remove_sub1 &&
319 $command remove_sub1 &&
320 test_superproject_content origin/remove_sub1 &&
321 test_submodule_content sub1 origin/add_sub1
324 # ... especially when it contains a .git directory.
325 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
326 prolog &&
327 reset_work_tree_to add_sub1 &&
329 cd submodule_update &&
330 git branch -t remove_sub1 origin/remove_sub1 &&
331 replace_gitfile_with_git_dir sub1 &&
332 $command remove_sub1 &&
333 test_superproject_content origin/remove_sub1 &&
334 test_git_directory_is_unchanged sub1 &&
335 test_submodule_content sub1 origin/add_sub1
338 # Replacing a submodule with files in a directory must fail as the
339 # submodule work tree isn't removed ...
340 if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
341 then
342 # Non fast-forward merges attempt to merge the former
343 # submodule files with the newly checked out ones in the
344 # directory of the same name while it shouldn't.
345 RESULT="failure"
346 else
347 RESULT="success"
349 test_expect_$RESULT "$command: replace submodule with a directory must fail" '
350 prolog &&
351 reset_work_tree_to add_sub1 &&
353 cd submodule_update &&
354 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
355 test_must_fail $command replace_sub1_with_directory &&
356 test_superproject_content origin/add_sub1 &&
357 test_submodule_content sub1 origin/add_sub1
360 # ... especially when it contains a .git directory.
361 test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
362 prolog &&
363 reset_work_tree_to add_sub1 &&
365 cd submodule_update &&
366 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
367 replace_gitfile_with_git_dir sub1 &&
368 test_must_fail $command replace_sub1_with_directory &&
369 test_superproject_content origin/add_sub1 &&
370 test_git_directory_is_unchanged sub1 &&
371 test_submodule_content sub1 origin/add_sub1
374 # Replacing it with a file must fail as it could throw away any local
375 # work tree changes ...
376 test_expect_failure "$command: replace submodule with a file must fail" '
377 prolog &&
378 reset_work_tree_to add_sub1 &&
380 cd submodule_update &&
381 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
382 test_must_fail $command replace_sub1_with_file &&
383 test_superproject_content origin/add_sub1 &&
384 test_submodule_content sub1 origin/add_sub1
387 # ... or even destroy unpushed parts of submodule history if that
388 # still uses a .git directory.
389 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
390 prolog &&
391 reset_work_tree_to add_sub1 &&
393 cd submodule_update &&
394 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
395 replace_gitfile_with_git_dir sub1 &&
396 test_must_fail $command replace_sub1_with_file &&
397 test_superproject_content origin/add_sub1 &&
398 test_git_directory_is_unchanged sub1 &&
399 test_submodule_content sub1 origin/add_sub1
403 ########################## Modified submodule #########################
404 # Updating a submodule sha1 doesn't update the submodule's work tree
405 if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
406 then
407 # When cherry picking a SHA-1 update for an ignored submodule
408 # the commit incorrectly fails with "The previous cherry-pick
409 # is now empty, possibly due to conflict resolution."
410 RESULT="failure"
411 else
412 RESULT="success"
414 test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
415 prolog &&
416 reset_work_tree_to add_sub1 &&
418 cd submodule_update &&
419 git branch -t modify_sub1 origin/modify_sub1 &&
420 $command modify_sub1 &&
421 test_superproject_content origin/modify_sub1 &&
422 test_submodule_content sub1 origin/add_sub1 &&
423 git submodule update &&
424 test_submodule_content sub1 origin/modify_sub1
428 # Updating a submodule to an invalid sha1 doesn't update the
429 # submodule's work tree, subsequent update will fail
430 test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
431 prolog &&
432 reset_work_tree_to add_sub1 &&
434 cd submodule_update &&
435 git branch -t invalid_sub1 origin/invalid_sub1 &&
436 $command invalid_sub1 &&
437 test_superproject_content origin/invalid_sub1 &&
438 test_submodule_content sub1 origin/add_sub1 &&
439 test_must_fail git submodule update &&
440 test_submodule_content sub1 origin/add_sub1
443 # Updating a submodule from an invalid sha1 doesn't update the
444 # submodule's work tree, subsequent update will succeed
445 test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
446 prolog &&
447 reset_work_tree_to invalid_sub1 &&
449 cd submodule_update &&
450 git branch -t valid_sub1 origin/valid_sub1 &&
451 $command valid_sub1 &&
452 test_superproject_content origin/valid_sub1 &&
453 test_dir_is_empty sub1 &&
454 git submodule update --init --recursive &&
455 test_submodule_content sub1 origin/valid_sub1
460 # Test that submodule contents are currently not updated when switching
461 # between commits that change a submodule, but throwing away local changes in
462 # the superproject is allowed.
463 test_submodule_forced_switch () {
464 command="$1"
465 ######################### Appearing submodule #########################
466 # Switching to a commit letting a submodule appear creates empty dir ...
467 test_expect_success "$command: added submodule creates empty directory" '
468 prolog &&
469 reset_work_tree_to no_submodule &&
471 cd submodule_update &&
472 git branch -t add_sub1 origin/add_sub1 &&
473 $command add_sub1 &&
474 test_superproject_content origin/add_sub1 &&
475 test_dir_is_empty sub1 &&
476 git submodule update --init --recursive &&
477 test_submodule_content sub1 origin/add_sub1
480 # ... and doesn't care if it already exists ...
481 test_expect_success "$command: added submodule leaves existing empty directory alone" '
482 prolog &&
483 reset_work_tree_to no_submodule &&
485 cd submodule_update &&
486 git branch -t add_sub1 origin/add_sub1 &&
487 mkdir sub1 &&
488 $command add_sub1 &&
489 test_superproject_content origin/add_sub1 &&
490 test_dir_is_empty sub1 &&
491 git submodule update --init --recursive &&
492 test_submodule_content sub1 origin/add_sub1
495 # ... unless there is an untracked file in its place.
496 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
497 prolog &&
498 reset_work_tree_to no_submodule &&
500 cd submodule_update &&
501 git branch -t add_sub1 origin/add_sub1 &&
502 >sub1 &&
503 $command add_sub1 &&
504 test_superproject_content origin/add_sub1 &&
505 test_dir_is_empty sub1
508 # Replacing a tracked file with a submodule produces an empty
509 # directory ...
510 test_expect_success "$command: replace tracked file with submodule creates empty directory" '
511 prolog &&
512 reset_work_tree_to replace_sub1_with_file &&
514 cd submodule_update &&
515 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
516 $command replace_file_with_sub1 &&
517 test_superproject_content origin/replace_file_with_sub1 &&
518 test_dir_is_empty sub1 &&
519 git submodule update --init --recursive &&
520 test_submodule_content sub1 origin/replace_file_with_sub1
523 # ... as does removing a directory with tracked files with a
524 # submodule.
525 test_expect_success "$command: replace directory with submodule" '
526 prolog &&
527 reset_work_tree_to replace_sub1_with_directory &&
529 cd submodule_update &&
530 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
531 $command replace_directory_with_sub1 &&
532 test_superproject_content origin/replace_directory_with_sub1 &&
533 test_dir_is_empty sub1 &&
534 git submodule update --init --recursive &&
535 test_submodule_content sub1 origin/replace_directory_with_sub1
539 ######################## Disappearing submodule #######################
540 # Removing a submodule doesn't remove its work tree ...
541 test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
542 prolog &&
543 reset_work_tree_to add_sub1 &&
545 cd submodule_update &&
546 git branch -t remove_sub1 origin/remove_sub1 &&
547 $command remove_sub1 &&
548 test_superproject_content origin/remove_sub1 &&
549 test_submodule_content sub1 origin/add_sub1
552 # ... especially when it contains a .git directory.
553 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
554 prolog &&
555 reset_work_tree_to add_sub1 &&
557 cd submodule_update &&
558 git branch -t remove_sub1 origin/remove_sub1 &&
559 replace_gitfile_with_git_dir sub1 &&
560 $command remove_sub1 &&
561 test_superproject_content origin/remove_sub1 &&
562 test_git_directory_is_unchanged sub1 &&
563 test_submodule_content sub1 origin/add_sub1
566 # Replacing a submodule with files in a directory must fail as the
567 # submodule work tree isn't removed ...
568 test_expect_failure "$command: replace submodule with a directory must fail" '
569 prolog &&
570 reset_work_tree_to add_sub1 &&
572 cd submodule_update &&
573 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
574 test_must_fail $command replace_sub1_with_directory &&
575 test_superproject_content origin/add_sub1 &&
576 test_submodule_content sub1 origin/add_sub1
579 # ... especially when it contains a .git directory.
580 test_expect_failure "$command: replace submodule containing a .git directory with a directory must fail" '
581 prolog &&
582 reset_work_tree_to add_sub1 &&
584 cd submodule_update &&
585 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
586 replace_gitfile_with_git_dir sub1 &&
587 test_must_fail $command replace_sub1_with_directory &&
588 test_superproject_content origin/add_sub1 &&
589 test_git_directory_is_unchanged sub1 &&
590 test_submodule_content sub1 origin/add_sub1
593 # Replacing it with a file must fail as it could throw away any local
594 # work tree changes ...
595 test_expect_failure "$command: replace submodule with a file must fail" '
596 prolog &&
597 reset_work_tree_to add_sub1 &&
599 cd submodule_update &&
600 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
601 test_must_fail $command replace_sub1_with_file &&
602 test_superproject_content origin/add_sub1 &&
603 test_submodule_content sub1 origin/add_sub1
606 # ... or even destroy unpushed parts of submodule history if that
607 # still uses a .git directory.
608 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
609 prolog &&
610 reset_work_tree_to add_sub1 &&
612 cd submodule_update &&
613 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
614 replace_gitfile_with_git_dir sub1 &&
615 test_must_fail $command replace_sub1_with_file &&
616 test_superproject_content origin/add_sub1 &&
617 test_git_directory_is_unchanged sub1 &&
618 test_submodule_content sub1 origin/add_sub1
622 ########################## Modified submodule #########################
623 # Updating a submodule sha1 doesn't update the submodule's work tree
624 test_expect_success "$command: modified submodule does not update submodule work tree" '
625 prolog &&
626 reset_work_tree_to add_sub1 &&
628 cd submodule_update &&
629 git branch -t modify_sub1 origin/modify_sub1 &&
630 $command modify_sub1 &&
631 test_superproject_content origin/modify_sub1 &&
632 test_submodule_content sub1 origin/add_sub1 &&
633 git submodule update &&
634 test_submodule_content sub1 origin/modify_sub1
637 # Updating a submodule to an invalid sha1 doesn't update the
638 # submodule's work tree, subsequent update will fail
639 test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
640 prolog &&
641 reset_work_tree_to add_sub1 &&
643 cd submodule_update &&
644 git branch -t invalid_sub1 origin/invalid_sub1 &&
645 $command invalid_sub1 &&
646 test_superproject_content origin/invalid_sub1 &&
647 test_submodule_content sub1 origin/add_sub1 &&
648 test_must_fail git submodule update &&
649 test_submodule_content sub1 origin/add_sub1
652 # Updating a submodule from an invalid sha1 doesn't update the
653 # submodule's work tree, subsequent update will succeed
654 test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
655 prolog &&
656 reset_work_tree_to invalid_sub1 &&
658 cd submodule_update &&
659 git branch -t valid_sub1 origin/valid_sub1 &&
660 $command valid_sub1 &&
661 test_superproject_content origin/valid_sub1 &&
662 test_dir_is_empty sub1 &&
663 git submodule update --init --recursive &&
664 test_submodule_content sub1 origin/valid_sub1