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