t / t7400-submodule-basic.shon commit t6000-t6999: fix broken &&-chains (c8ce376)
   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 'submodule deinit works on empty repository' '
  15        git submodule deinit --all
  16'
  17
  18test_expect_success 'setup - initial commit' '
  19        >t &&
  20        git add t &&
  21        git commit -m "initial commit" &&
  22        git branch initial
  23'
  24
  25test_expect_success 'submodule init aborts on missing .gitmodules file' '
  26        test_when_finished "git update-index --remove sub" &&
  27        git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
  28        # missing the .gitmodules file here
  29        test_must_fail git submodule init 2>actual &&
  30        test_i18ngrep "No url found for submodule path" actual
  31'
  32
  33test_expect_success 'submodule update aborts on missing .gitmodules file' '
  34        test_when_finished "git update-index --remove sub" &&
  35        git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
  36        # missing the .gitmodules file here
  37        git submodule update sub 2>actual &&
  38        test_i18ngrep "Submodule path .sub. not initialized" actual
  39'
  40
  41test_expect_success 'submodule update aborts on missing gitmodules url' '
  42        test_when_finished "git update-index --remove sub" &&
  43        git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
  44        test_when_finished "rm -f .gitmodules" &&
  45        git config -f .gitmodules submodule.s.path sub &&
  46        test_must_fail git submodule init
  47'
  48
  49test_expect_success 'setup - repository in init subdirectory' '
  50        mkdir init &&
  51        (
  52                cd init &&
  53                git init &&
  54                echo a >a &&
  55                git add a &&
  56                git commit -m "submodule commit 1" &&
  57                git tag -a -m "rev-1" rev-1
  58        )
  59'
  60
  61test_expect_success 'setup - commit with gitlink' '
  62        echo a >a &&
  63        echo z >z &&
  64        git add a init z &&
  65        git commit -m "super commit 1"
  66'
  67
  68test_expect_success 'setup - hide init subdirectory' '
  69        mv init .subrepo
  70'
  71
  72test_expect_success 'setup - repository to add submodules to' '
  73        git init addtest &&
  74        git init addtest-ignore
  75'
  76
  77# The 'submodule add' tests need some repository to add as a submodule.
  78# The trash directory is a good one as any. We need to canonicalize
  79# the name, though, as some tests compare it to the absolute path git
  80# generates, which will expand symbolic links.
  81submodurl=$(pwd -P)
  82
  83listbranches() {
  84        git for-each-ref --format='%(refname)' 'refs/heads/*'
  85}
  86
  87inspect() {
  88        dir=$1 &&
  89        dotdot="${2:-..}" &&
  90
  91        (
  92                cd "$dir" &&
  93                listbranches >"$dotdot/heads" &&
  94                { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
  95                git rev-parse HEAD >"$dotdot/head-sha1" &&
  96                git update-index --refresh &&
  97                git diff-files --exit-code &&
  98                git clean -n -d -x >"$dotdot/untracked"
  99        )
 100}
 101
 102test_expect_success 'submodule add' '
 103        echo "refs/heads/master" >expect &&
 104        >empty &&
 105
 106        (
 107                cd addtest &&
 108                git submodule add -q "$submodurl" submod >actual &&
 109                test_must_be_empty actual &&
 110                echo "gitdir: ../.git/modules/submod" >expect &&
 111                test_cmp expect submod/.git &&
 112                (
 113                        cd submod &&
 114                        git config core.worktree >actual &&
 115                        echo "../../../submod" >expect &&
 116                        test_cmp expect actual &&
 117                        rm -f actual expect
 118                ) &&
 119                git submodule init
 120        ) &&
 121
 122        rm -f heads head untracked &&
 123        inspect addtest/submod ../.. &&
 124        test_cmp expect heads &&
 125        test_cmp expect head &&
 126        test_cmp empty untracked
 127'
 128
 129test_expect_success 'setup parent and one repository' '
 130        test_create_repo parent &&
 131        test_commit -C parent one
 132'
 133
 134test_expect_success 'redirected submodule add does not show progress' '
 135        git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
 136                2>err &&
 137        ! grep % err &&
 138        test_i18ngrep ! "Checking connectivity" err
 139'
 140
 141test_expect_success 'redirected submodule add --progress does show progress' '
 142        git -C addtest submodule add --progress "file://$submodurl/parent" \
 143                submod-redirected-progress 2>err && \
 144        grep % err
 145'
 146
 147test_expect_success 'submodule add to .gitignored path fails' '
 148        (
 149                cd addtest-ignore &&
 150                cat <<-\EOF >expect &&
 151                The following path is ignored by one of your .gitignore files:
 152                submod
 153                Use -f if you really want to add it.
 154                EOF
 155                # Does not use test_commit due to the ignore
 156                echo "*" > .gitignore &&
 157                git add --force .gitignore &&
 158                git commit -m"Ignore everything" &&
 159                ! git submodule add "$submodurl" submod >actual 2>&1 &&
 160                test_i18ncmp expect actual
 161        )
 162'
 163
 164test_expect_success 'submodule add to .gitignored path with --force' '
 165        (
 166                cd addtest-ignore &&
 167                git submodule add --force "$submodurl" submod
 168        )
 169'
 170
 171test_expect_success 'submodule add to reconfigure existing submodule with --force' '
 172        (
 173                cd addtest-ignore &&
 174                bogus_url="$(pwd)/bogus-url" &&
 175                git submodule add --force "$bogus_url" submod &&
 176                git submodule add --force -b initial "$submodurl" submod-branch &&
 177                test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
 178                test "$bogus_url" = "$(git config submodule.submod.url)" &&
 179                # Restore the url
 180                git submodule add --force "$submodurl" submod &&
 181                test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
 182                test "$submodurl" = "$(git config submodule.submod.url)"
 183        )
 184'
 185
 186test_expect_success 'submodule add --branch' '
 187        echo "refs/heads/initial" >expect-head &&
 188        cat <<-\EOF >expect-heads &&
 189        refs/heads/initial
 190        refs/heads/master
 191        EOF
 192        >empty &&
 193
 194        (
 195                cd addtest &&
 196                git submodule add -b initial "$submodurl" submod-branch &&
 197                test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
 198                git submodule init
 199        ) &&
 200
 201        rm -f heads head untracked &&
 202        inspect addtest/submod-branch ../.. &&
 203        test_cmp expect-heads heads &&
 204        test_cmp expect-head 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" ././dotsubmod/./frotz/./ &&
 215                git submodule init
 216        ) &&
 217
 218        rm -f heads head untracked &&
 219        inspect addtest/dotsubmod/frotz ../../.. &&
 220        test_cmp expect heads &&
 221        test_cmp expect head &&
 222        test_cmp empty untracked
 223'
 224
 225test_expect_success 'submodule add with /././ in path' '
 226        echo "refs/heads/master" >expect &&
 227        >empty &&
 228
 229        (
 230                cd addtest &&
 231                git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
 232                git submodule init
 233        ) &&
 234
 235        rm -f heads head untracked &&
 236        inspect addtest/dotslashdotsubmod/frotz ../../.. &&
 237        test_cmp expect heads &&
 238        test_cmp expect head &&
 239        test_cmp empty untracked
 240'
 241
 242test_expect_success 'submodule add with // in path' '
 243        echo "refs/heads/master" >expect &&
 244        >empty &&
 245
 246        (
 247                cd addtest &&
 248                git submodule add "$submodurl" slashslashsubmod///frotz// &&
 249                git submodule init
 250        ) &&
 251
 252        rm -f heads head untracked &&
 253        inspect addtest/slashslashsubmod/frotz ../../.. &&
 254        test_cmp expect heads &&
 255        test_cmp expect head &&
 256        test_cmp empty untracked
 257'
 258
 259test_expect_success 'submodule add with /.. in path' '
 260        echo "refs/heads/master" >expect &&
 261        >empty &&
 262
 263        (
 264                cd addtest &&
 265                git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
 266                git submodule init
 267        ) &&
 268
 269        rm -f heads head untracked &&
 270        inspect addtest/realsubmod ../.. &&
 271        test_cmp expect heads &&
 272        test_cmp expect head &&
 273        test_cmp empty untracked
 274'
 275
 276test_expect_success 'submodule add with ./, /.. and // in path' '
 277        echo "refs/heads/master" >expect &&
 278        >empty &&
 279
 280        (
 281                cd addtest &&
 282                git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
 283                git submodule init
 284        ) &&
 285
 286        rm -f heads head untracked &&
 287        inspect addtest/realsubmod2 ../.. &&
 288        test_cmp expect heads &&
 289        test_cmp expect head &&
 290        test_cmp empty untracked
 291'
 292
 293test_expect_success !CYGWIN 'submodule add with \\ in path' '
 294        test_when_finished "rm -rf parent sub\\with\\backslash" &&
 295
 296        # Initialize a repo with a backslash in its name
 297        git init sub\\with\\backslash &&
 298        touch sub\\with\\backslash/empty.file &&
 299        git -C sub\\with\\backslash add empty.file &&
 300        git -C sub\\with\\backslash commit -m "Added empty.file" &&
 301
 302        # Add that repository as a submodule
 303        git init parent &&
 304        git -C parent submodule add ../sub\\with\\backslash
 305'
 306
 307test_expect_success 'submodule add in subdirectory' '
 308        echo "refs/heads/master" >expect &&
 309        >empty &&
 310
 311        mkdir addtest/sub &&
 312        (
 313                cd addtest/sub &&
 314                git submodule add "$submodurl" ../realsubmod3 &&
 315                git submodule init
 316        ) &&
 317
 318        rm -f heads head untracked &&
 319        inspect addtest/realsubmod3 ../.. &&
 320        test_cmp expect heads &&
 321        test_cmp expect head &&
 322        test_cmp empty untracked
 323'
 324
 325test_expect_success 'submodule add in subdirectory with relative path should fail' '
 326        (
 327                cd addtest/sub &&
 328                test_must_fail git submodule add ../../ submod3 2>../../output.err
 329        ) &&
 330        test_i18ngrep toplevel output.err
 331'
 332
 333test_expect_success 'setup - add an example entry to .gitmodules' '
 334        git config --file=.gitmodules submodule.example.url git://example.com/init.git
 335'
 336
 337test_expect_success 'status should fail for unmapped paths' '
 338        test_must_fail git submodule status
 339'
 340
 341test_expect_success 'setup - map path in .gitmodules' '
 342        cat <<\EOF >expect &&
 343[submodule "example"]
 344        url = git://example.com/init.git
 345        path = init
 346EOF
 347
 348        git config --file=.gitmodules submodule.example.path init &&
 349
 350        test_cmp expect .gitmodules
 351'
 352
 353test_expect_success 'status should only print one line' '
 354        git submodule status >lines &&
 355        test_line_count = 1 lines
 356'
 357
 358test_expect_success 'setup - fetch commit name from submodule' '
 359        rev1=$(cd .subrepo && git rev-parse HEAD) &&
 360        printf "rev1: %s\n" "$rev1" &&
 361        test -n "$rev1"
 362'
 363
 364test_expect_success 'status should initially be "missing"' '
 365        git submodule status >lines &&
 366        grep "^-$rev1" lines
 367'
 368
 369test_expect_success 'init should register submodule url in .git/config' '
 370        echo git://example.com/init.git >expect &&
 371
 372        git submodule init &&
 373        git config submodule.example.url >url &&
 374        git config submodule.example.url ./.subrepo &&
 375
 376        test_cmp expect url
 377'
 378
 379test_failure_with_unknown_submodule () {
 380        test_must_fail git submodule $1 no-such-submodule 2>output.err &&
 381        grep "^error: .*no-such-submodule" output.err
 382}
 383
 384test_expect_success 'init should fail with unknown submodule' '
 385        test_failure_with_unknown_submodule init
 386'
 387
 388test_expect_success 'update should fail with unknown submodule' '
 389        test_failure_with_unknown_submodule update
 390'
 391
 392test_expect_success 'status should fail with unknown submodule' '
 393        test_failure_with_unknown_submodule status
 394'
 395
 396test_expect_success 'sync should fail with unknown submodule' '
 397        test_failure_with_unknown_submodule sync
 398'
 399
 400test_expect_success 'update should fail when path is used by a file' '
 401        echo hello >expect &&
 402
 403        echo "hello" >init &&
 404        test_must_fail git submodule update &&
 405
 406        test_cmp expect init
 407'
 408
 409test_expect_success 'update should fail when path is used by a nonempty directory' '
 410        echo hello >expect &&
 411
 412        rm -fr init &&
 413        mkdir init &&
 414        echo "hello" >init/a &&
 415
 416        test_must_fail git submodule update &&
 417
 418        test_cmp expect init/a
 419'
 420
 421test_expect_success 'update should work when path is an empty dir' '
 422        rm -fr init &&
 423        rm -f head-sha1 &&
 424        echo "$rev1" >expect &&
 425
 426        mkdir init &&
 427        git submodule update -q >update.out &&
 428        test_must_be_empty update.out &&
 429
 430        inspect init &&
 431        test_cmp expect head-sha1
 432'
 433
 434test_expect_success 'status should be "up-to-date" after update' '
 435        git submodule status >list &&
 436        grep "^ $rev1" list
 437'
 438
 439test_expect_success 'status "up-to-date" from subdirectory' '
 440        mkdir -p sub &&
 441        (
 442                cd sub &&
 443                git submodule status >../list
 444        ) &&
 445        grep "^ $rev1" list &&
 446        grep "\\.\\./init" list
 447'
 448
 449test_expect_success 'status "up-to-date" from subdirectory with path' '
 450        mkdir -p sub &&
 451        (
 452                cd sub &&
 453                git submodule status ../init >../list
 454        ) &&
 455        grep "^ $rev1" list &&
 456        grep "\\.\\./init" list
 457'
 458
 459test_expect_success 'status should be "modified" after submodule commit' '
 460        (
 461                cd init &&
 462                echo b >b &&
 463                git add b &&
 464                git commit -m "submodule commit 2"
 465        ) &&
 466
 467        rev2=$(cd init && git rev-parse HEAD) &&
 468        test -n "$rev2" &&
 469        git submodule status >list &&
 470
 471        grep "^+$rev2" list
 472'
 473
 474test_expect_success 'the --cached sha1 should be rev1' '
 475        git submodule --cached status >list &&
 476        grep "^+$rev1" list
 477'
 478
 479test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
 480        git diff >diff &&
 481        grep "^+Subproject commit $rev2" diff
 482'
 483
 484test_expect_success 'update should checkout rev1' '
 485        rm -f head-sha1 &&
 486        echo "$rev1" >expect &&
 487
 488        git submodule update init &&
 489        inspect init &&
 490
 491        test_cmp expect head-sha1
 492'
 493
 494test_expect_success 'status should be "up-to-date" after update' '
 495        git submodule status >list &&
 496        grep "^ $rev1" list
 497'
 498
 499test_expect_success 'checkout superproject with subproject already present' '
 500        git checkout initial &&
 501        git checkout master
 502'
 503
 504test_expect_success 'apply submodule diff' '
 505        >empty &&
 506
 507        git branch second &&
 508        (
 509                cd init &&
 510                echo s >s &&
 511                git add s &&
 512                git commit -m "change subproject"
 513        ) &&
 514        git update-index --add init &&
 515        git commit -m "change init" &&
 516        git format-patch -1 --stdout >P.diff &&
 517        git checkout second &&
 518        git apply --index P.diff &&
 519
 520        git diff --cached master >staged &&
 521        test_cmp empty staged
 522'
 523
 524test_expect_success 'update --init' '
 525        mv init init2 &&
 526        git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
 527        git config --remove-section submodule.example &&
 528        test_must_fail git config submodule.example.url &&
 529
 530        git submodule update init 2> update.out &&
 531        cat update.out &&
 532        test_i18ngrep "not initialized" update.out &&
 533        test_must_fail git rev-parse --resolve-git-dir init/.git &&
 534
 535        git submodule update --init init &&
 536        git rev-parse --resolve-git-dir init/.git
 537'
 538
 539test_expect_success 'update --init from subdirectory' '
 540        mv init init2 &&
 541        git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
 542        git config --remove-section submodule.example &&
 543        test_must_fail git config submodule.example.url &&
 544
 545        mkdir -p sub &&
 546        (
 547                cd sub &&
 548                git submodule update ../init 2>update.out &&
 549                cat update.out &&
 550                test_i18ngrep "not initialized" update.out &&
 551                test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
 552
 553                git submodule update --init ../init
 554        ) &&
 555        git rev-parse --resolve-git-dir init/.git
 556'
 557
 558test_expect_success 'do not add files from a submodule' '
 559
 560        git reset --hard &&
 561        test_must_fail git add init/a
 562
 563'
 564
 565test_expect_success 'gracefully add/reset submodule with a trailing slash' '
 566
 567        git reset --hard &&
 568        git commit -m "commit subproject" init &&
 569        (cd init &&
 570         echo b > a) &&
 571        git add init/ &&
 572        git diff --exit-code --cached init &&
 573        commit=$(cd init &&
 574         git commit -m update a >/dev/null &&
 575         git rev-parse HEAD) &&
 576        git add init/ &&
 577        test_must_fail git diff --exit-code --cached init &&
 578        test $commit = $(git ls-files --stage |
 579                sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
 580        git reset init/ &&
 581        git diff --exit-code --cached init
 582
 583'
 584
 585test_expect_success 'ls-files gracefully handles trailing slash' '
 586
 587        test "init" = "$(git ls-files init/)"
 588
 589'
 590
 591test_expect_success 'moving to a commit without submodule does not leave empty dir' '
 592        rm -rf init &&
 593        mkdir init &&
 594        git reset --hard &&
 595        git checkout initial &&
 596        test ! -d init &&
 597        git checkout second
 598'
 599
 600test_expect_success 'submodule <invalid-subcommand> fails' '
 601        test_must_fail git submodule no-such-subcommand
 602'
 603
 604test_expect_success 'add submodules without specifying an explicit path' '
 605        mkdir repo &&
 606        (
 607                cd repo &&
 608                git init &&
 609                echo r >r &&
 610                git add r &&
 611                git commit -m "repo commit 1"
 612        ) &&
 613        git clone --bare repo/ bare.git &&
 614        (
 615                cd addtest &&
 616                git submodule add "$submodurl/repo" &&
 617                git config -f .gitmodules submodule.repo.path repo &&
 618                git submodule add "$submodurl/bare.git" &&
 619                git config -f .gitmodules submodule.bare.path bare
 620        )
 621'
 622
 623test_expect_success 'add should fail when path is used by a file' '
 624        (
 625                cd addtest &&
 626                touch file &&
 627                test_must_fail  git submodule add "$submodurl/repo" file
 628        )
 629'
 630
 631test_expect_success 'add should fail when path is used by an existing directory' '
 632        (
 633                cd addtest &&
 634                mkdir empty-dir &&
 635                test_must_fail git submodule add "$submodurl/repo" empty-dir
 636        )
 637'
 638
 639test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
 640        (
 641                cd addtest &&
 642                git submodule add ../repo relative &&
 643                test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
 644                git submodule sync relative &&
 645                test "$(git config submodule.relative.url)" = "$submodurl/repo"
 646        )
 647'
 648
 649test_expect_success 'set up for relative path tests' '
 650        mkdir reltest &&
 651        (
 652                cd reltest &&
 653                git init &&
 654                mkdir sub &&
 655                (
 656                        cd sub &&
 657                        git init &&
 658                        test_commit foo
 659                ) &&
 660                git add sub &&
 661                git config -f .gitmodules submodule.sub.path sub &&
 662                git config -f .gitmodules submodule.sub.url ../subrepo &&
 663                cp .git/config pristine-.git-config &&
 664                cp .gitmodules pristine-.gitmodules
 665        )
 666'
 667
 668test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
 669        (
 670                cd reltest &&
 671                cp pristine-.git-config .git/config &&
 672                cp pristine-.gitmodules .gitmodules &&
 673                git config remote.origin.url ssh://hostname/repo &&
 674                git submodule init &&
 675                test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
 676        )
 677'
 678
 679test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
 680        (
 681                cd reltest &&
 682                cp pristine-.git-config .git/config &&
 683                cp pristine-.gitmodules .gitmodules &&
 684                git config remote.origin.url ssh://hostname:22/repo &&
 685                git submodule init &&
 686                test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
 687        )
 688'
 689
 690# About the choice of the path in the next test:
 691# - double-slash side-steps path mangling issues on Windows
 692# - it is still an absolute local path
 693# - there cannot be a server with a blank in its name just in case the
 694#   path is used erroneously to access a //server/share style path
 695test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
 696        (
 697                cd reltest &&
 698                cp pristine-.git-config .git/config &&
 699                cp pristine-.gitmodules .gitmodules &&
 700                git config remote.origin.url "//somewhere else/repo" &&
 701                git submodule init &&
 702                test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
 703        )
 704'
 705
 706test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
 707        (
 708                cd reltest &&
 709                cp pristine-.git-config .git/config &&
 710                cp pristine-.gitmodules .gitmodules &&
 711                git config remote.origin.url file:///tmp/repo &&
 712                git submodule init &&
 713                test "$(git config submodule.sub.url)" = file:///tmp/subrepo
 714        )
 715'
 716
 717test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
 718        (
 719                cd reltest &&
 720                cp pristine-.git-config .git/config &&
 721                cp pristine-.gitmodules .gitmodules &&
 722                git config remote.origin.url helper:://hostname/repo &&
 723                git submodule init &&
 724                test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
 725        )
 726'
 727
 728test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
 729        (
 730                cd reltest &&
 731                cp pristine-.git-config .git/config &&
 732                git config remote.origin.url user@host:repo &&
 733                git submodule init &&
 734                test "$(git config submodule.sub.url)" = user@host:subrepo
 735        )
 736'
 737
 738test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
 739        (
 740                cd reltest &&
 741                cp pristine-.git-config .git/config &&
 742                cp pristine-.gitmodules .gitmodules &&
 743                git config remote.origin.url user@host:path/to/repo &&
 744                git submodule init &&
 745                test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
 746        )
 747'
 748
 749test_expect_success '../subrepo works with relative local path - foo' '
 750        (
 751                cd reltest &&
 752                cp pristine-.git-config .git/config &&
 753                cp pristine-.gitmodules .gitmodules &&
 754                git config remote.origin.url foo &&
 755                # actual: fails with an error
 756                git submodule init &&
 757                test "$(git config submodule.sub.url)" = subrepo
 758        )
 759'
 760
 761test_expect_success '../subrepo works with relative local path - foo/bar' '
 762        (
 763                cd reltest &&
 764                cp pristine-.git-config .git/config &&
 765                cp pristine-.gitmodules .gitmodules &&
 766                git config remote.origin.url foo/bar &&
 767                git submodule init &&
 768                test "$(git config submodule.sub.url)" = foo/subrepo
 769        )
 770'
 771
 772test_expect_success '../subrepo works with relative local path - ./foo' '
 773        (
 774                cd reltest &&
 775                cp pristine-.git-config .git/config &&
 776                cp pristine-.gitmodules .gitmodules &&
 777                git config remote.origin.url ./foo &&
 778                git submodule init &&
 779                test "$(git config submodule.sub.url)" = subrepo
 780        )
 781'
 782
 783test_expect_success '../subrepo works with relative local path - ./foo/bar' '
 784        (
 785                cd reltest &&
 786                cp pristine-.git-config .git/config &&
 787                cp pristine-.gitmodules .gitmodules &&
 788                git config remote.origin.url ./foo/bar &&
 789                git submodule init &&
 790                test "$(git config submodule.sub.url)" = foo/subrepo
 791        )
 792'
 793
 794test_expect_success '../subrepo works with relative local path - ../foo' '
 795        (
 796                cd reltest &&
 797                cp pristine-.git-config .git/config &&
 798                cp pristine-.gitmodules .gitmodules &&
 799                git config remote.origin.url ../foo &&
 800                git submodule init &&
 801                test "$(git config submodule.sub.url)" = ../subrepo
 802        )
 803'
 804
 805test_expect_success '../subrepo works with relative local path - ../foo/bar' '
 806        (
 807                cd reltest &&
 808                cp pristine-.git-config .git/config &&
 809                cp pristine-.gitmodules .gitmodules &&
 810                git config remote.origin.url ../foo/bar &&
 811                git submodule init &&
 812                test "$(git config submodule.sub.url)" = ../foo/subrepo
 813        )
 814'
 815
 816test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
 817        (
 818                cd reltest &&
 819                cp pristine-.git-config .git/config &&
 820                cp pristine-.gitmodules .gitmodules &&
 821                mkdir -p a/b/c &&
 822                (cd a/b/c; git init) &&
 823                git config remote.origin.url ../foo/bar.git &&
 824                git submodule add ../bar/a/b/c ./a/b/c &&
 825                git submodule init &&
 826                test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
 827        )
 828'
 829
 830test_expect_success 'moving the superproject does not break submodules' '
 831        (
 832                cd addtest &&
 833                git submodule status >expect
 834        ) &&
 835        mv addtest addtest2 &&
 836        (
 837                cd addtest2 &&
 838                git submodule status >actual &&
 839                test_cmp expect actual
 840        )
 841'
 842
 843test_expect_success 'moving the submodule does not break the superproject' '
 844        (
 845                cd addtest2 &&
 846                git submodule status
 847        ) >actual &&
 848        sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
 849        mv addtest2/repo addtest2/repo.bak &&
 850        test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
 851        (
 852                cd addtest2 &&
 853                git submodule status
 854        ) >actual &&
 855        test_cmp expect actual
 856'
 857
 858test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
 859        (
 860                cd addtest2 &&
 861                (
 862                        cd repo &&
 863                        echo "$submodurl/repo" >expect &&
 864                        git config remote.origin.url >actual &&
 865                        test_cmp expect actual &&
 866                        echo "gitdir: ../.git/modules/repo" >expect &&
 867                        test_cmp expect .git
 868                ) &&
 869                rm -rf repo &&
 870                git rm repo &&
 871                git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
 872                test_must_be_empty actual &&
 873                echo "gitdir: ../.git/modules/submod" >expect &&
 874                test_cmp expect submod/.git &&
 875                (
 876                        cd repo &&
 877                        echo "$submodurl/bare.git" >expect &&
 878                        git config remote.origin.url >actual &&
 879                        test_cmp expect actual &&
 880                        echo "gitdir: ../.git/modules/repo_new" >expect &&
 881                        test_cmp expect .git
 882                ) &&
 883                echo "repo" >expect &&
 884                test_must_fail git config -f .gitmodules submodule.repo.path &&
 885                git config -f .gitmodules submodule.repo_new.path >actual &&
 886                test_cmp expect actual&&
 887                echo "$submodurl/repo" >expect &&
 888                test_must_fail git config -f .gitmodules submodule.repo.url &&
 889                echo "$submodurl/bare.git" >expect &&
 890                git config -f .gitmodules submodule.repo_new.url >actual &&
 891                test_cmp expect actual &&
 892                echo "$submodurl/repo" >expect &&
 893                git config submodule.repo.url >actual &&
 894                test_cmp expect actual &&
 895                echo "$submodurl/bare.git" >expect &&
 896                git config submodule.repo_new.url >actual &&
 897                test_cmp expect actual
 898        )
 899'
 900
 901test_expect_success 'recursive relative submodules stay relative' '
 902        test_when_finished "rm -rf super clone2 subsub sub3" &&
 903        mkdir subsub &&
 904        (
 905                cd subsub &&
 906                git init &&
 907                >t &&
 908                git add t &&
 909                git commit -m "initial commit"
 910        ) &&
 911        mkdir sub3 &&
 912        (
 913                cd sub3 &&
 914                git init &&
 915                >t &&
 916                git add t &&
 917                git commit -m "initial commit" &&
 918                git submodule add ../subsub dirdir/subsub &&
 919                git commit -m "add submodule subsub"
 920        ) &&
 921        mkdir super &&
 922        (
 923                cd super &&
 924                git init &&
 925                >t &&
 926                git add t &&
 927                git commit -m "initial commit" &&
 928                git submodule add ../sub3 &&
 929                git commit -m "add submodule sub"
 930        ) &&
 931        git clone super clone2 &&
 932        (
 933                cd clone2 &&
 934                git submodule update --init --recursive &&
 935                echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
 936                echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
 937        ) &&
 938        test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
 939        test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
 940'
 941
 942test_expect_success 'submodule add with an existing name fails unless forced' '
 943        (
 944                cd addtest2 &&
 945                rm -rf repo &&
 946                git rm repo &&
 947                test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
 948                test ! -d repo &&
 949                test_must_fail git config -f .gitmodules submodule.repo_new.path &&
 950                test_must_fail git config -f .gitmodules submodule.repo_new.url &&
 951                echo "$submodurl/bare.git" >expect &&
 952                git config submodule.repo_new.url >actual &&
 953                test_cmp expect actual &&
 954                git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
 955                test -d repo &&
 956                echo "repo" >expect &&
 957                git config -f .gitmodules submodule.repo_new.path >actual &&
 958                test_cmp expect actual&&
 959                echo "$submodurl/repo.git" >expect &&
 960                git config -f .gitmodules submodule.repo_new.url >actual &&
 961                test_cmp expect actual &&
 962                echo "$submodurl/repo.git" >expect &&
 963                git config submodule.repo_new.url >actual &&
 964                test_cmp expect actual
 965        )
 966'
 967
 968test_expect_success 'set up a second submodule' '
 969        git submodule add ./init2 example2 &&
 970        git commit -m "submodule example2 added"
 971'
 972
 973test_expect_success 'submodule deinit works on repository without submodules' '
 974        test_when_finished "rm -rf newdirectory" &&
 975        mkdir newdirectory &&
 976        (
 977                cd newdirectory &&
 978                git init &&
 979                >file &&
 980                git add file &&
 981                git commit -m "repo should not be empty" &&
 982                git submodule deinit . &&
 983                git submodule deinit --all
 984        )
 985'
 986
 987test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
 988        git config submodule.example.foo bar &&
 989        git config submodule.example2.frotz nitfol &&
 990        git submodule deinit init &&
 991        test -z "$(git config --get-regexp "submodule\.example\.")" &&
 992        test -n "$(git config --get-regexp "submodule\.example2\.")" &&
 993        test -f example2/.git &&
 994        rmdir init
 995'
 996
 997test_expect_success 'submodule deinit from subdirectory' '
 998        git submodule update --init &&
 999        git config submodule.example.foo bar &&
1000        mkdir -p sub &&
1001        (
1002                cd sub &&
1003                git submodule deinit ../init >../output
1004        ) &&
1005        test_i18ngrep "\\.\\./init" output &&
1006        test -z "$(git config --get-regexp "submodule\.example\.")" &&
1007        test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1008        test -f example2/.git &&
1009        rmdir init
1010'
1011
1012test_expect_success 'submodule deinit . deinits all initialized submodules' '
1013        git submodule update --init &&
1014        git config submodule.example.foo bar &&
1015        git config submodule.example2.frotz nitfol &&
1016        test_must_fail git submodule deinit &&
1017        git submodule deinit . >actual &&
1018        test -z "$(git config --get-regexp "submodule\.example\.")" &&
1019        test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1020        test_i18ngrep "Cleared directory .init" actual &&
1021        test_i18ngrep "Cleared directory .example2" actual &&
1022        rmdir init example2
1023'
1024
1025test_expect_success 'submodule deinit --all deinits all initialized submodules' '
1026        git submodule update --init &&
1027        git config submodule.example.foo bar &&
1028        git config submodule.example2.frotz nitfol &&
1029        test_must_fail git submodule deinit &&
1030        git submodule deinit --all >actual &&
1031        test -z "$(git config --get-regexp "submodule\.example\.")" &&
1032        test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1033        test_i18ngrep "Cleared directory .init" actual &&
1034        test_i18ngrep "Cleared directory .example2" actual &&
1035        rmdir init example2
1036'
1037
1038test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
1039        git submodule update --init &&
1040        rm -rf init example2/* example2/.git &&
1041        git submodule deinit init example2 >actual &&
1042        test -z "$(git config --get-regexp "submodule\.example\.")" &&
1043        test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1044        test_i18ngrep ! "Cleared directory .init" actual &&
1045        test_i18ngrep "Cleared directory .example2" actual &&
1046        rmdir init
1047'
1048
1049test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
1050        git submodule update --init &&
1051        echo X >>init/s &&
1052        test_must_fail git submodule deinit init &&
1053        test -n "$(git config --get-regexp "submodule\.example\.")" &&
1054        test -f example2/.git &&
1055        git submodule deinit -f init >actual &&
1056        test -z "$(git config --get-regexp "submodule\.example\.")" &&
1057        test_i18ngrep "Cleared directory .init" actual &&
1058        rmdir init
1059'
1060
1061test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1062        git submodule update --init &&
1063        echo X >>init/untracked &&
1064        test_must_fail git submodule deinit init &&
1065        test -n "$(git config --get-regexp "submodule\.example\.")" &&
1066        test -f example2/.git &&
1067        git submodule deinit -f init >actual &&
1068        test -z "$(git config --get-regexp "submodule\.example\.")" &&
1069        test_i18ngrep "Cleared directory .init" actual &&
1070        rmdir init
1071'
1072
1073test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1074        git submodule update --init &&
1075        (
1076                cd init &&
1077                git checkout HEAD^
1078        ) &&
1079        test_must_fail git submodule deinit init &&
1080        test -n "$(git config --get-regexp "submodule\.example\.")" &&
1081        test -f example2/.git &&
1082        git submodule deinit -f init >actual &&
1083        test -z "$(git config --get-regexp "submodule\.example\.")" &&
1084        test_i18ngrep "Cleared directory .init" actual &&
1085        rmdir init
1086'
1087
1088test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1089        git submodule update --init &&
1090        git submodule deinit init >actual &&
1091        test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1092        test_i18ngrep "Cleared directory .init" actual &&
1093        git submodule deinit init >actual &&
1094        test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1095        test_i18ngrep "Cleared directory .init" actual &&
1096        git submodule deinit . >actual &&
1097        test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1098        test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1099        test_i18ngrep "Cleared directory .init" actual &&
1100        git submodule deinit . >actual &&
1101        test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1102        test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1103        test_i18ngrep "Cleared directory .init" actual &&
1104        git submodule deinit --all >actual &&
1105        test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1106        test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1107        test_i18ngrep "Cleared directory .init" actual &&
1108        rmdir init example2
1109'
1110
1111test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
1112        git submodule update --init &&
1113        (
1114                cd init &&
1115                rm .git &&
1116                cp -R ../.git/modules/example .git &&
1117                GIT_WORK_TREE=. git config --unset core.worktree
1118        ) &&
1119        test_must_fail git submodule deinit init &&
1120        test_must_fail git submodule deinit -f init &&
1121        test -d init/.git &&
1122        test -n "$(git config --get-regexp "submodule\.example\.")"
1123'
1124
1125test_expect_success 'submodule with UTF-8 name' '
1126        svname=$(printf "\303\245 \303\244\303\266") &&
1127        mkdir "$svname" &&
1128        (
1129                cd "$svname" &&
1130                git init &&
1131                >sub &&
1132                git add sub &&
1133                git commit -m "init sub"
1134        ) &&
1135        git submodule add ./"$svname" &&
1136        git submodule >&2 &&
1137        test -n "$(git submodule | grep "$svname")"
1138'
1139
1140test_expect_success 'submodule add clone shallow submodule' '
1141        mkdir super &&
1142        pwd=$(pwd) &&
1143        (
1144                cd super &&
1145                git init &&
1146                git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1147                (
1148                        cd submodule &&
1149                        test 1 = $(git log --oneline | wc -l)
1150                )
1151        )
1152'
1153
1154test_expect_success 'submodule helper list is not confused by common prefixes' '
1155        mkdir -p dir1/b &&
1156        (
1157                cd dir1/b &&
1158                git init &&
1159                echo hi >testfile2 &&
1160                git add . &&
1161                git commit -m "test1"
1162        ) &&
1163        mkdir -p dir2/b &&
1164        (
1165                cd dir2/b &&
1166                git init &&
1167                echo hello >testfile1 &&
1168                git add .  &&
1169                git commit -m "test2"
1170        ) &&
1171        git submodule add /dir1/b dir1/b &&
1172        git submodule add /dir2/b dir2/b &&
1173        git commit -m "first submodule commit" &&
1174        git submodule--helper list dir1/b |cut -c51- >actual &&
1175        echo "dir1/b" >expect &&
1176        test_cmp expect actual
1177'
1178
1179test_expect_success 'setup superproject with submodules' '
1180        git init sub1 &&
1181        test_commit -C sub1 test &&
1182        test_commit -C sub1 test2 &&
1183        git init multisuper &&
1184        git -C multisuper submodule add ../sub1 sub0 &&
1185        git -C multisuper submodule add ../sub1 sub1 &&
1186        git -C multisuper submodule add ../sub1 sub2 &&
1187        git -C multisuper submodule add ../sub1 sub3 &&
1188        git -C multisuper commit -m "add some submodules"
1189'
1190
1191cat >expect <<-EOF
1192-sub0
1193 sub1 (test2)
1194 sub2 (test2)
1195 sub3 (test2)
1196EOF
1197
1198test_expect_success 'submodule update --init with a specification' '
1199        test_when_finished "rm -rf multisuper_clone" &&
1200        pwd=$(pwd) &&
1201        git clone file://"$pwd"/multisuper multisuper_clone &&
1202        git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1203        git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1204        test_cmp expect actual
1205'
1206
1207test_expect_success 'submodule update --init with submodule.active set' '
1208        test_when_finished "rm -rf multisuper_clone" &&
1209        pwd=$(pwd) &&
1210        git clone file://"$pwd"/multisuper multisuper_clone &&
1211        git -C multisuper_clone config submodule.active "." &&
1212        git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1213        git -C multisuper_clone submodule update --init &&
1214        git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1215        test_cmp expect actual
1216'
1217
1218test_expect_success 'submodule update and setting submodule.<name>.active' '
1219        test_when_finished "rm -rf multisuper_clone" &&
1220        pwd=$(pwd) &&
1221        git clone file://"$pwd"/multisuper multisuper_clone &&
1222        git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1223        git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1224        git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1225
1226        cat >expect <<-\EOF &&
1227         sub0 (test2)
1228        -sub1
1229         sub2 (test2)
1230        -sub3
1231        EOF
1232        git -C multisuper_clone submodule update &&
1233        git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1234        test_cmp expect actual
1235'
1236
1237test_expect_success 'clone --recurse-submodules with a pathspec works' '
1238        test_when_finished "rm -rf multisuper_clone" &&
1239        cat >expected <<-\EOF &&
1240         sub0 (test2)
1241        -sub1
1242        -sub2
1243        -sub3
1244        EOF
1245
1246        git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1247        git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1248        test_cmp expected actual
1249'
1250
1251test_expect_success 'clone with multiple --recurse-submodules options' '
1252        test_when_finished "rm -rf multisuper_clone" &&
1253        cat >expect <<-\EOF &&
1254        -sub0
1255         sub1 (test2)
1256        -sub2
1257         sub3 (test2)
1258        EOF
1259
1260        git clone --recurse-submodules="." \
1261                  --recurse-submodules=":(exclude)sub0" \
1262                  --recurse-submodules=":(exclude)sub2" \
1263                  multisuper multisuper_clone &&
1264        git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1265        test_cmp expect actual
1266'
1267
1268test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
1269        test_when_finished "rm -rf multisuper_clone" &&
1270        cat <<-\EOF >expect &&
1271        -sub0
1272         sub1 (test2)
1273        -sub2
1274         sub3 (test2)
1275        EOF
1276
1277        cat <<-\EOF >expect2 &&
1278        -sub0
1279         sub1 (test2)
1280        -sub2
1281         sub3 (test2)
1282        -sub4
1283         sub5 (test2)
1284        EOF
1285
1286        git clone --recurse-submodules="." \
1287                  --recurse-submodules=":(exclude)sub0" \
1288                  --recurse-submodules=":(exclude)sub2" \
1289                  --recurse-submodules=":(exclude)sub4" \
1290                  multisuper multisuper_clone &&
1291
1292        git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1293        test_cmp expect actual &&
1294
1295        git -C multisuper submodule add ../sub1 sub4 &&
1296        git -C multisuper submodule add ../sub1 sub5 &&
1297        git -C multisuper commit -m "add more submodules" &&
1298        # obtain the new superproject
1299        git -C multisuper_clone pull &&
1300        git -C multisuper_clone submodule update --init &&
1301        git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1302        test_cmp expect2 actual
1303'
1304
1305test_expect_success 'init properly sets the config' '
1306        test_when_finished "rm -rf multisuper_clone" &&
1307        git clone --recurse-submodules="." \
1308                  --recurse-submodules=":(exclude)sub0" \
1309                  multisuper multisuper_clone &&
1310
1311        git -C multisuper_clone submodule init -- sub0 sub1 &&
1312        git -C multisuper_clone config --get submodule.sub0.active &&
1313        test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1314'
1315
1316test_expect_success 'recursive clone respects -q' '
1317        test_when_finished "rm -rf multisuper_clone" &&
1318        git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1319        test_must_be_empty actual
1320'
1321
1322test_done