Imported Upstream version 2.27.0
[platform/upstream/git.git] / t / t7400-submodule-basic.sh
index 75db023..956e17a 100755 (executable)
@@ -22,14 +22,52 @@ test_expect_success 'setup - initial commit' '
        git branch initial
 '
 
-test_expect_success 'configuration parsing' '
+test_expect_success 'submodule init aborts on missing .gitmodules file' '
+       test_when_finished "git update-index --remove sub" &&
+       git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
+       # missing the .gitmodules file here
+       test_must_fail git submodule init 2>actual &&
+       test_i18ngrep "No url found for submodule path" actual
+'
+
+test_expect_success 'submodule update aborts on missing .gitmodules file' '
+       test_when_finished "git update-index --remove sub" &&
+       git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
+       # missing the .gitmodules file here
+       git submodule update sub 2>actual &&
+       test_i18ngrep "Submodule path .sub. not initialized" actual
+'
+
+test_expect_success 'submodule update aborts on missing gitmodules url' '
+       test_when_finished "git update-index --remove sub" &&
+       git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
        test_when_finished "rm -f .gitmodules" &&
-       cat >.gitmodules <<-\EOF &&
-       [submodule "s"]
-               path
-               ignore
+       git config -f .gitmodules submodule.s.path sub &&
+       test_must_fail git submodule init
+'
+
+test_expect_success 'add aborts on repository with no commits' '
+       cat >expect <<-\EOF &&
+       '"'repo-no-commits'"' does not have a commit checked out
        EOF
-       test_must_fail git status
+       git init repo-no-commits &&
+       test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
+       test_i18ncmp expect actual
+'
+
+test_expect_success 'status should ignore inner git repo when not added' '
+       rm -fr inner &&
+       mkdir inner &&
+       (
+               cd inner &&
+               git init &&
+               >t &&
+               git add t &&
+               git commit -m "initial"
+       ) &&
+       test_must_fail git submodule status inner 2>output.err &&
+       rm -fr inner &&
+       test_i18ngrep "^error: .*did not match any file(s) known to git" output.err
 '
 
 test_expect_success 'setup - repository in init subdirectory' '
@@ -87,7 +125,6 @@ inspect() {
 
 test_expect_success 'submodule add' '
        echo "refs/heads/master" >expect &&
-       >empty &&
 
        (
                cd addtest &&
@@ -109,16 +146,36 @@ test_expect_success 'submodule add' '
        inspect addtest/submod ../.. &&
        test_cmp expect heads &&
        test_cmp expect head &&
-       test_cmp empty untracked
+       test_must_be_empty untracked
+'
+
+test_expect_success 'setup parent and one repository' '
+       test_create_repo parent &&
+       test_commit -C parent one
+'
+
+test_expect_success 'redirected submodule add does not show progress' '
+       git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
+               2>err &&
+       ! grep % err &&
+       test_i18ngrep ! "Checking connectivity" err
+'
+
+test_expect_success 'redirected submodule add --progress does show progress' '
+       git -C addtest submodule add --progress "file://$submodurl/parent" \
+               submod-redirected-progress 2>err && \
+       grep % err
 '
 
 test_expect_success 'submodule add to .gitignored path fails' '
        (
                cd addtest-ignore &&
                cat <<-\EOF >expect &&
-               The following path is ignored by one of your .gitignore files:
+               The following paths are ignored by one of your .gitignore files:
                submod
-               Use -f if you really want to add it.
+               hint: Use -f if you really want to add them.
+               hint: Turn this message off by running
+               hint: "git config advice.addIgnoredFile false"
                EOF
                # Does not use test_commit due to the ignore
                echo "*" > .gitignore &&
@@ -136,13 +193,38 @@ test_expect_success 'submodule add to .gitignored path with --force' '
        )
 '
 
+test_expect_success 'submodule add to reconfigure existing submodule with --force' '
+       (
+               cd addtest-ignore &&
+               bogus_url="$(pwd)/bogus-url" &&
+               git submodule add --force "$bogus_url" submod &&
+               git submodule add --force -b initial "$submodurl" submod-branch &&
+               test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
+               test "$bogus_url" = "$(git config submodule.submod.url)" &&
+               # Restore the url
+               git submodule add --force "$submodurl" submod &&
+               test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
+               test "$submodurl" = "$(git config submodule.submod.url)"
+       )
+'
+
+test_expect_success 'submodule add relays add --dry-run stderr' '
+       test_when_finished "rm -rf addtest/.git/index.lock" &&
+       (
+               cd addtest &&
+               : >.git/index.lock &&
+               ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
+               test_i18ngrep "^fatal: .*index\.lock" output.err &&
+               test_path_is_missing sub-while-locked
+       )
+'
+
 test_expect_success 'submodule add --branch' '
        echo "refs/heads/initial" >expect-head &&
        cat <<-\EOF >expect-heads &&
        refs/heads/initial
        refs/heads/master
        EOF
-       >empty &&
 
        (
                cd addtest &&
@@ -155,12 +237,11 @@ test_expect_success 'submodule add --branch' '
        inspect addtest/submod-branch ../.. &&
        test_cmp expect-heads heads &&
        test_cmp expect-head head &&
-       test_cmp empty untracked
+       test_must_be_empty untracked
 '
 
 test_expect_success 'submodule add with ./ in path' '
        echo "refs/heads/master" >expect &&
-       >empty &&
 
        (
                cd addtest &&
@@ -172,12 +253,11 @@ test_expect_success 'submodule add with ./ in path' '
        inspect addtest/dotsubmod/frotz ../../.. &&
        test_cmp expect heads &&
        test_cmp expect head &&
-       test_cmp empty untracked
+       test_must_be_empty untracked
 '
 
 test_expect_success 'submodule add with /././ in path' '
        echo "refs/heads/master" >expect &&
-       >empty &&
 
        (
                cd addtest &&
@@ -189,12 +269,11 @@ test_expect_success 'submodule add with /././ in path' '
        inspect addtest/dotslashdotsubmod/frotz ../../.. &&
        test_cmp expect heads &&
        test_cmp expect head &&
-       test_cmp empty untracked
+       test_must_be_empty untracked
 '
 
 test_expect_success 'submodule add with // in path' '
        echo "refs/heads/master" >expect &&
-       >empty &&
 
        (
                cd addtest &&
@@ -206,12 +285,11 @@ test_expect_success 'submodule add with // in path' '
        inspect addtest/slashslashsubmod/frotz ../../.. &&
        test_cmp expect heads &&
        test_cmp expect head &&
-       test_cmp empty untracked
+       test_must_be_empty untracked
 '
 
 test_expect_success 'submodule add with /.. in path' '
        echo "refs/heads/master" >expect &&
-       >empty &&
 
        (
                cd addtest &&
@@ -223,12 +301,11 @@ test_expect_success 'submodule add with /.. in path' '
        inspect addtest/realsubmod ../.. &&
        test_cmp expect heads &&
        test_cmp expect head &&
-       test_cmp empty untracked
+       test_must_be_empty untracked
 '
 
 test_expect_success 'submodule add with ./, /.. and // in path' '
        echo "refs/heads/master" >expect &&
-       >empty &&
 
        (
                cd addtest &&
@@ -240,12 +317,25 @@ test_expect_success 'submodule add with ./, /.. and // in path' '
        inspect addtest/realsubmod2 ../.. &&
        test_cmp expect heads &&
        test_cmp expect head &&
-       test_cmp empty untracked
+       test_must_be_empty untracked
+'
+
+test_expect_success !CYGWIN 'submodule add with \\ in path' '
+       test_when_finished "rm -rf parent sub\\with\\backslash" &&
+
+       # Initialize a repo with a backslash in its name
+       git init sub\\with\\backslash &&
+       touch sub\\with\\backslash/empty.file &&
+       git -C sub\\with\\backslash add empty.file &&
+       git -C sub\\with\\backslash commit -m "Added empty.file" &&
+
+       # Add that repository as a submodule
+       git init parent &&
+       git -C parent submodule add ../sub\\with\\backslash
 '
 
 test_expect_success 'submodule add in subdirectory' '
        echo "refs/heads/master" >expect &&
-       >empty &&
 
        mkdir addtest/sub &&
        (
@@ -258,7 +348,7 @@ test_expect_success 'submodule add in subdirectory' '
        inspect addtest/realsubmod3 ../.. &&
        test_cmp expect heads &&
        test_cmp expect head &&
-       test_cmp empty untracked
+       test_must_be_empty untracked
 '
 
 test_expect_success 'submodule add in subdirectory with relative path should fail' '
@@ -294,6 +384,28 @@ test_expect_success 'status should only print one line' '
        test_line_count = 1 lines
 '
 
+test_expect_success 'status from subdirectory should have the same SHA1' '
+       test_when_finished "rmdir addtest/subdir" &&
+       (
+               cd addtest &&
+               mkdir subdir &&
+               git submodule status >output &&
+               awk "{print \$1}" <output >expect &&
+               cd subdir &&
+               git submodule status >../output &&
+               awk "{print \$1}" <../output >../actual &&
+               test_cmp ../expect ../actual &&
+               git -C ../submod checkout HEAD^ &&
+               git submodule status >../output &&
+               awk "{print \$1}" <../output >../actual2 &&
+               cd .. &&
+               git submodule status >output &&
+               awk "{print \$1}" <output >expect2 &&
+               test_cmp expect2 actual2 &&
+               ! test_cmp actual actual2
+       )
+'
+
 test_expect_success 'setup - fetch commit name from submodule' '
        rev1=$(cd .subrepo && git rev-parse HEAD) &&
        printf "rev1: %s\n" "$rev1" &&
@@ -315,9 +427,17 @@ test_expect_success 'init should register submodule url in .git/config' '
        test_cmp expect url
 '
 
+test_expect_success 'status should still be "missing" after initializing' '
+       rm -fr init &&
+       mkdir init &&
+       git submodule status >lines &&
+       rm -fr init &&
+       grep "^-$rev1" lines
+'
+
 test_failure_with_unknown_submodule () {
        test_must_fail git submodule $1 no-such-submodule 2>output.err &&
-       grep "^error: .*no-such-submodule" output.err
+       test_i18ngrep "^error: .*no-such-submodule" output.err
 }
 
 test_expect_success 'init should fail with unknown submodule' '
@@ -441,8 +561,6 @@ test_expect_success 'checkout superproject with subproject already present' '
 '
 
 test_expect_success 'apply submodule diff' '
-       >empty &&
-
        git branch second &&
        (
                cd init &&
@@ -457,7 +575,7 @@ test_expect_success 'apply submodule diff' '
        git apply --index P.diff &&
 
        git diff --cached master >staged &&
-       test_cmp empty staged
+       test_must_be_empty staged
 '
 
 test_expect_success 'update --init' '
@@ -466,8 +584,7 @@ test_expect_success 'update --init' '
        git config --remove-section submodule.example &&
        test_must_fail git config submodule.example.url &&
 
-       git submodule update init > update.out &&
-       cat update.out &&
+       git submodule update init 2> update.out &&
        test_i18ngrep "not initialized" update.out &&
        test_must_fail git rev-parse --resolve-git-dir init/.git &&
 
@@ -484,8 +601,7 @@ test_expect_success 'update --init from subdirectory' '
        mkdir -p sub &&
        (
                cd sub &&
-               git submodule update ../init >update.out &&
-               cat update.out &&
+               git submodule update ../init 2>update.out &&
                test_i18ngrep "not initialized" update.out &&
                test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
 
@@ -758,7 +874,7 @@ test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.gi
                cp pristine-.git-config .git/config &&
                cp pristine-.gitmodules .gitmodules &&
                mkdir -p a/b/c &&
-               (cd a/b/c; git init) &&
+               (cd a/b/c && git init && test_commit msg) &&
                git config remote.origin.url ../foo/bar.git &&
                git submodule add ../bar/a/b/c ./a/b/c &&
                git submodule init &&
@@ -779,6 +895,21 @@ test_expect_success 'moving the superproject does not break submodules' '
        )
 '
 
+test_expect_success 'moving the submodule does not break the superproject' '
+       (
+               cd addtest2 &&
+               git submodule status
+       ) >actual &&
+       sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
+       mv addtest2/repo addtest2/repo.bak &&
+       test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
+       (
+               cd addtest2 &&
+               git submodule status
+       ) >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
        (
                cd addtest2 &&
@@ -807,7 +938,7 @@ test_expect_success 'submodule add --name allows to replace a submodule with ano
                echo "repo" >expect &&
                test_must_fail git config -f .gitmodules submodule.repo.path &&
                git config -f .gitmodules submodule.repo_new.path >actual &&
-               test_cmp expect actual&&
+               test_cmp expect actual &&
                echo "$submodurl/repo" >expect &&
                test_must_fail git config -f .gitmodules submodule.repo.url &&
                echo "$submodurl/bare.git" >expect &&
@@ -879,7 +1010,7 @@ test_expect_success 'submodule add with an existing name fails unless forced' '
                test -d repo &&
                echo "repo" >expect &&
                git config -f .gitmodules submodule.repo_new.path >actual &&
-               test_cmp expect actual&&
+               test_cmp expect actual &&
                echo "$submodurl/repo.git" >expect &&
                git config -f .gitmodules submodule.repo_new.url >actual &&
                test_cmp expect actual &&
@@ -902,7 +1033,7 @@ test_expect_success 'submodule deinit works on repository without submodules' '
                git init &&
                >file &&
                git add file &&
-               git commit -m "repo should not be empty"
+               git commit -m "repo should not be empty" &&
                git submodule deinit . &&
                git submodule deinit --all
        )
@@ -918,6 +1049,11 @@ test_expect_success 'submodule deinit should remove the whole submodule section
        rmdir init
 '
 
+test_expect_success 'submodule deinit should unset core.worktree' '
+       test_path_is_file .git/modules/example/config &&
+       test_must_fail git config -f .git/modules/example/config core.worktree
+'
+
 test_expect_success 'submodule deinit from subdirectory' '
        git submodule update --init &&
        git config submodule.example.foo bar &&
@@ -926,7 +1062,7 @@ test_expect_success 'submodule deinit from subdirectory' '
                cd sub &&
                git submodule deinit ../init >../output
        ) &&
-       grep "\\.\\./init" output &&
+       test_i18ngrep "\\.\\./init" output &&
        test -z "$(git config --get-regexp "submodule\.example\.")" &&
        test -n "$(git config --get-regexp "submodule\.example2\.")" &&
        test -f example2/.git &&
@@ -1100,5 +1236,171 @@ test_expect_success 'submodule helper list is not confused by common prefixes' '
        test_cmp expect actual
 '
 
+test_expect_success 'setup superproject with submodules' '
+       git init sub1 &&
+       test_commit -C sub1 test &&
+       test_commit -C sub1 test2 &&
+       git init multisuper &&
+       git -C multisuper submodule add ../sub1 sub0 &&
+       git -C multisuper submodule add ../sub1 sub1 &&
+       git -C multisuper submodule add ../sub1 sub2 &&
+       git -C multisuper submodule add ../sub1 sub3 &&
+       git -C multisuper commit -m "add some submodules"
+'
+
+cat >expect <<-EOF
+-sub0
+ sub1 (test2)
+ sub2 (test2)
+ sub3 (test2)
+EOF
+
+test_expect_success 'submodule update --init with a specification' '
+       test_when_finished "rm -rf multisuper_clone" &&
+       pwd=$(pwd) &&
+       git clone file://"$pwd"/multisuper multisuper_clone &&
+       git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
+       git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'submodule update --init with submodule.active set' '
+       test_when_finished "rm -rf multisuper_clone" &&
+       pwd=$(pwd) &&
+       git clone file://"$pwd"/multisuper multisuper_clone &&
+       git -C multisuper_clone config submodule.active "." &&
+       git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
+       git -C multisuper_clone submodule update --init &&
+       git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'submodule update and setting submodule.<name>.active' '
+       test_when_finished "rm -rf multisuper_clone" &&
+       pwd=$(pwd) &&
+       git clone file://"$pwd"/multisuper multisuper_clone &&
+       git -C multisuper_clone config --bool submodule.sub0.active "true" &&
+       git -C multisuper_clone config --bool submodule.sub1.active "false" &&
+       git -C multisuper_clone config --bool submodule.sub2.active "true" &&
+
+       cat >expect <<-\EOF &&
+        sub0 (test2)
+       -sub1
+        sub2 (test2)
+       -sub3
+       EOF
+       git -C multisuper_clone submodule update &&
+       git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'clone active submodule without submodule url set' '
+       test_when_finished "rm -rf test/test" &&
+       mkdir test &&
+       # another dir breaks accidental relative paths still being correct
+       git clone file://"$pwd"/multisuper test/test &&
+       (
+               cd test/test &&
+               git config submodule.active "." &&
+
+               # do not pass --init flag, as the submodule is already active:
+               git submodule update &&
+               git submodule status >actual_raw &&
+
+               cut -c 1,43- actual_raw >actual &&
+               cat >expect <<-\EOF &&
+                sub0 (test2)
+                sub1 (test2)
+                sub2 (test2)
+                sub3 (test2)
+               EOF
+               test_cmp expect actual
+       )
+'
+
+test_expect_success 'clone --recurse-submodules with a pathspec works' '
+       test_when_finished "rm -rf multisuper_clone" &&
+       cat >expected <<-\EOF &&
+        sub0 (test2)
+       -sub1
+       -sub2
+       -sub3
+       EOF
+
+       git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
+       git -C multisuper_clone submodule status |cut -c1,43- >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'clone with multiple --recurse-submodules options' '
+       test_when_finished "rm -rf multisuper_clone" &&
+       cat >expect <<-\EOF &&
+       -sub0
+        sub1 (test2)
+       -sub2
+        sub3 (test2)
+       EOF
+
+       git clone --recurse-submodules="." \
+                 --recurse-submodules=":(exclude)sub0" \
+                 --recurse-submodules=":(exclude)sub2" \
+                 multisuper multisuper_clone &&
+       git -C multisuper_clone submodule status |cut -c1,43- >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
+       test_when_finished "rm -rf multisuper_clone" &&
+       cat <<-\EOF >expect &&
+       -sub0
+        sub1 (test2)
+       -sub2
+        sub3 (test2)
+       EOF
+
+       cat <<-\EOF >expect2 &&
+       -sub0
+        sub1 (test2)
+       -sub2
+        sub3 (test2)
+       -sub4
+        sub5 (test2)
+       EOF
+
+       git clone --recurse-submodules="." \
+                 --recurse-submodules=":(exclude)sub0" \
+                 --recurse-submodules=":(exclude)sub2" \
+                 --recurse-submodules=":(exclude)sub4" \
+                 multisuper multisuper_clone &&
+
+       git -C multisuper_clone submodule status |cut -c1,43- >actual &&
+       test_cmp expect actual &&
+
+       git -C multisuper submodule add ../sub1 sub4 &&
+       git -C multisuper submodule add ../sub1 sub5 &&
+       git -C multisuper commit -m "add more submodules" &&
+       # obtain the new superproject
+       git -C multisuper_clone pull &&
+       git -C multisuper_clone submodule update --init &&
+       git -C multisuper_clone submodule status |cut -c1,43- >actual &&
+       test_cmp expect2 actual
+'
+
+test_expect_success 'init properly sets the config' '
+       test_when_finished "rm -rf multisuper_clone" &&
+       git clone --recurse-submodules="." \
+                 --recurse-submodules=":(exclude)sub0" \
+                 multisuper multisuper_clone &&
+
+       git -C multisuper_clone submodule init -- sub0 sub1 &&
+       git -C multisuper_clone config --get submodule.sub0.active &&
+       test_must_fail git -C multisuper_clone config --get submodule.sub1.active
+'
+
+test_expect_success 'recursive clone respects -q' '
+       test_when_finished "rm -rf multisuper_clone" &&
+       git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
+       test_must_be_empty actual
+'
 
 test_done