t / lib-submodule-update.shon commit builtin/fetch.c: respect 'submodule.recurse' option (58f4203)
   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# - Updated submodule recursively (add_nested_sub => modify_sub1_recursively)
   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#
  19#                     ----O
  20#                    /    ^
  21#                   /     remove_sub1
  22#                  /
  23#       add_sub1  /-------O---------O--------O  modify_sub1_recursively
  24#             |  /        ^         add_nested_sub
  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
  38#
  39
  40create_lib_submodule_repo () {
  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        ) &&
  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        ) &&
  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" &&
  75                git submodule add ../submodule_update_sub1 sub1 &&
  76                git submodule add ../submodule_update_sub1 uninitialized_sub &&
  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" &&
  81
  82                git checkout -b remove_sub1 add_sub1 &&
  83                git revert HEAD &&
  84
  85                git checkout -b modify_sub1 add_sub1 &&
  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
 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
 120                git checkout -b replace_sub1_with_directory add_sub1 &&
 121                git submodule update &&
 122                git -C sub1 checkout modifications &&
 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" &&
 128
 129                git checkout -b replace_directory_with_sub1 &&
 130                git revert HEAD &&
 131
 132                git checkout -b replace_sub1_with_file add_sub1 &&
 133                git rm sub1 &&
 134                echo "content" >sub1 &&
 135                git add sub1 &&
 136                git commit -m "Replace sub1 with file" &&
 137
 138                git checkout -b replace_file_with_sub1 &&
 139                git revert HEAD &&
 140
 141                git checkout -b invalid_sub1 add_sub1 &&
 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 &&
 146
 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
 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
 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 &&
 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}")
 219                then
 220                        git submodule update --init --recursive "sub1"
 221                fi
 222        )
 223}
 224
 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
 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 () {
 259        if test x"$1" = "x-C"
 260        then
 261                cd "$2"
 262                shift; shift;
 263        fi
 264        if test $# != 2
 265        then
 266                echo "test_submodule_content needs two arguments"
 267                return 1
 268        fi &&
 269        submodule="$1" &&
 270        commit="$2" &&
 271        test -d "$submodule"/ &&
 272        if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
 273        then
 274                echo "Submodule $submodule is not populated"
 275                return 1
 276        fi &&
 277        sha1=$(git rev-parse --verify "$commit:$submodule") &&
 278        if test -z "$sha1"
 279        then
 280                echo "Couldn't retrieve SHA-1 of $submodule for $commit"
 281                return 1
 282        fi &&
 283        (
 284                cd "$submodule" &&
 285                git status -u -s >actual &&
 286                test_must_be_empty actual &&
 287                git diff "$sha1" >actual &&
 288                test_must_be_empty actual
 289        )
 290}
 291
 292# Test that the following transitions are correctly handled:
 293# - Updated submodule
 294# - New submodule
 295# - Removed submodule
 296# - Directory containing tracked files replaced by submodule
 297# - Submodule replaced by tracked files in directory
 298# - Submodule replaced by tracked file with the same name
 299# - tracked file replaced by submodule
 300#
 301# The default is that submodule contents aren't changed until "git submodule
 302# update" is run. And even then that command doesn't delete the work tree of
 303# a removed submodule.
 304#
 305# Removing a submodule containing a .git directory must fail even when forced
 306# to protect the history!
 307#
 308
 309# Test that submodule contents are currently not updated when switching
 310# between commits that change a submodule.
 311test_submodule_switch () {
 312        command="$1"
 313        ######################### Appearing submodule #########################
 314        # Switching to a commit letting a submodule appear creates empty dir ...
 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" '
 323                prolog &&
 324                reset_work_tree_to no_submodule &&
 325                (
 326                        cd submodule_update &&
 327                        git branch -t add_sub1 origin/add_sub1 &&
 328                        $command add_sub1 &&
 329                        test_superproject_content origin/add_sub1 &&
 330                        test_dir_is_empty sub1 &&
 331                        git submodule update --init --recursive &&
 332                        test_submodule_content sub1 origin/add_sub1
 333                )
 334        '
 335        # ... and doesn't care if it already exists ...
 336        test_expect_$RESULT "$command: added submodule leaves existing empty directory alone" '
 337                prolog &&
 338                reset_work_tree_to no_submodule &&
 339                (
 340                        cd submodule_update &&
 341                        mkdir sub1 &&
 342                        git branch -t add_sub1 origin/add_sub1 &&
 343                        $command add_sub1 &&
 344                        test_superproject_content origin/add_sub1 &&
 345                        test_dir_is_empty sub1 &&
 346                        git submodule update --init --recursive &&
 347                        test_submodule_content sub1 origin/add_sub1
 348                )
 349        '
 350        # ... unless there is an untracked file in its place.
 351        test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
 352                prolog &&
 353                reset_work_tree_to no_submodule &&
 354                (
 355                        cd submodule_update &&
 356                        git branch -t add_sub1 origin/add_sub1 &&
 357                        >sub1 &&
 358                        test_must_fail $command add_sub1 &&
 359                        test_superproject_content origin/no_submodule &&
 360                        test_must_be_empty sub1
 361                )
 362        '
 363        # Replacing a tracked file with a submodule produces an empty
 364        # directory ...
 365        test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
 366                prolog &&
 367                reset_work_tree_to replace_sub1_with_file &&
 368                (
 369                        cd submodule_update &&
 370                        git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
 371                        $command replace_file_with_sub1 &&
 372                        test_superproject_content origin/replace_file_with_sub1 &&
 373                        test_dir_is_empty sub1 &&
 374                        git submodule update --init --recursive &&
 375                        test_submodule_content sub1 origin/replace_file_with_sub1
 376                )
 377        '
 378        # ... as does removing a directory with tracked files with a
 379        # submodule.
 380        if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
 381        then
 382                # Non fast-forward merges fail with "Directory sub1 doesn't
 383                # exist. sub1" because the empty submodule directory is not
 384                # created
 385                RESULT="failure"
 386        else
 387                RESULT="success"
 388        fi
 389        test_expect_$RESULT "$command: replace directory with submodule" '
 390                prolog &&
 391                reset_work_tree_to replace_sub1_with_directory &&
 392                (
 393                        cd submodule_update &&
 394                        git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
 395                        $command replace_directory_with_sub1 &&
 396                        test_superproject_content origin/replace_directory_with_sub1 &&
 397                        test_dir_is_empty sub1 &&
 398                        git submodule update --init --recursive &&
 399                        test_submodule_content sub1 origin/replace_directory_with_sub1
 400                )
 401        '
 402
 403        ######################## Disappearing submodule #######################
 404        # Removing a submodule doesn't remove its work tree ...
 405        if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
 406        then
 407                RESULT="failure"
 408        else
 409                RESULT="success"
 410        fi
 411        test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
 412                prolog &&
 413                reset_work_tree_to add_sub1 &&
 414                (
 415                        cd submodule_update &&
 416                        git branch -t remove_sub1 origin/remove_sub1 &&
 417                        $command remove_sub1 &&
 418                        test_superproject_content origin/remove_sub1 &&
 419                        test_submodule_content sub1 origin/add_sub1
 420                )
 421        '
 422        # ... especially when it contains a .git directory.
 423        test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
 424                prolog &&
 425                reset_work_tree_to add_sub1 &&
 426                (
 427                        cd submodule_update &&
 428                        git branch -t remove_sub1 origin/remove_sub1 &&
 429                        replace_gitfile_with_git_dir sub1 &&
 430                        $command remove_sub1 &&
 431                        test_superproject_content origin/remove_sub1 &&
 432                        test_git_directory_is_unchanged sub1 &&
 433                        test_submodule_content sub1 origin/add_sub1
 434                )
 435        '
 436        # Replacing a submodule with files in a directory must fail as the
 437        # submodule work tree isn't removed ...
 438        if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
 439        then
 440                # Non fast-forward merges attempt to merge the former
 441                # submodule files with the newly checked out ones in the
 442                # directory of the same name while it shouldn't.
 443                RESULT="failure"
 444        else
 445                RESULT="success"
 446        fi
 447        test_expect_$RESULT "$command: replace submodule with a directory must fail" '
 448                prolog &&
 449                reset_work_tree_to add_sub1 &&
 450                (
 451                        cd submodule_update &&
 452                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
 453                        test_must_fail $command replace_sub1_with_directory &&
 454                        test_superproject_content origin/add_sub1 &&
 455                        test_submodule_content sub1 origin/add_sub1
 456                )
 457        '
 458        # ... especially when it contains a .git directory.
 459        test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
 460                prolog &&
 461                reset_work_tree_to add_sub1 &&
 462                (
 463                        cd submodule_update &&
 464                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
 465                        replace_gitfile_with_git_dir sub1 &&
 466                        test_must_fail $command replace_sub1_with_directory &&
 467                        test_superproject_content origin/add_sub1 &&
 468                        test_git_directory_is_unchanged sub1 &&
 469                        test_submodule_content sub1 origin/add_sub1
 470                )
 471        '
 472        # Replacing it with a file must fail as it could throw away any local
 473        # work tree changes ...
 474        test_expect_failure "$command: replace submodule with a file must fail" '
 475                prolog &&
 476                reset_work_tree_to add_sub1 &&
 477                (
 478                        cd submodule_update &&
 479                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
 480                        test_must_fail $command replace_sub1_with_file &&
 481                        test_superproject_content origin/add_sub1 &&
 482                        test_submodule_content sub1 origin/add_sub1
 483                )
 484        '
 485        # ... or even destroy unpushed parts of submodule history if that
 486        # still uses a .git directory.
 487        test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
 488                prolog &&
 489                reset_work_tree_to add_sub1 &&
 490                (
 491                        cd submodule_update &&
 492                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
 493                        replace_gitfile_with_git_dir sub1 &&
 494                        test_must_fail $command replace_sub1_with_file &&
 495                        test_superproject_content origin/add_sub1 &&
 496                        test_git_directory_is_unchanged sub1 &&
 497                        test_submodule_content sub1 origin/add_sub1
 498                )
 499        '
 500
 501        ########################## Modified submodule #########################
 502        # Updating a submodule sha1 doesn't update the submodule's work tree
 503        if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
 504        then
 505                # When cherry picking a SHA-1 update for an ignored submodule
 506                # the commit incorrectly fails with "The previous cherry-pick
 507                # is now empty, possibly due to conflict resolution."
 508                RESULT="failure"
 509        else
 510                RESULT="success"
 511        fi
 512        test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
 513                prolog &&
 514                reset_work_tree_to add_sub1 &&
 515                (
 516                        cd submodule_update &&
 517                        git branch -t modify_sub1 origin/modify_sub1 &&
 518                        $command modify_sub1 &&
 519                        test_superproject_content origin/modify_sub1 &&
 520                        test_submodule_content sub1 origin/add_sub1 &&
 521                        git submodule update &&
 522                        test_submodule_content sub1 origin/modify_sub1
 523                )
 524        '
 525
 526        # Updating a submodule to an invalid sha1 doesn't update the
 527        # submodule's work tree, subsequent update will fail
 528        test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
 529                prolog &&
 530                reset_work_tree_to add_sub1 &&
 531                (
 532                        cd submodule_update &&
 533                        git branch -t invalid_sub1 origin/invalid_sub1 &&
 534                        $command invalid_sub1 &&
 535                        test_superproject_content origin/invalid_sub1 &&
 536                        test_submodule_content sub1 origin/add_sub1 &&
 537                        test_must_fail git submodule update &&
 538                        test_submodule_content sub1 origin/add_sub1
 539                )
 540        '
 541        # Updating a submodule from an invalid sha1 doesn't update the
 542        # submodule's work tree, subsequent update will succeed
 543        test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
 544                prolog &&
 545                reset_work_tree_to invalid_sub1 &&
 546                (
 547                        cd submodule_update &&
 548                        git branch -t valid_sub1 origin/valid_sub1 &&
 549                        $command valid_sub1 &&
 550                        test_superproject_content origin/valid_sub1 &&
 551                        test_dir_is_empty sub1 &&
 552                        git submodule update --init --recursive &&
 553                        test_submodule_content sub1 origin/valid_sub1
 554                )
 555        '
 556}
 557
 558# Test that submodule contents are currently not updated when switching
 559# between commits that change a submodule, but throwing away local changes in
 560# the superproject is allowed.
 561test_submodule_forced_switch () {
 562        command="$1"
 563        ######################### Appearing submodule #########################
 564        # Switching to a commit letting a submodule appear creates empty dir ...
 565        test_expect_success "$command: added submodule creates empty directory" '
 566                prolog &&
 567                reset_work_tree_to no_submodule &&
 568                (
 569                        cd submodule_update &&
 570                        git branch -t add_sub1 origin/add_sub1 &&
 571                        $command add_sub1 &&
 572                        test_superproject_content origin/add_sub1 &&
 573                        test_dir_is_empty sub1 &&
 574                        git submodule update --init --recursive &&
 575                        test_submodule_content sub1 origin/add_sub1
 576                )
 577        '
 578        # ... and doesn't care if it already exists ...
 579        test_expect_success "$command: added submodule leaves existing empty directory alone" '
 580                prolog &&
 581                reset_work_tree_to no_submodule &&
 582                (
 583                        cd submodule_update &&
 584                        git branch -t add_sub1 origin/add_sub1 &&
 585                        mkdir sub1 &&
 586                        $command add_sub1 &&
 587                        test_superproject_content origin/add_sub1 &&
 588                        test_dir_is_empty sub1 &&
 589                        git submodule update --init --recursive &&
 590                        test_submodule_content sub1 origin/add_sub1
 591                )
 592        '
 593        # ... unless there is an untracked file in its place.
 594        test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
 595                prolog &&
 596                reset_work_tree_to no_submodule &&
 597                (
 598                        cd submodule_update &&
 599                        git branch -t add_sub1 origin/add_sub1 &&
 600                        >sub1 &&
 601                        $command add_sub1 &&
 602                        test_superproject_content origin/add_sub1 &&
 603                        test_dir_is_empty sub1
 604                )
 605        '
 606        # Replacing a tracked file with a submodule produces an empty
 607        # directory ...
 608        test_expect_success "$command: replace tracked file with submodule creates empty directory" '
 609                prolog &&
 610                reset_work_tree_to replace_sub1_with_file &&
 611                (
 612                        cd submodule_update &&
 613                        git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
 614                        $command replace_file_with_sub1 &&
 615                        test_superproject_content origin/replace_file_with_sub1 &&
 616                        test_dir_is_empty sub1 &&
 617                        git submodule update --init --recursive &&
 618                        test_submodule_content sub1 origin/replace_file_with_sub1
 619                )
 620        '
 621        # ... as does removing a directory with tracked files with a
 622        # submodule.
 623        test_expect_success "$command: replace directory with submodule" '
 624                prolog &&
 625                reset_work_tree_to replace_sub1_with_directory &&
 626                (
 627                        cd submodule_update &&
 628                        git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
 629                        $command replace_directory_with_sub1 &&
 630                        test_superproject_content origin/replace_directory_with_sub1 &&
 631                        test_dir_is_empty sub1 &&
 632                        git submodule update --init --recursive &&
 633                        test_submodule_content sub1 origin/replace_directory_with_sub1
 634                )
 635        '
 636
 637        ######################## Disappearing submodule #######################
 638        # Removing a submodule doesn't remove its work tree ...
 639        test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
 640                prolog &&
 641                reset_work_tree_to add_sub1 &&
 642                (
 643                        cd submodule_update &&
 644                        git branch -t remove_sub1 origin/remove_sub1 &&
 645                        $command remove_sub1 &&
 646                        test_superproject_content origin/remove_sub1 &&
 647                        test_submodule_content sub1 origin/add_sub1
 648                )
 649        '
 650        # ... especially when it contains a .git directory.
 651        test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
 652                prolog &&
 653                reset_work_tree_to add_sub1 &&
 654                (
 655                        cd submodule_update &&
 656                        git branch -t remove_sub1 origin/remove_sub1 &&
 657                        replace_gitfile_with_git_dir sub1 &&
 658                        $command remove_sub1 &&
 659                        test_superproject_content origin/remove_sub1 &&
 660                        test_git_directory_is_unchanged sub1 &&
 661                        test_submodule_content sub1 origin/add_sub1
 662                )
 663        '
 664        # Replacing a submodule with files in a directory must fail as the
 665        # submodule work tree isn't removed ...
 666        test_expect_failure "$command: replace submodule with a directory must fail" '
 667                prolog &&
 668                reset_work_tree_to add_sub1 &&
 669                (
 670                        cd submodule_update &&
 671                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
 672                        test_must_fail $command replace_sub1_with_directory &&
 673                        test_superproject_content origin/add_sub1 &&
 674                        test_submodule_content sub1 origin/add_sub1
 675                )
 676        '
 677        # ... especially when it contains a .git directory.
 678        test_expect_failure "$command: replace submodule containing a .git directory with a directory must fail" '
 679                prolog &&
 680                reset_work_tree_to add_sub1 &&
 681                (
 682                        cd submodule_update &&
 683                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
 684                        replace_gitfile_with_git_dir sub1 &&
 685                        test_must_fail $command replace_sub1_with_directory &&
 686                        test_superproject_content origin/add_sub1 &&
 687                        test_git_directory_is_unchanged sub1 &&
 688                        test_submodule_content sub1 origin/add_sub1
 689                )
 690        '
 691        # Replacing it with a file must fail as it could throw away any local
 692        # work tree changes ...
 693        test_expect_failure "$command: replace submodule with a file must fail" '
 694                prolog &&
 695                reset_work_tree_to add_sub1 &&
 696                (
 697                        cd submodule_update &&
 698                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
 699                        test_must_fail $command replace_sub1_with_file &&
 700                        test_superproject_content origin/add_sub1 &&
 701                        test_submodule_content sub1 origin/add_sub1
 702                )
 703        '
 704        # ... or even destroy unpushed parts of submodule history if that
 705        # still uses a .git directory.
 706        test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
 707                prolog &&
 708                reset_work_tree_to add_sub1 &&
 709                (
 710                        cd submodule_update &&
 711                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
 712                        replace_gitfile_with_git_dir sub1 &&
 713                        test_must_fail $command replace_sub1_with_file &&
 714                        test_superproject_content origin/add_sub1 &&
 715                        test_git_directory_is_unchanged sub1 &&
 716                        test_submodule_content sub1 origin/add_sub1
 717                )
 718        '
 719
 720        ########################## Modified submodule #########################
 721        # Updating a submodule sha1 doesn't update the submodule's work tree
 722        test_expect_success "$command: modified submodule does not update submodule work tree" '
 723                prolog &&
 724                reset_work_tree_to add_sub1 &&
 725                (
 726                        cd submodule_update &&
 727                        git branch -t modify_sub1 origin/modify_sub1 &&
 728                        $command modify_sub1 &&
 729                        test_superproject_content origin/modify_sub1 &&
 730                        test_submodule_content sub1 origin/add_sub1 &&
 731                        git submodule update &&
 732                        test_submodule_content sub1 origin/modify_sub1
 733                )
 734        '
 735        # Updating a submodule to an invalid sha1 doesn't update the
 736        # submodule's work tree, subsequent update will fail
 737        test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
 738                prolog &&
 739                reset_work_tree_to add_sub1 &&
 740                (
 741                        cd submodule_update &&
 742                        git branch -t invalid_sub1 origin/invalid_sub1 &&
 743                        $command invalid_sub1 &&
 744                        test_superproject_content origin/invalid_sub1 &&
 745                        test_submodule_content sub1 origin/add_sub1 &&
 746                        test_must_fail git submodule update &&
 747                        test_submodule_content sub1 origin/add_sub1
 748                )
 749        '
 750        # Updating a submodule from an invalid sha1 doesn't update the
 751        # submodule's work tree, subsequent update will succeed
 752        test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
 753                prolog &&
 754                reset_work_tree_to invalid_sub1 &&
 755                (
 756                        cd submodule_update &&
 757                        git branch -t valid_sub1 origin/valid_sub1 &&
 758                        $command valid_sub1 &&
 759                        test_superproject_content origin/valid_sub1 &&
 760                        test_dir_is_empty sub1 &&
 761                        git submodule update --init --recursive &&
 762                        test_submodule_content sub1 origin/valid_sub1
 763                )
 764        '
 765}
 766
 767# Test that submodule contents are correctly updated when switching
 768# between commits that change a submodule.
 769# Test that the following transitions are correctly handled:
 770# (These tests are also above in the case where we expect no change
 771#  in the submodule)
 772# - Updated submodule
 773# - New submodule
 774# - Removed submodule
 775# - Directory containing tracked files replaced by submodule
 776# - Submodule replaced by tracked files in directory
 777# - Submodule replaced by tracked file with the same name
 778# - tracked file replaced by submodule
 779#
 780# New test cases
 781# - Removing a submodule with a git directory absorbs the submodules
 782#   git directory first into the superproject.
 783
 784test_submodule_switch_recursing_with_args () {
 785        cmd_args="$1"
 786        command="git $cmd_args --recurse-submodules"
 787        RESULTDS=success
 788        if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
 789        then
 790                RESULTDS=failure
 791        fi
 792        RESULTR=success
 793        if test "$KNOWN_FAILURE_SUBMODULE_RECURSIVE_NESTED" = 1
 794        then
 795                RESULTR=failure
 796        fi
 797        RESULTOI=success
 798        if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
 799        then
 800                RESULTOI=failure
 801        fi
 802        ######################### Appearing submodule #########################
 803        # Switching to a commit letting a submodule appear checks it out ...
 804        test_expect_success "$command: added submodule is checked out" '
 805                prolog &&
 806                reset_work_tree_to_interested no_submodule &&
 807                (
 808                        cd submodule_update &&
 809                        git branch -t add_sub1 origin/add_sub1 &&
 810                        $command add_sub1 &&
 811                        test_superproject_content origin/add_sub1 &&
 812                        test_submodule_content sub1 origin/add_sub1
 813                )
 814        '
 815        # ... ignoring an empty existing directory ...
 816        test_expect_success "$command: added submodule is checked out in empty dir" '
 817                prolog &&
 818                reset_work_tree_to_interested no_submodule &&
 819                (
 820                        cd submodule_update &&
 821                        mkdir sub1 &&
 822                        git branch -t add_sub1 origin/add_sub1 &&
 823                        $command add_sub1 &&
 824                        test_superproject_content origin/add_sub1 &&
 825                        test_submodule_content sub1 origin/add_sub1
 826                )
 827        '
 828        # ... unless there is an untracked file in its place.
 829        test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
 830                prolog &&
 831                reset_work_tree_to_interested no_submodule &&
 832                (
 833                        cd submodule_update &&
 834                        git branch -t add_sub1 origin/add_sub1 &&
 835                        : >sub1 &&
 836                        test_must_fail $command add_sub1 &&
 837                        test_superproject_content origin/no_submodule &&
 838                        test_must_be_empty sub1
 839                )
 840        '
 841        # ... but an ignored file is fine.
 842        test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
 843                test_when_finished "rm submodule_update/.git/info/exclude" &&
 844                prolog &&
 845                reset_work_tree_to_interested no_submodule &&
 846                (
 847                        cd submodule_update &&
 848                        git branch -t add_sub1 origin/add_sub1 &&
 849                        : >sub1 &&
 850                        echo sub1 >.git/info/exclude
 851                        $command add_sub1 &&
 852                        test_superproject_content origin/add_sub1 &&
 853                        test_submodule_content sub1 origin/add_sub1
 854                )
 855        '
 856        # Replacing a tracked file with a submodule produces a checked out submodule
 857        test_expect_success "$command: replace tracked file with submodule checks out submodule" '
 858                prolog &&
 859                reset_work_tree_to_interested replace_sub1_with_file &&
 860                (
 861                        cd submodule_update &&
 862                        git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
 863                        $command replace_file_with_sub1 &&
 864                        test_superproject_content origin/replace_file_with_sub1 &&
 865                        test_submodule_content sub1 origin/replace_file_with_sub1
 866                )
 867        '
 868        # ... as does removing a directory with tracked files with a submodule.
 869        test_expect_success "$command: replace directory with submodule" '
 870                prolog &&
 871                reset_work_tree_to_interested replace_sub1_with_directory &&
 872                (
 873                        cd submodule_update &&
 874                        git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
 875                        $command replace_directory_with_sub1 &&
 876                        test_superproject_content origin/replace_directory_with_sub1 &&
 877                        test_submodule_content sub1 origin/replace_directory_with_sub1
 878                )
 879        '
 880
 881        ######################## Disappearing submodule #######################
 882        # Removing a submodule removes its work tree ...
 883        test_expect_success "$command: removed submodule removes submodules working tree" '
 884                prolog &&
 885                reset_work_tree_to_interested add_sub1 &&
 886                (
 887                        cd submodule_update &&
 888                        git branch -t remove_sub1 origin/remove_sub1 &&
 889                        $command remove_sub1 &&
 890                        test_superproject_content origin/remove_sub1 &&
 891                        ! test -e sub1
 892                )
 893        '
 894        # ... absorbing a .git directory along the way.
 895        test_expect_success "$command: removed submodule absorbs submodules .git directory" '
 896                prolog &&
 897                reset_work_tree_to_interested add_sub1 &&
 898                (
 899                        cd submodule_update &&
 900                        git branch -t remove_sub1 origin/remove_sub1 &&
 901                        replace_gitfile_with_git_dir sub1 &&
 902                        rm -rf .git/modules &&
 903                        $command remove_sub1 &&
 904                        test_superproject_content origin/remove_sub1 &&
 905                        ! test -e sub1 &&
 906                        test_git_directory_exists sub1
 907                )
 908        '
 909        # Replacing a submodule with files in a directory must succeeds
 910        # when the submodule is clean
 911        test_expect_$RESULTDS "$command: replace submodule with a directory" '
 912                prolog &&
 913                reset_work_tree_to_interested add_sub1 &&
 914                (
 915                        cd submodule_update &&
 916                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
 917                        $command replace_sub1_with_directory &&
 918                        test_superproject_content origin/replace_sub1_with_directory &&
 919                        test_submodule_content sub1 origin/replace_sub1_with_directory
 920                )
 921        '
 922        # ... absorbing a .git directory.
 923        test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
 924                prolog &&
 925                reset_work_tree_to_interested add_sub1 &&
 926                (
 927                        cd submodule_update &&
 928                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
 929                        replace_gitfile_with_git_dir sub1 &&
 930                        rm -rf .git/modules &&
 931                        $command replace_sub1_with_directory &&
 932                        test_superproject_content origin/replace_sub1_with_directory &&
 933                        test_git_directory_exists sub1
 934                )
 935        '
 936
 937        # Replacing it with a file ...
 938        test_expect_success "$command: replace submodule with a file" '
 939                prolog &&
 940                reset_work_tree_to_interested add_sub1 &&
 941                (
 942                        cd submodule_update &&
 943                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
 944                        $command replace_sub1_with_file &&
 945                        test_superproject_content origin/replace_sub1_with_file &&
 946                        test -f sub1
 947                )
 948        '
 949
 950        # ... must check its local work tree for untracked files
 951        test_expect_$RESULTDS "$command: replace submodule with a file must fail with untracked files" '
 952                prolog &&
 953                reset_work_tree_to_interested add_sub1 &&
 954                (
 955                        cd submodule_update &&
 956                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
 957                        : >sub1/untrackedfile &&
 958                        test_must_fail $command replace_sub1_with_file &&
 959                        test_superproject_content origin/add_sub1 &&
 960                        test_submodule_content sub1 origin/add_sub1
 961                )
 962        '
 963
 964        # ... and ignored files are ignored
 965        test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
 966                test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
 967                prolog &&
 968                reset_work_tree_to_interested add_sub1 &&
 969                (
 970                        cd submodule_update &&
 971                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
 972                        : >sub1/ignored &&
 973                        $command replace_sub1_with_file &&
 974                        test_superproject_content origin/replace_sub1_with_file &&
 975                        test -f sub1
 976                )
 977        '
 978
 979        ########################## Modified submodule #########################
 980        # Updating a submodule sha1 updates the submodule's work tree
 981        test_expect_success "$command: modified submodule updates submodule work tree" '
 982                prolog &&
 983                reset_work_tree_to_interested add_sub1 &&
 984                (
 985                        cd submodule_update &&
 986                        git branch -t modify_sub1 origin/modify_sub1 &&
 987                        $command modify_sub1 &&
 988                        test_superproject_content origin/modify_sub1 &&
 989                        test_submodule_content sub1 origin/modify_sub1
 990                )
 991        '
 992
 993        test_expect_success "git -c submodule.recurse=true $cmd_args: modified submodule updates submodule work tree" '
 994                prolog &&
 995                reset_work_tree_to_interested add_sub1 &&
 996                (
 997                        cd submodule_update &&
 998                        git branch -t modify_sub1 origin/modify_sub1 &&
 999                        git -c submodule.recurse=true $cmd_args modify_sub1 &&
1000                        test_superproject_content origin/modify_sub1 &&
1001                        test_submodule_content sub1 origin/modify_sub1
1002                )
1003        '
1004
1005        # Updating a submodule to an invalid sha1 doesn't update the
1006        # superproject nor the submodule's work tree.
1007        test_expect_success "$command: updating to a missing submodule commit fails" '
1008                prolog &&
1009                reset_work_tree_to_interested add_sub1 &&
1010                (
1011                        cd submodule_update &&
1012                        git branch -t invalid_sub1 origin/invalid_sub1 &&
1013                        test_must_fail $command invalid_sub1 &&
1014                        test_superproject_content origin/add_sub1 &&
1015                        test_submodule_content sub1 origin/add_sub1
1016                )
1017        '
1018
1019        # recursing deeper than one level doesn't work yet.
1020        test_expect_$RESULTR "$command: modified submodule updates submodule recursively" '
1021                prolog &&
1022                reset_work_tree_to_interested add_nested_sub &&
1023                (
1024                        cd submodule_update &&
1025                        git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
1026                        $command modify_sub1_recursively &&
1027                        test_superproject_content origin/modify_sub1_recursively &&
1028                        test_submodule_content sub1 origin/modify_sub1_recursively &&
1029                        test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
1030                )
1031        '
1032}
1033
1034# Test that submodule contents are updated when switching between commits
1035# that change a submodule, but throwing away local changes in
1036# the superproject as well as the submodule is allowed.
1037test_submodule_forced_switch_recursing_with_args () {
1038        cmd_args="$1"
1039        command="git $cmd_args --recurse-submodules"
1040        RESULT=success
1041        if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
1042        then
1043                RESULT=failure
1044        fi
1045        ######################### Appearing submodule #########################
1046        # Switching to a commit letting a submodule appear creates empty dir ...
1047        test_expect_success "$command: added submodule is checked out" '
1048                prolog &&
1049                reset_work_tree_to_interested no_submodule &&
1050                (
1051                        cd submodule_update &&
1052                        git branch -t add_sub1 origin/add_sub1 &&
1053                        $command add_sub1 &&
1054                        test_superproject_content origin/add_sub1 &&
1055                        test_submodule_content sub1 origin/add_sub1
1056                )
1057        '
1058        # ... and doesn't care if it already exists ...
1059        test_expect_success "$command: added submodule ignores empty directory" '
1060                prolog &&
1061                reset_work_tree_to_interested no_submodule &&
1062                (
1063                        cd submodule_update &&
1064                        git branch -t add_sub1 origin/add_sub1 &&
1065                        mkdir sub1 &&
1066                        $command add_sub1 &&
1067                        test_superproject_content origin/add_sub1 &&
1068                        test_submodule_content sub1 origin/add_sub1
1069                )
1070        '
1071        # ... not caring about an untracked file either
1072        test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
1073                prolog &&
1074                reset_work_tree_to_interested no_submodule &&
1075                (
1076                        cd submodule_update &&
1077                        git branch -t add_sub1 origin/add_sub1 &&
1078                        >sub1 &&
1079                        $command add_sub1 &&
1080                        test_superproject_content origin/add_sub1 &&
1081                        test_submodule_content sub1 origin/add_sub1
1082                )
1083        '
1084        # Replacing a tracked file with a submodule checks out the submodule
1085        test_expect_success "$command: replace tracked file with submodule populates the submodule" '
1086                prolog &&
1087                reset_work_tree_to_interested replace_sub1_with_file &&
1088                (
1089                        cd submodule_update &&
1090                        git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
1091                        $command replace_file_with_sub1 &&
1092                        test_superproject_content origin/replace_file_with_sub1 &&
1093                        test_submodule_content sub1 origin/replace_file_with_sub1
1094                )
1095        '
1096        # ... as does removing a directory with tracked files with a
1097        # submodule.
1098        test_expect_success "$command: replace directory with submodule" '
1099                prolog &&
1100                reset_work_tree_to_interested replace_sub1_with_directory &&
1101                (
1102                        cd submodule_update &&
1103                        git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
1104                        $command replace_directory_with_sub1 &&
1105                        test_superproject_content origin/replace_directory_with_sub1 &&
1106                        test_submodule_content sub1 origin/replace_directory_with_sub1
1107                )
1108        '
1109
1110        ######################## Disappearing submodule #######################
1111        # Removing a submodule doesn't remove its work tree ...
1112        test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
1113                prolog &&
1114                reset_work_tree_to_interested add_sub1 &&
1115                (
1116                        cd submodule_update &&
1117                        git branch -t remove_sub1 origin/remove_sub1 &&
1118                        $command remove_sub1 &&
1119                        test_superproject_content origin/remove_sub1 &&
1120                        ! test -e sub1
1121                )
1122        '
1123        # ... especially when it contains a .git directory.
1124        test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
1125                prolog &&
1126                reset_work_tree_to_interested add_sub1 &&
1127                (
1128                        cd submodule_update &&
1129                        git branch -t remove_sub1 origin/remove_sub1 &&
1130                        replace_gitfile_with_git_dir sub1 &&
1131                        rm -rf .git/modules/sub1 &&
1132                        $command remove_sub1 &&
1133                        test_superproject_content origin/remove_sub1 &&
1134                        test_git_directory_exists sub1 &&
1135                        ! test -e sub1
1136                )
1137        '
1138        # Replacing a submodule with files in a directory ...
1139        test_expect_success "$command: replace submodule with a directory" '
1140                prolog &&
1141                reset_work_tree_to_interested add_sub1 &&
1142                (
1143                        cd submodule_update &&
1144                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1145                        $command replace_sub1_with_directory &&
1146                        test_superproject_content origin/replace_sub1_with_directory
1147                )
1148        '
1149        # ... absorbing a .git directory.
1150        test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
1151                prolog &&
1152                reset_work_tree_to_interested add_sub1 &&
1153                (
1154                        cd submodule_update &&
1155                        git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
1156                        replace_gitfile_with_git_dir sub1 &&
1157                        rm -rf .git/modules/sub1 &&
1158                        $command replace_sub1_with_directory &&
1159                        test_superproject_content origin/replace_sub1_with_directory &&
1160                        test_submodule_content sub1 origin/modify_sub1
1161                        test_git_directory_exists sub1
1162                )
1163        '
1164        # Replacing it with a file
1165        test_expect_success "$command: replace submodule with a file" '
1166                prolog &&
1167                reset_work_tree_to_interested add_sub1 &&
1168                (
1169                        cd submodule_update &&
1170                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1171                        $command replace_sub1_with_file &&
1172                        test_superproject_content origin/replace_sub1_with_file
1173                )
1174        '
1175
1176        # ... even if the submodule contains ignored files
1177        test_expect_success "$command: replace submodule with a file ignoring ignored files" '
1178                prolog &&
1179                reset_work_tree_to_interested add_sub1 &&
1180                (
1181                        cd submodule_update &&
1182                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1183                        : >sub1/expect &&
1184                        $command replace_sub1_with_file &&
1185                        test_superproject_content origin/replace_sub1_with_file
1186                )
1187        '
1188
1189        # ... but stops for untracked files that would be lost
1190        test_expect_$RESULT "$command: replace submodule with a file stops for untracked files" '
1191                prolog &&
1192                reset_work_tree_to_interested add_sub1 &&
1193                (
1194                        cd submodule_update &&
1195                        git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
1196                        : >sub1/untracked_file &&
1197                        test_must_fail $command replace_sub1_with_file &&
1198                        test_superproject_content origin/add_sub1 &&
1199                        test -f sub1/untracked_file
1200                )
1201        '
1202
1203        ########################## Modified submodule #########################
1204        # Updating a submodule sha1 updates the submodule's work tree
1205        test_expect_success "$command: modified submodule updates submodule work tree" '
1206                prolog &&
1207                reset_work_tree_to_interested add_sub1 &&
1208                (
1209                        cd submodule_update &&
1210                        git branch -t modify_sub1 origin/modify_sub1 &&
1211                        $command modify_sub1 &&
1212                        test_superproject_content origin/modify_sub1 &&
1213                        test_submodule_content sub1 origin/modify_sub1
1214                )
1215        '
1216        # Updating a submodule to an invalid sha1 doesn't update the
1217        # submodule's work tree, subsequent update will fail
1218        test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
1219                prolog &&
1220                reset_work_tree_to_interested add_sub1 &&
1221                (
1222                        cd submodule_update &&
1223                        git branch -t invalid_sub1 origin/invalid_sub1 &&
1224                        test_must_fail $command invalid_sub1 &&
1225                        test_superproject_content origin/add_sub1 &&
1226                        test_submodule_content sub1 origin/add_sub1
1227                )
1228        '
1229        # Updating a submodule from an invalid sha1 updates
1230        test_expect_success "$command: modified submodule does update submodule work tree from invalid commit" '
1231                prolog &&
1232                reset_work_tree_to_interested invalid_sub1 &&
1233                (
1234                        cd submodule_update &&
1235                        git branch -t valid_sub1 origin/valid_sub1 &&
1236                        $command valid_sub1 &&
1237                        test_superproject_content origin/valid_sub1 &&
1238                        test_submodule_content sub1 origin/valid_sub1
1239                )
1240        '
1241
1242        # Old versions of Git were buggy writing the .git link file
1243        # (e.g. before f8eaa0ba98b and then moving the superproject repo
1244        # whose submodules contained absolute paths)
1245        test_expect_success "$command: updating submodules fixes .git links" '
1246                prolog &&
1247                reset_work_tree_to_interested add_sub1 &&
1248                (
1249                        cd submodule_update &&
1250                        git branch -t modify_sub1 origin/modify_sub1 &&
1251                        echo "gitdir: bogus/path" >sub1/.git &&
1252                        $command modify_sub1 &&
1253                        test_superproject_content origin/modify_sub1 &&
1254                        test_submodule_content sub1 origin/modify_sub1
1255                )
1256        '
1257}