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
'submodule usage: -h' '
18 git submodule -h >out 2>err &&
19 grep "^usage: git submodule" out &&
20 test_must_be_empty err
23 test_expect_success
'submodule usage: --recursive' '
24 test_expect_code 1 git submodule --recursive >out 2>err &&
25 grep "^usage: git submodule" err &&
26 test_must_be_empty out
29 test_expect_success
'submodule usage: status --' '
30 test_expect_code 1 git submodule -- &&
31 test_expect_code 1 git submodule --end-of-options
34 for opt
in '--quiet' '--cached'
36 test_expect_success
"submodule usage: status $opt" '
38 git submodule status $opt &&
39 git submodule $opt status
43 test_expect_success
'submodule deinit works on empty repository' '
44 git submodule deinit --all
47 test_expect_success
'setup - initial commit' '
50 git commit -m "initial commit" &&
54 test_expect_success
'submodule init aborts on missing .gitmodules file' '
55 test_when_finished "git update-index --remove sub" &&
56 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
57 # missing the .gitmodules file here
58 test_must_fail git submodule init 2>actual &&
59 test_i18ngrep "No url found for submodule path" actual
62 test_expect_success
'submodule update aborts on missing .gitmodules file' '
63 test_when_finished "git update-index --remove sub" &&
64 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
65 # missing the .gitmodules file here
66 git submodule update sub 2>actual &&
67 test_i18ngrep "Submodule path .sub. not initialized" actual
70 test_expect_success
'submodule update aborts on missing gitmodules url' '
71 test_when_finished "git update-index --remove sub" &&
72 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
73 test_when_finished "rm -f .gitmodules" &&
74 git config -f .gitmodules submodule.s.path sub &&
75 test_must_fail git submodule init
78 test_expect_success
'add aborts on repository with no commits' '
79 cat >expect <<-\EOF &&
80 fatal: '"'repo-no-commits'"' does not have a commit checked out
82 git init repo-no-commits &&
83 test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
84 test_cmp expect actual
87 test_expect_success
'status should ignore inner git repo when not added' '
95 git commit -m "initial"
97 test_must_fail git submodule status inner 2>output.err &&
99 test_i18ngrep "^error: .*did not match any file(s) known to git" output.err
102 test_expect_success
'setup - repository in init subdirectory' '
109 git commit -m "submodule commit 1" &&
110 git tag -a -m "rev-1" rev-1
114 test_expect_success
'setup - commit with gitlink' '
118 git commit -m "super commit 1"
121 test_expect_success
'setup - hide init subdirectory' '
125 test_expect_success
'setup - repository to add submodules to' '
127 git init addtest-ignore
130 # The 'submodule add' tests need some repository to add as a submodule.
131 # The trash directory is a good one as any. We need to canonicalize
132 # the name, though, as some tests compare it to the absolute path git
133 # generates, which will expand symbolic links.
137 git for-each-ref
--format='%(refname)' 'refs/heads/*'
146 listbranches
>"$dotdot/heads" &&
147 { git symbolic-ref HEAD ||
:; } >"$dotdot/head" &&
148 git rev-parse HEAD
>"$dotdot/head-sha1" &&
149 git update-index
--refresh &&
150 git diff-files
--exit-code &&
151 git clean
-n -d -x >"$dotdot/untracked"
155 test_expect_success
'submodule add' '
156 echo "refs/heads/main" >expect &&
160 git submodule add -q "$submodurl" submod >actual &&
161 test_must_be_empty actual &&
162 echo "gitdir: ../.git/modules/submod" >expect &&
163 test_cmp expect submod/.git &&
166 git config core.worktree >actual &&
167 echo "../../../submod" >expect &&
168 test_cmp expect actual &&
174 rm -f heads head untracked &&
175 inspect addtest/submod ../.. &&
176 test_cmp expect heads &&
177 test_cmp expect head &&
178 test_must_be_empty untracked
181 test_expect_success
!WINDOWS
'submodule add (absolute path)' '
182 test_when_finished "git reset --hard" &&
183 git submodule add "$submodurl" "$submodurl/add-abs"
186 test_expect_success
'setup parent and one repository' '
187 test_create_repo parent &&
188 test_commit -C parent one
191 test_expect_success
'redirected submodule add does not show progress' '
192 git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
195 test_i18ngrep ! "Checking connectivity" err
198 test_expect_success
'redirected submodule add --progress does show progress' '
199 git -C addtest submodule add --progress "file://$submodurl/parent" \
200 submod-redirected-progress 2>err && \
204 test_expect_success
'submodule add to .gitignored path fails' '
207 cat <<-\EOF >expect &&
208 The following paths are ignored by one of your .gitignore files:
210 hint: Use -f if you really want to add them.
211 hint: Turn this message off by running
212 hint: "git config advice.addIgnoredFile false"
214 # Does not use test_commit due to the ignore
215 echo "*" > .gitignore &&
216 git add --force .gitignore &&
217 git commit -m"Ignore everything" &&
218 ! git submodule add "$submodurl" submod >actual 2>&1 &&
219 test_cmp expect actual
223 test_expect_success
'submodule add to .gitignored path with --force' '
226 git submodule add --force "$submodurl" submod
230 test_expect_success
'submodule add to path with tracked content fails' '
233 echo "fatal: '\''dir-tracked'\'' already exists in the index" >expect &&
235 test_commit foo dir-tracked/bar &&
236 test_must_fail git submodule add "$submodurl" dir-tracked >actual 2>&1 &&
237 test_cmp expect actual
241 test_expect_success
'submodule add to reconfigure existing submodule with --force' '
244 bogus_url="$(pwd)/bogus-url" &&
245 git submodule add --force "$bogus_url" submod &&
246 git submodule add --force -b initial "$submodurl" submod-branch &&
247 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
248 test "$bogus_url" = "$(git config submodule.submod.url)" &&
250 git submodule add --force "$submodurl" submod &&
251 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
252 test "$submodurl" = "$(git config submodule.submod.url)"
256 test_expect_success
'submodule add relays add --dry-run stderr' '
257 test_when_finished "rm -rf addtest/.git/index.lock" &&
260 : >.git/index.lock &&
261 ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
262 test_i18ngrep "^fatal: .*index\.lock" output.err &&
263 test_path_is_missing sub-while-locked
267 test_expect_success
'submodule add --branch' '
268 echo "refs/heads/initial" >expect-head &&
269 cat <<-\EOF >expect-heads &&
276 git submodule add -b initial "$submodurl" submod-branch &&
277 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
281 rm -f heads head untracked &&
282 inspect addtest/submod-branch ../.. &&
283 test_cmp expect-heads heads &&
284 test_cmp expect-head head &&
285 test_must_be_empty untracked
288 test_expect_success
'submodule add with ./ in path' '
289 echo "refs/heads/main" >expect &&
293 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
297 rm -f heads head untracked &&
298 inspect addtest/dotsubmod/frotz ../../.. &&
299 test_cmp expect heads &&
300 test_cmp expect head &&
301 test_must_be_empty untracked
304 test_expect_success
'submodule add with /././ in path' '
305 echo "refs/heads/main" >expect &&
309 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
313 rm -f heads head untracked &&
314 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
315 test_cmp expect heads &&
316 test_cmp expect head &&
317 test_must_be_empty untracked
320 test_expect_success
'submodule add with // in path' '
321 echo "refs/heads/main" >expect &&
325 git submodule add "$submodurl" slashslashsubmod///frotz// &&
329 rm -f heads head untracked &&
330 inspect addtest/slashslashsubmod/frotz ../../.. &&
331 test_cmp expect heads &&
332 test_cmp expect head &&
333 test_must_be_empty untracked
336 test_expect_success
'submodule add with /.. in path' '
337 echo "refs/heads/main" >expect &&
341 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
345 rm -f heads head untracked &&
346 inspect addtest/realsubmod ../.. &&
347 test_cmp expect heads &&
348 test_cmp expect head &&
349 test_must_be_empty untracked
352 test_expect_success
'submodule add with ./, /.. and // in path' '
353 echo "refs/heads/main" >expect &&
357 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
361 rm -f heads head untracked &&
362 inspect addtest/realsubmod2 ../.. &&
363 test_cmp expect heads &&
364 test_cmp expect head &&
365 test_must_be_empty untracked
368 test_expect_success
!CYGWIN
'submodule add with \\ in path' '
369 test_when_finished "rm -rf parent sub\\with\\backslash" &&
371 # Initialize a repo with a backslash in its name
372 git init sub\\with\\backslash &&
373 touch sub\\with\\backslash/empty.file &&
374 git -C sub\\with\\backslash add empty.file &&
375 git -C sub\\with\\backslash commit -m "Added empty.file" &&
377 # Add that repository as a submodule
379 git -C parent submodule add ../sub\\with\\backslash
382 test_expect_success
'submodule add in subdirectory' '
383 echo "refs/heads/main" >expect &&
388 git submodule add "$submodurl" ../realsubmod3 &&
392 rm -f heads head untracked &&
393 inspect addtest/realsubmod3 ../.. &&
394 test_cmp expect heads &&
395 test_cmp expect head &&
396 test_must_be_empty untracked
399 test_expect_success
'submodule add in subdirectory with relative path should fail' '
402 test_must_fail git submodule add ../../ submod3 2>../../output.err
404 test_i18ngrep toplevel output.err
407 test_expect_success
'setup - add an example entry to .gitmodules' '
408 git config --file=.gitmodules submodule.example.url git://example.com/init.git
411 test_expect_success
'status should fail for unmapped paths' '
412 test_must_fail git submodule status
415 test_expect_success
'setup - map path in .gitmodules' '
416 cat <<\EOF >expect &&
417 [submodule "example"]
418 url = git://example.com/init.git
422 git config --file=.gitmodules submodule.example.path init &&
424 test_cmp expect .gitmodules
427 test_expect_success
'status should only print one line' '
428 git submodule status >lines &&
429 test_line_count = 1 lines
432 test_expect_success
'status from subdirectory should have the same SHA1' '
433 test_when_finished "rmdir addtest/subdir" &&
437 git submodule status >output &&
438 awk "{print \$1}" <output >expect &&
440 git submodule status >../output &&
441 awk "{print \$1}" <../output >../actual &&
442 test_cmp ../expect ../actual &&
443 git -C ../submod checkout HEAD^ &&
444 git submodule status >../output &&
445 awk "{print \$1}" <../output >../actual2 &&
447 git submodule status >output &&
448 awk "{print \$1}" <output >expect2 &&
449 test_cmp expect2 actual2 &&
450 ! test_cmp actual actual2
454 test_expect_success
'setup - fetch commit name from submodule' '
455 rev1=$(cd .subrepo && git rev-parse HEAD) &&
456 printf "rev1: %s\n" "$rev1" &&
460 test_expect_success
'status should initially be "missing"' '
461 git submodule status >lines &&
465 test_expect_success
'init should register submodule url in .git/config' '
466 echo git://example.com/init.git >expect &&
468 git submodule init &&
469 git config submodule.example.url >url &&
470 git config submodule.example.url ./.subrepo &&
475 test_expect_success
'status should still be "missing" after initializing' '
478 git submodule status >lines &&
483 test_failure_with_unknown_submodule
() {
484 test_must_fail git submodule
$1 no-such-submodule
2>output.err
&&
485 test_i18ngrep
"^error: .*no-such-submodule" output.err
488 test_expect_success
'init should fail with unknown submodule' '
489 test_failure_with_unknown_submodule init
492 test_expect_success
'update should fail with unknown submodule' '
493 test_failure_with_unknown_submodule update
496 test_expect_success
'status should fail with unknown submodule' '
497 test_failure_with_unknown_submodule status
500 test_expect_success
'sync should fail with unknown submodule' '
501 test_failure_with_unknown_submodule sync
504 test_expect_success
'update should fail when path is used by a file' '
505 echo hello >expect &&
507 echo "hello" >init &&
508 test_must_fail git submodule update &&
513 test_expect_success
'update should fail when path is used by a nonempty directory' '
514 echo hello >expect &&
518 echo "hello" >init/a &&
520 test_must_fail git submodule update &&
522 test_cmp expect init/a
525 test_expect_success
'update should work when path is an empty dir' '
528 echo "$rev1" >expect &&
531 git submodule update -q >update.out &&
532 test_must_be_empty update.out &&
535 test_cmp expect head-sha1
538 test_expect_success
'status should be "up-to-date" after update' '
539 git submodule status >list &&
543 test_expect_success
'status "up-to-date" from subdirectory' '
547 git submodule status >../list
549 grep "^ $rev1" list &&
550 grep "\\.\\./init" list
553 test_expect_success
'status "up-to-date" from subdirectory with path' '
557 git submodule status ../init >../list
559 grep "^ $rev1" list &&
560 grep "\\.\\./init" list
563 test_expect_success
'status should be "modified" after submodule commit' '
568 git commit -m "submodule commit 2"
571 rev2=$(cd init && git rev-parse HEAD) &&
573 git submodule status >list &&
578 test_expect_success
'the --cached sha1 should be rev1' '
579 git submodule --cached status >list &&
583 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
585 grep "^+Subproject commit $rev2" diff
588 test_expect_success
'update should checkout rev1' '
590 echo "$rev1" >expect &&
592 git submodule update init &&
595 test_cmp expect head-sha1
598 test_expect_success
'status should be "up-to-date" after update' '
599 git submodule status >list &&
603 test_expect_success
'checkout superproject with subproject already present' '
604 git checkout initial &&
608 test_expect_success
'apply submodule diff' '
614 git commit -m "change subproject"
616 git update-index --add init &&
617 git commit -m "change init" &&
618 git format-patch -1 --stdout >P.diff &&
619 git checkout second &&
620 git apply --index P.diff &&
622 git diff --cached main >staged &&
623 test_must_be_empty staged
626 test_expect_success
'update --init' '
628 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
629 git config --remove-section submodule.example &&
630 test_must_fail git config submodule.example.url &&
632 git submodule update init 2> update.out &&
633 test_i18ngrep "not initialized" update.out &&
634 test_must_fail git rev-parse --resolve-git-dir init/.git &&
636 git submodule update --init init &&
637 git rev-parse --resolve-git-dir init/.git
640 test_expect_success
'update --init from subdirectory' '
642 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
643 git config --remove-section submodule.example &&
644 test_must_fail git config submodule.example.url &&
649 git submodule update ../init 2>update.out &&
650 test_i18ngrep "not initialized" update.out &&
651 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
653 git submodule update --init ../init
655 git rev-parse --resolve-git-dir init/.git
658 test_expect_success
'do not add files from a submodule' '
661 test_must_fail git add init/a
665 test_expect_success
'gracefully add/reset submodule with a trailing slash' '
668 git commit -m "commit subproject" init &&
672 git diff --exit-code --cached init &&
674 git commit -m update a >/dev/null &&
675 git rev-parse HEAD) &&
677 test_must_fail git diff --exit-code --cached init &&
678 test $commit = $(git ls-files --stage |
679 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
681 git diff --exit-code --cached init
685 test_expect_success
'ls-files gracefully handles trailing slash' '
687 test "init" = "$(git ls-files init/)"
691 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
695 git checkout initial &&
700 test_expect_success
'submodule <invalid-subcommand> fails' '
701 test_must_fail git submodule no-such-subcommand
704 test_expect_success
'add submodules without specifying an explicit path' '
711 git commit -m "repo commit 1"
713 git clone --bare repo/ bare.git &&
716 git submodule add "$submodurl/repo" &&
717 git config -f .gitmodules submodule.repo.path repo &&
718 git submodule add "$submodurl/bare.git" &&
719 git config -f .gitmodules submodule.bare.path bare
723 test_expect_success
'add should fail when path is used by a file' '
727 test_must_fail git submodule add "$submodurl/repo" file
731 test_expect_success
'add should fail when path is used by an existing directory' '
735 test_must_fail git submodule add "$submodurl/repo" empty-dir
739 test_expect_success
'use superproject as upstream when path is relative and no url is set there' '
742 git submodule add ../repo relative &&
743 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
744 git submodule sync relative &&
745 test "$(git config submodule.relative.url)" = "$submodurl/repo"
749 test_expect_success
'set up for relative path tests' '
761 git config -f .gitmodules submodule.sub.path sub &&
762 git config -f .gitmodules submodule.sub.url ../subrepo &&
763 cp .git/config pristine-.git-config &&
764 cp .gitmodules pristine-.gitmodules
768 test_expect_success
'../subrepo works with URL - ssh://hostname/repo' '
771 cp pristine-.git-config .git/config &&
772 cp pristine-.gitmodules .gitmodules &&
773 git config remote.origin.url ssh://hostname/repo &&
774 git submodule init &&
775 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
779 test_expect_success
'../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
782 cp pristine-.git-config .git/config &&
783 cp pristine-.gitmodules .gitmodules &&
784 git config remote.origin.url ssh://hostname:22/repo &&
785 git submodule init &&
786 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
790 # About the choice of the path in the next test:
791 # - double-slash side-steps path mangling issues on Windows
792 # - it is still an absolute local path
793 # - there cannot be a server with a blank in its name just in case the
794 # path is used erroneously to access a //server/share style path
795 test_expect_success
'../subrepo path works with local path - //somewhere else/repo' '
798 cp pristine-.git-config .git/config &&
799 cp pristine-.gitmodules .gitmodules &&
800 git config remote.origin.url "//somewhere else/repo" &&
801 git submodule init &&
802 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
806 test_expect_success
'../subrepo works with file URL - file:///tmp/repo' '
809 cp pristine-.git-config .git/config &&
810 cp pristine-.gitmodules .gitmodules &&
811 git config remote.origin.url file:///tmp/repo &&
812 git submodule init &&
813 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
817 test_expect_success
'../subrepo works with helper URL- helper:://hostname/repo' '
820 cp pristine-.git-config .git/config &&
821 cp pristine-.gitmodules .gitmodules &&
822 git config remote.origin.url helper:://hostname/repo &&
823 git submodule init &&
824 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
828 test_expect_success
'../subrepo works with scp-style URL - user@host:repo' '
831 cp pristine-.git-config .git/config &&
832 git config remote.origin.url user@host:repo &&
833 git submodule init &&
834 test "$(git config submodule.sub.url)" = user@host:subrepo
838 test_expect_success
'../subrepo works with scp-style URL - user@host:path/to/repo' '
841 cp pristine-.git-config .git/config &&
842 cp pristine-.gitmodules .gitmodules &&
843 git config remote.origin.url user@host:path/to/repo &&
844 git submodule init &&
845 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
849 test_expect_success
'../subrepo works with relative local path - foo' '
852 cp pristine-.git-config .git/config &&
853 cp pristine-.gitmodules .gitmodules &&
854 git config remote.origin.url foo &&
855 # actual: fails with an error
856 git submodule init &&
857 test "$(git config submodule.sub.url)" = subrepo
861 test_expect_success
'../subrepo works with relative local path - foo/bar' '
864 cp pristine-.git-config .git/config &&
865 cp pristine-.gitmodules .gitmodules &&
866 git config remote.origin.url foo/bar &&
867 git submodule init &&
868 test "$(git config submodule.sub.url)" = foo/subrepo
872 test_expect_success
'../subrepo works with relative local path - ./foo' '
875 cp pristine-.git-config .git/config &&
876 cp pristine-.gitmodules .gitmodules &&
877 git config remote.origin.url ./foo &&
878 git submodule init &&
879 test "$(git config submodule.sub.url)" = subrepo
883 test_expect_success
'../subrepo works with relative local path - ./foo/bar' '
886 cp pristine-.git-config .git/config &&
887 cp pristine-.gitmodules .gitmodules &&
888 git config remote.origin.url ./foo/bar &&
889 git submodule init &&
890 test "$(git config submodule.sub.url)" = foo/subrepo
894 test_expect_success
'../subrepo works with relative local path - ../foo' '
897 cp pristine-.git-config .git/config &&
898 cp pristine-.gitmodules .gitmodules &&
899 git config remote.origin.url ../foo &&
900 git submodule init &&
901 test "$(git config submodule.sub.url)" = ../subrepo
905 test_expect_success
'../subrepo works with relative local path - ../foo/bar' '
908 cp pristine-.git-config .git/config &&
909 cp pristine-.gitmodules .gitmodules &&
910 git config remote.origin.url ../foo/bar &&
911 git submodule init &&
912 test "$(git config submodule.sub.url)" = ../foo/subrepo
916 test_expect_success
'../bar/a/b/c works with relative local path - ../foo/bar.git' '
919 cp pristine-.git-config .git/config &&
920 cp pristine-.gitmodules .gitmodules &&
922 (cd a/b/c && git init && test_commit msg) &&
923 git config remote.origin.url ../foo/bar.git &&
924 git submodule add ../bar/a/b/c ./a/b/c &&
925 git submodule init &&
926 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
930 test_expect_success
'moving the superproject does not break submodules' '
933 git submodule status >expect
935 mv addtest addtest2 &&
938 git submodule status >actual &&
939 test_cmp expect actual
943 test_expect_success
'moving the submodule does not break the superproject' '
948 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
949 mv addtest2/repo addtest2/repo.bak &&
950 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
955 test_cmp expect actual
958 test_expect_success
'submodule add --name allows to replace a submodule with another at the same path' '
963 echo "$submodurl/repo" >expect &&
964 git config remote.origin.url >actual &&
965 test_cmp expect actual &&
966 echo "gitdir: ../.git/modules/repo" >expect &&
971 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
972 test_must_be_empty actual &&
973 echo "gitdir: ../.git/modules/submod" >expect &&
974 test_cmp expect submod/.git &&
977 echo "$submodurl/bare.git" >expect &&
978 git config remote.origin.url >actual &&
979 test_cmp expect actual &&
980 echo "gitdir: ../.git/modules/repo_new" >expect &&
983 echo "repo" >expect &&
984 test_must_fail git config -f .gitmodules submodule.repo.path &&
985 git config -f .gitmodules submodule.repo_new.path >actual &&
986 test_cmp expect actual &&
987 echo "$submodurl/repo" >expect &&
988 test_must_fail git config -f .gitmodules submodule.repo.url &&
989 echo "$submodurl/bare.git" >expect &&
990 git config -f .gitmodules submodule.repo_new.url >actual &&
991 test_cmp expect actual &&
992 echo "$submodurl/repo" >expect &&
993 git config submodule.repo.url >actual &&
994 test_cmp expect actual &&
995 echo "$submodurl/bare.git" >expect &&
996 git config submodule.repo_new.url >actual &&
997 test_cmp expect actual
1001 test_expect_success
'recursive relative submodules stay relative' '
1002 test_when_finished "rm -rf super clone2 subsub sub3" &&
1009 git commit -m "initial commit"
1017 git commit -m "initial commit" &&
1018 git submodule add ../subsub dirdir/subsub &&
1019 git commit -m "add submodule subsub"
1027 git commit -m "initial commit" &&
1028 git submodule add ../sub3 &&
1029 git commit -m "add submodule sub"
1031 git clone super clone2 &&
1034 git submodule update --init --recursive &&
1035 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
1036 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
1038 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
1039 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
1042 test_expect_success
'submodule add with an existing name fails unless forced' '
1047 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
1049 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
1050 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
1051 echo "$submodurl/bare.git" >expect &&
1052 git config submodule.repo_new.url >actual &&
1053 test_cmp expect actual &&
1054 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
1056 echo "repo" >expect &&
1057 git config -f .gitmodules submodule.repo_new.path >actual &&
1058 test_cmp expect actual &&
1059 echo "$submodurl/repo.git" >expect &&
1060 git config -f .gitmodules submodule.repo_new.url >actual &&
1061 test_cmp expect actual &&
1062 echo "$submodurl/repo.git" >expect &&
1063 git config submodule.repo_new.url >actual &&
1064 test_cmp expect actual
1068 test_expect_success
'set up a second submodule' '
1069 git submodule add ./init2 example2 &&
1070 git commit -m "submodule example2 added"
1073 test_expect_success
'submodule deinit works on repository without submodules' '
1074 test_when_finished "rm -rf newdirectory" &&
1075 mkdir newdirectory &&
1081 git commit -m "repo should not be empty" &&
1082 git submodule deinit . &&
1083 git submodule deinit --all
1087 test_expect_success
'submodule deinit should remove the whole submodule section from .git/config' '
1088 git config submodule.example.foo bar &&
1089 git config submodule.example2.frotz nitfol &&
1090 git submodule deinit init &&
1091 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1092 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1093 test -f example2/.git &&
1097 test_expect_success
'submodule deinit should unset core.worktree' '
1098 test_path_is_file .git/modules/example/config &&
1099 test_must_fail git config -f .git/modules/example/config core.worktree
1102 test_expect_success
'submodule deinit from subdirectory' '
1103 git submodule update --init &&
1104 git config submodule.example.foo bar &&
1108 git submodule deinit ../init >../output
1110 test_i18ngrep "\\.\\./init" output &&
1111 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1112 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1113 test -f example2/.git &&
1117 test_expect_success
'submodule deinit . deinits all initialized submodules' '
1118 git submodule update --init &&
1119 git config submodule.example.foo bar &&
1120 git config submodule.example2.frotz nitfol &&
1121 test_must_fail git submodule deinit &&
1122 git submodule deinit . >actual &&
1123 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1124 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1125 test_i18ngrep "Cleared directory .init" actual &&
1126 test_i18ngrep "Cleared directory .example2" actual &&
1130 test_expect_success
'submodule deinit --all deinits all initialized submodules' '
1131 git submodule update --init &&
1132 git config submodule.example.foo bar &&
1133 git config submodule.example2.frotz nitfol &&
1134 test_must_fail git submodule deinit &&
1135 git submodule deinit --all >actual &&
1136 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1137 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1138 test_i18ngrep "Cleared directory .init" actual &&
1139 test_i18ngrep "Cleared directory .example2" actual &&
1143 test_expect_success
'submodule deinit deinits a submodule when its work tree is missing or empty' '
1144 git submodule update --init &&
1145 rm -rf init example2/* example2/.git &&
1146 git submodule deinit init example2 >actual &&
1147 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1148 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1149 test_i18ngrep ! "Cleared directory .init" actual &&
1150 test_i18ngrep "Cleared directory .example2" actual &&
1154 test_expect_success
'submodule deinit fails when the submodule contains modifications unless forced' '
1155 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 fails when the submodule contains untracked files unless forced' '
1167 git submodule update --init &&
1168 echo X >>init/untracked &&
1169 test_must_fail git submodule deinit init &&
1170 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1171 test -f example2/.git &&
1172 git submodule deinit -f init >actual &&
1173 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1174 test_i18ngrep "Cleared directory .init" actual &&
1178 test_expect_success
'submodule deinit fails when the submodule HEAD does not match unless forced' '
1179 git submodule update --init &&
1184 test_must_fail git submodule deinit init &&
1185 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1186 test -f example2/.git &&
1187 git submodule deinit -f init >actual &&
1188 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1189 test_i18ngrep "Cleared directory .init" actual &&
1193 test_expect_success
'submodule deinit is silent when used on an uninitialized submodule' '
1194 git submodule update --init &&
1195 git submodule deinit init >actual &&
1196 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1197 test_i18ngrep "Cleared directory .init" actual &&
1198 git submodule deinit init >actual &&
1199 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1200 test_i18ngrep "Cleared directory .init" actual &&
1201 git submodule deinit . >actual &&
1202 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1203 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1204 test_i18ngrep "Cleared directory .init" actual &&
1205 git submodule deinit . >actual &&
1206 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1207 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1208 test_i18ngrep "Cleared directory .init" actual &&
1209 git submodule deinit --all >actual &&
1210 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1211 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1212 test_i18ngrep "Cleared directory .init" actual &&
1216 test_expect_success
'submodule deinit absorbs .git directory if .git is a directory' '
1217 git submodule update --init &&
1221 mv ../.git/modules/example .git &&
1222 GIT_WORK_TREE=. git config --unset core.worktree
1224 git submodule deinit init &&
1225 test_path_is_missing init/.git &&
1226 test -z "$(git config --get-regexp "submodule\.example\.")"
1229 test_expect_success
'submodule with UTF-8 name' '
1230 svname=$(printf "\303\245 \303\244\303\266") &&
1237 git commit -m "init sub"
1239 git submodule add ./"$svname" &&
1240 git submodule >&2 &&
1241 test -n "$(git submodule | grep "$svname")"
1244 test_expect_success
'submodule add clone shallow submodule' '
1250 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1253 test 1 = $(git log --oneline | wc -l)
1258 test_expect_success
'setup superproject with submodules' '
1260 test_commit -C sub1 test &&
1261 test_commit -C sub1 test2 &&
1262 git init multisuper &&
1263 git -C multisuper submodule add ../sub1 sub0 &&
1264 git -C multisuper submodule add ../sub1 sub1 &&
1265 git -C multisuper submodule add ../sub1 sub2 &&
1266 git -C multisuper submodule add ../sub1 sub3 &&
1267 git -C multisuper commit -m "add some submodules"
1277 test_expect_success
'submodule update --init with a specification' '
1278 test_when_finished "rm -rf multisuper_clone" &&
1280 git clone file://"$pwd"/multisuper multisuper_clone &&
1281 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1282 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1283 test_cmp expect actual
1286 test_expect_success
'submodule update --init with submodule.active set' '
1287 test_when_finished "rm -rf multisuper_clone" &&
1289 git clone file://"$pwd"/multisuper multisuper_clone &&
1290 git -C multisuper_clone config submodule.active "." &&
1291 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1292 git -C multisuper_clone submodule update --init &&
1293 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1294 test_cmp expect actual
1297 test_expect_success
'submodule update and setting submodule.<name>.active' '
1298 test_when_finished "rm -rf multisuper_clone" &&
1300 git clone file://"$pwd"/multisuper multisuper_clone &&
1301 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1302 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1303 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1305 cat >expect <<-\EOF &&
1311 git -C multisuper_clone submodule update &&
1312 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1313 test_cmp expect actual
1316 test_expect_success
'clone active submodule without submodule url set' '
1317 test_when_finished "rm -rf test/test" &&
1319 # another dir breaks accidental relative paths still being correct
1320 git clone file://"$pwd"/multisuper test/test &&
1323 git config submodule.active "." &&
1325 # do not pass --init flag, as the submodule is already active:
1326 git submodule update &&
1327 git submodule status >actual_raw &&
1329 cut -d" " -f3- actual_raw >actual &&
1330 cat >expect <<-\EOF &&
1336 test_cmp expect actual
1340 test_expect_success
'clone --recurse-submodules with a pathspec works' '
1341 test_when_finished "rm -rf multisuper_clone" &&
1342 cat >expected <<-\EOF &&
1349 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1350 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1351 test_cmp expected actual
1354 test_expect_success
'clone with multiple --recurse-submodules options' '
1355 test_when_finished "rm -rf multisuper_clone" &&
1356 cat >expect <<-\EOF &&
1363 git clone --recurse-submodules="." \
1364 --recurse-submodules=":(exclude)sub0" \
1365 --recurse-submodules=":(exclude)sub2" \
1366 multisuper multisuper_clone &&
1367 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1368 test_cmp expect actual
1371 test_expect_success
'clone and subsequent updates correctly auto-initialize submodules' '
1372 test_when_finished "rm -rf multisuper_clone" &&
1373 cat <<-\EOF >expect &&
1380 cat <<-\EOF >expect2 &&
1389 git clone --recurse-submodules="." \
1390 --recurse-submodules=":(exclude)sub0" \
1391 --recurse-submodules=":(exclude)sub2" \
1392 --recurse-submodules=":(exclude)sub4" \
1393 multisuper multisuper_clone &&
1395 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1396 test_cmp expect actual &&
1398 git -C multisuper submodule add ../sub1 sub4 &&
1399 git -C multisuper submodule add ../sub1 sub5 &&
1400 git -C multisuper commit -m "add more submodules" &&
1401 # obtain the new superproject
1402 git -C multisuper_clone pull &&
1403 git -C multisuper_clone submodule update --init &&
1404 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1405 test_cmp expect2 actual
1408 test_expect_success
'init properly sets the config' '
1409 test_when_finished "rm -rf multisuper_clone" &&
1410 git clone --recurse-submodules="." \
1411 --recurse-submodules=":(exclude)sub0" \
1412 multisuper multisuper_clone &&
1414 git -C multisuper_clone submodule init -- sub0 sub1 &&
1415 git -C multisuper_clone config --get submodule.sub0.active &&
1416 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1419 test_expect_success
'recursive clone respects -q' '
1420 test_when_finished "rm -rf multisuper_clone" &&
1421 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1422 test_must_be_empty actual