t / lib-submodule-update.shon commit Merge branch 'sb/submodule-embed-gitdir' (da2b74e)
   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        if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
 231        then
 232                # Restoring stash fails to restore submodule index entry
 233                RESULT="failure"
 234        else
 235                RESULT="success"
 236        fi
 237        test_expect_$RESULT "$command: added submodule creates empty directory" '
 238                prolog &&
 239                reset_work_tree_to no_submodule &&
 240                (
 241                        cd submodule_update &&
 242                        git branch -t add_sub1 origin/add_sub1 &&
 243                        $command add_sub1 &&
 244                        test_superproject_content origin/add_sub1 &&
 245                        test_dir_is_empty sub1 &&
 246                        git submodule update --init --recursive &&
 247                        test_submodule_content sub1 origin/add_sub1
 248                )
 249        '
 250        # ... and doesn't care if it already exists ...
 251        test_expect_$RESULT "$command: added submodule leaves existing empty directory alone" '
 252                prolog &&
 253                reset_work_tree_to no_submodule &&
 254                (
 255                        cd submodule_update &&
 256                        mkdir sub1 &&
 257                        git branch -t add_sub1 origin/add_sub1 &&
 258                        $command add_sub1 &&
 259                        test_superproject_content origin/add_sub1 &&
 260                        test_dir_is_empty sub1 &&
 261                        git submodule update --init --recursive &&
 262                        test_submodule_content sub1 origin/add_sub1
 263                )
 264        '
 265        # ... unless there is an untracked file in its place.
 266        test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
 267                prolog &&
 268                reset_work_tree_to no_submodule &&
 269                (
 270                        cd submodule_update &&
 271                        git branch -t add_sub1 origin/add_sub1 &&
 272                        >sub1 &&
 273                        test_must_fail $command add_sub1 &&
 274                        test_superproject_content origin/no_submodule &&
 275                        test_must_be_empty sub1
 276                )
 277        '
 278        # Replacing a tracked file with a submodule produces an empty
 279        # directory ...
 280        test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
 281                prolog &&
 282                reset_work_tree_to replace_sub1_with_file &&
 283                (
 284                        cd submodule_update &&
 285                        git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
 286                        $command replace_file_with_sub1 &&
 287                        test_superproject_content origin/replace_file_with_sub1 &&
 288                        test_dir_is_empty sub1 &&
 289                        git submodule update --init --recursive &&
 290                        test_submodule_content sub1 origin/replace_file_with_sub1
 291                )
 292        '
 293        # ... as does removing a directory with tracked files with a
 294        # submodule.
 295        if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
 296        then
 297                # Non fast-forward merges fail with "Directory sub1 doesn't
 298                # exist. sub1" because the empty submodule directory is not
 299                # created
 300                RESULT="failure"
 301        else
 302                RESULT="success"
 303        fi
 304        test_expect_$RESULT "$command: replace directory with submodule" '
 305                prolog &&
 306                reset_work_tree_to replace_sub1_with_directory &&
 307                (
 308                        cd submodule_update &&
 309                        git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
 310                        $command replace_directory_with_sub1 &&
 311                        test_superproject_content origin/replace_directory_with_sub1 &&
 312                        test_dir_is_empty sub1 &&
 313                        git submodule update --init --recursive &&
 314                        test_submodule_content sub1 origin/replace_directory_with_sub1
 315                )
 316        '
 317
 318        ######################## Disappearing submodule #######################
 319        # Removing a submodule doesn't remove its work tree ...
 320        if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
 321        then
 322                RESULT="failure"
 323        else
 324                RESULT="success"
 325        fi
 326        test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
 327                prolog &&
 328                reset_work_tree_to add_sub1 &&
 329                (
 330                        cd submodule_update &&
 331                        git branch -t remove_sub1 origin/remove_sub1 &&
 332                        $command remove_sub1 &&
 333                        test_superproject_content origin/remove_sub1 &&
 334                        test_submodule_content sub1 origin/add_sub1
 335                )
 336        '
 337        # ... especially when it contains a .git directory.
 338        test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
 339                prolog &&
 340                reset_work_tree_to add_sub1 &&
 341                (
 342                        cd submodule_update &&
 343                        git branch -t remove_sub1 origin/remove_sub1 &&
 344                        replace_gitfile_with_git_dir sub1 &&
 345                        $command remove_sub1 &&
 346                        test_superproject_content origin/remove_sub1 &&
 347                        test_git_directory_is_unchanged sub1 &&
 348                        test_submodule_content sub1 origin/add_sub1
 349                )
 350        '
 351        # Replacing a submodule with files in a directory must fail as the
 352        # submodule work tree isn't removed ...
 353        if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
 354        then
 355                # Non fast-forward merges attempt to merge the former
 356                # submodule files with the newly checked out ones in the
 357                # directory of the same name while it shouldn't.
 358                RESULT="failure"
 359        else
 360                RESULT="success"
 361        fi
 362        test_expect_$RESULT "$command: replace submodule with a directory must fail" '
 363                prolog &&
 364                reset_work_tree_to add_sub1 &&
 365                (
 366                        cd submodule_update &&
 367                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
 368                        test_must_fail $command replace_sub1_with_directory &&
 369                        test_superproject_content origin/add_sub1 &&
 370                        test_submodule_content sub1 origin/add_sub1
 371                )
 372        '
 373        # ... especially when it contains a .git directory.
 374        test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
 375                prolog &&
 376                reset_work_tree_to add_sub1 &&
 377                (
 378                        cd submodule_update &&
 379                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
 380                        replace_gitfile_with_git_dir sub1 &&
 381                        test_must_fail $command replace_sub1_with_directory &&
 382                        test_superproject_content origin/add_sub1 &&
 383                        test_git_directory_is_unchanged sub1 &&
 384                        test_submodule_content sub1 origin/add_sub1
 385                )
 386        '
 387        # Replacing it with a file must fail as it could throw away any local
 388        # work tree changes ...
 389        test_expect_failure "$command: replace submodule 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                        test_must_fail $command replace_sub1_with_file &&
 396                        test_superproject_content origin/add_sub1 &&
 397                        test_submodule_content sub1 origin/add_sub1
 398                )
 399        '
 400        # ... or even destroy unpushed parts of submodule history if that
 401        # still uses a .git directory.
 402        test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
 403                prolog &&
 404                reset_work_tree_to add_sub1 &&
 405                (
 406                        cd submodule_update &&
 407                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
 408                        replace_gitfile_with_git_dir sub1 &&
 409                        test_must_fail $command replace_sub1_with_file &&
 410                        test_superproject_content origin/add_sub1 &&
 411                        test_git_directory_is_unchanged sub1 &&
 412                        test_submodule_content sub1 origin/add_sub1
 413                )
 414        '
 415
 416        ########################## Modified submodule #########################
 417        # Updating a submodule sha1 doesn't update the submodule's work tree
 418        if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
 419        then
 420                # When cherry picking a SHA-1 update for an ignored submodule
 421                # the commit incorrectly fails with "The previous cherry-pick
 422                # is now empty, possibly due to conflict resolution."
 423                RESULT="failure"
 424        else
 425                RESULT="success"
 426        fi
 427        test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
 428                prolog &&
 429                reset_work_tree_to add_sub1 &&
 430                (
 431                        cd submodule_update &&
 432                        git branch -t modify_sub1 origin/modify_sub1 &&
 433                        $command modify_sub1 &&
 434                        test_superproject_content origin/modify_sub1 &&
 435                        test_submodule_content sub1 origin/add_sub1 &&
 436                        git submodule update &&
 437                        test_submodule_content sub1 origin/modify_sub1
 438                )
 439        '
 440
 441        # Updating a submodule to an invalid sha1 doesn't update the
 442        # submodule's work tree, subsequent update will fail
 443        test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
 444                prolog &&
 445                reset_work_tree_to add_sub1 &&
 446                (
 447                        cd submodule_update &&
 448                        git branch -t invalid_sub1 origin/invalid_sub1 &&
 449                        $command invalid_sub1 &&
 450                        test_superproject_content origin/invalid_sub1 &&
 451                        test_submodule_content sub1 origin/add_sub1 &&
 452                        test_must_fail git submodule update &&
 453                        test_submodule_content sub1 origin/add_sub1
 454                )
 455        '
 456        # Updating a submodule from an invalid sha1 doesn't update the
 457        # submodule's work tree, subsequent update will succeed
 458        test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
 459                prolog &&
 460                reset_work_tree_to invalid_sub1 &&
 461                (
 462                        cd submodule_update &&
 463                        git branch -t valid_sub1 origin/valid_sub1 &&
 464                        $command valid_sub1 &&
 465                        test_superproject_content origin/valid_sub1 &&
 466                        test_dir_is_empty sub1 &&
 467                        git submodule update --init --recursive &&
 468                        test_submodule_content sub1 origin/valid_sub1
 469                )
 470        '
 471}
 472
 473# Test that submodule contents are currently not updated when switching
 474# between commits that change a submodule, but throwing away local changes in
 475# the superproject is allowed.
 476test_submodule_forced_switch () {
 477        command="$1"
 478        ######################### Appearing submodule #########################
 479        # Switching to a commit letting a submodule appear creates empty dir ...
 480        test_expect_success "$command: added submodule creates empty directory" '
 481                prolog &&
 482                reset_work_tree_to no_submodule &&
 483                (
 484                        cd submodule_update &&
 485                        git branch -t add_sub1 origin/add_sub1 &&
 486                        $command add_sub1 &&
 487                        test_superproject_content origin/add_sub1 &&
 488                        test_dir_is_empty sub1 &&
 489                        git submodule update --init --recursive &&
 490                        test_submodule_content sub1 origin/add_sub1
 491                )
 492        '
 493        # ... and doesn't care if it already exists ...
 494        test_expect_success "$command: added submodule leaves existing empty directory alone" '
 495                prolog &&
 496                reset_work_tree_to no_submodule &&
 497                (
 498                        cd submodule_update &&
 499                        git branch -t add_sub1 origin/add_sub1 &&
 500                        mkdir sub1 &&
 501                        $command add_sub1 &&
 502                        test_superproject_content origin/add_sub1 &&
 503                        test_dir_is_empty sub1 &&
 504                        git submodule update --init --recursive &&
 505                        test_submodule_content sub1 origin/add_sub1
 506                )
 507        '
 508        # ... unless there is an untracked file in its place.
 509        test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
 510                prolog &&
 511                reset_work_tree_to no_submodule &&
 512                (
 513                        cd submodule_update &&
 514                        git branch -t add_sub1 origin/add_sub1 &&
 515                        >sub1 &&
 516                        $command add_sub1 &&
 517                        test_superproject_content origin/add_sub1 &&
 518                        test_dir_is_empty sub1
 519                )
 520        '
 521        # Replacing a tracked file with a submodule produces an empty
 522        # directory ...
 523        test_expect_success "$command: replace tracked file with submodule creates empty directory" '
 524                prolog &&
 525                reset_work_tree_to replace_sub1_with_file &&
 526                (
 527                        cd submodule_update &&
 528                        git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
 529                        $command replace_file_with_sub1 &&
 530                        test_superproject_content origin/replace_file_with_sub1 &&
 531                        test_dir_is_empty sub1 &&
 532                        git submodule update --init --recursive &&
 533                        test_submodule_content sub1 origin/replace_file_with_sub1
 534                )
 535        '
 536        # ... as does removing a directory with tracked files with a
 537        # submodule.
 538        test_expect_success "$command: replace directory with submodule" '
 539                prolog &&
 540                reset_work_tree_to replace_sub1_with_directory &&
 541                (
 542                        cd submodule_update &&
 543                        git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
 544                        $command replace_directory_with_sub1 &&
 545                        test_superproject_content origin/replace_directory_with_sub1 &&
 546                        test_dir_is_empty sub1 &&
 547                        git submodule update --init --recursive &&
 548                        test_submodule_content sub1 origin/replace_directory_with_sub1
 549                )
 550        '
 551
 552        ######################## Disappearing submodule #######################
 553        # Removing a submodule doesn't remove its work tree ...
 554        test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
 555                prolog &&
 556                reset_work_tree_to add_sub1 &&
 557                (
 558                        cd submodule_update &&
 559                        git branch -t remove_sub1 origin/remove_sub1 &&
 560                        $command remove_sub1 &&
 561                        test_superproject_content origin/remove_sub1 &&
 562                        test_submodule_content sub1 origin/add_sub1
 563                )
 564        '
 565        # ... especially when it contains a .git directory.
 566        test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
 567                prolog &&
 568                reset_work_tree_to add_sub1 &&
 569                (
 570                        cd submodule_update &&
 571                        git branch -t remove_sub1 origin/remove_sub1 &&
 572                        replace_gitfile_with_git_dir sub1 &&
 573                        $command remove_sub1 &&
 574                        test_superproject_content origin/remove_sub1 &&
 575                        test_git_directory_is_unchanged sub1 &&
 576                        test_submodule_content sub1 origin/add_sub1
 577                )
 578        '
 579        # Replacing a submodule with files in a directory must fail as the
 580        # submodule work tree isn't removed ...
 581        test_expect_failure "$command: replace submodule with a directory must fail" '
 582                prolog &&
 583                reset_work_tree_to add_sub1 &&
 584                (
 585                        cd submodule_update &&
 586                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
 587                        test_must_fail $command replace_sub1_with_directory &&
 588                        test_superproject_content origin/add_sub1 &&
 589                        test_submodule_content sub1 origin/add_sub1
 590                )
 591        '
 592        # ... especially when it contains a .git directory.
 593        test_expect_failure "$command: replace submodule containing a .git directory with a directory must fail" '
 594                prolog &&
 595                reset_work_tree_to add_sub1 &&
 596                (
 597                        cd submodule_update &&
 598                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
 599                        replace_gitfile_with_git_dir sub1 &&
 600                        test_must_fail $command replace_sub1_with_directory &&
 601                        test_superproject_content origin/add_sub1 &&
 602                        test_git_directory_is_unchanged sub1 &&
 603                        test_submodule_content sub1 origin/add_sub1
 604                )
 605        '
 606        # Replacing it with a file must fail as it could throw away any local
 607        # work tree changes ...
 608        test_expect_failure "$command: replace submodule with a file must fail" '
 609                prolog &&
 610                reset_work_tree_to add_sub1 &&
 611                (
 612                        cd submodule_update &&
 613                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
 614                        test_must_fail $command replace_sub1_with_file &&
 615                        test_superproject_content origin/add_sub1 &&
 616                        test_submodule_content sub1 origin/add_sub1
 617                )
 618        '
 619        # ... or even destroy unpushed parts of submodule history if that
 620        # still uses a .git directory.
 621        test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
 622                prolog &&
 623                reset_work_tree_to add_sub1 &&
 624                (
 625                        cd submodule_update &&
 626                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
 627                        replace_gitfile_with_git_dir sub1 &&
 628                        test_must_fail $command replace_sub1_with_file &&
 629                        test_superproject_content origin/add_sub1 &&
 630                        test_git_directory_is_unchanged sub1 &&
 631                        test_submodule_content sub1 origin/add_sub1
 632                )
 633        '
 634
 635        ########################## Modified submodule #########################
 636        # Updating a submodule sha1 doesn't update the submodule's work tree
 637        test_expect_success "$command: modified submodule does not update submodule work tree" '
 638                prolog &&
 639                reset_work_tree_to add_sub1 &&
 640                (
 641                        cd submodule_update &&
 642                        git branch -t modify_sub1 origin/modify_sub1 &&
 643                        $command modify_sub1 &&
 644                        test_superproject_content origin/modify_sub1 &&
 645                        test_submodule_content sub1 origin/add_sub1 &&
 646                        git submodule update &&
 647                        test_submodule_content sub1 origin/modify_sub1
 648                )
 649        '
 650        # Updating a submodule to an invalid sha1 doesn't update the
 651        # submodule's work tree, subsequent update will fail
 652        test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
 653                prolog &&
 654                reset_work_tree_to add_sub1 &&
 655                (
 656                        cd submodule_update &&
 657                        git branch -t invalid_sub1 origin/invalid_sub1 &&
 658                        $command invalid_sub1 &&
 659                        test_superproject_content origin/invalid_sub1 &&
 660                        test_submodule_content sub1 origin/add_sub1 &&
 661                        test_must_fail git submodule update &&
 662                        test_submodule_content sub1 origin/add_sub1
 663                )
 664        '
 665        # Updating a submodule from an invalid sha1 doesn't update the
 666        # submodule's work tree, subsequent update will succeed
 667        test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
 668                prolog &&
 669                reset_work_tree_to invalid_sub1 &&
 670                (
 671                        cd submodule_update &&
 672                        git branch -t valid_sub1 origin/valid_sub1 &&
 673                        $command valid_sub1 &&
 674                        test_superproject_content origin/valid_sub1 &&
 675                        test_dir_is_empty sub1 &&
 676                        git submodule update --init --recursive &&
 677                        test_submodule_content sub1 origin/valid_sub1
 678                )
 679        '
 680}