3 # Copyright (c) 2007 Lars Hjemli
6 test_description
='Basic porcelain support for submodules
8 This test tries to verify basic sanity of the init, update and status
9 subcommands of git submodule.
14 test_expect_success
'submodule deinit works on empty repository' '
15 git submodule deinit --all
18 test_expect_success
'setup - initial commit' '
21 git commit -m "initial commit" &&
25 test_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
33 test_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
41 test_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
49 test_expect_success
'configuration parsing' '
50 test_when_finished "rm -f .gitmodules" &&
51 cat >.gitmodules <<-\EOF &&
56 test_must_fail git status
59 test_expect_success
'setup - repository in init subdirectory' '
66 git commit -m "submodule commit 1" &&
67 git tag -a -m "rev-1" rev-1
71 test_expect_success
'setup - commit with gitlink' '
75 git commit -m "super commit 1"
78 test_expect_success
'setup - hide init subdirectory' '
82 test_expect_success
'setup - repository to add submodules to' '
84 git init addtest-ignore
87 # The 'submodule add' tests need some repository to add as a submodule.
88 # The trash directory is a good one as any. We need to canonicalize
89 # the name, though, as some tests compare it to the absolute path git
90 # generates, which will expand symbolic links.
94 git for-each-ref
--format='%(refname)' 'refs/heads/*'
103 listbranches
>"$dotdot/heads" &&
104 { git symbolic-ref HEAD ||
:; } >"$dotdot/head" &&
105 git rev-parse HEAD
>"$dotdot/head-sha1" &&
106 git update-index
--refresh &&
107 git diff-files
--exit-code &&
108 git clean
-n -d -x >"$dotdot/untracked"
112 test_expect_success
'submodule add' '
113 echo "refs/heads/master" >expect &&
118 git submodule add -q "$submodurl" submod >actual &&
119 test_must_be_empty actual &&
120 echo "gitdir: ../.git/modules/submod" >expect &&
121 test_cmp expect submod/.git &&
124 git config core.worktree >actual &&
125 echo "../../../submod" >expect &&
126 test_cmp expect actual &&
132 rm -f heads head untracked &&
133 inspect addtest/submod ../.. &&
134 test_cmp expect heads &&
135 test_cmp expect head &&
136 test_cmp empty untracked
139 test_expect_success
'submodule add to .gitignored path fails' '
142 cat <<-\EOF >expect &&
143 The following path is ignored by one of your .gitignore files:
145 Use -f if you really want to add it.
147 # Does not use test_commit due to the ignore
148 echo "*" > .gitignore &&
149 git add --force .gitignore &&
150 git commit -m"Ignore everything" &&
151 ! git submodule add "$submodurl" submod >actual 2>&1 &&
152 test_i18ncmp expect actual
156 test_expect_success
'submodule add to .gitignored path with --force' '
159 git submodule add --force "$submodurl" submod
163 test_expect_success
'submodule add to reconfigure existing submodule with --force' '
166 git submodule add --force bogus-url submod &&
167 git submodule add -b initial "$submodurl" submod-branch &&
168 test "bogus-url" = "$(git config -f .gitmodules submodule.submod.url)" &&
169 test "bogus-url" = "$(git config submodule.submod.url)" &&
171 git submodule add --force "$submodurl" submod
172 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
173 test "$submodurl" = "$(git config submodule.submod.url)"
177 test_expect_success
'submodule add --branch' '
178 echo "refs/heads/initial" >expect-head &&
179 cat <<-\EOF >expect-heads &&
187 git submodule add -b initial "$submodurl" submod-branch &&
188 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
192 rm -f heads head untracked &&
193 inspect addtest/submod-branch ../.. &&
194 test_cmp expect-heads heads &&
195 test_cmp expect-head head &&
196 test_cmp empty untracked
199 test_expect_success
'submodule add with ./ in path' '
200 echo "refs/heads/master" >expect &&
205 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
209 rm -f heads head untracked &&
210 inspect addtest/dotsubmod/frotz ../../.. &&
211 test_cmp expect heads &&
212 test_cmp expect head &&
213 test_cmp empty untracked
216 test_expect_success
'submodule add with /././ in path' '
217 echo "refs/heads/master" >expect &&
222 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
226 rm -f heads head untracked &&
227 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
228 test_cmp expect heads &&
229 test_cmp expect head &&
230 test_cmp empty untracked
233 test_expect_success
'submodule add with // in path' '
234 echo "refs/heads/master" >expect &&
239 git submodule add "$submodurl" slashslashsubmod///frotz// &&
243 rm -f heads head untracked &&
244 inspect addtest/slashslashsubmod/frotz ../../.. &&
245 test_cmp expect heads &&
246 test_cmp expect head &&
247 test_cmp empty untracked
250 test_expect_success
'submodule add with /.. in path' '
251 echo "refs/heads/master" >expect &&
256 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
260 rm -f heads head untracked &&
261 inspect addtest/realsubmod ../.. &&
262 test_cmp expect heads &&
263 test_cmp expect head &&
264 test_cmp empty untracked
267 test_expect_success
'submodule add with ./, /.. and // in path' '
268 echo "refs/heads/master" >expect &&
273 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
277 rm -f heads head untracked &&
278 inspect addtest/realsubmod2 ../.. &&
279 test_cmp expect heads &&
280 test_cmp expect head &&
281 test_cmp empty untracked
284 test_expect_success
'submodule add with \\ in path' '
285 test_when_finished "rm -rf parent sub\\with\\backslash" &&
287 # Initialize a repo with a backslash in its name
288 git init sub\\with\\backslash &&
289 touch sub\\with\\backslash/empty.file &&
290 git -C sub\\with\\backslash add empty.file &&
291 git -C sub\\with\\backslash commit -m "Added empty.file" &&
293 # Add that repository as a submodule
295 git -C parent submodule add ../sub\\with\\backslash
298 test_expect_success
'submodule add in subdirectory' '
299 echo "refs/heads/master" >expect &&
305 git submodule add "$submodurl" ../realsubmod3 &&
309 rm -f heads head untracked &&
310 inspect addtest/realsubmod3 ../.. &&
311 test_cmp expect heads &&
312 test_cmp expect head &&
313 test_cmp empty untracked
316 test_expect_success
'submodule add in subdirectory with relative path should fail' '
319 test_must_fail git submodule add ../../ submod3 2>../../output.err
321 test_i18ngrep toplevel output.err
324 test_expect_success
'setup - add an example entry to .gitmodules' '
325 git config --file=.gitmodules submodule.example.url git://example.com/init.git
328 test_expect_success
'status should fail for unmapped paths' '
329 test_must_fail git submodule status
332 test_expect_success
'setup - map path in .gitmodules' '
333 cat <<\EOF >expect &&
334 [submodule "example"]
335 url = git://example.com/init.git
339 git config --file=.gitmodules submodule.example.path init &&
341 test_cmp expect .gitmodules
344 test_expect_success
'status should only print one line' '
345 git submodule status >lines &&
346 test_line_count = 1 lines
349 test_expect_success
'setup - fetch commit name from submodule' '
350 rev1=$(cd .subrepo && git rev-parse HEAD) &&
351 printf "rev1: %s\n" "$rev1" &&
355 test_expect_success
'status should initially be "missing"' '
356 git submodule status >lines &&
360 test_expect_success
'init should register submodule url in .git/config' '
361 echo git://example.com/init.git >expect &&
363 git submodule init &&
364 git config submodule.example.url >url &&
365 git config submodule.example.url ./.subrepo &&
370 test_failure_with_unknown_submodule
() {
371 test_must_fail git submodule
$1 no-such-submodule
2>output.err
&&
372 grep "^error: .*no-such-submodule" output.err
375 test_expect_success
'init should fail with unknown submodule' '
376 test_failure_with_unknown_submodule init
379 test_expect_success
'update should fail with unknown submodule' '
380 test_failure_with_unknown_submodule update
383 test_expect_success
'status should fail with unknown submodule' '
384 test_failure_with_unknown_submodule status
387 test_expect_success
'sync should fail with unknown submodule' '
388 test_failure_with_unknown_submodule sync
391 test_expect_success
'update should fail when path is used by a file' '
392 echo hello >expect &&
394 echo "hello" >init &&
395 test_must_fail git submodule update &&
400 test_expect_success
'update should fail when path is used by a nonempty directory' '
401 echo hello >expect &&
405 echo "hello" >init/a &&
407 test_must_fail git submodule update &&
409 test_cmp expect init/a
412 test_expect_success
'update should work when path is an empty dir' '
415 echo "$rev1" >expect &&
418 git submodule update -q >update.out &&
419 test_must_be_empty update.out &&
422 test_cmp expect head-sha1
425 test_expect_success
'status should be "up-to-date" after update' '
426 git submodule status >list &&
430 test_expect_success
'status "up-to-date" from subdirectory' '
434 git submodule status >../list
436 grep "^ $rev1" list &&
437 grep "\\.\\./init" list
440 test_expect_success
'status "up-to-date" from subdirectory with path' '
444 git submodule status ../init >../list
446 grep "^ $rev1" list &&
447 grep "\\.\\./init" list
450 test_expect_success
'status should be "modified" after submodule commit' '
455 git commit -m "submodule commit 2"
458 rev2=$(cd init && git rev-parse HEAD) &&
460 git submodule status >list &&
465 test_expect_success
'the --cached sha1 should be rev1' '
466 git submodule --cached status >list &&
470 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
472 grep "^+Subproject commit $rev2" diff
475 test_expect_success
'update should checkout rev1' '
477 echo "$rev1" >expect &&
479 git submodule update init &&
482 test_cmp expect head-sha1
485 test_expect_success
'status should be "up-to-date" after update' '
486 git submodule status >list &&
490 test_expect_success
'checkout superproject with subproject already present' '
491 git checkout initial &&
495 test_expect_success
'apply submodule diff' '
503 git commit -m "change subproject"
505 git update-index --add init &&
506 git commit -m "change init" &&
507 git format-patch -1 --stdout >P.diff &&
508 git checkout second &&
509 git apply --index P.diff &&
511 git diff --cached master >staged &&
512 test_cmp empty staged
515 test_expect_success
'update --init' '
517 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
518 git config --remove-section submodule.example &&
519 test_must_fail git config submodule.example.url &&
521 git submodule update init 2> update.out &&
523 test_i18ngrep "not initialized" update.out &&
524 test_must_fail git rev-parse --resolve-git-dir init/.git &&
526 git submodule update --init init &&
527 git rev-parse --resolve-git-dir init/.git
530 test_expect_success
'update --init from subdirectory' '
532 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
533 git config --remove-section submodule.example &&
534 test_must_fail git config submodule.example.url &&
539 git submodule update ../init 2>update.out &&
541 test_i18ngrep "not initialized" update.out &&
542 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
544 git submodule update --init ../init
546 git rev-parse --resolve-git-dir init/.git
549 test_expect_success
'do not add files from a submodule' '
552 test_must_fail git add init/a
556 test_expect_success
'gracefully add/reset submodule with a trailing slash' '
559 git commit -m "commit subproject" init &&
563 git diff --exit-code --cached init &&
565 git commit -m update a >/dev/null &&
566 git rev-parse HEAD) &&
568 test_must_fail git diff --exit-code --cached init &&
569 test $commit = $(git ls-files --stage |
570 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
572 git diff --exit-code --cached init
576 test_expect_success
'ls-files gracefully handles trailing slash' '
578 test "init" = "$(git ls-files init/)"
582 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
586 git checkout initial &&
591 test_expect_success
'submodule <invalid-subcommand> fails' '
592 test_must_fail git submodule no-such-subcommand
595 test_expect_success
'add submodules without specifying an explicit path' '
602 git commit -m "repo commit 1"
604 git clone --bare repo/ bare.git &&
607 git submodule add "$submodurl/repo" &&
608 git config -f .gitmodules submodule.repo.path repo &&
609 git submodule add "$submodurl/bare.git" &&
610 git config -f .gitmodules submodule.bare.path bare
614 test_expect_success
'add should fail when path is used by a file' '
618 test_must_fail git submodule add "$submodurl/repo" file
622 test_expect_success
'add should fail when path is used by an existing directory' '
626 test_must_fail git submodule add "$submodurl/repo" empty-dir
630 test_expect_success
'use superproject as upstream when path is relative and no url is set there' '
633 git submodule add ../repo relative &&
634 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
635 git submodule sync relative &&
636 test "$(git config submodule.relative.url)" = "$submodurl/repo"
640 test_expect_success
'set up for relative path tests' '
652 git config -f .gitmodules submodule.sub.path sub &&
653 git config -f .gitmodules submodule.sub.url ../subrepo &&
654 cp .git/config pristine-.git-config &&
655 cp .gitmodules pristine-.gitmodules
659 test_expect_success
'../subrepo works with URL - ssh://hostname/repo' '
662 cp pristine-.git-config .git/config &&
663 cp pristine-.gitmodules .gitmodules &&
664 git config remote.origin.url ssh://hostname/repo &&
665 git submodule init &&
666 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
670 test_expect_success
'../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
673 cp pristine-.git-config .git/config &&
674 cp pristine-.gitmodules .gitmodules &&
675 git config remote.origin.url ssh://hostname:22/repo &&
676 git submodule init &&
677 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
681 # About the choice of the path in the next test:
682 # - double-slash side-steps path mangling issues on Windows
683 # - it is still an absolute local path
684 # - there cannot be a server with a blank in its name just in case the
685 # path is used erroneously to access a //server/share style path
686 test_expect_success
'../subrepo path works with local path - //somewhere else/repo' '
689 cp pristine-.git-config .git/config &&
690 cp pristine-.gitmodules .gitmodules &&
691 git config remote.origin.url "//somewhere else/repo" &&
692 git submodule init &&
693 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
697 test_expect_success
'../subrepo works with file URL - file:///tmp/repo' '
700 cp pristine-.git-config .git/config &&
701 cp pristine-.gitmodules .gitmodules &&
702 git config remote.origin.url file:///tmp/repo &&
703 git submodule init &&
704 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
708 test_expect_success
'../subrepo works with helper URL- helper:://hostname/repo' '
711 cp pristine-.git-config .git/config &&
712 cp pristine-.gitmodules .gitmodules &&
713 git config remote.origin.url helper:://hostname/repo &&
714 git submodule init &&
715 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
719 test_expect_success
'../subrepo works with scp-style URL - user@host:repo' '
722 cp pristine-.git-config .git/config &&
723 git config remote.origin.url user@host:repo &&
724 git submodule init &&
725 test "$(git config submodule.sub.url)" = user@host:subrepo
729 test_expect_success
'../subrepo works with scp-style URL - user@host:path/to/repo' '
732 cp pristine-.git-config .git/config &&
733 cp pristine-.gitmodules .gitmodules &&
734 git config remote.origin.url user@host:path/to/repo &&
735 git submodule init &&
736 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
740 test_expect_success
'../subrepo works with relative local path - foo' '
743 cp pristine-.git-config .git/config &&
744 cp pristine-.gitmodules .gitmodules &&
745 git config remote.origin.url foo &&
746 # actual: fails with an error
747 git submodule init &&
748 test "$(git config submodule.sub.url)" = subrepo
752 test_expect_success
'../subrepo works with relative local path - foo/bar' '
755 cp pristine-.git-config .git/config &&
756 cp pristine-.gitmodules .gitmodules &&
757 git config remote.origin.url foo/bar &&
758 git submodule init &&
759 test "$(git config submodule.sub.url)" = foo/subrepo
763 test_expect_success
'../subrepo works with relative local path - ./foo' '
766 cp pristine-.git-config .git/config &&
767 cp pristine-.gitmodules .gitmodules &&
768 git config remote.origin.url ./foo &&
769 git submodule init &&
770 test "$(git config submodule.sub.url)" = subrepo
774 test_expect_success
'../subrepo works with relative local path - ./foo/bar' '
777 cp pristine-.git-config .git/config &&
778 cp pristine-.gitmodules .gitmodules &&
779 git config remote.origin.url ./foo/bar &&
780 git submodule init &&
781 test "$(git config submodule.sub.url)" = foo/subrepo
785 test_expect_success
'../subrepo works with relative local path - ../foo' '
788 cp pristine-.git-config .git/config &&
789 cp pristine-.gitmodules .gitmodules &&
790 git config remote.origin.url ../foo &&
791 git submodule init &&
792 test "$(git config submodule.sub.url)" = ../subrepo
796 test_expect_success
'../subrepo works with relative local path - ../foo/bar' '
799 cp pristine-.git-config .git/config &&
800 cp pristine-.gitmodules .gitmodules &&
801 git config remote.origin.url ../foo/bar &&
802 git submodule init &&
803 test "$(git config submodule.sub.url)" = ../foo/subrepo
807 test_expect_success
'../bar/a/b/c works with relative local path - ../foo/bar.git' '
810 cp pristine-.git-config .git/config &&
811 cp pristine-.gitmodules .gitmodules &&
813 (cd a/b/c; git init) &&
814 git config remote.origin.url ../foo/bar.git &&
815 git submodule add ../bar/a/b/c ./a/b/c &&
816 git submodule init &&
817 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
821 test_expect_success
'moving the superproject does not break submodules' '
824 git submodule status >expect
826 mv addtest addtest2 &&
829 git submodule status >actual &&
830 test_cmp expect actual
834 test_expect_success
'submodule add --name allows to replace a submodule with another at the same path' '
839 echo "$submodurl/repo" >expect &&
840 git config remote.origin.url >actual &&
841 test_cmp expect actual &&
842 echo "gitdir: ../.git/modules/repo" >expect &&
847 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
848 test_must_be_empty actual &&
849 echo "gitdir: ../.git/modules/submod" >expect &&
850 test_cmp expect submod/.git &&
853 echo "$submodurl/bare.git" >expect &&
854 git config remote.origin.url >actual &&
855 test_cmp expect actual &&
856 echo "gitdir: ../.git/modules/repo_new" >expect &&
859 echo "repo" >expect &&
860 test_must_fail git config -f .gitmodules submodule.repo.path &&
861 git config -f .gitmodules submodule.repo_new.path >actual &&
862 test_cmp expect actual&&
863 echo "$submodurl/repo" >expect &&
864 test_must_fail git config -f .gitmodules submodule.repo.url &&
865 echo "$submodurl/bare.git" >expect &&
866 git config -f .gitmodules submodule.repo_new.url >actual &&
867 test_cmp expect actual &&
868 echo "$submodurl/repo" >expect &&
869 git config submodule.repo.url >actual &&
870 test_cmp expect actual &&
871 echo "$submodurl/bare.git" >expect &&
872 git config submodule.repo_new.url >actual &&
873 test_cmp expect actual
877 test_expect_success
'recursive relative submodules stay relative' '
878 test_when_finished "rm -rf super clone2 subsub sub3" &&
885 git commit -m "initial commit"
893 git commit -m "initial commit" &&
894 git submodule add ../subsub dirdir/subsub &&
895 git commit -m "add submodule subsub"
903 git commit -m "initial commit" &&
904 git submodule add ../sub3 &&
905 git commit -m "add submodule sub"
907 git clone super clone2 &&
910 git submodule update --init --recursive &&
911 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
912 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
914 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
915 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
918 test_expect_success
'submodule add with an existing name fails unless forced' '
923 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
925 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
926 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
927 echo "$submodurl/bare.git" >expect &&
928 git config submodule.repo_new.url >actual &&
929 test_cmp expect actual &&
930 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
932 echo "repo" >expect &&
933 git config -f .gitmodules submodule.repo_new.path >actual &&
934 test_cmp expect actual&&
935 echo "$submodurl/repo.git" >expect &&
936 git config -f .gitmodules submodule.repo_new.url >actual &&
937 test_cmp expect actual &&
938 echo "$submodurl/repo.git" >expect &&
939 git config submodule.repo_new.url >actual &&
940 test_cmp expect actual
944 test_expect_success
'set up a second submodule' '
945 git submodule add ./init2 example2 &&
946 git commit -m "submodule example2 added"
949 test_expect_success
'submodule deinit works on repository without submodules' '
950 test_when_finished "rm -rf newdirectory" &&
951 mkdir newdirectory &&
957 git commit -m "repo should not be empty" &&
958 git submodule deinit . &&
959 git submodule deinit --all
963 test_expect_success
'submodule deinit should remove the whole submodule section from .git/config' '
964 git config submodule.example.foo bar &&
965 git config submodule.example2.frotz nitfol &&
966 git submodule deinit init &&
967 test -z "$(git config --get-regexp "submodule\.example\.")" &&
968 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
969 test -f example2/.git &&
973 test_expect_success
'submodule deinit from subdirectory' '
974 git submodule update --init &&
975 git config submodule.example.foo bar &&
979 git submodule deinit ../init >../output
981 test_i18ngrep "\\.\\./init" output &&
982 test -z "$(git config --get-regexp "submodule\.example\.")" &&
983 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
984 test -f example2/.git &&
988 test_expect_success
'submodule deinit . deinits all initialized submodules' '
989 git submodule update --init &&
990 git config submodule.example.foo bar &&
991 git config submodule.example2.frotz nitfol &&
992 test_must_fail git submodule deinit &&
993 git submodule deinit . >actual &&
994 test -z "$(git config --get-regexp "submodule\.example\.")" &&
995 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
996 test_i18ngrep "Cleared directory .init" actual &&
997 test_i18ngrep "Cleared directory .example2" actual &&
1001 test_expect_success
'submodule deinit --all deinits all initialized submodules' '
1002 git submodule update --init &&
1003 git config submodule.example.foo bar &&
1004 git config submodule.example2.frotz nitfol &&
1005 test_must_fail git submodule deinit &&
1006 git submodule deinit --all >actual &&
1007 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1008 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1009 test_i18ngrep "Cleared directory .init" actual &&
1010 test_i18ngrep "Cleared directory .example2" actual &&
1014 test_expect_success
'submodule deinit deinits a submodule when its work tree is missing or empty' '
1015 git submodule update --init &&
1016 rm -rf init example2/* example2/.git &&
1017 git submodule deinit init example2 >actual &&
1018 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1019 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1020 test_i18ngrep ! "Cleared directory .init" actual &&
1021 test_i18ngrep "Cleared directory .example2" actual &&
1025 test_expect_success
'submodule deinit fails when the submodule contains modifications unless forced' '
1026 git submodule update --init &&
1028 test_must_fail git submodule deinit init &&
1029 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1030 test -f example2/.git &&
1031 git submodule deinit -f init >actual &&
1032 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1033 test_i18ngrep "Cleared directory .init" actual &&
1037 test_expect_success
'submodule deinit fails when the submodule contains untracked files unless forced' '
1038 git submodule update --init &&
1039 echo X >>init/untracked &&
1040 test_must_fail git submodule deinit init &&
1041 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1042 test -f example2/.git &&
1043 git submodule deinit -f init >actual &&
1044 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1045 test_i18ngrep "Cleared directory .init" actual &&
1049 test_expect_success
'submodule deinit fails when the submodule HEAD does not match unless forced' '
1050 git submodule update --init &&
1055 test_must_fail git submodule deinit init &&
1056 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1057 test -f example2/.git &&
1058 git submodule deinit -f init >actual &&
1059 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1060 test_i18ngrep "Cleared directory .init" actual &&
1064 test_expect_success
'submodule deinit is silent when used on an uninitialized submodule' '
1065 git submodule update --init &&
1066 git submodule deinit init >actual &&
1067 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1068 test_i18ngrep "Cleared directory .init" actual &&
1069 git submodule deinit init >actual &&
1070 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1071 test_i18ngrep "Cleared directory .init" actual &&
1072 git submodule deinit . >actual &&
1073 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1074 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1075 test_i18ngrep "Cleared directory .init" actual &&
1076 git submodule deinit . >actual &&
1077 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1078 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1079 test_i18ngrep "Cleared directory .init" actual &&
1080 git submodule deinit --all >actual &&
1081 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1082 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1083 test_i18ngrep "Cleared directory .init" actual &&
1087 test_expect_success
'submodule deinit fails when submodule has a .git directory even when forced' '
1088 git submodule update --init &&
1092 cp -R ../.git/modules/example .git &&
1093 GIT_WORK_TREE=. git config --unset core.worktree
1095 test_must_fail git submodule deinit init &&
1096 test_must_fail git submodule deinit -f init &&
1097 test -d init/.git &&
1098 test -n "$(git config --get-regexp "submodule\.example\.")"
1101 test_expect_success
'submodule with UTF-8 name' '
1102 svname=$(printf "\303\245 \303\244\303\266") &&
1109 git commit -m "init sub"
1111 git submodule add ./"$svname" &&
1112 git submodule >&2 &&
1113 test -n "$(git submodule | grep "$svname")"
1116 test_expect_success
'submodule add clone shallow submodule' '
1122 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1125 test 1 = $(git log --oneline | wc -l)
1130 test_expect_success
'submodule helper list is not confused by common prefixes' '
1135 echo hi >testfile2 &&
1137 git commit -m "test1"
1143 echo hello >testfile1 &&
1145 git commit -m "test2"
1147 git submodule add /dir1/b dir1/b &&
1148 git submodule add /dir2/b dir2/b &&
1149 git commit -m "first submodule commit" &&
1150 git submodule--helper list dir1/b |cut -c51- >actual &&
1151 echo "dir1/b" >expect &&
1152 test_cmp expect actual
1155 test_expect_success
'setup superproject with submodules' '
1157 test_commit -C sub1 test &&
1158 test_commit -C sub1 test2 &&
1159 git init multisuper &&
1160 git -C multisuper submodule add ../sub1 sub0 &&
1161 git -C multisuper submodule add ../sub1 sub1 &&
1162 git -C multisuper submodule add ../sub1 sub2 &&
1163 git -C multisuper submodule add ../sub1 sub3 &&
1164 git -C multisuper commit -m "add some submodules"
1174 test_expect_success
'submodule update --init with a specification' '
1175 test_when_finished "rm -rf multisuper_clone" &&
1177 git clone file://"$pwd"/multisuper multisuper_clone &&
1178 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1179 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1180 test_cmp expect actual
1183 test_expect_success
'submodule update --init with submodule.active set' '
1184 test_when_finished "rm -rf multisuper_clone" &&
1186 git clone file://"$pwd"/multisuper multisuper_clone &&
1187 git -C multisuper_clone config submodule.active "." &&
1188 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1189 git -C multisuper_clone submodule update --init &&
1190 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1191 test_cmp expect actual
1194 test_expect_success
'submodule update and setting submodule.<name>.active' '
1195 test_when_finished "rm -rf multisuper_clone" &&
1197 git clone file://"$pwd"/multisuper multisuper_clone &&
1198 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1199 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1200 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1202 cat >expect <<-\EOF &&
1208 git -C multisuper_clone submodule update &&
1209 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1210 test_cmp expect actual
1213 test_expect_success
'clone --recurse-submodules with a pathspec works' '
1214 test_when_finished "rm -rf multisuper_clone" &&
1215 cat >expected <<-\EOF &&
1222 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1223 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1224 test_cmp actual expected
1227 test_expect_success
'clone with multiple --recurse-submodules options' '
1228 test_when_finished "rm -rf multisuper_clone" &&
1229 cat >expect <<-\EOF &&
1236 git clone --recurse-submodules="." \
1237 --recurse-submodules=":(exclude)sub0" \
1238 --recurse-submodules=":(exclude)sub2" \
1239 multisuper multisuper_clone &&
1240 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1241 test_cmp expect actual
1244 test_expect_success
'clone and subsequent updates correctly auto-initialize submodules' '
1245 test_when_finished "rm -rf multisuper_clone" &&
1246 cat <<-\EOF >expect &&
1253 cat <<-\EOF >expect2 &&
1262 git clone --recurse-submodules="." \
1263 --recurse-submodules=":(exclude)sub0" \
1264 --recurse-submodules=":(exclude)sub2" \
1265 --recurse-submodules=":(exclude)sub4" \
1266 multisuper multisuper_clone &&
1268 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1269 test_cmp expect actual &&
1271 git -C multisuper submodule add ../sub1 sub4 &&
1272 git -C multisuper submodule add ../sub1 sub5 &&
1273 git -C multisuper commit -m "add more submodules" &&
1274 # obtain the new superproject
1275 git -C multisuper_clone pull &&
1276 git -C multisuper_clone submodule update --init &&
1277 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1278 test_cmp expect2 actual
1281 test_expect_success
'init properly sets the config' '
1282 test_when_finished "rm -rf multisuper_clone" &&
1283 git clone --recurse-submodules="." \
1284 --recurse-submodules=":(exclude)sub0" \
1285 multisuper multisuper_clone &&
1287 git -C multisuper_clone submodule init -- sub0 sub1 &&
1288 git -C multisuper_clone config --get submodule.sub0.active &&
1289 test_must_fail git -C multisuper_clone config --get submodule.sub1.active