Imported Upstream version 2.10.5
[platform/upstream/git.git] / t / t7400-submodule-basic.sh
index 5397037..b77cce8 100755 (executable)
@@ -11,6 +11,10 @@ subcommands of git submodule.
 
 . ./test-lib.sh
 
+test_expect_success 'submodule deinit works on empty repository' '
+       git submodule deinit --all
+'
+
 test_expect_success 'setup - initial commit' '
        >t &&
        git add t &&
@@ -18,6 +22,32 @@ test_expect_success 'setup - initial commit' '
        git branch initial
 '
 
+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 'configuration parsing' '
+       test_when_finished "rm -f .gitmodules" &&
+       cat >.gitmodules <<-\EOF &&
+       [submodule "s"]
+               path
+               ignore
+       EOF
+       test_must_fail git status
+'
+
 test_expect_success 'setup - repository in init subdirectory' '
        mkdir init &&
        (
@@ -78,7 +108,7 @@ test_expect_success 'submodule add' '
        (
                cd addtest &&
                git submodule add -q "$submodurl" submod >actual &&
-               test ! -s actual &&
+               test_must_be_empty actual &&
                echo "gitdir: ../.git/modules/submod" >expect &&
                test_cmp expect submod/.git &&
                (
@@ -133,6 +163,7 @@ test_expect_success 'submodule add --branch' '
        (
                cd addtest &&
                git submodule add -b initial "$submodurl" submod-branch &&
+               test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
                git submodule init
        ) &&
 
@@ -160,6 +191,23 @@ test_expect_success 'submodule add with ./ in path' '
        test_cmp empty untracked
 '
 
+test_expect_success 'submodule add with /././ in path' '
+       echo "refs/heads/master" >expect &&
+       >empty &&
+
+       (
+               cd addtest &&
+               git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
+               git submodule init
+       ) &&
+
+       rm -f heads head untracked &&
+       inspect addtest/dotslashdotsubmod/frotz ../../.. &&
+       test_cmp expect heads &&
+       test_cmp expect head &&
+       test_cmp empty untracked
+'
+
 test_expect_success 'submodule add with // in path' '
        echo "refs/heads/master" >expect &&
        >empty &&
@@ -211,9 +259,34 @@ test_expect_success 'submodule add with ./, /.. and // in path' '
        test_cmp empty untracked
 '
 
+test_expect_success 'submodule add in subdirectory' '
+       echo "refs/heads/master" >expect &&
+       >empty &&
+
+       mkdir addtest/sub &&
+       (
+               cd addtest/sub &&
+               git submodule add "$submodurl" ../realsubmod3 &&
+               git submodule init
+       ) &&
+
+       rm -f heads head untracked &&
+       inspect addtest/realsubmod3 ../.. &&
+       test_cmp expect heads &&
+       test_cmp expect head &&
+       test_cmp empty untracked
+'
+
+test_expect_success 'submodule add in subdirectory with relative path should fail' '
+       (
+               cd addtest/sub &&
+               test_must_fail git submodule add ../../ submod3 2>../../output.err
+       ) &&
+       test_i18ngrep toplevel output.err
+'
+
 test_expect_success 'setup - add an example entry to .gitmodules' '
-       GIT_CONFIG=.gitmodules \
-       git config submodule.example.url git://example.com/init.git
+       git config --file=.gitmodules submodule.example.url git://example.com/init.git
 '
 
 test_expect_success 'status should fail for unmapped paths' '
@@ -227,7 +300,7 @@ test_expect_success 'setup - map path in .gitmodules' '
        path = init
 EOF
 
-       GIT_CONFIG=.gitmodules git config submodule.example.path init &&
+       git config --file=.gitmodules submodule.example.path init &&
 
        test_cmp expect .gitmodules
 '
@@ -307,7 +380,7 @@ test_expect_success 'update should work when path is an empty dir' '
 
        mkdir init &&
        git submodule update -q >update.out &&
-       test ! -s update.out &&
+       test_must_be_empty update.out &&
 
        inspect init &&
        test_cmp expect head-sha1
@@ -318,6 +391,26 @@ test_expect_success 'status should be "up-to-date" after update' '
        grep "^ $rev1" list
 '
 
+test_expect_success 'status "up-to-date" from subdirectory' '
+       mkdir -p sub &&
+       (
+               cd sub &&
+               git submodule status >../list
+       ) &&
+       grep "^ $rev1" list &&
+       grep "\\.\\./init" list
+'
+
+test_expect_success 'status "up-to-date" from subdirectory with path' '
+       mkdir -p sub &&
+       (
+               cd sub &&
+               git submodule status ../init >../list
+       ) &&
+       grep "^ $rev1" list &&
+       grep "\\.\\./init" list
+'
+
 test_expect_success 'status should be "modified" after submodule commit' '
        (
                cd init &&
@@ -389,7 +482,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 &&
+       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 &&
@@ -398,6 +491,25 @@ test_expect_success 'update --init' '
        git rev-parse --resolve-git-dir init/.git
 '
 
+test_expect_success 'update --init from subdirectory' '
+       mv init init2 &&
+       git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
+       git config --remove-section submodule.example &&
+       test_must_fail git config submodule.example.url &&
+
+       mkdir -p sub &&
+       (
+               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 &&
+
+               git submodule update --init ../init
+       ) &&
+       git rev-parse --resolve-git-dir init/.git
+'
+
 test_expect_success 'do not add files from a submodule' '
 
        git reset --hard &&
@@ -405,7 +517,7 @@ test_expect_success 'do not add files from a submodule' '
 
 '
 
-test_expect_success 'gracefully add submodule with a trailing slash' '
+test_expect_success 'gracefully add/reset submodule with a trailing slash' '
 
        git reset --hard &&
        git commit -m "commit subproject" init &&
@@ -419,7 +531,9 @@ test_expect_success 'gracefully add submodule with a trailing slash' '
        git add init/ &&
        test_must_fail git diff --exit-code --cached init &&
        test $commit = $(git ls-files --stage |
-               sed -n "s/^160000 \([^ ]*\).*/\1/p")
+               sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
+       git reset init/ &&
+       git diff --exit-code --cached init
 
 '
 
@@ -672,7 +786,7 @@ test_expect_success 'moving the superproject does not break submodules' '
        (
                cd addtest &&
                git submodule status >expect
-       )
+       ) &&
        mv addtest addtest2 &&
        (
                cd addtest2 &&
@@ -681,4 +795,326 @@ test_expect_success 'moving the superproject does not break submodules' '
        )
 '
 
+test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
+       (
+               cd addtest2 &&
+               (
+                       cd repo &&
+                       echo "$submodurl/repo" >expect &&
+                       git config remote.origin.url >actual &&
+                       test_cmp expect actual &&
+                       echo "gitdir: ../.git/modules/repo" >expect &&
+                       test_cmp expect .git
+               ) &&
+               rm -rf repo &&
+               git rm repo &&
+               git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
+               test_must_be_empty actual &&
+               echo "gitdir: ../.git/modules/submod" >expect &&
+               test_cmp expect submod/.git &&
+               (
+                       cd repo &&
+                       echo "$submodurl/bare.git" >expect &&
+                       git config remote.origin.url >actual &&
+                       test_cmp expect actual &&
+                       echo "gitdir: ../.git/modules/repo_new" >expect &&
+                       test_cmp expect .git
+               ) &&
+               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&&
+               echo "$submodurl/repo" >expect &&
+               test_must_fail git config -f .gitmodules submodule.repo.url &&
+               echo "$submodurl/bare.git" >expect &&
+               git config -f .gitmodules submodule.repo_new.url >actual &&
+               test_cmp expect actual &&
+               echo "$submodurl/repo" >expect &&
+               git config submodule.repo.url >actual &&
+               test_cmp expect actual &&
+               echo "$submodurl/bare.git" >expect &&
+               git config submodule.repo_new.url >actual &&
+               test_cmp expect actual
+       )
+'
+
+test_expect_success 'recursive relative submodules stay relative' '
+       test_when_finished "rm -rf super clone2 subsub sub3" &&
+       mkdir subsub &&
+       (
+               cd subsub &&
+               git init &&
+               >t &&
+               git add t &&
+               git commit -m "initial commit"
+       ) &&
+       mkdir sub3 &&
+       (
+               cd sub3 &&
+               git init &&
+               >t &&
+               git add t &&
+               git commit -m "initial commit" &&
+               git submodule add ../subsub dirdir/subsub &&
+               git commit -m "add submodule subsub"
+       ) &&
+       mkdir super &&
+       (
+               cd super &&
+               git init &&
+               >t &&
+               git add t &&
+               git commit -m "initial commit" &&
+               git submodule add ../sub3 &&
+               git commit -m "add submodule sub"
+       ) &&
+       git clone super clone2 &&
+       (
+               cd clone2 &&
+               git submodule update --init --recursive &&
+               echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
+               echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
+       ) &&
+       test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
+       test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
+'
+
+test_expect_success 'submodule add with an existing name fails unless forced' '
+       (
+               cd addtest2 &&
+               rm -rf repo &&
+               git rm repo &&
+               test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
+               test ! -d repo &&
+               test_must_fail git config -f .gitmodules submodule.repo_new.path &&
+               test_must_fail git config -f .gitmodules submodule.repo_new.url &&
+               echo "$submodurl/bare.git" >expect &&
+               git config submodule.repo_new.url >actual &&
+               test_cmp expect actual &&
+               git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
+               test -d repo &&
+               echo "repo" >expect &&
+               git config -f .gitmodules submodule.repo_new.path >actual &&
+               test_cmp expect actual&&
+               echo "$submodurl/repo.git" >expect &&
+               git config -f .gitmodules submodule.repo_new.url >actual &&
+               test_cmp expect actual &&
+               echo "$submodurl/repo.git" >expect &&
+               git config submodule.repo_new.url >actual &&
+               test_cmp expect actual
+       )
+'
+
+test_expect_success 'set up a second submodule' '
+       git submodule add ./init2 example2 &&
+       git commit -m "submodule example2 added"
+'
+
+test_expect_success 'submodule deinit works on repository without submodules' '
+       test_when_finished "rm -rf newdirectory" &&
+       mkdir newdirectory &&
+       (
+               cd newdirectory &&
+               git init &&
+               >file &&
+               git add file &&
+               git commit -m "repo should not be empty" &&
+               git submodule deinit . &&
+               git submodule deinit --all
+       )
+'
+
+test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
+       git config submodule.example.foo bar &&
+       git config submodule.example2.frotz nitfol &&
+       git submodule deinit init &&
+       test -z "$(git config --get-regexp "submodule\.example\.")" &&
+       test -n "$(git config --get-regexp "submodule\.example2\.")" &&
+       test -f example2/.git &&
+       rmdir init
+'
+
+test_expect_success 'submodule deinit from subdirectory' '
+       git submodule update --init &&
+       git config submodule.example.foo bar &&
+       mkdir -p sub &&
+       (
+               cd sub &&
+               git submodule deinit ../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 &&
+       rmdir init
+'
+
+test_expect_success 'submodule deinit . deinits all initialized submodules' '
+       git submodule update --init &&
+       git config submodule.example.foo bar &&
+       git config submodule.example2.frotz nitfol &&
+       test_must_fail git submodule deinit &&
+       git submodule deinit . >actual &&
+       test -z "$(git config --get-regexp "submodule\.example\.")" &&
+       test -z "$(git config --get-regexp "submodule\.example2\.")" &&
+       test_i18ngrep "Cleared directory .init" actual &&
+       test_i18ngrep "Cleared directory .example2" actual &&
+       rmdir init example2
+'
+
+test_expect_success 'submodule deinit --all deinits all initialized submodules' '
+       git submodule update --init &&
+       git config submodule.example.foo bar &&
+       git config submodule.example2.frotz nitfol &&
+       test_must_fail git submodule deinit &&
+       git submodule deinit --all >actual &&
+       test -z "$(git config --get-regexp "submodule\.example\.")" &&
+       test -z "$(git config --get-regexp "submodule\.example2\.")" &&
+       test_i18ngrep "Cleared directory .init" actual &&
+       test_i18ngrep "Cleared directory .example2" actual &&
+       rmdir init example2
+'
+
+test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
+       git submodule update --init &&
+       rm -rf init example2/* example2/.git &&
+       git submodule deinit init example2 >actual &&
+       test -z "$(git config --get-regexp "submodule\.example\.")" &&
+       test -z "$(git config --get-regexp "submodule\.example2\.")" &&
+       test_i18ngrep ! "Cleared directory .init" actual &&
+       test_i18ngrep "Cleared directory .example2" actual &&
+       rmdir init
+'
+
+test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
+       git submodule update --init &&
+       echo X >>init/s &&
+       test_must_fail git submodule deinit init &&
+       test -n "$(git config --get-regexp "submodule\.example\.")" &&
+       test -f example2/.git &&
+       git submodule deinit -f init >actual &&
+       test -z "$(git config --get-regexp "submodule\.example\.")" &&
+       test_i18ngrep "Cleared directory .init" actual &&
+       rmdir init
+'
+
+test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
+       git submodule update --init &&
+       echo X >>init/untracked &&
+       test_must_fail git submodule deinit init &&
+       test -n "$(git config --get-regexp "submodule\.example\.")" &&
+       test -f example2/.git &&
+       git submodule deinit -f init >actual &&
+       test -z "$(git config --get-regexp "submodule\.example\.")" &&
+       test_i18ngrep "Cleared directory .init" actual &&
+       rmdir init
+'
+
+test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
+       git submodule update --init &&
+       (
+               cd init &&
+               git checkout HEAD^
+       ) &&
+       test_must_fail git submodule deinit init &&
+       test -n "$(git config --get-regexp "submodule\.example\.")" &&
+       test -f example2/.git &&
+       git submodule deinit -f init >actual &&
+       test -z "$(git config --get-regexp "submodule\.example\.")" &&
+       test_i18ngrep "Cleared directory .init" actual &&
+       rmdir init
+'
+
+test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
+       git submodule update --init &&
+       git submodule deinit init >actual &&
+       test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
+       test_i18ngrep "Cleared directory .init" actual &&
+       git submodule deinit init >actual &&
+       test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
+       test_i18ngrep "Cleared directory .init" actual &&
+       git submodule deinit . >actual &&
+       test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
+       test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
+       test_i18ngrep "Cleared directory .init" actual &&
+       git submodule deinit . >actual &&
+       test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
+       test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
+       test_i18ngrep "Cleared directory .init" actual &&
+       git submodule deinit --all >actual &&
+       test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
+       test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
+       test_i18ngrep "Cleared directory .init" actual &&
+       rmdir init example2
+'
+
+test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
+       git submodule update --init &&
+       (
+               cd init &&
+               rm .git &&
+               cp -R ../.git/modules/example .git &&
+               GIT_WORK_TREE=. git config --unset core.worktree
+       ) &&
+       test_must_fail git submodule deinit init &&
+       test_must_fail git submodule deinit -f init &&
+       test -d init/.git &&
+       test -n "$(git config --get-regexp "submodule\.example\.")"
+'
+
+test_expect_success 'submodule with UTF-8 name' '
+       svname=$(printf "\303\245 \303\244\303\266") &&
+       mkdir "$svname" &&
+       (
+               cd "$svname" &&
+               git init &&
+               >sub &&
+               git add sub &&
+               git commit -m "init sub"
+       ) &&
+       git submodule add ./"$svname" &&
+       git submodule >&2 &&
+       test -n "$(git submodule | grep "$svname")"
+'
+
+test_expect_success 'submodule add clone shallow submodule' '
+       mkdir super &&
+       pwd=$(pwd) &&
+       (
+               cd super &&
+               git init &&
+               git submodule add --depth=1 file://"$pwd"/example2 submodule &&
+               (
+                       cd submodule &&
+                       test 1 = $(git log --oneline | wc -l)
+               )
+       )
+'
+
+test_expect_success 'submodule helper list is not confused by common prefixes' '
+       mkdir -p dir1/b &&
+       (
+               cd dir1/b &&
+               git init &&
+               echo hi >testfile2 &&
+               git add . &&
+               git commit -m "test1"
+       ) &&
+       mkdir -p dir2/b &&
+       (
+               cd dir2/b &&
+               git init &&
+               echo hello >testfile1 &&
+               git add .  &&
+               git commit -m "test2"
+       ) &&
+       git submodule add /dir1/b dir1/b &&
+       git submodule add /dir2/b dir2/b &&
+       git commit -m "first submodule commit" &&
+       git submodule--helper list dir1/b |cut -c51- >actual &&
+       echo "dir1/b" >expect &&
+       test_cmp expect actual
+'
+
+
 test_done