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.
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
17 test_expect_success
'setup - enable local submodules' '
18 git config --global protocol.file.allow always
21 test_expect_success
'submodule deinit works on empty repository' '
22 git submodule deinit --all
25 test_expect_success
'setup - initial commit' '
28 git commit -m "initial commit" &&
32 test_expect_success
'submodule init aborts on missing .gitmodules file' '
33 test_when_finished "git update-index --remove sub" &&
34 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
35 # missing the .gitmodules file here
36 test_must_fail git submodule init 2>actual &&
37 test_i18ngrep "No url found for submodule path" actual
40 test_expect_success
'submodule update aborts on missing .gitmodules file' '
41 test_when_finished "git update-index --remove sub" &&
42 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
43 # missing the .gitmodules file here
44 git submodule update sub 2>actual &&
45 test_i18ngrep "Submodule path .sub. not initialized" actual
48 test_expect_success
'submodule update aborts on missing gitmodules url' '
49 test_when_finished "git update-index --remove sub" &&
50 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
51 test_when_finished "rm -f .gitmodules" &&
52 git config -f .gitmodules submodule.s.path sub &&
53 test_must_fail git submodule init
56 test_expect_success
'add aborts on repository with no commits' '
57 cat >expect <<-\EOF &&
58 fatal: '"'repo-no-commits'"' does not have a commit checked out
60 git init repo-no-commits &&
61 test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
62 test_cmp expect actual
65 test_expect_success
'status should ignore inner git repo when not added' '
73 git commit -m "initial"
75 test_must_fail git submodule status inner 2>output.err &&
77 test_i18ngrep "^error: .*did not match any file(s) known to git" output.err
80 test_expect_success
'setup - repository in init subdirectory' '
87 git commit -m "submodule commit 1" &&
88 git tag -a -m "rev-1" rev-1
92 test_expect_success
'setup - commit with gitlink' '
96 git commit -m "super commit 1"
99 test_expect_success
'setup - hide init subdirectory' '
103 test_expect_success
'setup - repository to add submodules to' '
105 git init addtest-ignore
108 # The 'submodule add' tests need some repository to add as a submodule.
109 # The trash directory is a good one as any. We need to canonicalize
110 # the name, though, as some tests compare it to the absolute path git
111 # generates, which will expand symbolic links.
115 git for-each-ref
--format='%(refname)' 'refs/heads/*'
124 listbranches
>"$dotdot/heads" &&
125 { git symbolic-ref HEAD ||
:; } >"$dotdot/head" &&
126 git rev-parse HEAD
>"$dotdot/head-sha1" &&
127 git update-index
--refresh &&
128 git diff-files
--exit-code &&
129 git clean
-n -d -x >"$dotdot/untracked"
133 test_expect_success
'submodule add' '
134 echo "refs/heads/main" >expect &&
138 git submodule add -q "$submodurl" submod >actual &&
139 test_must_be_empty actual &&
140 echo "gitdir: ../.git/modules/submod" >expect &&
141 test_cmp expect submod/.git &&
144 git config core.worktree >actual &&
145 echo "../../../submod" >expect &&
146 test_cmp expect actual &&
152 rm -f heads head untracked &&
153 inspect addtest/submod ../.. &&
154 test_cmp expect heads &&
155 test_cmp expect head &&
156 test_must_be_empty untracked
159 test_expect_success
'setup parent and one repository' '
160 test_create_repo parent &&
161 test_commit -C parent one
164 test_expect_success
'redirected submodule add does not show progress' '
165 git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
168 test_i18ngrep ! "Checking connectivity" err
171 test_expect_success
'redirected submodule add --progress does show progress' '
172 git -C addtest submodule add --progress "file://$submodurl/parent" \
173 submod-redirected-progress 2>err && \
177 test_expect_success
'submodule add to .gitignored path fails' '
180 cat <<-\EOF >expect &&
181 The following paths are ignored by one of your .gitignore files:
183 hint: Use -f if you really want to add them.
184 hint: Turn this message off by running
185 hint: "git config advice.addIgnoredFile false"
187 # Does not use test_commit due to the ignore
188 echo "*" > .gitignore &&
189 git add --force .gitignore &&
190 git commit -m"Ignore everything" &&
191 ! git submodule add "$submodurl" submod >actual 2>&1 &&
192 test_cmp expect actual
196 test_expect_success
'submodule add to .gitignored path with --force' '
199 git submodule add --force "$submodurl" submod
203 test_expect_success
'submodule add to path with tracked content fails' '
206 echo "fatal: '\''dir-tracked'\'' already exists in the index" >expect &&
208 test_commit foo dir-tracked/bar &&
209 test_must_fail git submodule add "$submodurl" dir-tracked >actual 2>&1 &&
210 test_cmp expect actual
214 test_expect_success
'submodule add to reconfigure existing submodule with --force' '
217 bogus_url="$(pwd)/bogus-url" &&
218 git submodule add --force "$bogus_url" submod &&
219 git submodule add --force -b initial "$submodurl" submod-branch &&
220 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
221 test "$bogus_url" = "$(git config submodule.submod.url)" &&
223 git submodule add --force "$submodurl" submod &&
224 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
225 test "$submodurl" = "$(git config submodule.submod.url)"
229 test_expect_success
'submodule add relays add --dry-run stderr' '
230 test_when_finished "rm -rf addtest/.git/index.lock" &&
233 : >.git/index.lock &&
234 ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
235 test_i18ngrep "^fatal: .*index\.lock" output.err &&
236 test_path_is_missing sub-while-locked
240 test_expect_success
'submodule add --branch' '
241 echo "refs/heads/initial" >expect-head &&
242 cat <<-\EOF >expect-heads &&
249 git submodule add -b initial "$submodurl" submod-branch &&
250 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
254 rm -f heads head untracked &&
255 inspect addtest/submod-branch ../.. &&
256 test_cmp expect-heads heads &&
257 test_cmp expect-head head &&
258 test_must_be_empty untracked
261 test_expect_success
'submodule add with ./ in path' '
262 echo "refs/heads/main" >expect &&
266 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
270 rm -f heads head untracked &&
271 inspect addtest/dotsubmod/frotz ../../.. &&
272 test_cmp expect heads &&
273 test_cmp expect head &&
274 test_must_be_empty untracked
277 test_expect_success
'submodule add with /././ in path' '
278 echo "refs/heads/main" >expect &&
282 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
286 rm -f heads head untracked &&
287 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
288 test_cmp expect heads &&
289 test_cmp expect head &&
290 test_must_be_empty untracked
293 test_expect_success
'submodule add with // in path' '
294 echo "refs/heads/main" >expect &&
298 git submodule add "$submodurl" slashslashsubmod///frotz// &&
302 rm -f heads head untracked &&
303 inspect addtest/slashslashsubmod/frotz ../../.. &&
304 test_cmp expect heads &&
305 test_cmp expect head &&
306 test_must_be_empty untracked
309 test_expect_success
'submodule add with /.. in path' '
310 echo "refs/heads/main" >expect &&
314 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
318 rm -f heads head untracked &&
319 inspect addtest/realsubmod ../.. &&
320 test_cmp expect heads &&
321 test_cmp expect head &&
322 test_must_be_empty untracked
325 test_expect_success
'submodule add with ./, /.. and // in path' '
326 echo "refs/heads/main" >expect &&
330 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
334 rm -f heads head untracked &&
335 inspect addtest/realsubmod2 ../.. &&
336 test_cmp expect heads &&
337 test_cmp expect head &&
338 test_must_be_empty untracked
341 test_expect_success
!CYGWIN
'submodule add with \\ in path' '
342 test_when_finished "rm -rf parent sub\\with\\backslash" &&
344 # Initialize a repo with a backslash in its name
345 git init sub\\with\\backslash &&
346 touch sub\\with\\backslash/empty.file &&
347 git -C sub\\with\\backslash add empty.file &&
348 git -C sub\\with\\backslash commit -m "Added empty.file" &&
350 # Add that repository as a submodule
352 git -C parent submodule add ../sub\\with\\backslash
355 test_expect_success
'submodule add in subdirectory' '
356 echo "refs/heads/main" >expect &&
361 git submodule add "$submodurl" ../realsubmod3 &&
365 rm -f heads head untracked &&
366 inspect addtest/realsubmod3 ../.. &&
367 test_cmp expect heads &&
368 test_cmp expect head &&
369 test_must_be_empty untracked
372 test_expect_success
'submodule add in subdirectory with relative path should fail' '
375 test_must_fail git submodule add ../../ submod3 2>../../output.err
377 test_i18ngrep toplevel output.err
380 test_expect_success
'setup - add an example entry to .gitmodules' '
381 git config --file=.gitmodules submodule.example.url git://example.com/init.git
384 test_expect_success
'status should fail for unmapped paths' '
385 test_must_fail git submodule status
388 test_expect_success
'setup - map path in .gitmodules' '
389 cat <<\EOF >expect &&
390 [submodule "example"]
391 url = git://example.com/init.git
395 git config --file=.gitmodules submodule.example.path init &&
397 test_cmp expect .gitmodules
400 test_expect_success
'status should only print one line' '
401 git submodule status >lines &&
402 test_line_count = 1 lines
405 test_expect_success
'status from subdirectory should have the same SHA1' '
406 test_when_finished "rmdir addtest/subdir" &&
410 git submodule status >output &&
411 awk "{print \$1}" <output >expect &&
413 git submodule status >../output &&
414 awk "{print \$1}" <../output >../actual &&
415 test_cmp ../expect ../actual &&
416 git -C ../submod checkout HEAD^ &&
417 git submodule status >../output &&
418 awk "{print \$1}" <../output >../actual2 &&
420 git submodule status >output &&
421 awk "{print \$1}" <output >expect2 &&
422 test_cmp expect2 actual2 &&
423 ! test_cmp actual actual2
427 test_expect_success
'setup - fetch commit name from submodule' '
428 rev1=$(cd .subrepo && git rev-parse HEAD) &&
429 printf "rev1: %s\n" "$rev1" &&
433 test_expect_success
'status should initially be "missing"' '
434 git submodule status >lines &&
438 test_expect_success
'init should register submodule url in .git/config' '
439 echo git://example.com/init.git >expect &&
441 git submodule init &&
442 git config submodule.example.url >url &&
443 git config submodule.example.url ./.subrepo &&
448 test_expect_success
'status should still be "missing" after initializing' '
451 git submodule status >lines &&
456 test_failure_with_unknown_submodule
() {
457 test_must_fail git submodule
$1 no-such-submodule
2>output.err
&&
458 test_i18ngrep
"^error: .*no-such-submodule" output.err
461 test_expect_success
'init should fail with unknown submodule' '
462 test_failure_with_unknown_submodule init
465 test_expect_success
'update should fail with unknown submodule' '
466 test_failure_with_unknown_submodule update
469 test_expect_success
'status should fail with unknown submodule' '
470 test_failure_with_unknown_submodule status
473 test_expect_success
'sync should fail with unknown submodule' '
474 test_failure_with_unknown_submodule sync
477 test_expect_success
'update should fail when path is used by a file' '
478 echo hello >expect &&
480 echo "hello" >init &&
481 test_must_fail git submodule update &&
486 test_expect_success
'update should fail when path is used by a nonempty directory' '
487 echo hello >expect &&
491 echo "hello" >init/a &&
493 test_must_fail git submodule update &&
495 test_cmp expect init/a
498 test_expect_success
'update should work when path is an empty dir' '
501 echo "$rev1" >expect &&
504 git submodule update -q >update.out &&
505 test_must_be_empty update.out &&
508 test_cmp expect head-sha1
511 test_expect_success
'status should be "up-to-date" after update' '
512 git submodule status >list &&
516 test_expect_success
'status "up-to-date" from subdirectory' '
520 git submodule status >../list
522 grep "^ $rev1" list &&
523 grep "\\.\\./init" list
526 test_expect_success
'status "up-to-date" from subdirectory with path' '
530 git submodule status ../init >../list
532 grep "^ $rev1" list &&
533 grep "\\.\\./init" list
536 test_expect_success
'status should be "modified" after submodule commit' '
541 git commit -m "submodule commit 2"
544 rev2=$(cd init && git rev-parse HEAD) &&
546 git submodule status >list &&
551 test_expect_success
'the --cached sha1 should be rev1' '
552 git submodule --cached status >list &&
556 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
558 grep "^+Subproject commit $rev2" diff
561 test_expect_success
'update should checkout rev1' '
563 echo "$rev1" >expect &&
565 git submodule update init &&
568 test_cmp expect head-sha1
571 test_expect_success
'status should be "up-to-date" after update' '
572 git submodule status >list &&
576 test_expect_success
'checkout superproject with subproject already present' '
577 git checkout initial &&
581 test_expect_success
'apply submodule diff' '
587 git commit -m "change subproject"
589 git update-index --add init &&
590 git commit -m "change init" &&
591 git format-patch -1 --stdout >P.diff &&
592 git checkout second &&
593 git apply --index P.diff &&
595 git diff --cached main >staged &&
596 test_must_be_empty staged
599 test_expect_success
'update --init' '
601 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
602 git config --remove-section submodule.example &&
603 test_must_fail git config submodule.example.url &&
605 git submodule update init 2> update.out &&
606 test_i18ngrep "not initialized" update.out &&
607 test_must_fail git rev-parse --resolve-git-dir init/.git &&
609 git submodule update --init init &&
610 git rev-parse --resolve-git-dir init/.git
613 test_expect_success
'update --init from subdirectory' '
615 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
616 git config --remove-section submodule.example &&
617 test_must_fail git config submodule.example.url &&
622 git submodule update ../init 2>update.out &&
623 test_i18ngrep "not initialized" update.out &&
624 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
626 git submodule update --init ../init
628 git rev-parse --resolve-git-dir init/.git
631 test_expect_success
'do not add files from a submodule' '
634 test_must_fail git add init/a
638 test_expect_success
'gracefully add/reset submodule with a trailing slash' '
641 git commit -m "commit subproject" init &&
645 git diff --exit-code --cached init &&
647 git commit -m update a >/dev/null &&
648 git rev-parse HEAD) &&
650 test_must_fail git diff --exit-code --cached init &&
651 test $commit = $(git ls-files --stage |
652 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
654 git diff --exit-code --cached init
658 test_expect_success
'ls-files gracefully handles trailing slash' '
660 test "init" = "$(git ls-files init/)"
664 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
668 git checkout initial &&
673 test_expect_success
'submodule <invalid-subcommand> fails' '
674 test_must_fail git submodule no-such-subcommand
677 test_expect_success
'add submodules without specifying an explicit path' '
684 git commit -m "repo commit 1"
686 git clone --bare repo/ bare.git &&
689 git submodule add "$submodurl/repo" &&
690 git config -f .gitmodules submodule.repo.path repo &&
691 git submodule add "$submodurl/bare.git" &&
692 git config -f .gitmodules submodule.bare.path bare
696 test_expect_success
'add should fail when path is used by a file' '
700 test_must_fail git submodule add "$submodurl/repo" file
704 test_expect_success
'add should fail when path is used by an existing directory' '
708 test_must_fail git submodule add "$submodurl/repo" empty-dir
712 test_expect_success
'use superproject as upstream when path is relative and no url is set there' '
715 git submodule add ../repo relative &&
716 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
717 git submodule sync relative &&
718 test "$(git config submodule.relative.url)" = "$submodurl/repo"
722 test_expect_success
'set up for relative path tests' '
734 git config -f .gitmodules submodule.sub.path sub &&
735 git config -f .gitmodules submodule.sub.url ../subrepo &&
736 cp .git/config pristine-.git-config &&
737 cp .gitmodules pristine-.gitmodules
741 test_expect_success
'../subrepo works with URL - ssh://hostname/repo' '
744 cp pristine-.git-config .git/config &&
745 cp pristine-.gitmodules .gitmodules &&
746 git config remote.origin.url ssh://hostname/repo &&
747 git submodule init &&
748 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
752 test_expect_success
'../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
755 cp pristine-.git-config .git/config &&
756 cp pristine-.gitmodules .gitmodules &&
757 git config remote.origin.url ssh://hostname:22/repo &&
758 git submodule init &&
759 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
763 # About the choice of the path in the next test:
764 # - double-slash side-steps path mangling issues on Windows
765 # - it is still an absolute local path
766 # - there cannot be a server with a blank in its name just in case the
767 # path is used erroneously to access a //server/share style path
768 test_expect_success
'../subrepo path works with local path - //somewhere else/repo' '
771 cp pristine-.git-config .git/config &&
772 cp pristine-.gitmodules .gitmodules &&
773 git config remote.origin.url "//somewhere else/repo" &&
774 git submodule init &&
775 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
779 test_expect_success
'../subrepo works with file URL - file:///tmp/repo' '
782 cp pristine-.git-config .git/config &&
783 cp pristine-.gitmodules .gitmodules &&
784 git config remote.origin.url file:///tmp/repo &&
785 git submodule init &&
786 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
790 test_expect_success
'../subrepo works with helper URL- helper:://hostname/repo' '
793 cp pristine-.git-config .git/config &&
794 cp pristine-.gitmodules .gitmodules &&
795 git config remote.origin.url helper:://hostname/repo &&
796 git submodule init &&
797 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
801 test_expect_success
'../subrepo works with scp-style URL - user@host:repo' '
804 cp pristine-.git-config .git/config &&
805 git config remote.origin.url user@host:repo &&
806 git submodule init &&
807 test "$(git config submodule.sub.url)" = user@host:subrepo
811 test_expect_success
'../subrepo works with scp-style URL - user@host:path/to/repo' '
814 cp pristine-.git-config .git/config &&
815 cp pristine-.gitmodules .gitmodules &&
816 git config remote.origin.url user@host:path/to/repo &&
817 git submodule init &&
818 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
822 test_expect_success
'../subrepo works with relative local path - foo' '
825 cp pristine-.git-config .git/config &&
826 cp pristine-.gitmodules .gitmodules &&
827 git config remote.origin.url foo &&
828 # actual: fails with an error
829 git submodule init &&
830 test "$(git config submodule.sub.url)" = subrepo
834 test_expect_success
'../subrepo works with relative local path - foo/bar' '
837 cp pristine-.git-config .git/config &&
838 cp pristine-.gitmodules .gitmodules &&
839 git config remote.origin.url foo/bar &&
840 git submodule init &&
841 test "$(git config submodule.sub.url)" = foo/subrepo
845 test_expect_success
'../subrepo works with relative local path - ./foo' '
848 cp pristine-.git-config .git/config &&
849 cp pristine-.gitmodules .gitmodules &&
850 git config remote.origin.url ./foo &&
851 git submodule init &&
852 test "$(git config submodule.sub.url)" = subrepo
856 test_expect_success
'../subrepo works with relative local path - ./foo/bar' '
859 cp pristine-.git-config .git/config &&
860 cp pristine-.gitmodules .gitmodules &&
861 git config remote.origin.url ./foo/bar &&
862 git submodule init &&
863 test "$(git config submodule.sub.url)" = foo/subrepo
867 test_expect_success
'../subrepo works with relative local path - ../foo' '
870 cp pristine-.git-config .git/config &&
871 cp pristine-.gitmodules .gitmodules &&
872 git config remote.origin.url ../foo &&
873 git submodule init &&
874 test "$(git config submodule.sub.url)" = ../subrepo
878 test_expect_success
'../subrepo works with relative local path - ../foo/bar' '
881 cp pristine-.git-config .git/config &&
882 cp pristine-.gitmodules .gitmodules &&
883 git config remote.origin.url ../foo/bar &&
884 git submodule init &&
885 test "$(git config submodule.sub.url)" = ../foo/subrepo
889 test_expect_success
'../bar/a/b/c works with relative local path - ../foo/bar.git' '
892 cp pristine-.git-config .git/config &&
893 cp pristine-.gitmodules .gitmodules &&
895 (cd a/b/c && git init && test_commit msg) &&
896 git config remote.origin.url ../foo/bar.git &&
897 git submodule add ../bar/a/b/c ./a/b/c &&
898 git submodule init &&
899 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
903 test_expect_success
'moving the superproject does not break submodules' '
906 git submodule status >expect
908 mv addtest addtest2 &&
911 git submodule status >actual &&
912 test_cmp expect actual
916 test_expect_success
'moving the submodule does not break the superproject' '
921 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
922 mv addtest2/repo addtest2/repo.bak &&
923 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
928 test_cmp expect actual
931 test_expect_success
'submodule add --name allows to replace a submodule with another at the same path' '
936 echo "$submodurl/repo" >expect &&
937 git config remote.origin.url >actual &&
938 test_cmp expect actual &&
939 echo "gitdir: ../.git/modules/repo" >expect &&
944 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
945 test_must_be_empty actual &&
946 echo "gitdir: ../.git/modules/submod" >expect &&
947 test_cmp expect submod/.git &&
950 echo "$submodurl/bare.git" >expect &&
951 git config remote.origin.url >actual &&
952 test_cmp expect actual &&
953 echo "gitdir: ../.git/modules/repo_new" >expect &&
956 echo "repo" >expect &&
957 test_must_fail git config -f .gitmodules submodule.repo.path &&
958 git config -f .gitmodules submodule.repo_new.path >actual &&
959 test_cmp expect actual &&
960 echo "$submodurl/repo" >expect &&
961 test_must_fail git config -f .gitmodules submodule.repo.url &&
962 echo "$submodurl/bare.git" >expect &&
963 git config -f .gitmodules submodule.repo_new.url >actual &&
964 test_cmp expect actual &&
965 echo "$submodurl/repo" >expect &&
966 git config submodule.repo.url >actual &&
967 test_cmp expect actual &&
968 echo "$submodurl/bare.git" >expect &&
969 git config submodule.repo_new.url >actual &&
970 test_cmp expect actual
974 test_expect_success
'recursive relative submodules stay relative' '
975 test_when_finished "rm -rf super clone2 subsub sub3" &&
982 git commit -m "initial commit"
990 git commit -m "initial commit" &&
991 git submodule add ../subsub dirdir/subsub &&
992 git commit -m "add submodule subsub"
1000 git commit -m "initial commit" &&
1001 git submodule add ../sub3 &&
1002 git commit -m "add submodule sub"
1004 git clone super clone2 &&
1007 git submodule update --init --recursive &&
1008 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
1009 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
1011 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
1012 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
1015 test_expect_success
'submodule add with an existing name fails unless forced' '
1020 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
1022 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
1023 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
1024 echo "$submodurl/bare.git" >expect &&
1025 git config submodule.repo_new.url >actual &&
1026 test_cmp expect actual &&
1027 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
1029 echo "repo" >expect &&
1030 git config -f .gitmodules submodule.repo_new.path >actual &&
1031 test_cmp expect actual &&
1032 echo "$submodurl/repo.git" >expect &&
1033 git config -f .gitmodules submodule.repo_new.url >actual &&
1034 test_cmp expect actual &&
1035 echo "$submodurl/repo.git" >expect &&
1036 git config submodule.repo_new.url >actual &&
1037 test_cmp expect actual
1041 test_expect_success
'set up a second submodule' '
1042 git submodule add ./init2 example2 &&
1043 git commit -m "submodule example2 added"
1046 test_expect_success
'submodule deinit works on repository without submodules' '
1047 test_when_finished "rm -rf newdirectory" &&
1048 mkdir newdirectory &&
1054 git commit -m "repo should not be empty" &&
1055 git submodule deinit . &&
1056 git submodule deinit --all
1060 test_expect_success
'submodule deinit should remove the whole submodule section from .git/config' '
1061 git config submodule.example.foo bar &&
1062 git config submodule.example2.frotz nitfol &&
1063 git submodule deinit init &&
1064 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1065 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1066 test -f example2/.git &&
1070 test_expect_success
'submodule deinit should unset core.worktree' '
1071 test_path_is_file .git/modules/example/config &&
1072 test_must_fail git config -f .git/modules/example/config core.worktree
1075 test_expect_success
'submodule deinit from subdirectory' '
1076 git submodule update --init &&
1077 git config submodule.example.foo bar &&
1081 git submodule deinit ../init >../output
1083 test_i18ngrep "\\.\\./init" output &&
1084 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1085 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1086 test -f example2/.git &&
1090 test_expect_success
'submodule deinit . deinits all initialized submodules' '
1091 git submodule update --init &&
1092 git config submodule.example.foo bar &&
1093 git config submodule.example2.frotz nitfol &&
1094 test_must_fail git submodule deinit &&
1095 git submodule deinit . >actual &&
1096 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1097 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1098 test_i18ngrep "Cleared directory .init" actual &&
1099 test_i18ngrep "Cleared directory .example2" actual &&
1103 test_expect_success
'submodule deinit --all deinits all initialized submodules' '
1104 git submodule update --init &&
1105 git config submodule.example.foo bar &&
1106 git config submodule.example2.frotz nitfol &&
1107 test_must_fail git submodule deinit &&
1108 git submodule deinit --all >actual &&
1109 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1110 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1111 test_i18ngrep "Cleared directory .init" actual &&
1112 test_i18ngrep "Cleared directory .example2" actual &&
1116 test_expect_success
'submodule deinit deinits a submodule when its work tree is missing or empty' '
1117 git submodule update --init &&
1118 rm -rf init example2/* example2/.git &&
1119 git submodule deinit init example2 >actual &&
1120 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1121 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1122 test_i18ngrep ! "Cleared directory .init" actual &&
1123 test_i18ngrep "Cleared directory .example2" actual &&
1127 test_expect_success
'submodule deinit fails when the submodule contains modifications unless forced' '
1128 git submodule update --init &&
1130 test_must_fail git submodule deinit init &&
1131 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1132 test -f example2/.git &&
1133 git submodule deinit -f init >actual &&
1134 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1135 test_i18ngrep "Cleared directory .init" actual &&
1139 test_expect_success
'submodule deinit fails when the submodule contains untracked files unless forced' '
1140 git submodule update --init &&
1141 echo X >>init/untracked &&
1142 test_must_fail git submodule deinit init &&
1143 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1144 test -f example2/.git &&
1145 git submodule deinit -f init >actual &&
1146 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1147 test_i18ngrep "Cleared directory .init" actual &&
1151 test_expect_success
'submodule deinit fails when the submodule HEAD does not match unless forced' '
1152 git submodule update --init &&
1157 test_must_fail git submodule deinit init &&
1158 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1159 test -f example2/.git &&
1160 git submodule deinit -f init >actual &&
1161 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1162 test_i18ngrep "Cleared directory .init" actual &&
1166 test_expect_success
'submodule deinit is silent when used on an uninitialized submodule' '
1167 git submodule update --init &&
1168 git submodule deinit init >actual &&
1169 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1170 test_i18ngrep "Cleared directory .init" actual &&
1171 git submodule deinit init >actual &&
1172 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1173 test_i18ngrep "Cleared directory .init" actual &&
1174 git submodule deinit . >actual &&
1175 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1176 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1177 test_i18ngrep "Cleared directory .init" actual &&
1178 git submodule deinit . >actual &&
1179 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1180 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1181 test_i18ngrep "Cleared directory .init" actual &&
1182 git submodule deinit --all >actual &&
1183 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1184 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1185 test_i18ngrep "Cleared directory .init" actual &&
1189 test_expect_success
'submodule deinit fails when submodule has a .git directory even when forced' '
1190 git submodule update --init &&
1194 cp -R ../.git/modules/example .git &&
1195 GIT_WORK_TREE=. git config --unset core.worktree
1197 test_must_fail git submodule deinit init &&
1198 test_must_fail git submodule deinit -f init &&
1199 test -d init/.git &&
1200 test -n "$(git config --get-regexp "submodule\.example\.")"
1203 test_expect_success
'submodule with UTF-8 name' '
1204 svname=$(printf "\303\245 \303\244\303\266") &&
1211 git commit -m "init sub"
1213 git submodule add ./"$svname" &&
1214 git submodule >&2 &&
1215 test -n "$(git submodule | grep "$svname")"
1218 test_expect_success
'submodule add clone shallow submodule' '
1224 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1227 test 1 = $(git log --oneline | wc -l)
1232 test_expect_success
'submodule helper list is not confused by common prefixes' '
1237 echo hi >testfile2 &&
1239 git commit -m "test1"
1245 echo hello >testfile1 &&
1247 git commit -m "test2"
1249 git submodule add /dir1/b dir1/b &&
1250 git submodule add /dir2/b dir2/b &&
1251 git commit -m "first submodule commit" &&
1252 git submodule--helper list dir1/b | cut -f 2 >actual &&
1253 echo "dir1/b" >expect &&
1254 test_cmp expect actual
1257 test_expect_success
'setup superproject with submodules' '
1259 test_commit -C sub1 test &&
1260 test_commit -C sub1 test2 &&
1261 git init multisuper &&
1262 git -C multisuper submodule add ../sub1 sub0 &&
1263 git -C multisuper submodule add ../sub1 sub1 &&
1264 git -C multisuper submodule add ../sub1 sub2 &&
1265 git -C multisuper submodule add ../sub1 sub3 &&
1266 git -C multisuper commit -m "add some submodules"
1276 test_expect_success
'submodule update --init with a specification' '
1277 test_when_finished "rm -rf multisuper_clone" &&
1279 git clone file://"$pwd"/multisuper multisuper_clone &&
1280 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1281 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1282 test_cmp expect actual
1285 test_expect_success
'submodule update --init with submodule.active set' '
1286 test_when_finished "rm -rf multisuper_clone" &&
1288 git clone file://"$pwd"/multisuper multisuper_clone &&
1289 git -C multisuper_clone config submodule.active "." &&
1290 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1291 git -C multisuper_clone submodule update --init &&
1292 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1293 test_cmp expect actual
1296 test_expect_success
'submodule update and setting submodule.<name>.active' '
1297 test_when_finished "rm -rf multisuper_clone" &&
1299 git clone file://"$pwd"/multisuper multisuper_clone &&
1300 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1301 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1302 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1304 cat >expect <<-\EOF &&
1310 git -C multisuper_clone submodule update &&
1311 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1312 test_cmp expect actual
1315 test_expect_success
'clone active submodule without submodule url set' '
1316 test_when_finished "rm -rf test/test" &&
1318 # another dir breaks accidental relative paths still being correct
1319 git clone file://"$pwd"/multisuper test/test &&
1322 git config submodule.active "." &&
1324 # do not pass --init flag, as the submodule is already active:
1325 git submodule update &&
1326 git submodule status >actual_raw &&
1328 cut -d" " -f3- actual_raw >actual &&
1329 cat >expect <<-\EOF &&
1335 test_cmp expect actual
1339 test_expect_success
'clone --recurse-submodules with a pathspec works' '
1340 test_when_finished "rm -rf multisuper_clone" &&
1341 cat >expected <<-\EOF &&
1348 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1349 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1350 test_cmp expected actual
1353 test_expect_success
'clone with multiple --recurse-submodules options' '
1354 test_when_finished "rm -rf multisuper_clone" &&
1355 cat >expect <<-\EOF &&
1362 git clone --recurse-submodules="." \
1363 --recurse-submodules=":(exclude)sub0" \
1364 --recurse-submodules=":(exclude)sub2" \
1365 multisuper multisuper_clone &&
1366 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1367 test_cmp expect actual
1370 test_expect_success
'clone and subsequent updates correctly auto-initialize submodules' '
1371 test_when_finished "rm -rf multisuper_clone" &&
1372 cat <<-\EOF >expect &&
1379 cat <<-\EOF >expect2 &&
1388 git clone --recurse-submodules="." \
1389 --recurse-submodules=":(exclude)sub0" \
1390 --recurse-submodules=":(exclude)sub2" \
1391 --recurse-submodules=":(exclude)sub4" \
1392 multisuper multisuper_clone &&
1394 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1395 test_cmp expect actual &&
1397 git -C multisuper submodule add ../sub1 sub4 &&
1398 git -C multisuper submodule add ../sub1 sub5 &&
1399 git -C multisuper commit -m "add more submodules" &&
1400 # obtain the new superproject
1401 git -C multisuper_clone pull &&
1402 git -C multisuper_clone submodule update --init &&
1403 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1404 test_cmp expect2 actual
1407 test_expect_success
'init properly sets the config' '
1408 test_when_finished "rm -rf multisuper_clone" &&
1409 git clone --recurse-submodules="." \
1410 --recurse-submodules=":(exclude)sub0" \
1411 multisuper multisuper_clone &&
1413 git -C multisuper_clone submodule init -- sub0 sub1 &&
1414 git -C multisuper_clone config --get submodule.sub0.active &&
1415 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1418 test_expect_success
'recursive clone respects -q' '
1419 test_when_finished "rm -rf multisuper_clone" &&
1420 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1421 test_must_be_empty actual