t / t7400-submodule-basic.shon commit git-submodule.sh: fix '/././' path normalization (8196e72)
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Lars Hjemli
   4#
   5
   6test_description='Basic porcelain support for submodules
   7
   8This test tries to verify basic sanity of the init, update and status
   9subcommands of git submodule.
  10'
  11
  12. ./test-lib.sh
  13
  14test_expect_success 'setup - initial commit' '
  15        >t &&
  16        git add t &&
  17        git commit -m "initial commit" &&
  18        git branch initial
  19'
  20
  21test_expect_success 'configuration parsing' '
  22        test_when_finished "rm -f .gitmodules" &&
  23        cat >.gitmodules <<-\EOF &&
  24        [submodule "s"]
  25                path
  26                ignore
  27        EOF
  28        test_must_fail git status
  29'
  30
  31test_expect_success 'setup - repository in init subdirectory' '
  32        mkdir init &&
  33        (
  34                cd init &&
  35                git init &&
  36                echo a >a &&
  37                git add a &&
  38                git commit -m "submodule commit 1" &&
  39                git tag -a -m "rev-1" rev-1
  40        )
  41'
  42
  43test_expect_success 'setup - commit with gitlink' '
  44        echo a >a &&
  45        echo z >z &&
  46        git add a init z &&
  47        git commit -m "super commit 1"
  48'
  49
  50test_expect_success 'setup - hide init subdirectory' '
  51        mv init .subrepo
  52'
  53
  54test_expect_success 'setup - repository to add submodules to' '
  55        git init addtest &&
  56        git init addtest-ignore
  57'
  58
  59# The 'submodule add' tests need some repository to add as a submodule.
  60# The trash directory is a good one as any. We need to canonicalize
  61# the name, though, as some tests compare it to the absolute path git
  62# generates, which will expand symbolic links.
  63submodurl=$(pwd -P)
  64
  65listbranches() {
  66        git for-each-ref --format='%(refname)' 'refs/heads/*'
  67}
  68
  69inspect() {
  70        dir=$1 &&
  71        dotdot="${2:-..}" &&
  72
  73        (
  74                cd "$dir" &&
  75                listbranches >"$dotdot/heads" &&
  76                { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
  77                git rev-parse HEAD >"$dotdot/head-sha1" &&
  78                git update-index --refresh &&
  79                git diff-files --exit-code &&
  80                git clean -n -d -x >"$dotdot/untracked"
  81        )
  82}
  83
  84test_expect_success 'submodule add' '
  85        echo "refs/heads/master" >expect &&
  86        >empty &&
  87
  88        (
  89                cd addtest &&
  90                git submodule add -q "$submodurl" submod >actual &&
  91                test_must_be_empty actual &&
  92                echo "gitdir: ../.git/modules/submod" >expect &&
  93                test_cmp expect submod/.git &&
  94                (
  95                        cd submod &&
  96                        git config core.worktree >actual &&
  97                        echo "../../../submod" >expect &&
  98                        test_cmp expect actual &&
  99                        rm -f actual expect
 100                ) &&
 101                git submodule init
 102        ) &&
 103
 104        rm -f heads head untracked &&
 105        inspect addtest/submod ../.. &&
 106        test_cmp expect heads &&
 107        test_cmp expect head &&
 108        test_cmp empty untracked
 109'
 110
 111test_expect_success 'submodule add to .gitignored path fails' '
 112        (
 113                cd addtest-ignore &&
 114                cat <<-\EOF >expect &&
 115                The following path is ignored by one of your .gitignore files:
 116                submod
 117                Use -f if you really want to add it.
 118                EOF
 119                # Does not use test_commit due to the ignore
 120                echo "*" > .gitignore &&
 121                git add --force .gitignore &&
 122                git commit -m"Ignore everything" &&
 123                ! git submodule add "$submodurl" submod >actual 2>&1 &&
 124                test_i18ncmp expect actual
 125        )
 126'
 127
 128test_expect_success 'submodule add to .gitignored path with --force' '
 129        (
 130                cd addtest-ignore &&
 131                git submodule add --force "$submodurl" submod
 132        )
 133'
 134
 135test_expect_success 'submodule add --branch' '
 136        echo "refs/heads/initial" >expect-head &&
 137        cat <<-\EOF >expect-heads &&
 138        refs/heads/initial
 139        refs/heads/master
 140        EOF
 141        >empty &&
 142
 143        (
 144                cd addtest &&
 145                git submodule add -b initial "$submodurl" submod-branch &&
 146                test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
 147                git submodule init
 148        ) &&
 149
 150        rm -f heads head untracked &&
 151        inspect addtest/submod-branch ../.. &&
 152        test_cmp expect-heads heads &&
 153        test_cmp expect-head head &&
 154        test_cmp empty untracked
 155'
 156
 157test_expect_success 'submodule add with ./ in path' '
 158        echo "refs/heads/master" >expect &&
 159        >empty &&
 160
 161        (
 162                cd addtest &&
 163                git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
 164                git submodule init
 165        ) &&
 166
 167        rm -f heads head untracked &&
 168        inspect addtest/dotsubmod/frotz ../../.. &&
 169        test_cmp expect heads &&
 170        test_cmp expect head &&
 171        test_cmp empty untracked
 172'
 173
 174test_expect_success 'submodule add with /././ in path' '
 175        echo "refs/heads/master" >expect &&
 176        >empty &&
 177
 178        (
 179                cd addtest &&
 180                git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
 181                git submodule init
 182        ) &&
 183
 184        rm -f heads head untracked &&
 185        inspect addtest/dotslashdotsubmod/frotz ../../.. &&
 186        test_cmp expect heads &&
 187        test_cmp expect head &&
 188        test_cmp empty untracked
 189'
 190
 191test_expect_success 'submodule add with // in path' '
 192        echo "refs/heads/master" >expect &&
 193        >empty &&
 194
 195        (
 196                cd addtest &&
 197                git submodule add "$submodurl" slashslashsubmod///frotz// &&
 198                git submodule init
 199        ) &&
 200
 201        rm -f heads head untracked &&
 202        inspect addtest/slashslashsubmod/frotz ../../.. &&
 203        test_cmp expect heads &&
 204        test_cmp expect head &&
 205        test_cmp empty untracked
 206'
 207
 208test_expect_success 'submodule add with /.. in path' '
 209        echo "refs/heads/master" >expect &&
 210        >empty &&
 211
 212        (
 213                cd addtest &&
 214                git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
 215                git submodule init
 216        ) &&
 217
 218        rm -f heads head untracked &&
 219        inspect addtest/realsubmod ../.. &&
 220        test_cmp expect heads &&
 221        test_cmp expect head &&
 222        test_cmp empty untracked
 223'
 224
 225test_expect_success 'submodule add with ./, /.. and // in path' '
 226        echo "refs/heads/master" >expect &&
 227        >empty &&
 228
 229        (
 230                cd addtest &&
 231                git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
 232                git submodule init
 233        ) &&
 234
 235        rm -f heads head untracked &&
 236        inspect addtest/realsubmod2 ../.. &&
 237        test_cmp expect heads &&
 238        test_cmp expect head &&
 239        test_cmp empty untracked
 240'
 241
 242test_expect_success 'submodule add in subdirectory' '
 243        echo "refs/heads/master" >expect &&
 244        >empty &&
 245
 246        mkdir addtest/sub &&
 247        (
 248                cd addtest/sub &&
 249                git submodule add "$submodurl" ../realsubmod3 &&
 250                git submodule init
 251        ) &&
 252
 253        rm -f heads head untracked &&
 254        inspect addtest/realsubmod3 ../.. &&
 255        test_cmp expect heads &&
 256        test_cmp expect head &&
 257        test_cmp empty untracked
 258'
 259
 260test_expect_success 'submodule add in subdirectory with relative path should fail' '
 261        (
 262                cd addtest/sub &&
 263                test_must_fail git submodule add ../../ submod3 2>../../output.err
 264        ) &&
 265        test_i18ngrep toplevel output.err
 266'
 267
 268test_expect_success 'setup - add an example entry to .gitmodules' '
 269        GIT_CONFIG=.gitmodules \
 270        git config submodule.example.url git://example.com/init.git
 271'
 272
 273test_expect_success 'status should fail for unmapped paths' '
 274        test_must_fail git submodule status
 275'
 276
 277test_expect_success 'setup - map path in .gitmodules' '
 278        cat <<\EOF >expect &&
 279[submodule "example"]
 280        url = git://example.com/init.git
 281        path = init
 282EOF
 283
 284        GIT_CONFIG=.gitmodules git config submodule.example.path init &&
 285
 286        test_cmp expect .gitmodules
 287'
 288
 289test_expect_success 'status should only print one line' '
 290        git submodule status >lines &&
 291        test_line_count = 1 lines
 292'
 293
 294test_expect_success 'setup - fetch commit name from submodule' '
 295        rev1=$(cd .subrepo && git rev-parse HEAD) &&
 296        printf "rev1: %s\n" "$rev1" &&
 297        test -n "$rev1"
 298'
 299
 300test_expect_success 'status should initially be "missing"' '
 301        git submodule status >lines &&
 302        grep "^-$rev1" lines
 303'
 304
 305test_expect_success 'init should register submodule url in .git/config' '
 306        echo git://example.com/init.git >expect &&
 307
 308        git submodule init &&
 309        git config submodule.example.url >url &&
 310        git config submodule.example.url ./.subrepo &&
 311
 312        test_cmp expect url
 313'
 314
 315test_failure_with_unknown_submodule () {
 316        test_must_fail git submodule $1 no-such-submodule 2>output.err &&
 317        grep "^error: .*no-such-submodule" output.err
 318}
 319
 320test_expect_success 'init should fail with unknown submodule' '
 321        test_failure_with_unknown_submodule init
 322'
 323
 324test_expect_success 'update should fail with unknown submodule' '
 325        test_failure_with_unknown_submodule update
 326'
 327
 328test_expect_success 'status should fail with unknown submodule' '
 329        test_failure_with_unknown_submodule status
 330'
 331
 332test_expect_success 'sync should fail with unknown submodule' '
 333        test_failure_with_unknown_submodule sync
 334'
 335
 336test_expect_success 'update should fail when path is used by a file' '
 337        echo hello >expect &&
 338
 339        echo "hello" >init &&
 340        test_must_fail git submodule update &&
 341
 342        test_cmp expect init
 343'
 344
 345test_expect_success 'update should fail when path is used by a nonempty directory' '
 346        echo hello >expect &&
 347
 348        rm -fr init &&
 349        mkdir init &&
 350        echo "hello" >init/a &&
 351
 352        test_must_fail git submodule update &&
 353
 354        test_cmp expect init/a
 355'
 356
 357test_expect_success 'update should work when path is an empty dir' '
 358        rm -fr init &&
 359        rm -f head-sha1 &&
 360        echo "$rev1" >expect &&
 361
 362        mkdir init &&
 363        git submodule update -q >update.out &&
 364        test_must_be_empty update.out &&
 365
 366        inspect init &&
 367        test_cmp expect head-sha1
 368'
 369
 370test_expect_success 'status should be "up-to-date" after update' '
 371        git submodule status >list &&
 372        grep "^ $rev1" list
 373'
 374
 375test_expect_success 'status "up-to-date" from subdirectory' '
 376        mkdir -p sub &&
 377        (
 378                cd sub &&
 379                git submodule status >../list
 380        ) &&
 381        grep "^ $rev1" list &&
 382        grep "\\.\\./init" list
 383'
 384
 385test_expect_success 'status "up-to-date" from subdirectory with path' '
 386        mkdir -p sub &&
 387        (
 388                cd sub &&
 389                git submodule status ../init >../list
 390        ) &&
 391        grep "^ $rev1" list &&
 392        grep "\\.\\./init" list
 393'
 394
 395test_expect_success 'status should be "modified" after submodule commit' '
 396        (
 397                cd init &&
 398                echo b >b &&
 399                git add b &&
 400                git commit -m "submodule commit 2"
 401        ) &&
 402
 403        rev2=$(cd init && git rev-parse HEAD) &&
 404        test -n "$rev2" &&
 405        git submodule status >list &&
 406
 407        grep "^+$rev2" list
 408'
 409
 410test_expect_success 'the --cached sha1 should be rev1' '
 411        git submodule --cached status >list &&
 412        grep "^+$rev1" list
 413'
 414
 415test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
 416        git diff >diff &&
 417        grep "^+Subproject commit $rev2" diff
 418'
 419
 420test_expect_success 'update should checkout rev1' '
 421        rm -f head-sha1 &&
 422        echo "$rev1" >expect &&
 423
 424        git submodule update init &&
 425        inspect init &&
 426
 427        test_cmp expect head-sha1
 428'
 429
 430test_expect_success 'status should be "up-to-date" after update' '
 431        git submodule status >list &&
 432        grep "^ $rev1" list
 433'
 434
 435test_expect_success 'checkout superproject with subproject already present' '
 436        git checkout initial &&
 437        git checkout master
 438'
 439
 440test_expect_success 'apply submodule diff' '
 441        >empty &&
 442
 443        git branch second &&
 444        (
 445                cd init &&
 446                echo s >s &&
 447                git add s &&
 448                git commit -m "change subproject"
 449        ) &&
 450        git update-index --add init &&
 451        git commit -m "change init" &&
 452        git format-patch -1 --stdout >P.diff &&
 453        git checkout second &&
 454        git apply --index P.diff &&
 455
 456        git diff --cached master >staged &&
 457        test_cmp empty staged
 458'
 459
 460test_expect_success 'update --init' '
 461        mv init init2 &&
 462        git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
 463        git config --remove-section submodule.example &&
 464        test_must_fail git config submodule.example.url &&
 465
 466        git submodule update init > update.out &&
 467        cat update.out &&
 468        test_i18ngrep "not initialized" update.out &&
 469        test_must_fail git rev-parse --resolve-git-dir init/.git &&
 470
 471        git submodule update --init init &&
 472        git rev-parse --resolve-git-dir init/.git
 473'
 474
 475test_expect_success 'update --init from subdirectory' '
 476        mv init init2 &&
 477        git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
 478        git config --remove-section submodule.example &&
 479        test_must_fail git config submodule.example.url &&
 480
 481        mkdir -p sub &&
 482        (
 483                cd sub &&
 484                git submodule update ../init >update.out &&
 485                cat update.out &&
 486                test_i18ngrep "not initialized" update.out &&
 487                test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
 488
 489                git submodule update --init ../init
 490        ) &&
 491        git rev-parse --resolve-git-dir init/.git
 492'
 493
 494test_expect_success 'do not add files from a submodule' '
 495
 496        git reset --hard &&
 497        test_must_fail git add init/a
 498
 499'
 500
 501test_expect_success 'gracefully add/reset submodule with a trailing slash' '
 502
 503        git reset --hard &&
 504        git commit -m "commit subproject" init &&
 505        (cd init &&
 506         echo b > a) &&
 507        git add init/ &&
 508        git diff --exit-code --cached init &&
 509        commit=$(cd init &&
 510         git commit -m update a >/dev/null &&
 511         git rev-parse HEAD) &&
 512        git add init/ &&
 513        test_must_fail git diff --exit-code --cached init &&
 514        test $commit = $(git ls-files --stage |
 515                sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
 516        git reset init/ &&
 517        git diff --exit-code --cached init
 518
 519'
 520
 521test_expect_success 'ls-files gracefully handles trailing slash' '
 522
 523        test "init" = "$(git ls-files init/)"
 524
 525'
 526
 527test_expect_success 'moving to a commit without submodule does not leave empty dir' '
 528        rm -rf init &&
 529        mkdir init &&
 530        git reset --hard &&
 531        git checkout initial &&
 532        test ! -d init &&
 533        git checkout second
 534'
 535
 536test_expect_success 'submodule <invalid-subcommand> fails' '
 537        test_must_fail git submodule no-such-subcommand
 538'
 539
 540test_expect_success 'add submodules without specifying an explicit path' '
 541        mkdir repo &&
 542        (
 543                cd repo &&
 544                git init &&
 545                echo r >r &&
 546                git add r &&
 547                git commit -m "repo commit 1"
 548        ) &&
 549        git clone --bare repo/ bare.git &&
 550        (
 551                cd addtest &&
 552                git submodule add "$submodurl/repo" &&
 553                git config -f .gitmodules submodule.repo.path repo &&
 554                git submodule add "$submodurl/bare.git" &&
 555                git config -f .gitmodules submodule.bare.path bare
 556        )
 557'
 558
 559test_expect_success 'add should fail when path is used by a file' '
 560        (
 561                cd addtest &&
 562                touch file &&
 563                test_must_fail  git submodule add "$submodurl/repo" file
 564        )
 565'
 566
 567test_expect_success 'add should fail when path is used by an existing directory' '
 568        (
 569                cd addtest &&
 570                mkdir empty-dir &&
 571                test_must_fail git submodule add "$submodurl/repo" empty-dir
 572        )
 573'
 574
 575test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
 576        (
 577                cd addtest &&
 578                git submodule add ../repo relative &&
 579                test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
 580                git submodule sync relative &&
 581                test "$(git config submodule.relative.url)" = "$submodurl/repo"
 582        )
 583'
 584
 585test_expect_success 'set up for relative path tests' '
 586        mkdir reltest &&
 587        (
 588                cd reltest &&
 589                git init &&
 590                mkdir sub &&
 591                (
 592                        cd sub &&
 593                        git init &&
 594                        test_commit foo
 595                ) &&
 596                git add sub &&
 597                git config -f .gitmodules submodule.sub.path sub &&
 598                git config -f .gitmodules submodule.sub.url ../subrepo &&
 599                cp .git/config pristine-.git-config &&
 600                cp .gitmodules pristine-.gitmodules
 601        )
 602'
 603
 604test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
 605        (
 606                cd reltest &&
 607                cp pristine-.git-config .git/config &&
 608                cp pristine-.gitmodules .gitmodules &&
 609                git config remote.origin.url ssh://hostname/repo &&
 610                git submodule init &&
 611                test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
 612        )
 613'
 614
 615test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
 616        (
 617                cd reltest &&
 618                cp pristine-.git-config .git/config &&
 619                cp pristine-.gitmodules .gitmodules &&
 620                git config remote.origin.url ssh://hostname:22/repo &&
 621                git submodule init &&
 622                test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
 623        )
 624'
 625
 626# About the choice of the path in the next test:
 627# - double-slash side-steps path mangling issues on Windows
 628# - it is still an absolute local path
 629# - there cannot be a server with a blank in its name just in case the
 630#   path is used erroneously to access a //server/share style path
 631test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
 632        (
 633                cd reltest &&
 634                cp pristine-.git-config .git/config &&
 635                cp pristine-.gitmodules .gitmodules &&
 636                git config remote.origin.url "//somewhere else/repo" &&
 637                git submodule init &&
 638                test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
 639        )
 640'
 641
 642test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
 643        (
 644                cd reltest &&
 645                cp pristine-.git-config .git/config &&
 646                cp pristine-.gitmodules .gitmodules &&
 647                git config remote.origin.url file:///tmp/repo &&
 648                git submodule init &&
 649                test "$(git config submodule.sub.url)" = file:///tmp/subrepo
 650        )
 651'
 652
 653test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
 654        (
 655                cd reltest &&
 656                cp pristine-.git-config .git/config &&
 657                cp pristine-.gitmodules .gitmodules &&
 658                git config remote.origin.url helper:://hostname/repo &&
 659                git submodule init &&
 660                test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
 661        )
 662'
 663
 664test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
 665        (
 666                cd reltest &&
 667                cp pristine-.git-config .git/config &&
 668                git config remote.origin.url user@host:repo &&
 669                git submodule init &&
 670                test "$(git config submodule.sub.url)" = user@host:subrepo
 671        )
 672'
 673
 674test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
 675        (
 676                cd reltest &&
 677                cp pristine-.git-config .git/config &&
 678                cp pristine-.gitmodules .gitmodules &&
 679                git config remote.origin.url user@host:path/to/repo &&
 680                git submodule init &&
 681                test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
 682        )
 683'
 684
 685test_expect_success '../subrepo works with relative local path - foo' '
 686        (
 687                cd reltest &&
 688                cp pristine-.git-config .git/config &&
 689                cp pristine-.gitmodules .gitmodules &&
 690                git config remote.origin.url foo &&
 691                # actual: fails with an error
 692                git submodule init &&
 693                test "$(git config submodule.sub.url)" = subrepo
 694        )
 695'
 696
 697test_expect_success '../subrepo works with relative local path - foo/bar' '
 698        (
 699                cd reltest &&
 700                cp pristine-.git-config .git/config &&
 701                cp pristine-.gitmodules .gitmodules &&
 702                git config remote.origin.url foo/bar &&
 703                git submodule init &&
 704                test "$(git config submodule.sub.url)" = foo/subrepo
 705        )
 706'
 707
 708test_expect_success '../subrepo works with relative local path - ./foo' '
 709        (
 710                cd reltest &&
 711                cp pristine-.git-config .git/config &&
 712                cp pristine-.gitmodules .gitmodules &&
 713                git config remote.origin.url ./foo &&
 714                git submodule init &&
 715                test "$(git config submodule.sub.url)" = subrepo
 716        )
 717'
 718
 719test_expect_success '../subrepo works with relative local path - ./foo/bar' '
 720        (
 721                cd reltest &&
 722                cp pristine-.git-config .git/config &&
 723                cp pristine-.gitmodules .gitmodules &&
 724                git config remote.origin.url ./foo/bar &&
 725                git submodule init &&
 726                test "$(git config submodule.sub.url)" = foo/subrepo
 727        )
 728'
 729
 730test_expect_success '../subrepo works with relative local path - ../foo' '
 731        (
 732                cd reltest &&
 733                cp pristine-.git-config .git/config &&
 734                cp pristine-.gitmodules .gitmodules &&
 735                git config remote.origin.url ../foo &&
 736                git submodule init &&
 737                test "$(git config submodule.sub.url)" = ../subrepo
 738        )
 739'
 740
 741test_expect_success '../subrepo works with relative local path - ../foo/bar' '
 742        (
 743                cd reltest &&
 744                cp pristine-.git-config .git/config &&
 745                cp pristine-.gitmodules .gitmodules &&
 746                git config remote.origin.url ../foo/bar &&
 747                git submodule init &&
 748                test "$(git config submodule.sub.url)" = ../foo/subrepo
 749        )
 750'
 751
 752test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
 753        (
 754                cd reltest &&
 755                cp pristine-.git-config .git/config &&
 756                cp pristine-.gitmodules .gitmodules &&
 757                mkdir -p a/b/c &&
 758                (cd a/b/c; git init) &&
 759                git config remote.origin.url ../foo/bar.git &&
 760                git submodule add ../bar/a/b/c ./a/b/c &&
 761                git submodule init &&
 762                test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
 763        )
 764'
 765
 766test_expect_success 'moving the superproject does not break submodules' '
 767        (
 768                cd addtest &&
 769                git submodule status >expect
 770        )
 771        mv addtest addtest2 &&
 772        (
 773                cd addtest2 &&
 774                git submodule status >actual &&
 775                test_cmp expect actual
 776        )
 777'
 778
 779test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
 780        (
 781                cd addtest2 &&
 782                (
 783                        cd repo &&
 784                        echo "$submodurl/repo" >expect &&
 785                        git config remote.origin.url >actual &&
 786                        test_cmp expect actual &&
 787                        echo "gitdir: ../.git/modules/repo" >expect &&
 788                        test_cmp expect .git
 789                ) &&
 790                rm -rf repo &&
 791                git rm repo &&
 792                git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
 793                test_must_be_empty actual &&
 794                echo "gitdir: ../.git/modules/submod" >expect &&
 795                test_cmp expect submod/.git &&
 796                (
 797                        cd repo &&
 798                        echo "$submodurl/bare.git" >expect &&
 799                        git config remote.origin.url >actual &&
 800                        test_cmp expect actual &&
 801                        echo "gitdir: ../.git/modules/repo_new" >expect &&
 802                        test_cmp expect .git
 803                ) &&
 804                echo "repo" >expect &&
 805                test_must_fail git config -f .gitmodules submodule.repo.path &&
 806                git config -f .gitmodules submodule.repo_new.path >actual &&
 807                test_cmp expect actual&&
 808                echo "$submodurl/repo" >expect &&
 809                test_must_fail git config -f .gitmodules submodule.repo.url &&
 810                echo "$submodurl/bare.git" >expect &&
 811                git config -f .gitmodules submodule.repo_new.url >actual &&
 812                test_cmp expect actual &&
 813                echo "$submodurl/repo" >expect &&
 814                git config submodule.repo.url >actual &&
 815                test_cmp expect actual &&
 816                echo "$submodurl/bare.git" >expect &&
 817                git config submodule.repo_new.url >actual &&
 818                test_cmp expect actual
 819        )
 820'
 821
 822test_expect_success 'submodule add with an existing name fails unless forced' '
 823        (
 824                cd addtest2 &&
 825                rm -rf repo &&
 826                git rm repo &&
 827                test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
 828                test ! -d repo &&
 829                test_must_fail git config -f .gitmodules submodule.repo_new.path &&
 830                test_must_fail git config -f .gitmodules submodule.repo_new.url &&
 831                echo "$submodurl/bare.git" >expect &&
 832                git config submodule.repo_new.url >actual &&
 833                test_cmp expect actual &&
 834                git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
 835                test -d repo &&
 836                echo "repo" >expect &&
 837                git config -f .gitmodules submodule.repo_new.path >actual &&
 838                test_cmp expect actual&&
 839                echo "$submodurl/repo.git" >expect &&
 840                git config -f .gitmodules submodule.repo_new.url >actual &&
 841                test_cmp expect actual &&
 842                echo "$submodurl/repo.git" >expect &&
 843                git config submodule.repo_new.url >actual &&
 844                test_cmp expect actual
 845        )
 846'
 847
 848test_expect_success 'set up a second submodule' '
 849        git submodule add ./init2 example2 &&
 850        git commit -m "submodule example2 added"
 851'
 852
 853test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
 854        git config submodule.example.foo bar &&
 855        git config submodule.example2.frotz nitfol &&
 856        git submodule deinit init &&
 857        test -z "$(git config --get-regexp "submodule\.example\.")" &&
 858        test -n "$(git config --get-regexp "submodule\.example2\.")" &&
 859        test -f example2/.git &&
 860        rmdir init
 861'
 862
 863test_expect_success 'submodule deinit from subdirectory' '
 864        git submodule update --init &&
 865        git config submodule.example.foo bar &&
 866        mkdir -p sub &&
 867        (
 868                cd sub &&
 869                git submodule deinit ../init >../output
 870        ) &&
 871        grep "\\.\\./init" output &&
 872        test -z "$(git config --get-regexp "submodule\.example\.")" &&
 873        test -n "$(git config --get-regexp "submodule\.example2\.")" &&
 874        test -f example2/.git &&
 875        rmdir init
 876'
 877
 878test_expect_success 'submodule deinit . deinits all initialized submodules' '
 879        git submodule update --init &&
 880        git config submodule.example.foo bar &&
 881        git config submodule.example2.frotz nitfol &&
 882        test_must_fail git submodule deinit &&
 883        git submodule deinit . >actual &&
 884        test -z "$(git config --get-regexp "submodule\.example\.")" &&
 885        test -z "$(git config --get-regexp "submodule\.example2\.")" &&
 886        test_i18ngrep "Cleared directory .init" actual &&
 887        test_i18ngrep "Cleared directory .example2" actual &&
 888        rmdir init example2
 889'
 890
 891test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
 892        git submodule update --init &&
 893        rm -rf init example2/* example2/.git &&
 894        git submodule deinit init example2 >actual &&
 895        test -z "$(git config --get-regexp "submodule\.example\.")" &&
 896        test -z "$(git config --get-regexp "submodule\.example2\.")" &&
 897        test_i18ngrep ! "Cleared directory .init" actual &&
 898        test_i18ngrep "Cleared directory .example2" actual &&
 899        rmdir init
 900'
 901
 902test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
 903        git submodule update --init &&
 904        echo X >>init/s &&
 905        test_must_fail git submodule deinit init &&
 906        test -n "$(git config --get-regexp "submodule\.example\.")" &&
 907        test -f example2/.git &&
 908        git submodule deinit -f init >actual &&
 909        test -z "$(git config --get-regexp "submodule\.example\.")" &&
 910        test_i18ngrep "Cleared directory .init" actual &&
 911        rmdir init
 912'
 913
 914test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
 915        git submodule update --init &&
 916        echo X >>init/untracked &&
 917        test_must_fail git submodule deinit init &&
 918        test -n "$(git config --get-regexp "submodule\.example\.")" &&
 919        test -f example2/.git &&
 920        git submodule deinit -f init >actual &&
 921        test -z "$(git config --get-regexp "submodule\.example\.")" &&
 922        test_i18ngrep "Cleared directory .init" actual &&
 923        rmdir init
 924'
 925
 926test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
 927        git submodule update --init &&
 928        (
 929                cd init &&
 930                git checkout HEAD^
 931        ) &&
 932        test_must_fail git submodule deinit init &&
 933        test -n "$(git config --get-regexp "submodule\.example\.")" &&
 934        test -f example2/.git &&
 935        git submodule deinit -f init >actual &&
 936        test -z "$(git config --get-regexp "submodule\.example\.")" &&
 937        test_i18ngrep "Cleared directory .init" actual &&
 938        rmdir init
 939'
 940
 941test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
 942        git submodule update --init &&
 943        git submodule deinit init >actual &&
 944        test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
 945        test_i18ngrep "Cleared directory .init" actual &&
 946        git submodule deinit init >actual &&
 947        test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
 948        test_i18ngrep "Cleared directory .init" actual &&
 949        git submodule deinit . >actual &&
 950        test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
 951        test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
 952        test_i18ngrep "Cleared directory .init" actual &&
 953        git submodule deinit . >actual &&
 954        test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
 955        test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
 956        test_i18ngrep "Cleared directory .init" actual &&
 957        rmdir init example2
 958'
 959
 960test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
 961        git submodule update --init &&
 962        (
 963                cd init &&
 964                rm .git &&
 965                cp -R ../.git/modules/example .git &&
 966                GIT_WORK_TREE=. git config --unset core.worktree
 967        ) &&
 968        test_must_fail git submodule deinit init &&
 969        test_must_fail git submodule deinit -f init &&
 970        test -d init/.git &&
 971        test -n "$(git config --get-regexp "submodule\.example\.")"
 972'
 973
 974test_expect_success 'submodule with UTF-8 name' '
 975        svname=$(printf "\303\245 \303\244\303\266") &&
 976        mkdir "$svname" &&
 977        (
 978                cd "$svname" &&
 979                git init &&
 980                >sub &&
 981                git add sub &&
 982                git commit -m "init sub"
 983        ) &&
 984        git submodule add ./"$svname" &&
 985        git submodule >&2 &&
 986        test -n "$(git submodule | grep "$svname")"
 987'
 988
 989test_expect_success 'submodule add clone shallow submodule' '
 990        mkdir super &&
 991        pwd=$(pwd)
 992        (
 993                cd super &&
 994                git init &&
 995                git submodule add --depth=1 file://"$pwd"/example2 submodule &&
 996                (
 997                        cd submodule &&
 998                        test 1 = $(git log --oneline | wc -l)
 999                )
1000        )
1001'
1002
1003
1004test_done