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 'add aborts on repository with no commits' '
50 cat >expect <<-\EOF &&
51 '"'repo-no-commits'"' does not have a commit checked out
53 git init repo-no-commits &&
54 test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
55 test_i18ncmp expect actual
58 test_expect_success 'setup - repository in init subdirectory' '
65 git commit -m "submodule commit 1" &&
66 git tag -a -m "rev-1" rev-1
70 test_expect_success 'setup - commit with gitlink' '
74 git commit -m "super commit 1"
77 test_expect_success 'setup - hide init subdirectory' '
81 test_expect_success 'setup - repository to add submodules to' '
83 git init addtest-ignore
86 # The 'submodule add' tests need some repository to add as a submodule.
87 # The trash directory is a good one as any. We need to canonicalize
88 # the name, though, as some tests compare it to the absolute path git
89 # generates, which will expand symbolic links.
93 git for-each-ref --format='%(refname)' 'refs/heads/*'
102 listbranches >"$dotdot/heads" &&
103 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
104 git rev-parse HEAD >"$dotdot/head-sha1" &&
105 git update-index --refresh &&
106 git diff-files --exit-code &&
107 git clean -n -d -x >"$dotdot/untracked"
111 test_expect_success 'submodule add' '
112 echo "refs/heads/master" >expect &&
116 git submodule add -q "$submodurl" submod >actual &&
117 test_must_be_empty actual &&
118 echo "gitdir: ../.git/modules/submod" >expect &&
119 test_cmp expect submod/.git &&
122 git config core.worktree >actual &&
123 echo "../../../submod" >expect &&
124 test_cmp expect actual &&
130 rm -f heads head untracked &&
131 inspect addtest/submod ../.. &&
132 test_cmp expect heads &&
133 test_cmp expect head &&
134 test_must_be_empty untracked
137 test_expect_success 'setup parent and one repository' '
138 test_create_repo parent &&
139 test_commit -C parent one
142 test_expect_success 'redirected submodule add does not show progress' '
143 git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
146 test_i18ngrep ! "Checking connectivity" err
149 test_expect_success 'redirected submodule add --progress does show progress' '
150 git -C addtest submodule add --progress "file://$submodurl/parent" \
151 submod-redirected-progress 2>err && \
155 test_expect_success 'submodule add to .gitignored path fails' '
158 cat <<-\EOF >expect &&
159 The following paths are ignored by one of your .gitignore files:
161 Use -f if you really want to add them.
163 # Does not use test_commit due to the ignore
164 echo "*" > .gitignore &&
165 git add --force .gitignore &&
166 git commit -m"Ignore everything" &&
167 ! git submodule add "$submodurl" submod >actual 2>&1 &&
168 test_i18ncmp expect actual
172 test_expect_success 'submodule add to .gitignored path with --force' '
175 git submodule add --force "$submodurl" submod
179 test_expect_success 'submodule add to reconfigure existing submodule with --force' '
182 bogus_url="$(pwd)/bogus-url" &&
183 git submodule add --force "$bogus_url" submod &&
184 git submodule add --force -b initial "$submodurl" submod-branch &&
185 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
186 test "$bogus_url" = "$(git config submodule.submod.url)" &&
188 git submodule add --force "$submodurl" submod &&
189 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
190 test "$submodurl" = "$(git config submodule.submod.url)"
194 test_expect_success 'submodule add relays add --dry-run stderr' '
195 test_when_finished "rm -rf addtest/.git/index.lock" &&
198 : >.git/index.lock &&
199 ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
200 test_i18ngrep "^fatal: .*index\.lock" output.err &&
201 test_path_is_missing sub-while-locked
205 test_expect_success 'submodule add --branch' '
206 echo "refs/heads/initial" >expect-head &&
207 cat <<-\EOF >expect-heads &&
214 git submodule add -b initial "$submodurl" submod-branch &&
215 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
219 rm -f heads head untracked &&
220 inspect addtest/submod-branch ../.. &&
221 test_cmp expect-heads heads &&
222 test_cmp expect-head head &&
223 test_must_be_empty untracked
226 test_expect_success 'submodule add with ./ in path' '
227 echo "refs/heads/master" >expect &&
231 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
235 rm -f heads head untracked &&
236 inspect addtest/dotsubmod/frotz ../../.. &&
237 test_cmp expect heads &&
238 test_cmp expect head &&
239 test_must_be_empty untracked
242 test_expect_success 'submodule add with /././ in path' '
243 echo "refs/heads/master" >expect &&
247 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
251 rm -f heads head untracked &&
252 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
253 test_cmp expect heads &&
254 test_cmp expect head &&
255 test_must_be_empty untracked
258 test_expect_success 'submodule add with // in path' '
259 echo "refs/heads/master" >expect &&
263 git submodule add "$submodurl" slashslashsubmod///frotz// &&
267 rm -f heads head untracked &&
268 inspect addtest/slashslashsubmod/frotz ../../.. &&
269 test_cmp expect heads &&
270 test_cmp expect head &&
271 test_must_be_empty untracked
274 test_expect_success 'submodule add with /.. in path' '
275 echo "refs/heads/master" >expect &&
279 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
283 rm -f heads head untracked &&
284 inspect addtest/realsubmod ../.. &&
285 test_cmp expect heads &&
286 test_cmp expect head &&
287 test_must_be_empty untracked
290 test_expect_success 'submodule add with ./, /.. and // in path' '
291 echo "refs/heads/master" >expect &&
295 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
299 rm -f heads head untracked &&
300 inspect addtest/realsubmod2 ../.. &&
301 test_cmp expect heads &&
302 test_cmp expect head &&
303 test_must_be_empty untracked
306 test_expect_success !CYGWIN 'submodule add with \\ in path' '
307 test_when_finished "rm -rf parent sub\\with\\backslash" &&
309 # Initialize a repo with a backslash in its name
310 git init sub\\with\\backslash &&
311 touch sub\\with\\backslash/empty.file &&
312 git -C sub\\with\\backslash add empty.file &&
313 git -C sub\\with\\backslash commit -m "Added empty.file" &&
315 # Add that repository as a submodule
317 git -C parent submodule add ../sub\\with\\backslash
320 test_expect_success 'submodule add in subdirectory' '
321 echo "refs/heads/master" >expect &&
326 git submodule add "$submodurl" ../realsubmod3 &&
330 rm -f heads head untracked &&
331 inspect addtest/realsubmod3 ../.. &&
332 test_cmp expect heads &&
333 test_cmp expect head &&
334 test_must_be_empty untracked
337 test_expect_success 'submodule add in subdirectory with relative path should fail' '
340 test_must_fail git submodule add ../../ submod3 2>../../output.err
342 test_i18ngrep toplevel output.err
345 test_expect_success 'setup - add an example entry to .gitmodules' '
346 git config --file=.gitmodules submodule.example.url git://example.com/init.git
349 test_expect_success 'status should fail for unmapped paths' '
350 test_must_fail git submodule status
353 test_expect_success 'setup - map path in .gitmodules' '
354 cat <<\EOF >expect &&
355 [submodule "example"]
356 url = git://example.com/init.git
360 git config --file=.gitmodules submodule.example.path init &&
362 test_cmp expect .gitmodules
365 test_expect_success 'status should only print one line' '
366 git submodule status >lines &&
367 test_line_count = 1 lines
370 test_expect_success 'status from subdirectory should have the same SHA1' '
371 test_when_finished "rmdir addtest/subdir" &&
375 git submodule status >output &&
376 awk "{print \$1}" <output >expect &&
378 git submodule status >../output &&
379 awk "{print \$1}" <../output >../actual &&
380 test_cmp ../expect ../actual &&
381 git -C ../submod checkout HEAD^ &&
382 git submodule status >../output &&
383 awk "{print \$1}" <../output >../actual2 &&
385 git submodule status >output &&
386 awk "{print \$1}" <output >expect2 &&
387 test_cmp expect2 actual2 &&
388 ! test_cmp actual actual2
392 test_expect_success 'setup - fetch commit name from submodule' '
393 rev1=$(cd .subrepo && git rev-parse HEAD) &&
394 printf "rev1: %s\n" "$rev1" &&
398 test_expect_success 'status should initially be "missing"' '
399 git submodule status >lines &&
403 test_expect_success 'init should register submodule url in .git/config' '
404 echo git://example.com/init.git >expect &&
406 git submodule init &&
407 git config submodule.example.url >url &&
408 git config submodule.example.url ./.subrepo &&
413 test_failure_with_unknown_submodule () {
414 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
415 test_i18ngrep "^error: .*no-such-submodule" output.err
418 test_expect_success 'init should fail with unknown submodule' '
419 test_failure_with_unknown_submodule init
422 test_expect_success 'update should fail with unknown submodule' '
423 test_failure_with_unknown_submodule update
426 test_expect_success 'status should fail with unknown submodule' '
427 test_failure_with_unknown_submodule status
430 test_expect_success 'sync should fail with unknown submodule' '
431 test_failure_with_unknown_submodule sync
434 test_expect_success 'update should fail when path is used by a file' '
435 echo hello >expect &&
437 echo "hello" >init &&
438 test_must_fail git submodule update &&
443 test_expect_success 'update should fail when path is used by a nonempty directory' '
444 echo hello >expect &&
448 echo "hello" >init/a &&
450 test_must_fail git submodule update &&
452 test_cmp expect init/a
455 test_expect_success 'update should work when path is an empty dir' '
458 echo "$rev1" >expect &&
461 git submodule update -q >update.out &&
462 test_must_be_empty update.out &&
465 test_cmp expect head-sha1
468 test_expect_success 'status should be "up-to-date" after update' '
469 git submodule status >list &&
473 test_expect_success 'status "up-to-date" from subdirectory' '
477 git submodule status >../list
479 grep "^ $rev1" list &&
480 grep "\\.\\./init" list
483 test_expect_success 'status "up-to-date" from subdirectory with path' '
487 git submodule status ../init >../list
489 grep "^ $rev1" list &&
490 grep "\\.\\./init" list
493 test_expect_success 'status should be "modified" after submodule commit' '
498 git commit -m "submodule commit 2"
501 rev2=$(cd init && git rev-parse HEAD) &&
503 git submodule status >list &&
508 test_expect_success 'the --cached sha1 should be rev1' '
509 git submodule --cached status >list &&
513 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
515 grep "^+Subproject commit $rev2" diff
518 test_expect_success 'update should checkout rev1' '
520 echo "$rev1" >expect &&
522 git submodule update init &&
525 test_cmp expect head-sha1
528 test_expect_success 'status should be "up-to-date" after update' '
529 git submodule status >list &&
533 test_expect_success 'checkout superproject with subproject already present' '
534 git checkout initial &&
538 test_expect_success 'apply submodule diff' '
544 git commit -m "change subproject"
546 git update-index --add init &&
547 git commit -m "change init" &&
548 git format-patch -1 --stdout >P.diff &&
549 git checkout second &&
550 git apply --index P.diff &&
552 git diff --cached master >staged &&
553 test_must_be_empty staged
556 test_expect_success 'update --init' '
558 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
559 git config --remove-section submodule.example &&
560 test_must_fail git config submodule.example.url &&
562 git submodule update init 2> update.out &&
563 test_i18ngrep "not initialized" update.out &&
564 test_must_fail git rev-parse --resolve-git-dir init/.git &&
566 git submodule update --init init &&
567 git rev-parse --resolve-git-dir init/.git
570 test_expect_success 'update --init from subdirectory' '
572 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
573 git config --remove-section submodule.example &&
574 test_must_fail git config submodule.example.url &&
579 git submodule update ../init 2>update.out &&
580 test_i18ngrep "not initialized" update.out &&
581 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
583 git submodule update --init ../init
585 git rev-parse --resolve-git-dir init/.git
588 test_expect_success 'do not add files from a submodule' '
591 test_must_fail git add init/a
595 test_expect_success 'gracefully add/reset submodule with a trailing slash' '
598 git commit -m "commit subproject" init &&
602 git diff --exit-code --cached init &&
604 git commit -m update a >/dev/null &&
605 git rev-parse HEAD) &&
607 test_must_fail git diff --exit-code --cached init &&
608 test $commit = $(git ls-files --stage |
609 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
611 git diff --exit-code --cached init
615 test_expect_success 'ls-files gracefully handles trailing slash' '
617 test "init" = "$(git ls-files init/)"
621 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
625 git checkout initial &&
630 test_expect_success 'submodule <invalid-subcommand> fails' '
631 test_must_fail git submodule no-such-subcommand
634 test_expect_success 'add submodules without specifying an explicit path' '
641 git commit -m "repo commit 1"
643 git clone --bare repo/ bare.git &&
646 git submodule add "$submodurl/repo" &&
647 git config -f .gitmodules submodule.repo.path repo &&
648 git submodule add "$submodurl/bare.git" &&
649 git config -f .gitmodules submodule.bare.path bare
653 test_expect_success 'add should fail when path is used by a file' '
657 test_must_fail git submodule add "$submodurl/repo" file
661 test_expect_success 'add should fail when path is used by an existing directory' '
665 test_must_fail git submodule add "$submodurl/repo" empty-dir
669 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
672 git submodule add ../repo relative &&
673 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
674 git submodule sync relative &&
675 test "$(git config submodule.relative.url)" = "$submodurl/repo"
679 test_expect_success 'set up for relative path tests' '
691 git config -f .gitmodules submodule.sub.path sub &&
692 git config -f .gitmodules submodule.sub.url ../subrepo &&
693 cp .git/config pristine-.git-config &&
694 cp .gitmodules pristine-.gitmodules
698 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
701 cp pristine-.git-config .git/config &&
702 cp pristine-.gitmodules .gitmodules &&
703 git config remote.origin.url ssh://hostname/repo &&
704 git submodule init &&
705 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
709 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
712 cp pristine-.git-config .git/config &&
713 cp pristine-.gitmodules .gitmodules &&
714 git config remote.origin.url ssh://hostname:22/repo &&
715 git submodule init &&
716 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
720 # About the choice of the path in the next test:
721 # - double-slash side-steps path mangling issues on Windows
722 # - it is still an absolute local path
723 # - there cannot be a server with a blank in its name just in case the
724 # path is used erroneously to access a //server/share style path
725 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
728 cp pristine-.git-config .git/config &&
729 cp pristine-.gitmodules .gitmodules &&
730 git config remote.origin.url "//somewhere else/repo" &&
731 git submodule init &&
732 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
736 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
739 cp pristine-.git-config .git/config &&
740 cp pristine-.gitmodules .gitmodules &&
741 git config remote.origin.url file:///tmp/repo &&
742 git submodule init &&
743 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
747 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
750 cp pristine-.git-config .git/config &&
751 cp pristine-.gitmodules .gitmodules &&
752 git config remote.origin.url helper:://hostname/repo &&
753 git submodule init &&
754 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
758 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
761 cp pristine-.git-config .git/config &&
762 git config remote.origin.url user@host:repo &&
763 git submodule init &&
764 test "$(git config submodule.sub.url)" = user@host:subrepo
768 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
771 cp pristine-.git-config .git/config &&
772 cp pristine-.gitmodules .gitmodules &&
773 git config remote.origin.url user@host:path/to/repo &&
774 git submodule init &&
775 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
779 test_expect_success '../subrepo works with relative local path - foo' '
782 cp pristine-.git-config .git/config &&
783 cp pristine-.gitmodules .gitmodules &&
784 git config remote.origin.url foo &&
785 # actual: fails with an error
786 git submodule init &&
787 test "$(git config submodule.sub.url)" = subrepo
791 test_expect_success '../subrepo works with relative local path - foo/bar' '
794 cp pristine-.git-config .git/config &&
795 cp pristine-.gitmodules .gitmodules &&
796 git config remote.origin.url foo/bar &&
797 git submodule init &&
798 test "$(git config submodule.sub.url)" = foo/subrepo
802 test_expect_success '../subrepo works with relative local path - ./foo' '
805 cp pristine-.git-config .git/config &&
806 cp pristine-.gitmodules .gitmodules &&
807 git config remote.origin.url ./foo &&
808 git submodule init &&
809 test "$(git config submodule.sub.url)" = subrepo
813 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
816 cp pristine-.git-config .git/config &&
817 cp pristine-.gitmodules .gitmodules &&
818 git config remote.origin.url ./foo/bar &&
819 git submodule init &&
820 test "$(git config submodule.sub.url)" = foo/subrepo
824 test_expect_success '../subrepo works with relative local path - ../foo' '
827 cp pristine-.git-config .git/config &&
828 cp pristine-.gitmodules .gitmodules &&
829 git config remote.origin.url ../foo &&
830 git submodule init &&
831 test "$(git config submodule.sub.url)" = ../subrepo
835 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
838 cp pristine-.git-config .git/config &&
839 cp pristine-.gitmodules .gitmodules &&
840 git config remote.origin.url ../foo/bar &&
841 git submodule init &&
842 test "$(git config submodule.sub.url)" = ../foo/subrepo
846 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
849 cp pristine-.git-config .git/config &&
850 cp pristine-.gitmodules .gitmodules &&
852 (cd a/b/c && git init && test_commit msg) &&
853 git config remote.origin.url ../foo/bar.git &&
854 git submodule add ../bar/a/b/c ./a/b/c &&
855 git submodule init &&
856 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
860 test_expect_success 'moving the superproject does not break submodules' '
863 git submodule status >expect
865 mv addtest addtest2 &&
868 git submodule status >actual &&
869 test_cmp expect actual
873 test_expect_success 'moving the submodule does not break the superproject' '
878 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
879 mv addtest2/repo addtest2/repo.bak &&
880 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
885 test_cmp expect actual
888 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
893 echo "$submodurl/repo" >expect &&
894 git config remote.origin.url >actual &&
895 test_cmp expect actual &&
896 echo "gitdir: ../.git/modules/repo" >expect &&
901 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
902 test_must_be_empty actual &&
903 echo "gitdir: ../.git/modules/submod" >expect &&
904 test_cmp expect submod/.git &&
907 echo "$submodurl/bare.git" >expect &&
908 git config remote.origin.url >actual &&
909 test_cmp expect actual &&
910 echo "gitdir: ../.git/modules/repo_new" >expect &&
913 echo "repo" >expect &&
914 test_must_fail git config -f .gitmodules submodule.repo.path &&
915 git config -f .gitmodules submodule.repo_new.path >actual &&
916 test_cmp expect actual&&
917 echo "$submodurl/repo" >expect &&
918 test_must_fail git config -f .gitmodules submodule.repo.url &&
919 echo "$submodurl/bare.git" >expect &&
920 git config -f .gitmodules submodule.repo_new.url >actual &&
921 test_cmp expect actual &&
922 echo "$submodurl/repo" >expect &&
923 git config submodule.repo.url >actual &&
924 test_cmp expect actual &&
925 echo "$submodurl/bare.git" >expect &&
926 git config submodule.repo_new.url >actual &&
927 test_cmp expect actual
931 test_expect_success 'recursive relative submodules stay relative' '
932 test_when_finished "rm -rf super clone2 subsub sub3" &&
939 git commit -m "initial commit"
947 git commit -m "initial commit" &&
948 git submodule add ../subsub dirdir/subsub &&
949 git commit -m "add submodule subsub"
957 git commit -m "initial commit" &&
958 git submodule add ../sub3 &&
959 git commit -m "add submodule sub"
961 git clone super clone2 &&
964 git submodule update --init --recursive &&
965 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
966 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
968 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
969 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
972 test_expect_success 'submodule add with an existing name fails unless forced' '
977 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
979 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
980 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
981 echo "$submodurl/bare.git" >expect &&
982 git config submodule.repo_new.url >actual &&
983 test_cmp expect actual &&
984 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
986 echo "repo" >expect &&
987 git config -f .gitmodules submodule.repo_new.path >actual &&
988 test_cmp expect actual&&
989 echo "$submodurl/repo.git" >expect &&
990 git config -f .gitmodules submodule.repo_new.url >actual &&
991 test_cmp expect actual &&
992 echo "$submodurl/repo.git" >expect &&
993 git config submodule.repo_new.url >actual &&
994 test_cmp expect actual
998 test_expect_success 'set up a second submodule' '
999 git submodule add ./init2 example2 &&
1000 git commit -m "submodule example2 added"
1003 test_expect_success 'submodule deinit works on repository without submodules' '
1004 test_when_finished "rm -rf newdirectory" &&
1005 mkdir newdirectory &&
1011 git commit -m "repo should not be empty" &&
1012 git submodule deinit . &&
1013 git submodule deinit --all
1017 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
1018 git config submodule.example.foo bar &&
1019 git config submodule.example2.frotz nitfol &&
1020 git submodule deinit init &&
1021 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1022 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1023 test -f example2/.git &&
1027 test_expect_success 'submodule deinit should unset core.worktree' '
1028 test_path_is_file .git/modules/example/config &&
1029 test_must_fail git config -f .git/modules/example/config core.worktree
1032 test_expect_success 'submodule deinit from subdirectory' '
1033 git submodule update --init &&
1034 git config submodule.example.foo bar &&
1038 git submodule deinit ../init >../output
1040 test_i18ngrep "\\.\\./init" output &&
1041 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1042 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1043 test -f example2/.git &&
1047 test_expect_success 'submodule deinit . deinits all initialized submodules' '
1048 git submodule update --init &&
1049 git config submodule.example.foo bar &&
1050 git config submodule.example2.frotz nitfol &&
1051 test_must_fail git submodule deinit &&
1052 git submodule deinit . >actual &&
1053 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1054 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1055 test_i18ngrep "Cleared directory .init" actual &&
1056 test_i18ngrep "Cleared directory .example2" actual &&
1060 test_expect_success 'submodule deinit --all deinits all initialized submodules' '
1061 git submodule update --init &&
1062 git config submodule.example.foo bar &&
1063 git config submodule.example2.frotz nitfol &&
1064 test_must_fail git submodule deinit &&
1065 git submodule deinit --all >actual &&
1066 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1067 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1068 test_i18ngrep "Cleared directory .init" actual &&
1069 test_i18ngrep "Cleared directory .example2" actual &&
1073 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
1074 git submodule update --init &&
1075 rm -rf init example2/* example2/.git &&
1076 git submodule deinit init example2 >actual &&
1077 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1078 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1079 test_i18ngrep ! "Cleared directory .init" actual &&
1080 test_i18ngrep "Cleared directory .example2" actual &&
1084 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
1085 git submodule update --init &&
1087 test_must_fail git submodule deinit init &&
1088 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1089 test -f example2/.git &&
1090 git submodule deinit -f init >actual &&
1091 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1092 test_i18ngrep "Cleared directory .init" actual &&
1096 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1097 git submodule update --init &&
1098 echo X >>init/untracked &&
1099 test_must_fail git submodule deinit init &&
1100 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1101 test -f example2/.git &&
1102 git submodule deinit -f init >actual &&
1103 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1104 test_i18ngrep "Cleared directory .init" actual &&
1108 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1109 git submodule update --init &&
1114 test_must_fail git submodule deinit init &&
1115 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1116 test -f example2/.git &&
1117 git submodule deinit -f init >actual &&
1118 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1119 test_i18ngrep "Cleared directory .init" actual &&
1123 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1124 git submodule update --init &&
1125 git submodule deinit init >actual &&
1126 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1127 test_i18ngrep "Cleared directory .init" actual &&
1128 git submodule deinit init >actual &&
1129 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1130 test_i18ngrep "Cleared directory .init" actual &&
1131 git submodule deinit . >actual &&
1132 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1133 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1134 test_i18ngrep "Cleared directory .init" actual &&
1135 git submodule deinit . >actual &&
1136 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1137 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1138 test_i18ngrep "Cleared directory .init" actual &&
1139 git submodule deinit --all >actual &&
1140 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1141 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1142 test_i18ngrep "Cleared directory .init" actual &&
1146 test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
1147 git submodule update --init &&
1151 cp -R ../.git/modules/example .git &&
1152 GIT_WORK_TREE=. git config --unset core.worktree
1154 test_must_fail git submodule deinit init &&
1155 test_must_fail git submodule deinit -f init &&
1156 test -d init/.git &&
1157 test -n "$(git config --get-regexp "submodule\.example\.")"
1160 test_expect_success 'submodule with UTF-8 name' '
1161 svname=$(printf "\303\245 \303\244\303\266") &&
1168 git commit -m "init sub"
1170 git submodule add ./"$svname" &&
1171 git submodule >&2 &&
1172 test -n "$(git submodule | grep "$svname")"
1175 test_expect_success 'submodule add clone shallow submodule' '
1181 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1184 test 1 = $(git log --oneline | wc -l)
1189 test_expect_success 'submodule helper list is not confused by common prefixes' '
1194 echo hi >testfile2 &&
1196 git commit -m "test1"
1202 echo hello >testfile1 &&
1204 git commit -m "test2"
1206 git submodule add /dir1/b dir1/b &&
1207 git submodule add /dir2/b dir2/b &&
1208 git commit -m "first submodule commit" &&
1209 git submodule--helper list dir1/b |cut -c51- >actual &&
1210 echo "dir1/b" >expect &&
1211 test_cmp expect actual
1214 test_expect_success 'setup superproject with submodules' '
1216 test_commit -C sub1 test &&
1217 test_commit -C sub1 test2 &&
1218 git init multisuper &&
1219 git -C multisuper submodule add ../sub1 sub0 &&
1220 git -C multisuper submodule add ../sub1 sub1 &&
1221 git -C multisuper submodule add ../sub1 sub2 &&
1222 git -C multisuper submodule add ../sub1 sub3 &&
1223 git -C multisuper commit -m "add some submodules"
1233 test_expect_success 'submodule update --init with a specification' '
1234 test_when_finished "rm -rf multisuper_clone" &&
1236 git clone file://"$pwd"/multisuper multisuper_clone &&
1237 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1238 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1239 test_cmp expect actual
1242 test_expect_success 'submodule update --init with submodule.active set' '
1243 test_when_finished "rm -rf multisuper_clone" &&
1245 git clone file://"$pwd"/multisuper multisuper_clone &&
1246 git -C multisuper_clone config submodule.active "." &&
1247 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1248 git -C multisuper_clone submodule update --init &&
1249 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1250 test_cmp expect actual
1253 test_expect_success 'submodule update and setting submodule.<name>.active' '
1254 test_when_finished "rm -rf multisuper_clone" &&
1256 git clone file://"$pwd"/multisuper multisuper_clone &&
1257 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1258 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1259 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1261 cat >expect <<-\EOF &&
1267 git -C multisuper_clone submodule update &&
1268 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1269 test_cmp expect actual
1272 test_expect_success 'clone active submodule without submodule url set' '
1273 test_when_finished "rm -rf test/test" &&
1275 # another dir breaks accidental relative paths still being correct
1276 git clone file://"$pwd"/multisuper test/test &&
1279 git config submodule.active "." &&
1281 # do not pass --init flag, as the submodule is already active:
1282 git submodule update &&
1283 git submodule status >actual_raw &&
1285 cut -c 1,43- actual_raw >actual &&
1286 cat >expect <<-\EOF &&
1292 test_cmp expect actual
1296 test_expect_success 'clone --recurse-submodules with a pathspec works' '
1297 test_when_finished "rm -rf multisuper_clone" &&
1298 cat >expected <<-\EOF &&
1305 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1306 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1307 test_cmp expected actual
1310 test_expect_success 'clone with multiple --recurse-submodules options' '
1311 test_when_finished "rm -rf multisuper_clone" &&
1312 cat >expect <<-\EOF &&
1319 git clone --recurse-submodules="." \
1320 --recurse-submodules=":(exclude)sub0" \
1321 --recurse-submodules=":(exclude)sub2" \
1322 multisuper multisuper_clone &&
1323 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1324 test_cmp expect actual
1327 test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
1328 test_when_finished "rm -rf multisuper_clone" &&
1329 cat <<-\EOF >expect &&
1336 cat <<-\EOF >expect2 &&
1345 git clone --recurse-submodules="." \
1346 --recurse-submodules=":(exclude)sub0" \
1347 --recurse-submodules=":(exclude)sub2" \
1348 --recurse-submodules=":(exclude)sub4" \
1349 multisuper multisuper_clone &&
1351 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1352 test_cmp expect actual &&
1354 git -C multisuper submodule add ../sub1 sub4 &&
1355 git -C multisuper submodule add ../sub1 sub5 &&
1356 git -C multisuper commit -m "add more submodules" &&
1357 # obtain the new superproject
1358 git -C multisuper_clone pull &&
1359 git -C multisuper_clone submodule update --init &&
1360 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1361 test_cmp expect2 actual
1364 test_expect_success 'init properly sets the config' '
1365 test_when_finished "rm -rf multisuper_clone" &&
1366 git clone --recurse-submodules="." \
1367 --recurse-submodules=":(exclude)sub0" \
1368 multisuper multisuper_clone &&
1370 git -C multisuper_clone submodule init -- sub0 sub1 &&
1371 git -C multisuper_clone config --get submodule.sub0.active &&
1372 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1375 test_expect_success 'recursive clone respects -q' '
1376 test_when_finished "rm -rf multisuper_clone" &&
1377 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1378 test_must_be_empty actual