Tests: clean up and document submodule helpers
[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
d3b5a497
JT
309# Internal function; use test_submodule_switch() or
310# test_submodule_forced_switch() instead.
311test_submodule_switch_common() {
42639d23
JL
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 '
d3b5a497 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 '
42639d23
JL
350 # Replacing a tracked file with a submodule produces an empty
351 # directory ...
da7fe3fb 352 test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
42639d23
JL
353 prolog &&
354 reset_work_tree_to replace_sub1_with_file &&
355 (
356 cd submodule_update &&
357 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
358 $command replace_file_with_sub1 &&
359 test_superproject_content origin/replace_file_with_sub1 &&
360 test_dir_is_empty sub1 &&
361 git submodule update --init --recursive &&
362 test_submodule_content sub1 origin/replace_file_with_sub1
363 )
364 '
365 # ... as does removing a directory with tracked files with a
366 # submodule.
663ed39a
JL
367 if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
368 then
369 # Non fast-forward merges fail with "Directory sub1 doesn't
370 # exist. sub1" because the empty submodule directory is not
371 # created
372 RESULT="failure"
373 else
374 RESULT="success"
375 fi
376 test_expect_$RESULT "$command: replace directory with submodule" '
42639d23
JL
377 prolog &&
378 reset_work_tree_to replace_sub1_with_directory &&
379 (
380 cd submodule_update &&
381 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
382 $command replace_directory_with_sub1 &&
383 test_superproject_content origin/replace_directory_with_sub1 &&
384 test_dir_is_empty sub1 &&
385 git submodule update --init --recursive &&
386 test_submodule_content sub1 origin/replace_directory_with_sub1
387 )
388 '
389
390 ######################## Disappearing submodule #######################
391 # Removing a submodule doesn't remove its work tree ...
da7fe3fb
JL
392 if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
393 then
394 RESULT="failure"
395 else
396 RESULT="success"
397 fi
398 test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
42639d23
JL
399 prolog &&
400 reset_work_tree_to add_sub1 &&
401 (
402 cd submodule_update &&
403 git branch -t remove_sub1 origin/remove_sub1 &&
404 $command remove_sub1 &&
405 test_superproject_content origin/remove_sub1 &&
406 test_submodule_content sub1 origin/add_sub1
407 )
408 '
409 # ... especially when it contains a .git directory.
da7fe3fb 410 test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
42639d23
JL
411 prolog &&
412 reset_work_tree_to add_sub1 &&
413 (
414 cd submodule_update &&
415 git branch -t remove_sub1 origin/remove_sub1 &&
416 replace_gitfile_with_git_dir sub1 &&
417 $command remove_sub1 &&
418 test_superproject_content origin/remove_sub1 &&
419 test_git_directory_is_unchanged sub1 &&
420 test_submodule_content sub1 origin/add_sub1
421 )
422 '
423 # Replacing a submodule with files in a directory must fail as the
424 # submodule work tree isn't removed ...
663ed39a
JL
425 if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
426 then
427 # Non fast-forward merges attempt to merge the former
428 # submodule files with the newly checked out ones in the
429 # directory of the same name while it shouldn't.
430 RESULT="failure"
d3b5a497
JT
431 elif test "$KNOWN_FAILURE_FORCED_SWITCH_TESTS" = 1
432 then
433 # All existing tests that use test_submodule_forced_switch()
434 # require this.
435 RESULT="failure"
663ed39a
JL
436 else
437 RESULT="success"
438 fi
439 test_expect_$RESULT "$command: replace submodule with a directory must fail" '
42639d23
JL
440 prolog &&
441 reset_work_tree_to add_sub1 &&
442 (
443 cd submodule_update &&
444 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
445 test_must_fail $command replace_sub1_with_directory &&
446 test_superproject_content origin/add_sub1 &&
447 test_submodule_content sub1 origin/add_sub1
448 )
449 '
450 # ... especially when it contains a .git directory.
663ed39a 451 test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
42639d23
JL
452 prolog &&
453 reset_work_tree_to add_sub1 &&
454 (
455 cd submodule_update &&
456 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
457 replace_gitfile_with_git_dir sub1 &&
458 test_must_fail $command replace_sub1_with_directory &&
459 test_superproject_content origin/add_sub1 &&
460 test_git_directory_is_unchanged sub1 &&
461 test_submodule_content sub1 origin/add_sub1
462 )
463 '
464 # Replacing it with a file must fail as it could throw away any local
465 # work tree changes ...
466 test_expect_failure "$command: replace submodule with a file must fail" '
467 prolog &&
468 reset_work_tree_to add_sub1 &&
469 (
470 cd submodule_update &&
471 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
472 test_must_fail $command replace_sub1_with_file &&
473 test_superproject_content origin/add_sub1 &&
474 test_submodule_content sub1 origin/add_sub1
475 )
476 '
477 # ... or even destroy unpushed parts of submodule history if that
478 # still uses a .git directory.
479 test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
480 prolog &&
481 reset_work_tree_to add_sub1 &&
482 (
483 cd submodule_update &&
484 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
485 replace_gitfile_with_git_dir sub1 &&
486 test_must_fail $command replace_sub1_with_file &&
487 test_superproject_content origin/add_sub1 &&
488 test_git_directory_is_unchanged sub1 &&
489 test_submodule_content sub1 origin/add_sub1
490 )
491 '
492
493 ########################## Modified submodule #########################
494 # Updating a submodule sha1 doesn't update the submodule's work tree
283f56a4
JL
495 if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
496 then
497 # When cherry picking a SHA-1 update for an ignored submodule
498 # the commit incorrectly fails with "The previous cherry-pick
499 # is now empty, possibly due to conflict resolution."
500 RESULT="failure"
501 else
502 RESULT="success"
503 fi
504 test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
42639d23
JL
505 prolog &&
506 reset_work_tree_to add_sub1 &&
507 (
508 cd submodule_update &&
509 git branch -t modify_sub1 origin/modify_sub1 &&
510 $command modify_sub1 &&
511 test_superproject_content origin/modify_sub1 &&
512 test_submodule_content sub1 origin/add_sub1 &&
513 git submodule update &&
514 test_submodule_content sub1 origin/modify_sub1
515 )
516 '
42639d23
JL
517 # Updating a submodule to an invalid sha1 doesn't update the
518 # submodule's work tree, subsequent update will fail
283f56a4 519 test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
42639d23
JL
520 prolog &&
521 reset_work_tree_to add_sub1 &&
522 (
523 cd submodule_update &&
524 git branch -t invalid_sub1 origin/invalid_sub1 &&
525 $command invalid_sub1 &&
526 test_superproject_content origin/invalid_sub1 &&
527 test_submodule_content sub1 origin/add_sub1 &&
528 test_must_fail git submodule update &&
529 test_submodule_content sub1 origin/add_sub1
530 )
531 '
532 # Updating a submodule from an invalid sha1 doesn't update the
533 # submodule's work tree, subsequent update will succeed
283f56a4 534 test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
42639d23
JL
535 prolog &&
536 reset_work_tree_to invalid_sub1 &&
537 (
538 cd submodule_update &&
539 git branch -t valid_sub1 origin/valid_sub1 &&
540 $command valid_sub1 &&
541 test_superproject_content origin/valid_sub1 &&
542 test_dir_is_empty sub1 &&
543 git submodule update --init --recursive &&
544 test_submodule_content sub1 origin/valid_sub1
545 )
546 '
547}
548
d3b5a497
JT
549# Declares and invokes several tests that, in various situations, checks that
550# the provided transition function:
551# - succeeds in updating the worktree and index of a superproject to a target
552# commit, or fails atomically (depending on the test situation)
553# - if succeeds, the contents of submodule directories are unchanged
554# - if succeeds, once "git submodule update" is invoked, the contents of
555# submodule directories are updated
556#
557# Use as follows:
558#
559# my_func () {
560# target=$1
561# # Do something here that updates the worktree and index to match target,
562# # but not any submodule directories.
563# }
564# test_submodule_switch "my_func"
565test_submodule_switch () {
42639d23 566 command="$1"
d3b5a497
JT
567 test_submodule_switch_common "$command"
568
569 # An empty directory does not prevent the creation of a submodule of
570 # the same name, but a file does.
571 test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
42639d23
JL
572 prolog &&
573 reset_work_tree_to no_submodule &&
574 (
575 cd submodule_update &&
576 git branch -t add_sub1 origin/add_sub1 &&
d3b5a497
JT
577 >sub1 &&
578 test_must_fail $command add_sub1 &&
579 test_superproject_content origin/no_submodule &&
580 test_must_be_empty sub1
42639d23
JL
581 )
582 '
d3b5a497
JT
583}
584
585# Same as test_submodule_switch(), except that throwing away local changes in
586# the superproject is allowed.
587test_submodule_forced_switch () {
588 command="$1"
589 KNOWN_FAILURE_FORCED_SWITCH_TESTS=1
590 test_submodule_switch_common "$command"
591
592 # When forced, a file in the superproject does not prevent creating a
593 # submodule of the same name.
42639d23
JL
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 '
42639d23 606}
259f3ee2
SB
607
608# Test that submodule contents are correctly updated when switching
609# between commits that change a submodule.
610# Test that the following transitions are correctly handled:
611# (These tests are also above in the case where we expect no change
612# in the submodule)
613# - Updated submodule
614# - New submodule
615# - Removed submodule
616# - Directory containing tracked files replaced by submodule
617# - Submodule replaced by tracked files in directory
618# - Submodule replaced by tracked file with the same name
619# - tracked file replaced by submodule
620#
621# New test cases
622# - Removing a submodule with a git directory absorbs the submodules
623# git directory first into the superproject.
624
17f38cb7
SB
625test_submodule_switch_recursing_with_args () {
626 cmd_args="$1"
627 command="git $cmd_args --recurse-submodules"
1fc458d9
SB
628 RESULTDS=success
629 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
630 then
631 RESULTDS=failure
632 fi
25804914
SB
633 RESULTOI=success
634 if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
635 then
636 RESULTOI=failure
637 fi
259f3ee2
SB
638 ######################### Appearing submodule #########################
639 # Switching to a commit letting a submodule appear checks it out ...
640 test_expect_success "$command: added submodule is checked out" '
641 prolog &&
642 reset_work_tree_to_interested no_submodule &&
643 (
644 cd submodule_update &&
645 git branch -t add_sub1 origin/add_sub1 &&
646 $command add_sub1 &&
647 test_superproject_content origin/add_sub1 &&
648 test_submodule_content sub1 origin/add_sub1
649 )
650 '
651 # ... ignoring an empty existing directory ...
652 test_expect_success "$command: added submodule is checked out in empty dir" '
653 prolog &&
654 reset_work_tree_to_interested no_submodule &&
655 (
656 cd submodule_update &&
657 mkdir sub1 &&
658 git branch -t add_sub1 origin/add_sub1 &&
659 $command add_sub1 &&
660 test_superproject_content origin/add_sub1 &&
661 test_submodule_content sub1 origin/add_sub1
662 )
663 '
664 # ... unless there is an untracked file in its place.
665 test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
666 prolog &&
667 reset_work_tree_to_interested no_submodule &&
668 (
669 cd submodule_update &&
670 git branch -t add_sub1 origin/add_sub1 &&
671 : >sub1 &&
672 test_must_fail $command add_sub1 &&
673 test_superproject_content origin/no_submodule &&
674 test_must_be_empty sub1
675 )
676 '
677 # ... but an ignored file is fine.
25804914 678 test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
259f3ee2
SB
679 test_when_finished "rm submodule_update/.git/info/exclude" &&
680 prolog &&
681 reset_work_tree_to_interested no_submodule &&
682 (
683 cd submodule_update &&
684 git branch -t add_sub1 origin/add_sub1 &&
685 : >sub1 &&
686 echo sub1 >.git/info/exclude
687 $command add_sub1 &&
688 test_superproject_content origin/add_sub1 &&
689 test_submodule_content sub1 origin/add_sub1
690 )
691 '
692 # Replacing a tracked file with a submodule produces a checked out submodule
693 test_expect_success "$command: replace tracked file with submodule checks out submodule" '
694 prolog &&
695 reset_work_tree_to_interested replace_sub1_with_file &&
696 (
697 cd submodule_update &&
698 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
699 $command replace_file_with_sub1 &&
700 test_superproject_content origin/replace_file_with_sub1 &&
701 test_submodule_content sub1 origin/replace_file_with_sub1
702 )
703 '
704 # ... as does removing a directory with tracked files with a submodule.
705 test_expect_success "$command: replace directory with submodule" '
706 prolog &&
707 reset_work_tree_to_interested replace_sub1_with_directory &&
708 (
709 cd submodule_update &&
710 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
711 $command replace_directory_with_sub1 &&
712 test_superproject_content origin/replace_directory_with_sub1 &&
713 test_submodule_content sub1 origin/replace_directory_with_sub1
714 )
715 '
716
717 ######################## Disappearing submodule #######################
718 # Removing a submodule removes its work tree ...
719 test_expect_success "$command: removed submodule removes submodules working tree" '
720 prolog &&
721 reset_work_tree_to_interested add_sub1 &&
722 (
723 cd submodule_update &&
724 git branch -t remove_sub1 origin/remove_sub1 &&
725 $command remove_sub1 &&
726 test_superproject_content origin/remove_sub1 &&
727 ! test -e sub1
728 )
729 '
730 # ... absorbing a .git directory along the way.
731 test_expect_success "$command: removed submodule absorbs submodules .git directory" '
732 prolog &&
733 reset_work_tree_to_interested add_sub1 &&
734 (
735 cd submodule_update &&
736 git branch -t remove_sub1 origin/remove_sub1 &&
737 replace_gitfile_with_git_dir sub1 &&
738 rm -rf .git/modules &&
739 $command remove_sub1 &&
740 test_superproject_content origin/remove_sub1 &&
741 ! test -e sub1 &&
742 test_git_directory_exists sub1
743 )
744 '
745 # Replacing a submodule with files in a directory must succeeds
746 # when the submodule is clean
1fc458d9 747 test_expect_$RESULTDS "$command: replace submodule with a directory" '
259f3ee2
SB
748 prolog &&
749 reset_work_tree_to_interested add_sub1 &&
750 (
751 cd submodule_update &&
752 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
753 $command replace_sub1_with_directory &&
754 test_superproject_content origin/replace_sub1_with_directory &&
755 test_submodule_content sub1 origin/replace_sub1_with_directory
756 )
757 '
758 # ... absorbing a .git directory.
1fc458d9 759 test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
259f3ee2
SB
760 prolog &&
761 reset_work_tree_to_interested add_sub1 &&
762 (
763 cd submodule_update &&
764 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
765 replace_gitfile_with_git_dir sub1 &&
766 rm -rf .git/modules &&
767 $command replace_sub1_with_directory &&
768 test_superproject_content origin/replace_sub1_with_directory &&
769 test_git_directory_exists sub1
770 )
771 '
772
773 # Replacing it with a file ...
774 test_expect_success "$command: replace submodule with a file" '
775 prolog &&
776 reset_work_tree_to_interested add_sub1 &&
777 (
778 cd submodule_update &&
779 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
780 $command replace_sub1_with_file &&
781 test_superproject_content origin/replace_sub1_with_file &&
782 test -f sub1
783 )
784 '
785
786 # ... must check its local work tree for untracked files
1fc458d9 787 test_expect_$RESULTDS "$command: replace submodule with a file must fail with untracked files" '
259f3ee2
SB
788 prolog &&
789 reset_work_tree_to_interested add_sub1 &&
790 (
791 cd submodule_update &&
792 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
793 : >sub1/untrackedfile &&
794 test_must_fail $command replace_sub1_with_file &&
795 test_superproject_content origin/add_sub1 &&
796 test_submodule_content sub1 origin/add_sub1
797 )
798 '
799
800 # ... and ignored files are ignored
801 test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
802 test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
803 prolog &&
804 reset_work_tree_to_interested add_sub1 &&
805 (
806 cd submodule_update &&
807 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
808 : >sub1/ignored &&
809 $command replace_sub1_with_file &&
810 test_superproject_content origin/replace_sub1_with_file &&
811 test -f sub1
812 )
813 '
814
815 ########################## Modified submodule #########################
816 # Updating a submodule sha1 updates the submodule's work tree
817 test_expect_success "$command: modified submodule updates submodule work tree" '
818 prolog &&
819 reset_work_tree_to_interested add_sub1 &&
820 (
821 cd submodule_update &&
822 git branch -t modify_sub1 origin/modify_sub1 &&
823 $command modify_sub1 &&
824 test_superproject_content origin/modify_sub1 &&
825 test_submodule_content sub1 origin/modify_sub1
826 )
827 '
828
046b4823
SB
829 test_expect_success "git -c submodule.recurse=true $cmd_args: modified submodule updates submodule work tree" '
830 prolog &&
831 reset_work_tree_to_interested add_sub1 &&
832 (
833 cd submodule_update &&
834 git branch -t modify_sub1 origin/modify_sub1 &&
835 git -c submodule.recurse=true $cmd_args modify_sub1 &&
836 test_superproject_content origin/modify_sub1 &&
837 test_submodule_content sub1 origin/modify_sub1
838 )
839 '
840
259f3ee2
SB
841 # Updating a submodule to an invalid sha1 doesn't update the
842 # superproject nor the submodule's work tree.
843 test_expect_success "$command: updating to a missing submodule commit fails" '
844 prolog &&
845 reset_work_tree_to_interested add_sub1 &&
846 (
847 cd submodule_update &&
848 git branch -t invalid_sub1 origin/invalid_sub1 &&
849 test_must_fail $command invalid_sub1 &&
850 test_superproject_content origin/add_sub1 &&
851 test_submodule_content sub1 origin/add_sub1
852 )
853 '
854
1fc458d9 855 # recursing deeper than one level doesn't work yet.
218c8837 856 test_expect_success "$command: modified submodule updates submodule recursively" '
259f3ee2
SB
857 prolog &&
858 reset_work_tree_to_interested add_nested_sub &&
859 (
860 cd submodule_update &&
861 git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
862 $command modify_sub1_recursively &&
863 test_superproject_content origin/modify_sub1_recursively &&
864 test_submodule_content sub1 origin/modify_sub1_recursively &&
865 test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
866 )
867 '
868}
869
870# Test that submodule contents are updated when switching between commits
871# that change a submodule, but throwing away local changes in
872# the superproject as well as the submodule is allowed.
17f38cb7
SB
873test_submodule_forced_switch_recursing_with_args () {
874 cmd_args="$1"
875 command="git $cmd_args --recurse-submodules"
1fc458d9
SB
876 RESULT=success
877 if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
878 then
879 RESULT=failure
880 fi
259f3ee2
SB
881 ######################### Appearing submodule #########################
882 # Switching to a commit letting a submodule appear creates empty dir ...
883 test_expect_success "$command: added submodule is checked out" '
884 prolog &&
885 reset_work_tree_to_interested no_submodule &&
886 (
887 cd submodule_update &&
888 git branch -t add_sub1 origin/add_sub1 &&
889 $command add_sub1 &&
890 test_superproject_content origin/add_sub1 &&
891 test_submodule_content sub1 origin/add_sub1
892 )
893 '
894 # ... and doesn't care if it already exists ...
895 test_expect_success "$command: added submodule ignores empty directory" '
896 prolog &&
897 reset_work_tree_to_interested no_submodule &&
898 (
899 cd submodule_update &&
900 git branch -t add_sub1 origin/add_sub1 &&
901 mkdir sub1 &&
902 $command add_sub1 &&
903 test_superproject_content origin/add_sub1 &&
904 test_submodule_content sub1 origin/add_sub1
905 )
906 '
907 # ... not caring about an untracked file either
908 test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
909 prolog &&
910 reset_work_tree_to_interested no_submodule &&
911 (
912 cd submodule_update &&
913 git branch -t add_sub1 origin/add_sub1 &&
914 >sub1 &&
915 $command add_sub1 &&
916 test_superproject_content origin/add_sub1 &&
917 test_submodule_content sub1 origin/add_sub1
918 )
919 '
920 # Replacing a tracked file with a submodule checks out the submodule
921 test_expect_success "$command: replace tracked file with submodule populates the submodule" '
922 prolog &&
923 reset_work_tree_to_interested replace_sub1_with_file &&
924 (
925 cd submodule_update &&
926 git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
927 $command replace_file_with_sub1 &&
928 test_superproject_content origin/replace_file_with_sub1 &&
929 test_submodule_content sub1 origin/replace_file_with_sub1
930 )
931 '
932 # ... as does removing a directory with tracked files with a
933 # submodule.
934 test_expect_success "$command: replace directory with submodule" '
935 prolog &&
936 reset_work_tree_to_interested replace_sub1_with_directory &&
937 (
938 cd submodule_update &&
939 git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
940 $command replace_directory_with_sub1 &&
941 test_superproject_content origin/replace_directory_with_sub1 &&
942 test_submodule_content sub1 origin/replace_directory_with_sub1
943 )
944 '
945
946 ######################## Disappearing submodule #######################
947 # Removing a submodule doesn't remove its work tree ...
948 test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
949 prolog &&
950 reset_work_tree_to_interested add_sub1 &&
951 (
952 cd submodule_update &&
953 git branch -t remove_sub1 origin/remove_sub1 &&
954 $command remove_sub1 &&
955 test_superproject_content origin/remove_sub1 &&
956 ! test -e sub1
957 )
958 '
959 # ... especially when it contains a .git directory.
960 test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
961 prolog &&
962 reset_work_tree_to_interested add_sub1 &&
963 (
964 cd submodule_update &&
965 git branch -t remove_sub1 origin/remove_sub1 &&
966 replace_gitfile_with_git_dir sub1 &&
967 rm -rf .git/modules/sub1 &&
968 $command remove_sub1 &&
969 test_superproject_content origin/remove_sub1 &&
970 test_git_directory_exists sub1 &&
971 ! test -e sub1
972 )
973 '
974 # Replacing a submodule with files in a directory ...
975 test_expect_success "$command: replace submodule with a directory" '
976 prolog &&
977 reset_work_tree_to_interested add_sub1 &&
978 (
979 cd submodule_update &&
980 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
981 $command replace_sub1_with_directory &&
982 test_superproject_content origin/replace_sub1_with_directory
983 )
984 '
985 # ... absorbing a .git directory.
986 test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
987 prolog &&
988 reset_work_tree_to_interested add_sub1 &&
989 (
990 cd submodule_update &&
991 git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
992 replace_gitfile_with_git_dir sub1 &&
993 rm -rf .git/modules/sub1 &&
994 $command replace_sub1_with_directory &&
995 test_superproject_content origin/replace_sub1_with_directory &&
996 test_submodule_content sub1 origin/modify_sub1
997 test_git_directory_exists sub1
998 )
999 '
1000 # Replacing it with a file
1001 test_expect_success "$command: replace submodule with a file" '
1002 prolog &&
1003 reset_work_tree_to_interested add_sub1 &&
1004 (
1005 cd submodule_update &&
1006 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1007 $command replace_sub1_with_file &&
1008 test_superproject_content origin/replace_sub1_with_file
1009 )
1010 '
1011
1012 # ... even if the submodule contains ignored files
1013 test_expect_success "$command: replace submodule with a file ignoring ignored files" '
1014 prolog &&
1015 reset_work_tree_to_interested add_sub1 &&
1016 (
1017 cd submodule_update &&
1018 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1019 : >sub1/expect &&
1020 $command replace_sub1_with_file &&
1021 test_superproject_content origin/replace_sub1_with_file
1022 )
1023 '
1024
1025 # ... but stops for untracked files that would be lost
1fc458d9 1026 test_expect_$RESULT "$command: replace submodule with a file stops for untracked files" '
259f3ee2
SB
1027 prolog &&
1028 reset_work_tree_to_interested add_sub1 &&
1029 (
1030 cd submodule_update &&
1031 git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1032 : >sub1/untracked_file &&
1033 test_must_fail $command replace_sub1_with_file &&
1034 test_superproject_content origin/add_sub1 &&
1035 test -f sub1/untracked_file
1036 )
1037 '
1038
1039 ########################## Modified submodule #########################
1040 # Updating a submodule sha1 updates the submodule's work tree
1041 test_expect_success "$command: modified submodule updates submodule work tree" '
1042 prolog &&
1043 reset_work_tree_to_interested add_sub1 &&
1044 (
1045 cd submodule_update &&
1046 git branch -t modify_sub1 origin/modify_sub1 &&
1047 $command modify_sub1 &&
1048 test_superproject_content origin/modify_sub1 &&
1049 test_submodule_content sub1 origin/modify_sub1
1050 )
1051 '
1052 # Updating a submodule to an invalid sha1 doesn't update the
1053 # submodule's work tree, subsequent update will fail
1054 test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
1055 prolog &&
1056 reset_work_tree_to_interested add_sub1 &&
1057 (
1058 cd submodule_update &&
1059 git branch -t invalid_sub1 origin/invalid_sub1 &&
1060 test_must_fail $command invalid_sub1 &&
1061 test_superproject_content origin/add_sub1 &&
1062 test_submodule_content sub1 origin/add_sub1
1063 )
1064 '
1065 # Updating a submodule from an invalid sha1 updates
f2d48994 1066 test_expect_success "$command: modified submodule does update submodule work tree from invalid commit" '
259f3ee2
SB
1067 prolog &&
1068 reset_work_tree_to_interested invalid_sub1 &&
1069 (
1070 cd submodule_update &&
1071 git branch -t valid_sub1 origin/valid_sub1 &&
f2d48994
SB
1072 $command valid_sub1 &&
1073 test_superproject_content origin/valid_sub1 &&
1074 test_submodule_content sub1 origin/valid_sub1
1075 )
1076 '
1077
1078 # Old versions of Git were buggy writing the .git link file
1079 # (e.g. before f8eaa0ba98b and then moving the superproject repo
1080 # whose submodules contained absolute paths)
1081 test_expect_success "$command: updating submodules fixes .git links" '
1082 prolog &&
1083 reset_work_tree_to_interested add_sub1 &&
1084 (
1085 cd submodule_update &&
1086 git branch -t modify_sub1 origin/modify_sub1 &&
1087 echo "gitdir: bogus/path" >sub1/.git &&
1088 $command modify_sub1 &&
1089 test_superproject_content origin/modify_sub1 &&
1090 test_submodule_content sub1 origin/modify_sub1
259f3ee2
SB
1091 )
1092 '
1093}