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 &&
552 test_i18ngrep "not initialized" update.out &&
553 test_must_fail git rev-parse --resolve-git-dir init/.git &&
555 git submodule update --init init &&
556 git rev-parse --resolve-git-dir init/.git
559 test_expect_success 'update --init from subdirectory' '
561 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
562 git config --remove-section submodule.example &&
563 test_must_fail git config submodule.example.url &&
568 git submodule update ../init 2>update.out &&
569 test_i18ngrep "not initialized" update.out &&
570 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
572 git submodule update --init ../init
574 git rev-parse --resolve-git-dir init/.git
577 test_expect_success 'do not add files from a submodule' '
580 test_must_fail git add init/a
584 test_expect_success 'gracefully add/reset submodule with a trailing slash' '
587 git commit -m "commit subproject" init &&
591 git diff --exit-code --cached init &&
593 git commit -m update a >/dev/null &&
594 git rev-parse HEAD) &&
596 test_must_fail git diff --exit-code --cached init &&
597 test $commit = $(git ls-files --stage |
598 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
600 git diff --exit-code --cached init
604 test_expect_success 'ls-files gracefully handles trailing slash' '
606 test "init" = "$(git ls-files init/)"
610 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
614 git checkout initial &&
619 test_expect_success 'submodule <invalid-subcommand> fails' '
620 test_must_fail git submodule no-such-subcommand
623 test_expect_success 'add submodules without specifying an explicit path' '
630 git commit -m "repo commit 1"
632 git clone --bare repo/ bare.git &&
635 git submodule add "$submodurl/repo" &&
636 git config -f .gitmodules submodule.repo.path repo &&
637 git submodule add "$submodurl/bare.git" &&
638 git config -f .gitmodules submodule.bare.path bare
642 test_expect_success 'add should fail when path is used by a file' '
646 test_must_fail git submodule add "$submodurl/repo" file
650 test_expect_success 'add should fail when path is used by an existing directory' '
654 test_must_fail git submodule add "$submodurl/repo" empty-dir
658 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
661 git submodule add ../repo relative &&
662 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
663 git submodule sync relative &&
664 test "$(git config submodule.relative.url)" = "$submodurl/repo"
668 test_expect_success 'set up for relative path tests' '
680 git config -f .gitmodules submodule.sub.path sub &&
681 git config -f .gitmodules submodule.sub.url ../subrepo &&
682 cp .git/config pristine-.git-config &&
683 cp .gitmodules pristine-.gitmodules
687 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
690 cp pristine-.git-config .git/config &&
691 cp pristine-.gitmodules .gitmodules &&
692 git config remote.origin.url ssh://hostname/repo &&
693 git submodule init &&
694 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
698 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
701 cp pristine-.git-config .git/config &&
702 cp pristine-.gitmodules .gitmodules &&
703 git config remote.origin.url ssh://hostname:22/repo &&
704 git submodule init &&
705 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
709 # About the choice of the path in the next test:
710 # - double-slash side-steps path mangling issues on Windows
711 # - it is still an absolute local path
712 # - there cannot be a server with a blank in its name just in case the
713 # path is used erroneously to access a //server/share style path
714 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
717 cp pristine-.git-config .git/config &&
718 cp pristine-.gitmodules .gitmodules &&
719 git config remote.origin.url "//somewhere else/repo" &&
720 git submodule init &&
721 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
725 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
728 cp pristine-.git-config .git/config &&
729 cp pristine-.gitmodules .gitmodules &&
730 git config remote.origin.url file:///tmp/repo &&
731 git submodule init &&
732 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
736 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
739 cp pristine-.git-config .git/config &&
740 cp pristine-.gitmodules .gitmodules &&
741 git config remote.origin.url helper:://hostname/repo &&
742 git submodule init &&
743 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
747 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
750 cp pristine-.git-config .git/config &&
751 git config remote.origin.url user@host:repo &&
752 git submodule init &&
753 test "$(git config submodule.sub.url)" = user@host:subrepo
757 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
760 cp pristine-.git-config .git/config &&
761 cp pristine-.gitmodules .gitmodules &&
762 git config remote.origin.url user@host:path/to/repo &&
763 git submodule init &&
764 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
768 test_expect_success '../subrepo works with relative local path - foo' '
771 cp pristine-.git-config .git/config &&
772 cp pristine-.gitmodules .gitmodules &&
773 git config remote.origin.url foo &&
774 # actual: fails with an error
775 git submodule init &&
776 test "$(git config submodule.sub.url)" = subrepo
780 test_expect_success '../subrepo works with relative local path - foo/bar' '
783 cp pristine-.git-config .git/config &&
784 cp pristine-.gitmodules .gitmodules &&
785 git config remote.origin.url foo/bar &&
786 git submodule init &&
787 test "$(git config submodule.sub.url)" = foo/subrepo
791 test_expect_success '../subrepo works with relative local path - ./foo' '
794 cp pristine-.git-config .git/config &&
795 cp pristine-.gitmodules .gitmodules &&
796 git config remote.origin.url ./foo &&
797 git submodule init &&
798 test "$(git config submodule.sub.url)" = subrepo
802 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
805 cp pristine-.git-config .git/config &&
806 cp pristine-.gitmodules .gitmodules &&
807 git config remote.origin.url ./foo/bar &&
808 git submodule init &&
809 test "$(git config submodule.sub.url)" = foo/subrepo
813 test_expect_success '../subrepo works with relative local path - ../foo' '
816 cp pristine-.git-config .git/config &&
817 cp pristine-.gitmodules .gitmodules &&
818 git config remote.origin.url ../foo &&
819 git submodule init &&
820 test "$(git config submodule.sub.url)" = ../subrepo
824 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
827 cp pristine-.git-config .git/config &&
828 cp pristine-.gitmodules .gitmodules &&
829 git config remote.origin.url ../foo/bar &&
830 git submodule init &&
831 test "$(git config submodule.sub.url)" = ../foo/subrepo
835 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
838 cp pristine-.git-config .git/config &&
839 cp pristine-.gitmodules .gitmodules &&
841 (cd a/b/c && git init && test_commit msg) &&
842 git config remote.origin.url ../foo/bar.git &&
843 git submodule add ../bar/a/b/c ./a/b/c &&
844 git submodule init &&
845 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
849 test_expect_success 'moving the superproject does not break submodules' '
852 git submodule status >expect
854 mv addtest addtest2 &&
857 git submodule status >actual &&
858 test_cmp expect actual
862 test_expect_success 'moving the submodule does not break the superproject' '
867 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
868 mv addtest2/repo addtest2/repo.bak &&
869 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
874 test_cmp expect actual
877 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
882 echo "$submodurl/repo" >expect &&
883 git config remote.origin.url >actual &&
884 test_cmp expect actual &&
885 echo "gitdir: ../.git/modules/repo" >expect &&
890 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
891 test_must_be_empty actual &&
892 echo "gitdir: ../.git/modules/submod" >expect &&
893 test_cmp expect submod/.git &&
896 echo "$submodurl/bare.git" >expect &&
897 git config remote.origin.url >actual &&
898 test_cmp expect actual &&
899 echo "gitdir: ../.git/modules/repo_new" >expect &&
902 echo "repo" >expect &&
903 test_must_fail git config -f .gitmodules submodule.repo.path &&
904 git config -f .gitmodules submodule.repo_new.path >actual &&
905 test_cmp expect actual&&
906 echo "$submodurl/repo" >expect &&
907 test_must_fail git config -f .gitmodules submodule.repo.url &&
908 echo "$submodurl/bare.git" >expect &&
909 git config -f .gitmodules submodule.repo_new.url >actual &&
910 test_cmp expect actual &&
911 echo "$submodurl/repo" >expect &&
912 git config submodule.repo.url >actual &&
913 test_cmp expect actual &&
914 echo "$submodurl/bare.git" >expect &&
915 git config submodule.repo_new.url >actual &&
916 test_cmp expect actual
920 test_expect_success 'recursive relative submodules stay relative' '
921 test_when_finished "rm -rf super clone2 subsub sub3" &&
928 git commit -m "initial commit"
936 git commit -m "initial commit" &&
937 git submodule add ../subsub dirdir/subsub &&
938 git commit -m "add submodule subsub"
946 git commit -m "initial commit" &&
947 git submodule add ../sub3 &&
948 git commit -m "add submodule sub"
950 git clone super clone2 &&
953 git submodule update --init --recursive &&
954 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
955 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
957 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
958 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
961 test_expect_success 'submodule add with an existing name fails unless forced' '
966 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
968 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
969 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
970 echo "$submodurl/bare.git" >expect &&
971 git config submodule.repo_new.url >actual &&
972 test_cmp expect actual &&
973 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
975 echo "repo" >expect &&
976 git config -f .gitmodules submodule.repo_new.path >actual &&
977 test_cmp expect actual&&
978 echo "$submodurl/repo.git" >expect &&
979 git config -f .gitmodules submodule.repo_new.url >actual &&
980 test_cmp expect actual &&
981 echo "$submodurl/repo.git" >expect &&
982 git config submodule.repo_new.url >actual &&
983 test_cmp expect actual
987 test_expect_success 'set up a second submodule' '
988 git submodule add ./init2 example2 &&
989 git commit -m "submodule example2 added"
992 test_expect_success 'submodule deinit works on repository without submodules' '
993 test_when_finished "rm -rf newdirectory" &&
994 mkdir newdirectory &&
1000 git commit -m "repo should not be empty" &&
1001 git submodule deinit . &&
1002 git submodule deinit --all
1006 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
1007 git config submodule.example.foo bar &&
1008 git config submodule.example2.frotz nitfol &&
1009 git submodule deinit init &&
1010 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1011 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1012 test -f example2/.git &&
1016 test_expect_success 'submodule deinit should unset core.worktree' '
1017 test_path_is_file .git/modules/example/config &&
1018 test_must_fail git config -f .git/modules/example/config core.worktree
1021 test_expect_success 'submodule deinit from subdirectory' '
1022 git submodule update --init &&
1023 git config submodule.example.foo bar &&
1027 git submodule deinit ../init >../output
1029 test_i18ngrep "\\.\\./init" output &&
1030 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1031 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1032 test -f example2/.git &&
1036 test_expect_success 'submodule deinit . deinits all initialized submodules' '
1037 git submodule update --init &&
1038 git config submodule.example.foo bar &&
1039 git config submodule.example2.frotz nitfol &&
1040 test_must_fail git submodule deinit &&
1041 git submodule deinit . >actual &&
1042 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1043 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1044 test_i18ngrep "Cleared directory .init" actual &&
1045 test_i18ngrep "Cleared directory .example2" actual &&
1049 test_expect_success 'submodule deinit --all deinits all initialized submodules' '
1050 git submodule update --init &&
1051 git config submodule.example.foo bar &&
1052 git config submodule.example2.frotz nitfol &&
1053 test_must_fail git submodule deinit &&
1054 git submodule deinit --all >actual &&
1055 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1056 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1057 test_i18ngrep "Cleared directory .init" actual &&
1058 test_i18ngrep "Cleared directory .example2" actual &&
1062 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
1063 git submodule update --init &&
1064 rm -rf init example2/* example2/.git &&
1065 git submodule deinit init example2 >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 fails when the submodule contains modifications unless forced' '
1074 git submodule update --init &&
1076 test_must_fail git submodule deinit init &&
1077 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1078 test -f example2/.git &&
1079 git submodule deinit -f init >actual &&
1080 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1081 test_i18ngrep "Cleared directory .init" actual &&
1085 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1086 git submodule update --init &&
1087 echo X >>init/untracked &&
1088 test_must_fail git submodule deinit init &&
1089 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1090 test -f example2/.git &&
1091 git submodule deinit -f init >actual &&
1092 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1093 test_i18ngrep "Cleared directory .init" actual &&
1097 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1098 git submodule update --init &&
1103 test_must_fail git submodule deinit init &&
1104 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1105 test -f example2/.git &&
1106 git submodule deinit -f init >actual &&
1107 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1108 test_i18ngrep "Cleared directory .init" actual &&
1112 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1113 git submodule update --init &&
1114 git submodule deinit init >actual &&
1115 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1116 test_i18ngrep "Cleared directory .init" actual &&
1117 git submodule deinit init >actual &&
1118 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1119 test_i18ngrep "Cleared directory .init" actual &&
1120 git submodule deinit . >actual &&
1121 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1122 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1123 test_i18ngrep "Cleared directory .init" actual &&
1124 git submodule deinit . >actual &&
1125 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1126 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1127 test_i18ngrep "Cleared directory .init" actual &&
1128 git submodule deinit --all >actual &&
1129 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1130 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1131 test_i18ngrep "Cleared directory .init" actual &&
1135 test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
1136 git submodule update --init &&
1140 cp -R ../.git/modules/example .git &&
1141 GIT_WORK_TREE=. git config --unset core.worktree
1143 test_must_fail git submodule deinit init &&
1144 test_must_fail git submodule deinit -f init &&
1145 test -d init/.git &&
1146 test -n "$(git config --get-regexp "submodule\.example\.")"
1149 test_expect_success 'submodule with UTF-8 name' '
1150 svname=$(printf "\303\245 \303\244\303\266") &&
1157 git commit -m "init sub"
1159 git submodule add ./"$svname" &&
1160 git submodule >&2 &&
1161 test -n "$(git submodule | grep "$svname")"
1164 test_expect_success 'submodule add clone shallow submodule' '
1170 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1173 test 1 = $(git log --oneline | wc -l)
1178 test_expect_success 'submodule helper list is not confused by common prefixes' '
1183 echo hi >testfile2 &&
1185 git commit -m "test1"
1191 echo hello >testfile1 &&
1193 git commit -m "test2"
1195 git submodule add /dir1/b dir1/b &&
1196 git submodule add /dir2/b dir2/b &&
1197 git commit -m "first submodule commit" &&
1198 git submodule--helper list dir1/b |cut -c51- >actual &&
1199 echo "dir1/b" >expect &&
1200 test_cmp expect actual
1203 test_expect_success 'setup superproject with submodules' '
1205 test_commit -C sub1 test &&
1206 test_commit -C sub1 test2 &&
1207 git init multisuper &&
1208 git -C multisuper submodule add ../sub1 sub0 &&
1209 git -C multisuper submodule add ../sub1 sub1 &&
1210 git -C multisuper submodule add ../sub1 sub2 &&
1211 git -C multisuper submodule add ../sub1 sub3 &&
1212 git -C multisuper commit -m "add some submodules"
1222 test_expect_success 'submodule update --init with a specification' '
1223 test_when_finished "rm -rf multisuper_clone" &&
1225 git clone file://"$pwd"/multisuper multisuper_clone &&
1226 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1227 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1228 test_cmp expect actual
1231 test_expect_success 'submodule update --init with submodule.active set' '
1232 test_when_finished "rm -rf multisuper_clone" &&
1234 git clone file://"$pwd"/multisuper multisuper_clone &&
1235 git -C multisuper_clone config submodule.active "." &&
1236 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1237 git -C multisuper_clone submodule update --init &&
1238 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1239 test_cmp expect actual
1242 test_expect_success 'submodule update and setting submodule.<name>.active' '
1243 test_when_finished "rm -rf multisuper_clone" &&
1245 git clone file://"$pwd"/multisuper multisuper_clone &&
1246 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1247 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1248 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1250 cat >expect <<-\EOF &&
1256 git -C multisuper_clone submodule update &&
1257 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1258 test_cmp expect actual
1261 test_expect_success 'clone active submodule without submodule url set' '
1262 test_when_finished "rm -rf test/test" &&
1264 # another dir breaks accidental relative paths still being correct
1265 git clone file://"$pwd"/multisuper test/test &&
1268 git config submodule.active "." &&
1270 # do not pass --init flag, as the submodule is already active:
1271 git submodule update &&
1272 git submodule status >actual_raw &&
1274 cut -c 1,43- actual_raw >actual &&
1275 cat >expect <<-\EOF &&
1281 test_cmp expect actual
1285 test_expect_success 'clone --recurse-submodules with a pathspec works' '
1286 test_when_finished "rm -rf multisuper_clone" &&
1287 cat >expected <<-\EOF &&
1294 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1295 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1296 test_cmp expected actual
1299 test_expect_success 'clone with multiple --recurse-submodules options' '
1300 test_when_finished "rm -rf multisuper_clone" &&
1301 cat >expect <<-\EOF &&
1308 git clone --recurse-submodules="." \
1309 --recurse-submodules=":(exclude)sub0" \
1310 --recurse-submodules=":(exclude)sub2" \
1311 multisuper multisuper_clone &&
1312 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1313 test_cmp expect actual
1316 test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
1317 test_when_finished "rm -rf multisuper_clone" &&
1318 cat <<-\EOF >expect &&
1325 cat <<-\EOF >expect2 &&
1334 git clone --recurse-submodules="." \
1335 --recurse-submodules=":(exclude)sub0" \
1336 --recurse-submodules=":(exclude)sub2" \
1337 --recurse-submodules=":(exclude)sub4" \
1338 multisuper multisuper_clone &&
1340 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1341 test_cmp expect actual &&
1343 git -C multisuper submodule add ../sub1 sub4 &&
1344 git -C multisuper submodule add ../sub1 sub5 &&
1345 git -C multisuper commit -m "add more submodules" &&
1346 # obtain the new superproject
1347 git -C multisuper_clone pull &&
1348 git -C multisuper_clone submodule update --init &&
1349 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1350 test_cmp expect2 actual
1353 test_expect_success 'init properly sets the config' '
1354 test_when_finished "rm -rf multisuper_clone" &&
1355 git clone --recurse-submodules="." \
1356 --recurse-submodules=":(exclude)sub0" \
1357 multisuper multisuper_clone &&
1359 git -C multisuper_clone submodule init -- sub0 sub1 &&
1360 git -C multisuper_clone config --get submodule.sub0.active &&
1361 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1364 test_expect_success 'recursive clone respects -q' '
1365 test_when_finished "rm -rf multisuper_clone" &&
1366 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1367 test_must_be_empty actual