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