@@ -306,9 +306,9 @@ test_submodule_content () {
306306# to protect the history!
307307#
308308
309- # Test that submodule contents are currently not updated when switching
310- # between commits that change a submodule .
311- test_submodule_switch () {
309+ # Internal function; use test_submodule_switch() or
310+ # test_submodule_forced_switch() instead .
311+ test_submodule_switch_common () {
312312 command=" $1 "
313313 # ######################## Appearing submodule #########################
314314 # Switching to a commit letting a submodule appear creates empty dir ...
@@ -332,7 +332,7 @@ test_submodule_switch () {
332332 test_submodule_content sub1 origin/add_sub1
333333 )
334334 '
335- # ... and doesn't care if it already exists .. .
335+ # ... and doesn't care if it already exists.
336336 test_expect_$RESULT " $command : added submodule leaves existing empty directory alone" '
337337 prolog &&
338338 reset_work_tree_to no_submodule &&
@@ -347,19 +347,6 @@ test_submodule_switch () {
347347 test_submodule_content sub1 origin/add_sub1
348348 )
349349 '
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- '
363350 # Replacing a tracked file with a submodule produces an empty
364351 # directory ...
365352 test_expect_$RESULT " $command : replace tracked file with submodule creates empty directory" '
@@ -441,6 +428,11 @@ test_submodule_switch () {
441428 # submodule files with the newly checked out ones in the
442429 # directory of the same name while it shouldn't.
443430 RESULT=" failure"
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"
444436 else
445437 RESULT=" success"
446438 fi
@@ -522,7 +514,6 @@ test_submodule_switch () {
522514 test_submodule_content sub1 origin/modify_sub1
523515 )
524516 '
525-
526517 # Updating a submodule to an invalid sha1 doesn't update the
527518 # submodule's work tree, subsequent update will fail
528519 test_expect_$RESULT " $command : modified submodule does not update submodule work tree to invalid commit" '
@@ -555,42 +546,51 @@ test_submodule_switch () {
555546 '
556547}
557548
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.
561- test_submodule_forced_switch () {
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"
565+ test_submodule_switch () {
562566 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" '
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" '
580572 prolog &&
581573 reset_work_tree_to no_submodule &&
582574 (
583575 cd submodule_update &&
584576 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
577+ >sub1 &&
578+ test_must_fail $command add_sub1 &&
579+ test_superproject_content origin/no_submodule &&
580+ test_must_be_empty sub1
591581 )
592582 '
593- # ... unless there is an untracked file in its place.
583+ }
584+
585+ # Same as test_submodule_switch(), except that throwing away local changes in
586+ # the superproject is allowed.
587+ test_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.
594594 test_expect_success " $command : added submodule does remove untracked unignored file with same name when forced" '
595595 prolog &&
596596 reset_work_tree_to no_submodule &&
@@ -603,165 +603,6 @@ test_submodule_forced_switch () {
603603 test_dir_is_empty sub1
604604 )
605605 '
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- '
765606}
766607
767608# Test that submodule contents are correctly updated when switching
0 commit comments