submodule.c: uninitialized submodules are ignored in recursive commands
[git/git.git] / t / lib-submodule-update.sh
CommitLineData
42639d23
JL
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)
259f3ee2 7# - Updated submodule recursively (add_nested_sub => modify_sub1_recursively)
42639d23
JL
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#
eb41e7fe
SB
19# ----O
20# / ^
21# / remove_sub1
22# /
259f3ee2
SB
23# add_sub1 /-------O---------O--------O modify_sub1_recursively
24# | / ^ add_nested_sub
eb41e7fe
SB
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
42639d23 38#
eb41e7fe 39
42639d23 40create_lib_submodule_repo () {
a70ef2da
SB
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 ) &&
259f3ee2
SB
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 ) &&
42639d23
JL
63 git init submodule_update_repo &&
64 (
65 cd submodule_update_repo &&
66 echo "expect" >>.gitignore &&
67 echo "actual" >>.gitignore &&
68 echo "x" >file1 &&
69 echo "y" >file2 &&
70 git add .gitignore file1 file2 &&
71 git commit -m "Base" &&
72 git branch "no_submodule" &&
73
74 git checkout -b "add_sub1" &&
a70ef2da 75 git submodule add ../submodule_update_sub1 sub1 &&
823bab09 76 git submodule add ../submodule_update_sub1 uninitialized_sub &&
42639d23
JL
77 git config -f .gitmodules submodule.sub1.ignore all &&
78 git config submodule.sub1.ignore all &&
79 git add .gitmodules &&
80 git commit -m "Add sub1" &&
eb41e7fe
SB
81
82 git checkout -b remove_sub1 add_sub1 &&
42639d23
JL
83 git revert HEAD &&
84
eb41e7fe 85 git checkout -b modify_sub1 add_sub1 &&
42639d23
JL
86 git submodule update &&
87 (
88 cd sub1 &&
89 git fetch &&
90 git checkout -b "modifications" &&
91 echo "z" >file2 &&
92 echo "x" >file3 &&
93 git add file2 file3 &&
94 git commit -m "modified file2 and added file3" &&
95 git push origin modifications
96 ) &&
97 git add sub1 &&
98 git commit -m "Modify sub1" &&
99
259f3ee2
SB
100 git checkout -b add_nested_sub modify_sub1 &&
101 git -C sub1 checkout -b "add_nested_sub" &&
102 git -C sub1 submodule add --branch no_submodule ../submodule_update_sub2 sub2 &&
103 git -C sub1 commit -a -m "add a nested submodule" &&
104 git add sub1 &&
105 git commit -a -m "update submodule, that updates a nested submodule" &&
106 git checkout -b modify_sub1_recursively &&
107 git -C sub1 checkout -b modify_sub1_recursively &&
108 git -C sub1/sub2 checkout -b modify_sub1_recursively &&
109 echo change >sub1/sub2/file3 &&
110 git -C sub1/sub2 add file3 &&
111 git -C sub1/sub2 commit -m "make a change in nested sub" &&
112 git -C sub1 add sub2 &&
113 git -C sub1 commit -m "update nested sub" &&
114 git add sub1 &&
115 git commit -m "update sub1, that updates nested sub" &&
116 git -C sub1 push origin modify_sub1_recursively &&
117 git -C sub1/sub2 push origin modify_sub1_recursively &&
118 git -C sub1 submodule deinit -f --all &&
119
eb41e7fe 120 git checkout -b replace_sub1_with_directory add_sub1 &&
42639d23 121 git submodule update &&
3290fe6d 122 git -C sub1 checkout modifications &&
42639d23
JL
123 git rm --cached sub1 &&
124 rm sub1/.git* &&
125 git config -f .gitmodules --remove-section "submodule.sub1" &&
126 git add .gitmodules sub1/* &&
127 git commit -m "Replace sub1 with directory" &&
eb41e7fe 128
42639d23
JL
129 git checkout -b replace_directory_with_sub1 &&
130 git revert HEAD &&
131
eb41e7fe 132 git checkout -b replace_sub1_with_file add_sub1 &&
42639d23
JL
133 git rm sub1 &&
134 echo "content" >sub1 &&
135 git add sub1 &&
136 git commit -m "Replace sub1 with file" &&
eb41e7fe 137
42639d23
JL
138 git checkout -b replace_file_with_sub1 &&
139 git revert HEAD &&
140
eb41e7fe 141 git checkout -b invalid_sub1 add_sub1 &&
42639d23
JL
142 git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 sub1 &&
143 git commit -m "Invalid sub1 commit" &&
144 git checkout -b valid_sub1 &&
145 git revert HEAD &&
eb41e7fe 146
42639d23
JL
147 git checkout master
148 )
149}
150
151# Helper function to replace gitfile with .git directory
152replace_gitfile_with_git_dir () {
153 (
154 cd "$1" &&
155 git_dir="$(git rev-parse --git-dir)" &&
156 rm -f .git &&
157 cp -R "$git_dir" .git &&
158 GIT_WORK_TREE=. git config --unset core.worktree
159 )
160}
161
162# Test that the .git directory in the submodule is unchanged (except for the
163# core.worktree setting, which appears only in $GIT_DIR/modules/$1/config).
164# Call this function before test_submodule_content as the latter might
165# write the index file leading to false positive index differences.
166#
167# Note that this only supports submodules at the root level of the
168# superproject, with the default name, i.e. same as its path.
169test_git_directory_is_unchanged () {
170 (
171 cd ".git/modules/$1" &&
172 # does core.worktree point at the right place?
173 test "$(git config core.worktree)" = "../../../$1" &&
174 # remove it temporarily before comparing, as
175 # "$1/.git/config" lacks it...
176 git config --unset core.worktree
177 ) &&
178 diff -r ".git/modules/$1" "$1/.git" &&
179 (
180 # ... and then restore.
181 cd ".git/modules/$1" &&
182 git config core.worktree "../../../$1"
183 )
184}
185
259f3ee2
SB
186test_git_directory_exists() {
187 test -e ".git/modules/$1" &&
188 if test -f sub1/.git
189 then
190 # does core.worktree point at the right place?
191 test "$(git -C .git/modules/$1 config core.worktree)" = "../../../$1"
192 fi
193}
194
42639d23
JL
195# Helper function to be executed at the start of every test below, it sets up
196# the submodule repo if it doesn't exist and configures the most problematic
197# settings for diff.ignoreSubmodules.
198prolog () {
199 (test -d submodule_update_repo || create_lib_submodule_repo) &&
200 test_config_global diff.ignoreSubmodules all &&
201 test_config diff.ignoreSubmodules all
202}
203
204# Helper function to bring work tree back into the state given by the
205# commit. This includes trying to populate sub1 accordingly if it exists and
206# should be updated to an existing commit.
207reset_work_tree_to () {
208 rm -rf submodule_update &&
209 git clone submodule_update_repo submodule_update &&
210 (
211 cd submodule_update &&
212 rm -rf sub1 &&
213 git checkout -f "$1" &&
214 git status -u -s >actual &&
215 test_must_be_empty actual &&
03c7e2a3
SB
216 hash=$(git rev-parse --revs-only HEAD:sub1) &&
217 if test -n "$hash" &&
218 test $(cd "../submodule_update_sub1" && git rev-parse --verify "$hash^{commit}")
42639d23
JL
219 then
220 git submodule update --init --recursive "sub1"
221 fi
222 )
223}
224
259f3ee2
SB
225reset_work_tree_to_interested () {
226 reset_work_tree_to $1 &&
227 # make the submodule git dirs available
228 if ! test -d submodule_update/.git/modules/sub1
229 then
230 mkdir -p submodule_update/.git/modules &&
231 cp -r submodule_update_repo/.git/modules/sub1 submodule_update/.git/modules/sub1
232 GIT_WORK_TREE=. git -C submodule_update/.git/modules/sub1 config --unset core.worktree
233 fi &&
234 if ! test -d submodule_update/.git/modules/sub1/modules/sub2
235 then
236 mkdir -p submodule_update/.git/modules/sub1/modules &&
237 cp -r submodule_update_repo/.git/modules/sub1/modules/sub2 submodule_update/.git/modules/sub1/modules/sub2
238 GIT_WORK_TREE=. git -C submodule_update/.git/modules/sub1/modules/sub2 config --unset core.worktree
239 fi &&
240 # indicate we are interested in the submodule:
241 git -C submodule_update config submodule.sub1.url "bogus" &&
242 # sub1 might not be checked out, so use the git dir
243 git -C submodule_update/.git/modules/sub1 config submodule.sub2.url "bogus"
244}
245
42639d23
JL
246# Test that the superproject contains the content according to commit "$1"
247# (the work tree must match the index for everything but submodules but the
248# index must exactly match the given commit including any submodule SHA-1s).
249test_superproject_content () {
250 git diff-index --cached "$1" >actual &&
251 test_must_be_empty actual &&
252 git diff-files --ignore-submodules >actual &&
253 test_must_be_empty actual
254}
255
256# Test that the given submodule at path "$1" contains the content according
257# to the submodule commit recorded in the superproject's commit "$2"
258test_submodule_content () {
2f70edcc
SB
259 if test x"$1" = "x-C"
260 then
261 cd "$2"
262 shift; shift;
263 fi
42639d23
JL
264 if test $# != 2
265 then
266 echo "test_submodule_content needs two arguments"
267 return 1
268 fi &&
269 submodule="$1" &&
270 commit="$2" &&
271 test -d "$submodule"/ &&
272 if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
273 then
274 echo "Submodule $submodule is not populated"
275 return 1
276 fi &&
277 sha1=$(git rev-parse --verify "$commit:$submodule") &&
278 if test -z "$sha1"
279 then
280 echo "Couldn't retrieve SHA-1 of $submodule for $commit"
281 return 1
282 fi &&
283 (
284 cd "$submodule" &&
285 git status -u -s >actual &&
286 test_must_be_empty actual &&
287 git diff "$sha1" >actual &&
288 test_must_be_empty actual
289 )
290}
291
292# Test that the following transitions are correctly handled:
293# - Updated submodule
294# - New submodule
295# - Removed submodule
296# - Directory containing tracked files replaced by submodule
297# - Submodule replaced by tracked files in directory
298# - Submodule replaced by tracked file with the same name
299# - tracked file replaced by submodule
300#
301# The default is that submodule contents aren't changed until "git submodule
302# update" is run. And even then that command doesn't delete the work tree of
303# a removed submodule.
304#
305# Removing a submodule containing a .git directory must fail even when forced
306# to protect the history!
307#
308
309# Test that submodule contents are currently not updated when switching
310# between commits that change a submodule.
311test_submodule_switch () {
312 command="$1"
313 ######################### Appearing submodule #########################
314 # Switching to a commit letting a submodule appear creates empty dir ...
da7fe3fb
JL
315 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
316 then
317 # Restoring stash fails to restore submodule index entry
318 RESULT="failure"
319 else
320 RESULT="success"
321 fi
322 test_expect_$RESULT "$command: added submodule creates empty directory" '
42639d23
JL
323 prolog &&
324 reset_work_tree_to no_submodule &&
325 (
326 cd submodule_update &&
327 git branch -t add_sub1 origin/add_sub1 &&
328 $command add_sub1 &&
329 test_superproject_content origin/add_sub1 &&
330 test_dir_is_empty sub1 &&
331 git submodule update --init --recursive &&
332 test_submodule_content sub1 origin/add_sub1
333 )
334 '
335 # ... and doesn't care if it already exists ...
da7fe3fb 336 test_expect_$RESULT "$command: added submodule leaves existing empty directory alone" '
42639d23
JL
337 prolog &&
338 reset_work_tree_to no_submodule &&
339 (
340 cd submodule_update &&
341 mkdir sub1 &&
342 git branch -t add_sub1 origin/add_sub1 &&
343 $command add_sub1 &&
344 test_superproject_content origin/add_sub1 &&
345 test_dir_is_empty sub1 &&
346 git submodule update --init --recursive &&
347 test_submodule_content sub1 origin/add_sub1
348 )
349 '
350 # ... unless there is an untracked file in its place.
351 test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
352 prolog &&
353 reset_work_tree_to no_submodule &&
354 (
355 cd submodule_update &&
356 git branch -t add_sub1 origin/add_sub1 &&
357 >sub1 &&
358 test_must_fail $command add_sub1 &&
359 test_superproject_content origin/no_submodule &&
360 test_must_be_empty sub1
361 )
362 '
363 # Replacing a tracked file with a submodule produces an empty
364 # directory ...
da7fe3fb 365 test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
42639d23
JL
366 prolog &&
367 reset_work_tree_to replace_sub1_with_file &&
368 (
369 cd submodule_update &&
370 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
371 $command replace_file_with_sub1 &&
372 test_superproject_content origin/replace_file_with_sub1 &&
373 test_dir_is_empty sub1 &&
374 git submodule update --init --recursive &&
375 test_submodule_content sub1 origin/replace_file_with_sub1
376 )
377 '
378 # ... as does removing a directory with tracked files with a
379 # submodule.
663ed39a
JL
380 if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
381 then
382 # Non fast-forward merges fail with "Directory sub1 doesn't
383 # exist. sub1" because the empty submodule directory is not
384 # created
385 RESULT="failure"
386 else
387 RESULT="success"
388 fi
389 test_expect_$RESULT "$command: replace directory with submodule" '
42639d23
JL
390 prolog &&
391 reset_work_tree_to replace_sub1_with_directory &&
392 (
393 cd submodule_update &&
394 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
395 $command replace_directory_with_sub1 &&
396 test_superproject_content origin/replace_directory_with_sub1 &&
397 test_dir_is_empty sub1 &&
398 git submodule update --init --recursive &&
399 test_submodule_content sub1 origin/replace_directory_with_sub1
400 )
401 '
402
403 ######################## Disappearing submodule #######################
404 # Removing a submodule doesn't remove its work tree ...
da7fe3fb
JL
405 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
406 then
407 RESULT="failure"
408 else
409 RESULT="success"
410 fi
411 test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
42639d23
JL
412 prolog &&
413 reset_work_tree_to add_sub1 &&
414 (
415 cd submodule_update &&
416 git branch -t remove_sub1 origin/remove_sub1 &&
417 $command remove_sub1 &&
418 test_superproject_content origin/remove_sub1 &&
419 test_submodule_content sub1 origin/add_sub1
420 )
421 '
422 # ... especially when it contains a .git directory.
da7fe3fb 423 test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
42639d23
JL
424 prolog &&
425 reset_work_tree_to add_sub1 &&
426 (
427 cd submodule_update &&
428 git branch -t remove_sub1 origin/remove_sub1 &&
429 replace_gitfile_with_git_dir sub1 &&
430 $command remove_sub1 &&
431 test_superproject_content origin/remove_sub1 &&
432 test_git_directory_is_unchanged sub1 &&
433 test_submodule_content sub1 origin/add_sub1
434 )
435 '
436 # Replacing a submodule with files in a directory must fail as the
437 # submodule work tree isn't removed ...
663ed39a
JL
438 if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
439 then
440 # Non fast-forward merges attempt to merge the former
441 # submodule files with the newly checked out ones in the
442 # directory of the same name while it shouldn't.
443 RESULT="failure"
444 else
445 RESULT="success"
446 fi
447 test_expect_$RESULT "$command: replace submodule with a directory must fail" '
42639d23
JL
448 prolog &&
449 reset_work_tree_to add_sub1 &&
450 (
451 cd submodule_update &&
452 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
453 test_must_fail $command replace_sub1_with_directory &&
454 test_superproject_content origin/add_sub1 &&
455 test_submodule_content sub1 origin/add_sub1
456 )
457 '
458 # ... especially when it contains a .git directory.
663ed39a 459 test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
42639d23
JL
460 prolog &&
461 reset_work_tree_to add_sub1 &&
462 (
463 cd submodule_update &&
464 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
465 replace_gitfile_with_git_dir sub1 &&
466 test_must_fail $command replace_sub1_with_directory &&
467 test_superproject_content origin/add_sub1 &&
468 test_git_directory_is_unchanged sub1 &&
469 test_submodule_content sub1 origin/add_sub1
470 )
471 '
472 # Replacing it with a file must fail as it could throw away any local
473 # work tree changes ...
474 test_expect_failure "$command: replace submodule with a file must fail" '
475 prolog &&
476 reset_work_tree_to add_sub1 &&
477 (
478 cd submodule_update &&
479 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
480 test_must_fail $command replace_sub1_with_file &&
481 test_superproject_content origin/add_sub1 &&
482 test_submodule_content sub1 origin/add_sub1
483 )
484 '
485 # ... or even destroy unpushed parts of submodule history if that
486 # still uses a .git directory.
487 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
488 prolog &&
489 reset_work_tree_to add_sub1 &&
490 (
491 cd submodule_update &&
492 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
493 replace_gitfile_with_git_dir sub1 &&
494 test_must_fail $command replace_sub1_with_file &&
495 test_superproject_content origin/add_sub1 &&
496 test_git_directory_is_unchanged sub1 &&
497 test_submodule_content sub1 origin/add_sub1
498 )
499 '
500
501 ########################## Modified submodule #########################
502 # Updating a submodule sha1 doesn't update the submodule's work tree
283f56a4
JL
503 if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
504 then
505 # When cherry picking a SHA-1 update for an ignored submodule
506 # the commit incorrectly fails with "The previous cherry-pick
507 # is now empty, possibly due to conflict resolution."
508 RESULT="failure"
509 else
510 RESULT="success"
511 fi
512 test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
42639d23
JL
513 prolog &&
514 reset_work_tree_to add_sub1 &&
515 (
516 cd submodule_update &&
517 git branch -t modify_sub1 origin/modify_sub1 &&
518 $command modify_sub1 &&
519 test_superproject_content origin/modify_sub1 &&
520 test_submodule_content sub1 origin/add_sub1 &&
521 git submodule update &&
522 test_submodule_content sub1 origin/modify_sub1
523 )
524 '
525
526 # Updating a submodule to an invalid sha1 doesn't update the
527 # submodule's work tree, subsequent update will fail
283f56a4 528 test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
42639d23
JL
529 prolog &&
530 reset_work_tree_to add_sub1 &&
531 (
532 cd submodule_update &&
533 git branch -t invalid_sub1 origin/invalid_sub1 &&
534 $command invalid_sub1 &&
535 test_superproject_content origin/invalid_sub1 &&
536 test_submodule_content sub1 origin/add_sub1 &&
537 test_must_fail git submodule update &&
538 test_submodule_content sub1 origin/add_sub1
539 )
540 '
541 # Updating a submodule from an invalid sha1 doesn't update the
542 # submodule's work tree, subsequent update will succeed
283f56a4 543 test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
42639d23
JL
544 prolog &&
545 reset_work_tree_to invalid_sub1 &&
546 (
547 cd submodule_update &&
548 git branch -t valid_sub1 origin/valid_sub1 &&
549 $command valid_sub1 &&
550 test_superproject_content origin/valid_sub1 &&
551 test_dir_is_empty sub1 &&
552 git submodule update --init --recursive &&
553 test_submodule_content sub1 origin/valid_sub1
554 )
555 '
556}
557
558# Test that submodule contents are currently not updated when switching
559# between commits that change a submodule, but throwing away local changes in
560# the superproject is allowed.
561test_submodule_forced_switch () {
562 command="$1"
563 ######################### Appearing submodule #########################
564 # Switching to a commit letting a submodule appear creates empty dir ...
565 test_expect_success "$command: added submodule creates empty directory" '
566 prolog &&
567 reset_work_tree_to no_submodule &&
568 (
569 cd submodule_update &&
570 git branch -t add_sub1 origin/add_sub1 &&
571 $command add_sub1 &&
572 test_superproject_content origin/add_sub1 &&
573 test_dir_is_empty sub1 &&
574 git submodule update --init --recursive &&
575 test_submodule_content sub1 origin/add_sub1
576 )
577 '
578 # ... and doesn't care if it already exists ...
579 test_expect_success "$command: added submodule leaves existing empty directory alone" '
580 prolog &&
581 reset_work_tree_to no_submodule &&
582 (
583 cd submodule_update &&
584 git branch -t add_sub1 origin/add_sub1 &&
585 mkdir sub1 &&
586 $command add_sub1 &&
587 test_superproject_content origin/add_sub1 &&
588 test_dir_is_empty sub1 &&
589 git submodule update --init --recursive &&
590 test_submodule_content sub1 origin/add_sub1
591 )
592 '
593 # ... unless there is an untracked file in its place.
594 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
595 prolog &&
596 reset_work_tree_to no_submodule &&
597 (
598 cd submodule_update &&
599 git branch -t add_sub1 origin/add_sub1 &&
600 >sub1 &&
601 $command add_sub1 &&
602 test_superproject_content origin/add_sub1 &&
603 test_dir_is_empty sub1
604 )
605 '
606 # Replacing a tracked file with a submodule produces an empty
607 # directory ...
608 test_expect_success "$command: replace tracked file with submodule creates empty directory" '
609 prolog &&
610 reset_work_tree_to replace_sub1_with_file &&
611 (
612 cd submodule_update &&
613 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
614 $command replace_file_with_sub1 &&
615 test_superproject_content origin/replace_file_with_sub1 &&
616 test_dir_is_empty sub1 &&
617 git submodule update --init --recursive &&
618 test_submodule_content sub1 origin/replace_file_with_sub1
619 )
620 '
621 # ... as does removing a directory with tracked files with a
622 # submodule.
623 test_expect_success "$command: replace directory with submodule" '
624 prolog &&
625 reset_work_tree_to replace_sub1_with_directory &&
626 (
627 cd submodule_update &&
628 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
629 $command replace_directory_with_sub1 &&
630 test_superproject_content origin/replace_directory_with_sub1 &&
631 test_dir_is_empty sub1 &&
632 git submodule update --init --recursive &&
633 test_submodule_content sub1 origin/replace_directory_with_sub1
634 )
635 '
636
637 ######################## Disappearing submodule #######################
638 # Removing a submodule doesn't remove its work tree ...
639 test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
640 prolog &&
641 reset_work_tree_to add_sub1 &&
642 (
643 cd submodule_update &&
644 git branch -t remove_sub1 origin/remove_sub1 &&
645 $command remove_sub1 &&
646 test_superproject_content origin/remove_sub1 &&
647 test_submodule_content sub1 origin/add_sub1
648 )
649 '
650 # ... especially when it contains a .git directory.
651 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
652 prolog &&
653 reset_work_tree_to add_sub1 &&
654 (
655 cd submodule_update &&
656 git branch -t remove_sub1 origin/remove_sub1 &&
657 replace_gitfile_with_git_dir sub1 &&
658 $command remove_sub1 &&
659 test_superproject_content origin/remove_sub1 &&
660 test_git_directory_is_unchanged sub1 &&
661 test_submodule_content sub1 origin/add_sub1
662 )
663 '
664 # Replacing a submodule with files in a directory must fail as the
665 # submodule work tree isn't removed ...
666 test_expect_failure "$command: replace submodule with a directory must fail" '
667 prolog &&
668 reset_work_tree_to add_sub1 &&
669 (
670 cd submodule_update &&
671 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
672 test_must_fail $command replace_sub1_with_directory &&
673 test_superproject_content origin/add_sub1 &&
674 test_submodule_content sub1 origin/add_sub1
675 )
676 '
677 # ... especially when it contains a .git directory.
678 test_expect_failure "$command: replace submodule containing a .git directory with a directory must fail" '
679 prolog &&
680 reset_work_tree_to add_sub1 &&
681 (
682 cd submodule_update &&
683 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
684 replace_gitfile_with_git_dir sub1 &&
685 test_must_fail $command replace_sub1_with_directory &&
686 test_superproject_content origin/add_sub1 &&
687 test_git_directory_is_unchanged sub1 &&
688 test_submodule_content sub1 origin/add_sub1
689 )
690 '
691 # Replacing it with a file must fail as it could throw away any local
692 # work tree changes ...
693 test_expect_failure "$command: replace submodule with a file must fail" '
694 prolog &&
695 reset_work_tree_to add_sub1 &&
696 (
697 cd submodule_update &&
698 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
699 test_must_fail $command replace_sub1_with_file &&
700 test_superproject_content origin/add_sub1 &&
701 test_submodule_content sub1 origin/add_sub1
702 )
703 '
704 # ... or even destroy unpushed parts of submodule history if that
705 # still uses a .git directory.
706 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
707 prolog &&
708 reset_work_tree_to add_sub1 &&
709 (
710 cd submodule_update &&
711 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
712 replace_gitfile_with_git_dir sub1 &&
713 test_must_fail $command replace_sub1_with_file &&
714 test_superproject_content origin/add_sub1 &&
715 test_git_directory_is_unchanged sub1 &&
716 test_submodule_content sub1 origin/add_sub1
717 )
718 '
719
720 ########################## Modified submodule #########################
721 # Updating a submodule sha1 doesn't update the submodule's work tree
722 test_expect_success "$command: modified submodule does not update submodule work tree" '
723 prolog &&
724 reset_work_tree_to add_sub1 &&
725 (
726 cd submodule_update &&
727 git branch -t modify_sub1 origin/modify_sub1 &&
728 $command modify_sub1 &&
729 test_superproject_content origin/modify_sub1 &&
730 test_submodule_content sub1 origin/add_sub1 &&
731 git submodule update &&
732 test_submodule_content sub1 origin/modify_sub1
733 )
734 '
735 # Updating a submodule to an invalid sha1 doesn't update the
736 # submodule's work tree, subsequent update will fail
737 test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
738 prolog &&
739 reset_work_tree_to add_sub1 &&
740 (
741 cd submodule_update &&
742 git branch -t invalid_sub1 origin/invalid_sub1 &&
743 $command invalid_sub1 &&
744 test_superproject_content origin/invalid_sub1 &&
745 test_submodule_content sub1 origin/add_sub1 &&
746 test_must_fail git submodule update &&
747 test_submodule_content sub1 origin/add_sub1
748 )
749 '
750 # Updating a submodule from an invalid sha1 doesn't update the
751 # submodule's work tree, subsequent update will succeed
752 test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
753 prolog &&
754 reset_work_tree_to invalid_sub1 &&
755 (
756 cd submodule_update &&
757 git branch -t valid_sub1 origin/valid_sub1 &&
758 $command valid_sub1 &&
759 test_superproject_content origin/valid_sub1 &&
760 test_dir_is_empty sub1 &&
761 git submodule update --init --recursive &&
762 test_submodule_content sub1 origin/valid_sub1
763 )
764 '
765}
259f3ee2
SB
766
767# Test that submodule contents are correctly updated when switching
768# between commits that change a submodule.
769# Test that the following transitions are correctly handled:
770# (These tests are also above in the case where we expect no change
771# in the submodule)
772# - Updated submodule
773# - New submodule
774# - Removed submodule
775# - Directory containing tracked files replaced by submodule
776# - Submodule replaced by tracked files in directory
777# - Submodule replaced by tracked file with the same name
778# - tracked file replaced by submodule
779#
780# New test cases
781# - Removing a submodule with a git directory absorbs the submodules
782# git directory first into the superproject.
783
784test_submodule_switch_recursing () {
785 command="$1"
1fc458d9
SB
786 RESULTDS=success
787 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
788 then
789 RESULTDS=failure
790 fi
791 RESULTR=success
792 if test "$KNOWN_FAILURE_SUBMODULE_RECURSIVE_NESTED" = 1
793 then
794 RESULTR=failure
795 fi
25804914
SB
796 RESULTOI=success
797 if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
798 then
799 RESULTOI=failure
800 fi
259f3ee2
SB
801 ######################### Appearing submodule #########################
802 # Switching to a commit letting a submodule appear checks it out ...
803 test_expect_success "$command: added submodule is checked out" '
804 prolog &&
805 reset_work_tree_to_interested no_submodule &&
806 (
807 cd submodule_update &&
808 git branch -t add_sub1 origin/add_sub1 &&
809 $command add_sub1 &&
810 test_superproject_content origin/add_sub1 &&
811 test_submodule_content sub1 origin/add_sub1
812 )
813 '
814 # ... ignoring an empty existing directory ...
815 test_expect_success "$command: added submodule is checked out in empty dir" '
816 prolog &&
817 reset_work_tree_to_interested no_submodule &&
818 (
819 cd submodule_update &&
820 mkdir sub1 &&
821 git branch -t add_sub1 origin/add_sub1 &&
822 $command add_sub1 &&
823 test_superproject_content origin/add_sub1 &&
824 test_submodule_content sub1 origin/add_sub1
825 )
826 '
827 # ... unless there is an untracked file in its place.
828 test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
829 prolog &&
830 reset_work_tree_to_interested no_submodule &&
831 (
832 cd submodule_update &&
833 git branch -t add_sub1 origin/add_sub1 &&
834 : >sub1 &&
835 test_must_fail $command add_sub1 &&
836 test_superproject_content origin/no_submodule &&
837 test_must_be_empty sub1
838 )
839 '
840 # ... but an ignored file is fine.
25804914 841 test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
259f3ee2
SB
842 test_when_finished "rm submodule_update/.git/info/exclude" &&
843 prolog &&
844 reset_work_tree_to_interested no_submodule &&
845 (
846 cd submodule_update &&
847 git branch -t add_sub1 origin/add_sub1 &&
848 : >sub1 &&
849 echo sub1 >.git/info/exclude
850 $command add_sub1 &&
851 test_superproject_content origin/add_sub1 &&
852 test_submodule_content sub1 origin/add_sub1
853 )
854 '
855 # Replacing a tracked file with a submodule produces a checked out submodule
856 test_expect_success "$command: replace tracked file with submodule checks out submodule" '
857 prolog &&
858 reset_work_tree_to_interested replace_sub1_with_file &&
859 (
860 cd submodule_update &&
861 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
862 $command replace_file_with_sub1 &&
863 test_superproject_content origin/replace_file_with_sub1 &&
864 test_submodule_content sub1 origin/replace_file_with_sub1
865 )
866 '
867 # ... as does removing a directory with tracked files with a submodule.
868 test_expect_success "$command: replace directory with submodule" '
869 prolog &&
870 reset_work_tree_to_interested replace_sub1_with_directory &&
871 (
872 cd submodule_update &&
873 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
874 $command replace_directory_with_sub1 &&
875 test_superproject_content origin/replace_directory_with_sub1 &&
876 test_submodule_content sub1 origin/replace_directory_with_sub1
877 )
878 '
879
880 ######################## Disappearing submodule #######################
881 # Removing a submodule removes its work tree ...
882 test_expect_success "$command: removed submodule removes submodules working tree" '
883 prolog &&
884 reset_work_tree_to_interested add_sub1 &&
885 (
886 cd submodule_update &&
887 git branch -t remove_sub1 origin/remove_sub1 &&
888 $command remove_sub1 &&
889 test_superproject_content origin/remove_sub1 &&
890 ! test -e sub1
891 )
892 '
893 # ... absorbing a .git directory along the way.
894 test_expect_success "$command: removed submodule absorbs submodules .git directory" '
895 prolog &&
896 reset_work_tree_to_interested add_sub1 &&
897 (
898 cd submodule_update &&
899 git branch -t remove_sub1 origin/remove_sub1 &&
900 replace_gitfile_with_git_dir sub1 &&
901 rm -rf .git/modules &&
902 $command remove_sub1 &&
903 test_superproject_content origin/remove_sub1 &&
904 ! test -e sub1 &&
905 test_git_directory_exists sub1
906 )
907 '
908 # Replacing a submodule with files in a directory must succeeds
909 # when the submodule is clean
1fc458d9 910 test_expect_$RESULTDS "$command: replace submodule with a directory" '
259f3ee2
SB
911 prolog &&
912 reset_work_tree_to_interested add_sub1 &&
913 (
914 cd submodule_update &&
915 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
916 $command replace_sub1_with_directory &&
917 test_superproject_content origin/replace_sub1_with_directory &&
918 test_submodule_content sub1 origin/replace_sub1_with_directory
919 )
920 '
921 # ... absorbing a .git directory.
1fc458d9 922 test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
259f3ee2
SB
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 replace_gitfile_with_git_dir sub1 &&
929 rm -rf .git/modules &&
930 $command replace_sub1_with_directory &&
931 test_superproject_content origin/replace_sub1_with_directory &&
932 test_git_directory_exists sub1
933 )
934 '
935
936 # Replacing it with a file ...
937 test_expect_success "$command: replace submodule with a file" '
938 prolog &&
939 reset_work_tree_to_interested add_sub1 &&
940 (
941 cd submodule_update &&
942 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
943 $command replace_sub1_with_file &&
944 test_superproject_content origin/replace_sub1_with_file &&
945 test -f sub1
946 )
947 '
948
949 # ... must check its local work tree for untracked files
1fc458d9 950 test_expect_$RESULTDS "$command: replace submodule with a file must fail with untracked files" '
259f3ee2
SB
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 : >sub1/untrackedfile &&
957 test_must_fail $command replace_sub1_with_file &&
958 test_superproject_content origin/add_sub1 &&
959 test_submodule_content sub1 origin/add_sub1
960 )
961 '
962
963 # ... and ignored files are ignored
964 test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
965 test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
966 prolog &&
967 reset_work_tree_to_interested add_sub1 &&
968 (
969 cd submodule_update &&
970 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
971 : >sub1/ignored &&
972 $command replace_sub1_with_file &&
973 test_superproject_content origin/replace_sub1_with_file &&
974 test -f sub1
975 )
976 '
977
978 ########################## Modified submodule #########################
979 # Updating a submodule sha1 updates the submodule's work tree
980 test_expect_success "$command: modified submodule updates submodule work tree" '
981 prolog &&
982 reset_work_tree_to_interested add_sub1 &&
983 (
984 cd submodule_update &&
985 git branch -t modify_sub1 origin/modify_sub1 &&
986 $command modify_sub1 &&
987 test_superproject_content origin/modify_sub1 &&
988 test_submodule_content sub1 origin/modify_sub1
989 )
990 '
991
992 # Updating a submodule to an invalid sha1 doesn't update the
993 # superproject nor the submodule's work tree.
994 test_expect_success "$command: updating to a missing submodule commit fails" '
995 prolog &&
996 reset_work_tree_to_interested add_sub1 &&
997 (
998 cd submodule_update &&
999 git branch -t invalid_sub1 origin/invalid_sub1 &&
1000 test_must_fail $command invalid_sub1 &&
1001 test_superproject_content origin/add_sub1 &&
1002 test_submodule_content sub1 origin/add_sub1
1003 )
1004 '
1005
1fc458d9
SB
1006 # recursing deeper than one level doesn't work yet.
1007 test_expect_$RESULTR "$command: modified submodule updates submodule recursively" '
259f3ee2
SB
1008 prolog &&
1009 reset_work_tree_to_interested add_nested_sub &&
1010 (
1011 cd submodule_update &&
1012 git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
1013 $command modify_sub1_recursively &&
1014 test_superproject_content origin/modify_sub1_recursively &&
1015 test_submodule_content sub1 origin/modify_sub1_recursively &&
1016 test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
1017 )
1018 '
1019}
1020
1021# Test that submodule contents are updated when switching between commits
1022# that change a submodule, but throwing away local changes in
1023# the superproject as well as the submodule is allowed.
1024test_submodule_forced_switch_recursing () {
1025 command="$1"
1fc458d9
SB
1026 RESULT=success
1027 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
1028 then
1029 RESULT=failure
1030 fi
259f3ee2
SB
1031 ######################### Appearing submodule #########################
1032 # Switching to a commit letting a submodule appear creates empty dir ...
1033 test_expect_success "$command: added submodule is checked out" '
1034 prolog &&
1035 reset_work_tree_to_interested no_submodule &&
1036 (
1037 cd submodule_update &&
1038 git branch -t add_sub1 origin/add_sub1 &&
1039 $command add_sub1 &&
1040 test_superproject_content origin/add_sub1 &&
1041 test_submodule_content sub1 origin/add_sub1
1042 )
1043 '
1044 # ... and doesn't care if it already exists ...
1045 test_expect_success "$command: added submodule ignores empty directory" '
1046 prolog &&
1047 reset_work_tree_to_interested no_submodule &&
1048 (
1049 cd submodule_update &&
1050 git branch -t add_sub1 origin/add_sub1 &&
1051 mkdir sub1 &&
1052 $command add_sub1 &&
1053 test_superproject_content origin/add_sub1 &&
1054 test_submodule_content sub1 origin/add_sub1
1055 )
1056 '
1057 # ... not caring about an untracked file either
1058 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
1059 prolog &&
1060 reset_work_tree_to_interested no_submodule &&
1061 (
1062 cd submodule_update &&
1063 git branch -t add_sub1 origin/add_sub1 &&
1064 >sub1 &&
1065 $command add_sub1 &&
1066 test_superproject_content origin/add_sub1 &&
1067 test_submodule_content sub1 origin/add_sub1
1068 )
1069 '
1070 # Replacing a tracked file with a submodule checks out the submodule
1071 test_expect_success "$command: replace tracked file with submodule populates the submodule" '
1072 prolog &&
1073 reset_work_tree_to_interested replace_sub1_with_file &&
1074 (
1075 cd submodule_update &&
1076 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
1077 $command replace_file_with_sub1 &&
1078 test_superproject_content origin/replace_file_with_sub1 &&
1079 test_submodule_content sub1 origin/replace_file_with_sub1
1080 )
1081 '
1082 # ... as does removing a directory with tracked files with a
1083 # submodule.
1084 test_expect_success "$command: replace directory with submodule" '
1085 prolog &&
1086 reset_work_tree_to_interested replace_sub1_with_directory &&
1087 (
1088 cd submodule_update &&
1089 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
1090 $command replace_directory_with_sub1 &&
1091 test_superproject_content origin/replace_directory_with_sub1 &&
1092 test_submodule_content sub1 origin/replace_directory_with_sub1
1093 )
1094 '
1095
1096 ######################## Disappearing submodule #######################
1097 # Removing a submodule doesn't remove its work tree ...
1098 test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
1099 prolog &&
1100 reset_work_tree_to_interested add_sub1 &&
1101 (
1102 cd submodule_update &&
1103 git branch -t remove_sub1 origin/remove_sub1 &&
1104 $command remove_sub1 &&
1105 test_superproject_content origin/remove_sub1 &&
1106 ! test -e sub1
1107 )
1108 '
1109 # ... especially when it contains a .git directory.
1110 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
1111 prolog &&
1112 reset_work_tree_to_interested add_sub1 &&
1113 (
1114 cd submodule_update &&
1115 git branch -t remove_sub1 origin/remove_sub1 &&
1116 replace_gitfile_with_git_dir sub1 &&
1117 rm -rf .git/modules/sub1 &&
1118 $command remove_sub1 &&
1119 test_superproject_content origin/remove_sub1 &&
1120 test_git_directory_exists sub1 &&
1121 ! test -e sub1
1122 )
1123 '
1124 # Replacing a submodule with files in a directory ...
1125 test_expect_success "$command: replace submodule with a directory" '
1126 prolog &&
1127 reset_work_tree_to_interested add_sub1 &&
1128 (
1129 cd submodule_update &&
1130 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1131 $command replace_sub1_with_directory &&
1132 test_superproject_content origin/replace_sub1_with_directory
1133 )
1134 '
1135 # ... absorbing a .git directory.
1136 test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
1137 prolog &&
1138 reset_work_tree_to_interested add_sub1 &&
1139 (
1140 cd submodule_update &&
1141 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1142 replace_gitfile_with_git_dir sub1 &&
1143 rm -rf .git/modules/sub1 &&
1144 $command replace_sub1_with_directory &&
1145 test_superproject_content origin/replace_sub1_with_directory &&
1146 test_submodule_content sub1 origin/modify_sub1
1147 test_git_directory_exists sub1
1148 )
1149 '
1150 # Replacing it with a file
1151 test_expect_success "$command: replace submodule with a file" '
1152 prolog &&
1153 reset_work_tree_to_interested add_sub1 &&
1154 (
1155 cd submodule_update &&
1156 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1157 $command replace_sub1_with_file &&
1158 test_superproject_content origin/replace_sub1_with_file
1159 )
1160 '
1161
1162 # ... even if the submodule contains ignored files
1163 test_expect_success "$command: replace submodule with a file ignoring ignored files" '
1164 prolog &&
1165 reset_work_tree_to_interested add_sub1 &&
1166 (
1167 cd submodule_update &&
1168 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1169 : >sub1/expect &&
1170 $command replace_sub1_with_file &&
1171 test_superproject_content origin/replace_sub1_with_file
1172 )
1173 '
1174
1175 # ... but stops for untracked files that would be lost
1fc458d9 1176 test_expect_$RESULT "$command: replace submodule with a file stops for untracked files" '
259f3ee2
SB
1177 prolog &&
1178 reset_work_tree_to_interested add_sub1 &&
1179 (
1180 cd submodule_update &&
1181 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1182 : >sub1/untracked_file &&
1183 test_must_fail $command replace_sub1_with_file &&
1184 test_superproject_content origin/add_sub1 &&
1185 test -f sub1/untracked_file
1186 )
1187 '
1188
1189 ########################## Modified submodule #########################
1190 # Updating a submodule sha1 updates the submodule's work tree
1191 test_expect_success "$command: modified submodule updates submodule work tree" '
1192 prolog &&
1193 reset_work_tree_to_interested add_sub1 &&
1194 (
1195 cd submodule_update &&
1196 git branch -t modify_sub1 origin/modify_sub1 &&
1197 $command modify_sub1 &&
1198 test_superproject_content origin/modify_sub1 &&
1199 test_submodule_content sub1 origin/modify_sub1
1200 )
1201 '
1202 # Updating a submodule to an invalid sha1 doesn't update the
1203 # submodule's work tree, subsequent update will fail
1204 test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
1205 prolog &&
1206 reset_work_tree_to_interested add_sub1 &&
1207 (
1208 cd submodule_update &&
1209 git branch -t invalid_sub1 origin/invalid_sub1 &&
1210 test_must_fail $command invalid_sub1 &&
1211 test_superproject_content origin/add_sub1 &&
1212 test_submodule_content sub1 origin/add_sub1
1213 )
1214 '
1215 # Updating a submodule from an invalid sha1 updates
1216 test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
1217 prolog &&
1218 reset_work_tree_to_interested invalid_sub1 &&
1219 (
1220 cd submodule_update &&
1221 git branch -t valid_sub1 origin/valid_sub1 &&
1222 test_must_fail $command valid_sub1 &&
1223 test_superproject_content origin/invalid_sub1
1224 )
1225 '
1226}