Imported Upstream version 2.27.0
[platform/upstream/git.git] / t / t7400-submodule-basic.sh
index dcac364..956e17a 100755 (executable)
@@ -46,14 +46,28 @@ test_expect_success 'submodule update aborts on missing gitmodules url' '
        test_must_fail git submodule init
 '
 
-test_expect_success 'configuration parsing' '
-       test_when_finished "rm -f .gitmodules" &&
-       cat >.gitmodules <<-\EOF &&
-       [submodule "s"]
-               path
-               ignore
+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' '
@@ -111,7 +125,6 @@ inspect() {
 
 test_expect_success 'submodule add' '
        echo "refs/heads/master" >expect &&
-       >empty &&
 
        (
                cd addtest &&
@@ -133,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 &&
@@ -163,24 +196,35 @@ test_expect_success 'submodule add to .gitignored path with --force' '
 test_expect_success 'submodule add to reconfigure existing submodule with --force' '
        (
                cd addtest-ignore &&
-               git submodule add --force bogus-url submod &&
-               git submodule add -b initial "$submodurl" submod-branch &&
-               test "bogus-url" = "$(git config -f .gitmodules submodule.submod.url)" &&
-               test "bogus-url" = "$(git config submodule.submod.url)" &&
+               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
+               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 &&
@@ -193,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 &&
@@ -210,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 &&
@@ -227,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 &&
@@ -244,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 &&
@@ -261,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 &&
@@ -278,7 +317,7 @@ 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' '
@@ -297,7 +336,6 @@ test_expect_success !CYGWIN 'submodule add with \\ in path' '
 
 test_expect_success 'submodule add in subdirectory' '
        echo "refs/heads/master" >expect &&
-       >empty &&
 
        mkdir addtest/sub &&
        (
@@ -310,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' '
@@ -346,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" &&
@@ -367,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' '
@@ -493,8 +561,6 @@ test_expect_success 'checkout superproject with subproject already present' '
 '
 
 test_expect_success 'apply submodule diff' '
-       >empty &&
-
        git branch second &&
        (
                cd init &&
@@ -509,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' '
@@ -519,7 +585,6 @@ test_expect_success 'update --init' '
        test_must_fail git config submodule.example.url &&
 
        git submodule update init 2> update.out &&
-       cat update.out &&
        test_i18ngrep "not initialized" update.out &&
        test_must_fail git rev-parse --resolve-git-dir init/.git &&
 
@@ -537,7 +602,6 @@ test_expect_success 'update --init from subdirectory' '
        (
                cd sub &&
                git submodule update ../init 2>update.out &&
-               cat update.out &&
                test_i18ngrep "not initialized" update.out &&
                test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
 
@@ -810,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 &&
@@ -831,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 &&
@@ -859,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 &&
@@ -931,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 &&
@@ -970,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 &&
@@ -1210,6 +1294,30 @@ test_expect_success 'submodule update and setting submodule.<name>.active' '
        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 &&
@@ -1221,7 +1329,7 @@ test_expect_success 'clone --recurse-submodules with a pathspec works' '
 
        git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
        git -C multisuper_clone submodule status |cut -c1,43- >actual &&
-       test_cmp actual expected
+       test_cmp expected actual
 '
 
 test_expect_success 'clone with multiple --recurse-submodules options' '
@@ -1289,4 +1397,10 @@ test_expect_success 'init properly sets the config' '
        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