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 path is ignored by one of your .gitignore files:
161 Use -f if you really want to add it.
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 --branch' '
195 echo "refs/heads/initial" >expect-head &&
196 cat <<-\EOF >expect-heads &&
203 git submodule add -b initial "$submodurl" submod-branch &&
204 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
208 rm -f heads head untracked &&
209 inspect addtest/submod-branch ../.. &&
210 test_cmp expect-heads heads &&
211 test_cmp expect-head head &&
212 test_must_be_empty untracked
215 test_expect_success 'submodule add with ./ in path' '
216 echo "refs/heads/master" >expect &&
220 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
224 rm -f heads head untracked &&
225 inspect addtest/dotsubmod/frotz ../../.. &&
226 test_cmp expect heads &&
227 test_cmp expect head &&
228 test_must_be_empty untracked
231 test_expect_success 'submodule add with /././ in path' '
232 echo "refs/heads/master" >expect &&
236 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
240 rm -f heads head untracked &&
241 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
242 test_cmp expect heads &&
243 test_cmp expect head &&
244 test_must_be_empty untracked
247 test_expect_success 'submodule add with // in path' '
248 echo "refs/heads/master" >expect &&
252 git submodule add "$submodurl" slashslashsubmod///frotz// &&
256 rm -f heads head untracked &&
257 inspect addtest/slashslashsubmod/frotz ../../.. &&
258 test_cmp expect heads &&
259 test_cmp expect head &&
260 test_must_be_empty untracked
263 test_expect_success 'submodule add with /.. in path' '
264 echo "refs/heads/master" >expect &&
268 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
272 rm -f heads head untracked &&
273 inspect addtest/realsubmod ../.. &&
274 test_cmp expect heads &&
275 test_cmp expect head &&
276 test_must_be_empty untracked
279 test_expect_success 'submodule add with ./, /.. and // in path' '
280 echo "refs/heads/master" >expect &&
284 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
288 rm -f heads head untracked &&
289 inspect addtest/realsubmod2 ../.. &&
290 test_cmp expect heads &&
291 test_cmp expect head &&
292 test_must_be_empty untracked
295 test_expect_success !CYGWIN 'submodule add with \\ in path' '
296 test_when_finished "rm -rf parent sub\\with\\backslash" &&
298 # Initialize a repo with a backslash in its name
299 git init sub\\with\\backslash &&
300 touch sub\\with\\backslash/empty.file &&
301 git -C sub\\with\\backslash add empty.file &&
302 git -C sub\\with\\backslash commit -m "Added empty.file" &&
304 # Add that repository as a submodule
306 git -C parent submodule add ../sub\\with\\backslash
309 test_expect_success 'submodule add in subdirectory' '
310 echo "refs/heads/master" >expect &&
315 git submodule add "$submodurl" ../realsubmod3 &&
319 rm -f heads head untracked &&
320 inspect addtest/realsubmod3 ../.. &&
321 test_cmp expect heads &&
322 test_cmp expect head &&
323 test_must_be_empty untracked
326 test_expect_success 'submodule add in subdirectory with relative path should fail' '
329 test_must_fail git submodule add ../../ submod3 2>../../output.err
331 test_i18ngrep toplevel output.err
334 test_expect_success 'setup - add an example entry to .gitmodules' '
335 git config --file=.gitmodules submodule.example.url git://example.com/init.git
338 test_expect_success 'status should fail for unmapped paths' '
339 test_must_fail git submodule status
342 test_expect_success 'setup - map path in .gitmodules' '
343 cat <<\EOF >expect &&
344 [submodule "example"]
345 url = git://example.com/init.git
349 git config --file=.gitmodules submodule.example.path init &&
351 test_cmp expect .gitmodules
354 test_expect_success 'status should only print one line' '
355 git submodule status >lines &&
356 test_line_count = 1 lines
359 test_expect_success 'status from subdirectory should have the same SHA1' '
360 test_when_finished "rmdir addtest/subdir" &&
364 git submodule status >output &&
365 awk "{print \$1}" <output >expect &&
367 git submodule status >../output &&
368 awk "{print \$1}" <../output >../actual &&
369 test_cmp ../expect ../actual &&
370 git -C ../submod checkout HEAD^ &&
371 git submodule status >../output &&
372 awk "{print \$1}" <../output >../actual2 &&
374 git submodule status >output &&
375 awk "{print \$1}" <output >expect2 &&
376 test_cmp expect2 actual2 &&
377 ! test_cmp actual actual2
381 test_expect_success 'setup - fetch commit name from submodule' '
382 rev1=$(cd .subrepo && git rev-parse HEAD) &&
383 printf "rev1: %s\n" "$rev1" &&
387 test_expect_success 'status should initially be "missing"' '
388 git submodule status >lines &&
392 test_expect_success 'init should register submodule url in .git/config' '
393 echo git://example.com/init.git >expect &&
395 git submodule init &&
396 git config submodule.example.url >url &&
397 git config submodule.example.url ./.subrepo &&
402 test_failure_with_unknown_submodule () {
403 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
404 test_i18ngrep "^error: .*no-such-submodule" output.err
407 test_expect_success 'init should fail with unknown submodule' '
408 test_failure_with_unknown_submodule init
411 test_expect_success 'update should fail with unknown submodule' '
412 test_failure_with_unknown_submodule update
415 test_expect_success 'status should fail with unknown submodule' '
416 test_failure_with_unknown_submodule status
419 test_expect_success 'sync should fail with unknown submodule' '
420 test_failure_with_unknown_submodule sync
423 test_expect_success 'update should fail when path is used by a file' '
424 echo hello >expect &&
426 echo "hello" >init &&
427 test_must_fail git submodule update &&
432 test_expect_success 'update should fail when path is used by a nonempty directory' '
433 echo hello >expect &&
437 echo "hello" >init/a &&
439 test_must_fail git submodule update &&
441 test_cmp expect init/a
444 test_expect_success 'update should work when path is an empty dir' '
447 echo "$rev1" >expect &&
450 git submodule update -q >update.out &&
451 test_must_be_empty update.out &&
454 test_cmp expect head-sha1
457 test_expect_success 'status should be "up-to-date" after update' '
458 git submodule status >list &&
462 test_expect_success 'status "up-to-date" from subdirectory' '
466 git submodule status >../list
468 grep "^ $rev1" list &&
469 grep "\\.\\./init" list
472 test_expect_success 'status "up-to-date" from subdirectory with path' '
476 git submodule status ../init >../list
478 grep "^ $rev1" list &&
479 grep "\\.\\./init" list
482 test_expect_success 'status should be "modified" after submodule commit' '
487 git commit -m "submodule commit 2"
490 rev2=$(cd init && git rev-parse HEAD) &&
492 git submodule status >list &&
497 test_expect_success 'the --cached sha1 should be rev1' '
498 git submodule --cached status >list &&
502 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
504 grep "^+Subproject commit $rev2" diff
507 test_expect_success 'update should checkout rev1' '
509 echo "$rev1" >expect &&
511 git submodule update init &&
514 test_cmp expect head-sha1
517 test_expect_success 'status should be "up-to-date" after update' '
518 git submodule status >list &&
522 test_expect_success 'checkout superproject with subproject already present' '
523 git checkout initial &&
527 test_expect_success 'apply submodule diff' '
533 git commit -m "change subproject"
535 git update-index --add init &&
536 git commit -m "change init" &&
537 git format-patch -1 --stdout >P.diff &&
538 git checkout second &&
539 git apply --index P.diff &&
541 git diff --cached master >staged &&
542 test_must_be_empty staged
545 test_expect_success 'update --init' '
547 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
548 git config --remove-section submodule.example &&
549 test_must_fail git config submodule.example.url &&
551 git submodule update init 2> update.out &&
553 test_i18ngrep "not initialized" update.out &&
554 test_must_fail git rev-parse --resolve-git-dir init/.git &&
556 git submodule update --init init &&
557 git rev-parse --resolve-git-dir init/.git
560 test_expect_success 'update --init from subdirectory' '
562 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
563 git config --remove-section submodule.example &&
564 test_must_fail git config submodule.example.url &&
569 git submodule update ../init 2>update.out &&
571 test_i18ngrep "not initialized" update.out &&
572 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
574 git submodule update --init ../init
576 git rev-parse --resolve-git-dir init/.git
579 test_expect_success 'do not add files from a submodule' '
582 test_must_fail git add init/a
586 test_expect_success 'gracefully add/reset submodule with a trailing slash' '
589 git commit -m "commit subproject" init &&
593 git diff --exit-code --cached init &&
595 git commit -m update a >/dev/null &&
596 git rev-parse HEAD) &&
598 test_must_fail git diff --exit-code --cached init &&
599 test $commit = $(git ls-files --stage |
600 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
602 git diff --exit-code --cached init
606 test_expect_success 'ls-files gracefully handles trailing slash' '
608 test "init" = "$(git ls-files init/)"
612 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
616 git checkout initial &&
621 test_expect_success 'submodule <invalid-subcommand> fails' '
622 test_must_fail git submodule no-such-subcommand
625 test_expect_success 'add submodules without specifying an explicit path' '
632 git commit -m "repo commit 1"
634 git clone --bare repo/ bare.git &&
637 git submodule add "$submodurl/repo" &&
638 git config -f .gitmodules submodule.repo.path repo &&
639 git submodule add "$submodurl/bare.git" &&
640 git config -f .gitmodules submodule.bare.path bare
644 test_expect_success 'add should fail when path is used by a file' '
648 test_must_fail git submodule add "$submodurl/repo" file
652 test_expect_success 'add should fail when path is used by an existing directory' '
656 test_must_fail git submodule add "$submodurl/repo" empty-dir
660 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
663 git submodule add ../repo relative &&
664 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
665 git submodule sync relative &&
666 test "$(git config submodule.relative.url)" = "$submodurl/repo"
670 test_expect_success 'set up for relative path tests' '
682 git config -f .gitmodules submodule.sub.path sub &&
683 git config -f .gitmodules submodule.sub.url ../subrepo &&
684 cp .git/config pristine-.git-config &&
685 cp .gitmodules pristine-.gitmodules
689 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
692 cp pristine-.git-config .git/config &&
693 cp pristine-.gitmodules .gitmodules &&
694 git config remote.origin.url ssh://hostname/repo &&
695 git submodule init &&
696 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
700 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
703 cp pristine-.git-config .git/config &&
704 cp pristine-.gitmodules .gitmodules &&
705 git config remote.origin.url ssh://hostname:22/repo &&
706 git submodule init &&
707 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
711 # About the choice of the path in the next test:
712 # - double-slash side-steps path mangling issues on Windows
713 # - it is still an absolute local path
714 # - there cannot be a server with a blank in its name just in case the
715 # path is used erroneously to access a //server/share style path
716 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
719 cp pristine-.git-config .git/config &&
720 cp pristine-.gitmodules .gitmodules &&
721 git config remote.origin.url "//somewhere else/repo" &&
722 git submodule init &&
723 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
727 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
730 cp pristine-.git-config .git/config &&
731 cp pristine-.gitmodules .gitmodules &&
732 git config remote.origin.url file:///tmp/repo &&
733 git submodule init &&
734 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
738 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
741 cp pristine-.git-config .git/config &&
742 cp pristine-.gitmodules .gitmodules &&
743 git config remote.origin.url helper:://hostname/repo &&
744 git submodule init &&
745 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
749 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
752 cp pristine-.git-config .git/config &&
753 git config remote.origin.url user@host:repo &&
754 git submodule init &&
755 test "$(git config submodule.sub.url)" = user@host:subrepo
759 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
762 cp pristine-.git-config .git/config &&
763 cp pristine-.gitmodules .gitmodules &&
764 git config remote.origin.url user@host:path/to/repo &&
765 git submodule init &&
766 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
770 test_expect_success '../subrepo works with relative local path - foo' '
773 cp pristine-.git-config .git/config &&
774 cp pristine-.gitmodules .gitmodules &&
775 git config remote.origin.url foo &&
776 # actual: fails with an error
777 git submodule init &&
778 test "$(git config submodule.sub.url)" = subrepo
782 test_expect_success '../subrepo works with relative local path - foo/bar' '
785 cp pristine-.git-config .git/config &&
786 cp pristine-.gitmodules .gitmodules &&
787 git config remote.origin.url foo/bar &&
788 git submodule init &&
789 test "$(git config submodule.sub.url)" = foo/subrepo
793 test_expect_success '../subrepo works with relative local path - ./foo' '
796 cp pristine-.git-config .git/config &&
797 cp pristine-.gitmodules .gitmodules &&
798 git config remote.origin.url ./foo &&
799 git submodule init &&
800 test "$(git config submodule.sub.url)" = subrepo
804 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
807 cp pristine-.git-config .git/config &&
808 cp pristine-.gitmodules .gitmodules &&
809 git config remote.origin.url ./foo/bar &&
810 git submodule init &&
811 test "$(git config submodule.sub.url)" = foo/subrepo
815 test_expect_success '../subrepo works with relative local path - ../foo' '
818 cp pristine-.git-config .git/config &&
819 cp pristine-.gitmodules .gitmodules &&
820 git config remote.origin.url ../foo &&
821 git submodule init &&
822 test "$(git config submodule.sub.url)" = ../subrepo
826 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
829 cp pristine-.git-config .git/config &&
830 cp pristine-.gitmodules .gitmodules &&
831 git config remote.origin.url ../foo/bar &&
832 git submodule init &&
833 test "$(git config submodule.sub.url)" = ../foo/subrepo
837 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
840 cp pristine-.git-config .git/config &&
841 cp pristine-.gitmodules .gitmodules &&
843 (cd a/b/c && git init && test_commit msg) &&
844 git config remote.origin.url ../foo/bar.git &&
845 git submodule add ../bar/a/b/c ./a/b/c &&
846 git submodule init &&
847 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
851 test_expect_success 'moving the superproject does not break submodules' '
854 git submodule status >expect
856 mv addtest addtest2 &&
859 git submodule status >actual &&
860 test_cmp expect actual
864 test_expect_success 'moving the submodule does not break the superproject' '
869 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
870 mv addtest2/repo addtest2/repo.bak &&
871 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
876 test_cmp expect actual
879 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
884 echo "$submodurl/repo" >expect &&
885 git config remote.origin.url >actual &&
886 test_cmp expect actual &&
887 echo "gitdir: ../.git/modules/repo" >expect &&
892 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
893 test_must_be_empty actual &&
894 echo "gitdir: ../.git/modules/submod" >expect &&
895 test_cmp expect submod/.git &&
898 echo "$submodurl/bare.git" >expect &&
899 git config remote.origin.url >actual &&
900 test_cmp expect actual &&
901 echo "gitdir: ../.git/modules/repo_new" >expect &&
904 echo "repo" >expect &&
905 test_must_fail git config -f .gitmodules submodule.repo.path &&
906 git config -f .gitmodules submodule.repo_new.path >actual &&
907 test_cmp expect actual&&
908 echo "$submodurl/repo" >expect &&
909 test_must_fail git config -f .gitmodules submodule.repo.url &&
910 echo "$submodurl/bare.git" >expect &&
911 git config -f .gitmodules submodule.repo_new.url >actual &&
912 test_cmp expect actual &&
913 echo "$submodurl/repo" >expect &&
914 git config submodule.repo.url >actual &&
915 test_cmp expect actual &&
916 echo "$submodurl/bare.git" >expect &&
917 git config submodule.repo_new.url >actual &&
918 test_cmp expect actual
922 test_expect_success 'recursive relative submodules stay relative' '
923 test_when_finished "rm -rf super clone2 subsub sub3" &&
930 git commit -m "initial commit"
938 git commit -m "initial commit" &&
939 git submodule add ../subsub dirdir/subsub &&
940 git commit -m "add submodule subsub"
948 git commit -m "initial commit" &&
949 git submodule add ../sub3 &&
950 git commit -m "add submodule sub"
952 git clone super clone2 &&
955 git submodule update --init --recursive &&
956 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
957 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
959 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
960 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
963 test_expect_success 'submodule add with an existing name fails unless forced' '
968 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
970 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
971 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
972 echo "$submodurl/bare.git" >expect &&
973 git config submodule.repo_new.url >actual &&
974 test_cmp expect actual &&
975 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
977 echo "repo" >expect &&
978 git config -f .gitmodules submodule.repo_new.path >actual &&
979 test_cmp expect actual&&
980 echo "$submodurl/repo.git" >expect &&
981 git config -f .gitmodules submodule.repo_new.url >actual &&
982 test_cmp expect actual &&
983 echo "$submodurl/repo.git" >expect &&
984 git config submodule.repo_new.url >actual &&
985 test_cmp expect actual
989 test_expect_success 'set up a second submodule' '
990 git submodule add ./init2 example2 &&
991 git commit -m "submodule example2 added"
994 test_expect_success 'submodule deinit works on repository without submodules' '
995 test_when_finished "rm -rf newdirectory" &&
996 mkdir newdirectory &&
1002 git commit -m "repo should not be empty" &&
1003 git submodule deinit . &&
1004 git submodule deinit --all
1008 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
1009 git config submodule.example.foo bar &&
1010 git config submodule.example2.frotz nitfol &&
1011 git submodule deinit init &&
1012 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1013 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1014 test -f example2/.git &&
1018 test_expect_success 'submodule deinit should unset core.worktree' '
1019 test_path_is_file .git/modules/example/config &&
1020 test_must_fail git config -f .git/modules/example/config core.worktree
1023 test_expect_success 'submodule deinit from subdirectory' '
1024 git submodule update --init &&
1025 git config submodule.example.foo bar &&
1029 git submodule deinit ../init >../output
1031 test_i18ngrep "\\.\\./init" output &&
1032 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1033 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1034 test -f example2/.git &&
1038 test_expect_success 'submodule deinit . deinits all initialized submodules' '
1039 git submodule update --init &&
1040 git config submodule.example.foo bar &&
1041 git config submodule.example2.frotz nitfol &&
1042 test_must_fail git submodule deinit &&
1043 git submodule deinit . >actual &&
1044 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1045 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1046 test_i18ngrep "Cleared directory .init" actual &&
1047 test_i18ngrep "Cleared directory .example2" actual &&
1051 test_expect_success 'submodule deinit --all deinits all initialized submodules' '
1052 git submodule update --init &&
1053 git config submodule.example.foo bar &&
1054 git config submodule.example2.frotz nitfol &&
1055 test_must_fail git submodule deinit &&
1056 git submodule deinit --all >actual &&
1057 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1058 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1059 test_i18ngrep "Cleared directory .init" actual &&
1060 test_i18ngrep "Cleared directory .example2" actual &&
1064 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
1065 git submodule update --init &&
1066 rm -rf init example2/* example2/.git &&
1067 git submodule deinit init example2 >actual &&
1068 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1069 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1070 test_i18ngrep ! "Cleared directory .init" actual &&
1071 test_i18ngrep "Cleared directory .example2" actual &&
1075 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
1076 git submodule update --init &&
1078 test_must_fail git submodule deinit init &&
1079 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1080 test -f example2/.git &&
1081 git submodule deinit -f init >actual &&
1082 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1083 test_i18ngrep "Cleared directory .init" actual &&
1087 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1088 git submodule update --init &&
1089 echo X >>init/untracked &&
1090 test_must_fail git submodule deinit init &&
1091 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1092 test -f example2/.git &&
1093 git submodule deinit -f init >actual &&
1094 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1095 test_i18ngrep "Cleared directory .init" actual &&
1099 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1100 git submodule update --init &&
1105 test_must_fail git submodule deinit init &&
1106 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1107 test -f example2/.git &&
1108 git submodule deinit -f init >actual &&
1109 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1110 test_i18ngrep "Cleared directory .init" actual &&
1114 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1115 git submodule update --init &&
1116 git submodule deinit init >actual &&
1117 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1118 test_i18ngrep "Cleared directory .init" actual &&
1119 git submodule deinit init >actual &&
1120 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1121 test_i18ngrep "Cleared directory .init" actual &&
1122 git submodule deinit . >actual &&
1123 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1124 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1125 test_i18ngrep "Cleared directory .init" actual &&
1126 git submodule deinit . >actual &&
1127 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1128 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1129 test_i18ngrep "Cleared directory .init" actual &&
1130 git submodule deinit --all >actual &&
1131 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1132 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1133 test_i18ngrep "Cleared directory .init" actual &&
1137 test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
1138 git submodule update --init &&
1142 cp -R ../.git/modules/example .git &&
1143 GIT_WORK_TREE=. git config --unset core.worktree
1145 test_must_fail git submodule deinit init &&
1146 test_must_fail git submodule deinit -f init &&
1147 test -d init/.git &&
1148 test -n "$(git config --get-regexp "submodule\.example\.")"
1151 test_expect_success 'submodule with UTF-8 name' '
1152 svname=$(printf "\303\245 \303\244\303\266") &&
1159 git commit -m "init sub"
1161 git submodule add ./"$svname" &&
1162 git submodule >&2 &&
1163 test -n "$(git submodule | grep "$svname")"
1166 test_expect_success 'submodule add clone shallow submodule' '
1172 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1175 test 1 = $(git log --oneline | wc -l)
1180 test_expect_success 'submodule helper list is not confused by common prefixes' '
1185 echo hi >testfile2 &&
1187 git commit -m "test1"
1193 echo hello >testfile1 &&
1195 git commit -m "test2"
1197 git submodule add /dir1/b dir1/b &&
1198 git submodule add /dir2/b dir2/b &&
1199 git commit -m "first submodule commit" &&
1200 git submodule--helper list dir1/b |cut -c51- >actual &&
1201 echo "dir1/b" >expect &&
1202 test_cmp expect actual
1205 test_expect_success 'setup superproject with submodules' '
1207 test_commit -C sub1 test &&
1208 test_commit -C sub1 test2 &&
1209 git init multisuper &&
1210 git -C multisuper submodule add ../sub1 sub0 &&
1211 git -C multisuper submodule add ../sub1 sub1 &&
1212 git -C multisuper submodule add ../sub1 sub2 &&
1213 git -C multisuper submodule add ../sub1 sub3 &&
1214 git -C multisuper commit -m "add some submodules"
1224 test_expect_success 'submodule update --init with a specification' '
1225 test_when_finished "rm -rf multisuper_clone" &&
1227 git clone file://"$pwd"/multisuper multisuper_clone &&
1228 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1229 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1230 test_cmp expect actual
1233 test_expect_success 'submodule update --init with submodule.active set' '
1234 test_when_finished "rm -rf multisuper_clone" &&
1236 git clone file://"$pwd"/multisuper multisuper_clone &&
1237 git -C multisuper_clone config submodule.active "." &&
1238 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1239 git -C multisuper_clone submodule update --init &&
1240 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1241 test_cmp expect actual
1244 test_expect_success 'submodule update and setting submodule.<name>.active' '
1245 test_when_finished "rm -rf multisuper_clone" &&
1247 git clone file://"$pwd"/multisuper multisuper_clone &&
1248 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1249 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1250 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1252 cat >expect <<-\EOF &&
1258 git -C multisuper_clone submodule update &&
1259 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1260 test_cmp expect actual
1263 test_expect_success 'clone active submodule without submodule url set' '
1264 test_when_finished "rm -rf test/test" &&
1266 # another dir breaks accidental relative paths still being correct
1267 git clone file://"$pwd"/multisuper test/test &&
1270 git config submodule.active "." &&
1272 # do not pass --init flag, as the submodule is already active:
1273 git submodule update &&
1274 git submodule status >actual_raw &&
1276 cut -c 1,43- actual_raw >actual &&
1277 cat >expect <<-\EOF &&
1283 test_cmp expect actual
1287 test_expect_success 'clone --recurse-submodules with a pathspec works' '
1288 test_when_finished "rm -rf multisuper_clone" &&
1289 cat >expected <<-\EOF &&
1296 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1297 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1298 test_cmp expected actual
1301 test_expect_success 'clone with multiple --recurse-submodules options' '
1302 test_when_finished "rm -rf multisuper_clone" &&
1303 cat >expect <<-\EOF &&
1310 git clone --recurse-submodules="." \
1311 --recurse-submodules=":(exclude)sub0" \
1312 --recurse-submodules=":(exclude)sub2" \
1313 multisuper multisuper_clone &&
1314 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1315 test_cmp expect actual
1318 test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
1319 test_when_finished "rm -rf multisuper_clone" &&
1320 cat <<-\EOF >expect &&
1327 cat <<-\EOF >expect2 &&
1336 git clone --recurse-submodules="." \
1337 --recurse-submodules=":(exclude)sub0" \
1338 --recurse-submodules=":(exclude)sub2" \
1339 --recurse-submodules=":(exclude)sub4" \
1340 multisuper multisuper_clone &&
1342 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1343 test_cmp expect actual &&
1345 git -C multisuper submodule add ../sub1 sub4 &&
1346 git -C multisuper submodule add ../sub1 sub5 &&
1347 git -C multisuper commit -m "add more submodules" &&
1348 # obtain the new superproject
1349 git -C multisuper_clone pull &&
1350 git -C multisuper_clone submodule update --init &&
1351 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1352 test_cmp expect2 actual
1355 test_expect_success 'init properly sets the config' '
1356 test_when_finished "rm -rf multisuper_clone" &&
1357 git clone --recurse-submodules="." \
1358 --recurse-submodules=":(exclude)sub0" \
1359 multisuper multisuper_clone &&
1361 git -C multisuper_clone submodule init -- sub0 sub1 &&
1362 git -C multisuper_clone config --get submodule.sub0.active &&
1363 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1366 test_expect_success 'recursive clone respects -q' '
1367 test_when_finished "rm -rf multisuper_clone" &&
1368 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1369 test_must_be_empty actual