Merge branch 'rj/cygwin-has-dev-tty'
[git/gitster.git] / t / t7400-submodule-basic.sh
blob098d8833b6599d935e095453b3b3a865f20b6bc2
1 #!/bin/sh
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
15 TEST_PASSES_SANITIZE_LEAK=true
16 . ./test-lib.sh
18 test_expect_success 'setup - enable local submodules' '
19 git config --global protocol.file.allow always
22 test_expect_success 'submodule usage: -h' '
23 git submodule -h >out 2>err &&
24 grep "^usage: git submodule" out &&
25 test_must_be_empty err
28 test_expect_success 'submodule usage: --recursive' '
29 test_expect_code 1 git submodule --recursive >out 2>err &&
30 grep "^usage: git submodule" err &&
31 test_must_be_empty out
34 test_expect_success 'submodule usage: status --' '
35 test_expect_code 1 git submodule -- &&
36 test_expect_code 1 git submodule --end-of-options
39 for opt in '--quiet' '--cached'
41 test_expect_success "submodule usage: status $opt" '
42 git submodule $opt &&
43 git submodule status $opt &&
44 git submodule $opt status
46 done
48 test_expect_success 'submodule deinit works on empty repository' '
49 git submodule deinit --all
52 test_expect_success 'setup - initial commit' '
53 >t &&
54 git add t &&
55 git commit -m "initial commit" &&
56 git branch initial
59 test_expect_success 'submodule init aborts on missing .gitmodules file' '
60 test_when_finished "git update-index --remove sub" &&
61 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
62 # missing the .gitmodules file here
63 test_must_fail git submodule init 2>actual &&
64 test_grep "No url found for submodule path" actual
67 test_expect_success 'submodule update aborts on missing .gitmodules file' '
68 test_when_finished "git update-index --remove sub" &&
69 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
70 # missing the .gitmodules file here
71 git submodule update sub 2>actual &&
72 test_grep "Submodule path .sub. not initialized" actual
75 test_expect_success 'submodule update aborts on missing gitmodules url' '
76 test_when_finished "git update-index --remove sub" &&
77 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
78 test_when_finished "rm -f .gitmodules" &&
79 git config -f .gitmodules submodule.s.path sub &&
80 test_must_fail git submodule init
83 test_expect_success 'add aborts on repository with no commits' '
84 cat >expect <<-\EOF &&
85 fatal: '"'repo-no-commits'"' does not have a commit checked out
86 EOF
87 git init repo-no-commits &&
88 test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
89 test_cmp expect actual
92 test_expect_success 'status should ignore inner git repo when not added' '
93 rm -fr inner &&
94 mkdir inner &&
96 cd inner &&
97 git init &&
98 >t &&
99 git add t &&
100 git commit -m "initial"
101 ) &&
102 test_must_fail git submodule status inner 2>output.err &&
103 rm -fr inner &&
104 test_grep "^error: .*did not match any file(s) known to git" output.err
107 test_expect_success 'setup - repository in init subdirectory' '
108 mkdir init &&
110 cd init &&
111 git init &&
112 echo a >a &&
113 git add a &&
114 git commit -m "submodule commit 1" &&
115 git tag -a -m "rev-1" rev-1
119 test_expect_success 'setup - commit with gitlink' '
120 echo a >a &&
121 echo z >z &&
122 git add a init z &&
123 git commit -m "super commit 1"
126 test_expect_success 'setup - hide init subdirectory' '
127 mv init .subrepo
130 test_expect_success 'setup - repository to add submodules to' '
131 git init addtest &&
132 git init addtest-ignore
135 # The 'submodule add' tests need some repository to add as a submodule.
136 # The trash directory is a good one as any. We need to canonicalize
137 # the name, though, as some tests compare it to the absolute path git
138 # generates, which will expand symbolic links.
139 submodurl=$(pwd -P)
141 listbranches() {
142 git for-each-ref --format='%(refname)' 'refs/heads/*'
145 inspect() {
146 dir=$1 &&
147 dotdot="${2:-..}" &&
150 cd "$dir" &&
151 listbranches >"$dotdot/heads" &&
152 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
153 git rev-parse HEAD >"$dotdot/head-sha1" &&
154 git update-index --refresh &&
155 git diff-files --exit-code &&
156 git clean -n -d -x >"$dotdot/untracked"
160 test_expect_success 'submodule add' '
161 echo "refs/heads/main" >expect &&
164 cd addtest &&
165 git submodule add -q "$submodurl" submod >actual &&
166 test_must_be_empty actual &&
167 echo "gitdir: ../.git/modules/submod" >expect &&
168 test_cmp expect submod/.git &&
170 cd submod &&
171 git config core.worktree >actual &&
172 echo "../../../submod" >expect &&
173 test_cmp expect actual &&
174 rm -f actual expect
175 ) &&
176 git submodule init
177 ) &&
179 rm -f heads head untracked &&
180 inspect addtest/submod ../.. &&
181 test_cmp expect heads &&
182 test_cmp expect head &&
183 test_must_be_empty untracked
186 test_expect_success !WINDOWS 'submodule add (absolute path)' '
187 test_when_finished "git reset --hard" &&
188 git submodule add "$submodurl" "$submodurl/add-abs"
191 test_expect_success 'setup parent and one repository' '
192 test_create_repo parent &&
193 test_commit -C parent one
196 test_expect_success 'redirected submodule add does not show progress' '
197 git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
198 2>err &&
199 ! grep % err &&
200 test_grep ! "Checking connectivity" err
203 test_expect_success 'redirected submodule add --progress does show progress' '
204 git -C addtest submodule add --progress "file://$submodurl/parent" \
205 submod-redirected-progress 2>err && \
206 grep % err
209 test_expect_success 'submodule add to .gitignored path fails' '
211 cd addtest-ignore &&
212 cat <<-\EOF >expect &&
213 The following paths are ignored by one of your .gitignore files:
214 submod
215 hint: Use -f if you really want to add them.
216 hint: Disable this message with "git config advice.addIgnoredFile false"
218 # Does not use test_commit due to the ignore
219 echo "*" > .gitignore &&
220 git add --force .gitignore &&
221 git commit -m"Ignore everything" &&
222 ! git submodule add "$submodurl" submod >actual 2>&1 &&
223 test_cmp expect actual
227 test_expect_success 'submodule add to .gitignored path with --force' '
229 cd addtest-ignore &&
230 git submodule add --force "$submodurl" submod
234 test_expect_success 'submodule add to path with tracked content fails' '
236 cd addtest &&
237 echo "fatal: '\''dir-tracked'\'' already exists in the index" >expect &&
238 mkdir dir-tracked &&
239 test_commit foo dir-tracked/bar &&
240 test_must_fail git submodule add "$submodurl" dir-tracked >actual 2>&1 &&
241 test_cmp expect actual
245 test_expect_success 'submodule add to reconfigure existing submodule with --force' '
247 cd addtest-ignore &&
248 bogus_url="$(pwd)/bogus-url" &&
249 git submodule add --force "$bogus_url" submod &&
250 git submodule add --force -b initial "$submodurl" submod-branch &&
251 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
252 test "$bogus_url" = "$(git config submodule.submod.url)" &&
253 # Restore the url
254 git submodule add --force "$submodurl" submod &&
255 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
256 test "$submodurl" = "$(git config submodule.submod.url)"
260 test_expect_success 'submodule add relays add --dry-run stderr' '
261 test_when_finished "rm -rf addtest/.git/index.lock" &&
263 cd addtest &&
264 : >.git/index.lock &&
265 ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
266 test_grep "^fatal: .*index\.lock" output.err &&
267 test_path_is_missing sub-while-locked
271 test_expect_success 'submodule add --branch' '
272 echo "refs/heads/initial" >expect-head &&
273 cat <<-\EOF >expect-heads &&
274 refs/heads/initial
275 refs/heads/main
279 cd addtest &&
280 git submodule add -b initial "$submodurl" submod-branch &&
281 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
282 git submodule init
283 ) &&
285 rm -f heads head untracked &&
286 inspect addtest/submod-branch ../.. &&
287 test_cmp expect-heads heads &&
288 test_cmp expect-head head &&
289 test_must_be_empty untracked
292 test_expect_success 'submodule add with ./ in path' '
293 echo "refs/heads/main" >expect &&
296 cd addtest &&
297 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
298 git submodule init
299 ) &&
301 rm -f heads head untracked &&
302 inspect addtest/dotsubmod/frotz ../../.. &&
303 test_cmp expect heads &&
304 test_cmp expect head &&
305 test_must_be_empty untracked
308 test_expect_success 'submodule add with /././ in path' '
309 echo "refs/heads/main" >expect &&
312 cd addtest &&
313 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
314 git submodule init
315 ) &&
317 rm -f heads head untracked &&
318 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
319 test_cmp expect heads &&
320 test_cmp expect head &&
321 test_must_be_empty untracked
324 test_expect_success 'submodule add with // in path' '
325 echo "refs/heads/main" >expect &&
328 cd addtest &&
329 git submodule add "$submodurl" slashslashsubmod///frotz// &&
330 git submodule init
331 ) &&
333 rm -f heads head untracked &&
334 inspect addtest/slashslashsubmod/frotz ../../.. &&
335 test_cmp expect heads &&
336 test_cmp expect head &&
337 test_must_be_empty untracked
340 test_expect_success 'submodule add with /.. in path' '
341 echo "refs/heads/main" >expect &&
344 cd addtest &&
345 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
346 git submodule init
347 ) &&
349 rm -f heads head untracked &&
350 inspect addtest/realsubmod ../.. &&
351 test_cmp expect heads &&
352 test_cmp expect head &&
353 test_must_be_empty untracked
356 test_expect_success 'submodule add with ./, /.. and // in path' '
357 echo "refs/heads/main" >expect &&
360 cd addtest &&
361 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
362 git submodule init
363 ) &&
365 rm -f heads head untracked &&
366 inspect addtest/realsubmod2 ../.. &&
367 test_cmp expect heads &&
368 test_cmp expect head &&
369 test_must_be_empty untracked
372 test_expect_success !CYGWIN 'submodule add with \\ in path' '
373 test_when_finished "rm -rf parent sub\\with\\backslash" &&
375 # Initialize a repo with a backslash in its name
376 git init sub\\with\\backslash &&
377 touch sub\\with\\backslash/empty.file &&
378 git -C sub\\with\\backslash add empty.file &&
379 git -C sub\\with\\backslash commit -m "Added empty.file" &&
381 # Add that repository as a submodule
382 git init parent &&
383 git -C parent submodule add ../sub\\with\\backslash
386 test_expect_success 'submodule add in subdirectory' '
387 echo "refs/heads/main" >expect &&
389 mkdir addtest/sub &&
391 cd addtest/sub &&
392 git submodule add "$submodurl" ../realsubmod3 &&
393 git submodule init
394 ) &&
396 rm -f heads head untracked &&
397 inspect addtest/realsubmod3 ../.. &&
398 test_cmp expect heads &&
399 test_cmp expect head &&
400 test_must_be_empty untracked
403 test_expect_success 'submodule add in subdirectory with relative path should fail' '
405 cd addtest/sub &&
406 test_must_fail git submodule add ../../ submod3 2>../../output.err
407 ) &&
408 test_grep toplevel output.err
411 test_expect_success 'setup - add an example entry to .gitmodules' '
412 git config --file=.gitmodules submodule.example.url git://example.com/init.git
415 test_expect_success 'status should fail for unmapped paths' '
416 test_must_fail git submodule status
419 test_expect_success 'setup - map path in .gitmodules' '
420 cat <<\EOF >expect &&
421 [submodule "example"]
422 url = git://example.com/init.git
423 path = init
426 git config --file=.gitmodules submodule.example.path init &&
428 test_cmp expect .gitmodules
431 test_expect_success 'status should only print one line' '
432 git submodule status >lines &&
433 test_line_count = 1 lines
436 test_expect_success 'status from subdirectory should have the same SHA1' '
437 test_when_finished "rmdir addtest/subdir" &&
439 cd addtest &&
440 mkdir subdir &&
441 git submodule status >output &&
442 awk "{print \$1}" <output >expect &&
443 cd subdir &&
444 git submodule status >../output &&
445 awk "{print \$1}" <../output >../actual &&
446 test_cmp ../expect ../actual &&
447 git -C ../submod checkout HEAD^ &&
448 git submodule status >../output &&
449 awk "{print \$1}" <../output >../actual2 &&
450 cd .. &&
451 git submodule status >output &&
452 awk "{print \$1}" <output >expect2 &&
453 test_cmp expect2 actual2 &&
454 ! test_cmp actual actual2
458 test_expect_success 'setup - fetch commit name from submodule' '
459 rev1=$(cd .subrepo && git rev-parse HEAD) &&
460 printf "rev1: %s\n" "$rev1" &&
461 test -n "$rev1"
464 test_expect_success 'status should initially be "missing"' '
465 git submodule status >lines &&
466 grep "^-$rev1" lines
469 test_expect_success 'init should register submodule url in .git/config' '
470 echo git://example.com/init.git >expect &&
472 git submodule init &&
473 git config submodule.example.url >url &&
474 git config submodule.example.url ./.subrepo &&
476 test_cmp expect url
479 test_expect_success 'status should still be "missing" after initializing' '
480 rm -fr init &&
481 mkdir init &&
482 git submodule status >lines &&
483 rm -fr init &&
484 grep "^-$rev1" lines
487 test_failure_with_unknown_submodule () {
488 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
489 test_grep "^error: .*no-such-submodule" output.err
492 test_expect_success 'init should fail with unknown submodule' '
493 test_failure_with_unknown_submodule init
496 test_expect_success 'update should fail with unknown submodule' '
497 test_failure_with_unknown_submodule update
500 test_expect_success 'status should fail with unknown submodule' '
501 test_failure_with_unknown_submodule status
504 test_expect_success 'sync should fail with unknown submodule' '
505 test_failure_with_unknown_submodule sync
508 test_expect_success 'update should fail when path is used by a file' '
509 echo hello >expect &&
511 echo "hello" >init &&
512 test_must_fail git submodule update &&
514 test_cmp expect init
517 test_expect_success 'update should fail when path is used by a nonempty directory' '
518 echo hello >expect &&
520 rm -fr init &&
521 mkdir init &&
522 echo "hello" >init/a &&
524 test_must_fail git submodule update &&
526 test_cmp expect init/a
529 test_expect_success 'update should work when path is an empty dir' '
530 rm -fr init &&
531 rm -f head-sha1 &&
532 echo "$rev1" >expect &&
534 mkdir init &&
535 git submodule update -q >update.out &&
536 test_must_be_empty update.out &&
538 inspect init &&
539 test_cmp expect head-sha1
542 test_expect_success 'status should be "up-to-date" after update' '
543 git submodule status >list &&
544 grep "^ $rev1" list
547 test_expect_success 'status "up-to-date" from subdirectory' '
548 mkdir -p sub &&
550 cd sub &&
551 git submodule status >../list
552 ) &&
553 grep "^ $rev1" list &&
554 grep "\\.\\./init" list
557 test_expect_success 'status "up-to-date" from subdirectory with path' '
558 mkdir -p sub &&
560 cd sub &&
561 git submodule status ../init >../list
562 ) &&
563 grep "^ $rev1" list &&
564 grep "\\.\\./init" list
567 test_expect_success 'status should be "modified" after submodule commit' '
569 cd init &&
570 echo b >b &&
571 git add b &&
572 git commit -m "submodule commit 2"
573 ) &&
575 rev2=$(cd init && git rev-parse HEAD) &&
576 test -n "$rev2" &&
577 git submodule status >list &&
579 grep "^+$rev2" list
582 test_expect_success '"submodule --cached" command forms should be identical' '
583 git submodule status --cached >expect &&
585 git submodule --cached >actual &&
586 test_cmp expect actual &&
588 git submodule --cached status >actual &&
589 test_cmp expect actual
592 test_expect_success 'the --cached sha1 should be rev1' '
593 git submodule --cached status >list &&
594 grep "^+$rev1" list
597 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
598 git diff >diff &&
599 grep "^+Subproject commit $rev2" diff
602 test_expect_success 'update should checkout rev1' '
603 rm -f head-sha1 &&
604 echo "$rev1" >expect &&
606 git submodule update init &&
607 inspect init &&
609 test_cmp expect head-sha1
612 test_expect_success 'status should be "up-to-date" after update' '
613 git submodule status >list &&
614 grep "^ $rev1" list
617 test_expect_success 'checkout superproject with subproject already present' '
618 git checkout initial &&
619 git checkout main
622 test_expect_success 'apply submodule diff' '
623 git branch second &&
625 cd init &&
626 echo s >s &&
627 git add s &&
628 git commit -m "change subproject"
629 ) &&
630 git update-index --add init &&
631 git commit -m "change init" &&
632 git format-patch -1 --stdout >P.diff &&
633 git checkout second &&
634 git apply --index P.diff &&
636 git diff --cached main >staged &&
637 test_must_be_empty staged
640 test_expect_success 'update --init' '
641 mv init init2 &&
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 &&
646 git submodule update init 2> update.out &&
647 test_grep "not initialized" update.out &&
648 test_must_fail git rev-parse --resolve-git-dir init/.git &&
650 git submodule update --init init &&
651 git rev-parse --resolve-git-dir init/.git
654 test_expect_success 'update --init from subdirectory' '
655 mv init init2 &&
656 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
657 git config --remove-section submodule.example &&
658 test_must_fail git config submodule.example.url &&
660 mkdir -p sub &&
662 cd sub &&
663 git submodule update ../init 2>update.out &&
664 test_grep "not initialized" update.out &&
665 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
667 git submodule update --init ../init
668 ) &&
669 git rev-parse --resolve-git-dir init/.git
672 test_expect_success 'do not add files from a submodule' '
674 git reset --hard &&
675 test_must_fail git add init/a
679 test_expect_success 'gracefully add/reset submodule with a trailing slash' '
681 git reset --hard &&
682 git commit -m "commit subproject" init &&
683 (cd init &&
684 echo b > a) &&
685 git add init/ &&
686 git diff --exit-code --cached init &&
687 commit=$(cd init &&
688 git commit -m update a >/dev/null &&
689 git rev-parse HEAD) &&
690 git add init/ &&
691 test_must_fail git diff --exit-code --cached init &&
692 test $commit = $(git ls-files --stage |
693 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
694 git reset init/ &&
695 git diff --exit-code --cached init
699 test_expect_success 'ls-files gracefully handles trailing slash' '
701 test "init" = "$(git ls-files init/)"
705 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
706 rm -rf init &&
707 mkdir init &&
708 git reset --hard &&
709 git checkout initial &&
710 test ! -d init &&
711 git checkout second
714 test_expect_success 'submodule <invalid-subcommand> fails' '
715 test_must_fail git submodule no-such-subcommand
718 test_expect_success 'add submodules without specifying an explicit path' '
719 mkdir repo &&
721 cd repo &&
722 git init &&
723 echo r >r &&
724 git add r &&
725 git commit -m "repo commit 1"
726 ) &&
727 git clone --bare repo/ bare.git &&
729 cd addtest &&
730 git submodule add "$submodurl/repo" &&
731 git config -f .gitmodules submodule.repo.path repo &&
732 git submodule add "$submodurl/bare.git" &&
733 git config -f .gitmodules submodule.bare.path bare
737 test_expect_success 'add should fail when path is used by a file' '
739 cd addtest &&
740 touch file &&
741 test_must_fail git submodule add "$submodurl/repo" file
745 test_expect_success 'add should fail when path is used by an existing directory' '
747 cd addtest &&
748 mkdir empty-dir &&
749 test_must_fail git submodule add "$submodurl/repo" empty-dir
753 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
755 cd addtest &&
756 git submodule add ../repo relative &&
757 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
758 git submodule sync relative &&
759 test "$(git config submodule.relative.url)" = "$submodurl/repo"
763 test_expect_success 'set up for relative path tests' '
764 mkdir reltest &&
766 cd reltest &&
767 git init &&
768 mkdir sub &&
770 cd sub &&
771 git init &&
772 test_commit foo
773 ) &&
774 git add sub &&
775 git config -f .gitmodules submodule.sub.path sub &&
776 git config -f .gitmodules submodule.sub.url ../subrepo &&
777 cp .git/config pristine-.git-config &&
778 cp .gitmodules pristine-.gitmodules
782 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
784 cd reltest &&
785 cp pristine-.git-config .git/config &&
786 cp pristine-.gitmodules .gitmodules &&
787 git config remote.origin.url ssh://hostname/repo &&
788 git submodule init &&
789 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
793 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
795 cd reltest &&
796 cp pristine-.git-config .git/config &&
797 cp pristine-.gitmodules .gitmodules &&
798 git config remote.origin.url ssh://hostname:22/repo &&
799 git submodule init &&
800 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
804 # About the choice of the path in the next test:
805 # - double-slash side-steps path mangling issues on Windows
806 # - it is still an absolute local path
807 # - there cannot be a server with a blank in its name just in case the
808 # path is used erroneously to access a //server/share style path
809 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
811 cd reltest &&
812 cp pristine-.git-config .git/config &&
813 cp pristine-.gitmodules .gitmodules &&
814 git config remote.origin.url "//somewhere else/repo" &&
815 git submodule init &&
816 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
820 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
822 cd reltest &&
823 cp pristine-.git-config .git/config &&
824 cp pristine-.gitmodules .gitmodules &&
825 git config remote.origin.url file:///tmp/repo &&
826 git submodule init &&
827 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
831 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
833 cd reltest &&
834 cp pristine-.git-config .git/config &&
835 cp pristine-.gitmodules .gitmodules &&
836 git config remote.origin.url helper:://hostname/repo &&
837 git submodule init &&
838 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
842 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
844 cd reltest &&
845 cp pristine-.git-config .git/config &&
846 git config remote.origin.url user@host:repo &&
847 git submodule init &&
848 test "$(git config submodule.sub.url)" = user@host:subrepo
852 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
854 cd reltest &&
855 cp pristine-.git-config .git/config &&
856 cp pristine-.gitmodules .gitmodules &&
857 git config remote.origin.url user@host:path/to/repo &&
858 git submodule init &&
859 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
863 test_expect_success '../subrepo works with relative local path - foo' '
865 cd reltest &&
866 cp pristine-.git-config .git/config &&
867 cp pristine-.gitmodules .gitmodules &&
868 git config remote.origin.url foo &&
869 # actual: fails with an error
870 git submodule init &&
871 test "$(git config submodule.sub.url)" = subrepo
875 test_expect_success '../subrepo works with relative local path - foo/bar' '
877 cd reltest &&
878 cp pristine-.git-config .git/config &&
879 cp pristine-.gitmodules .gitmodules &&
880 git config remote.origin.url foo/bar &&
881 git submodule init &&
882 test "$(git config submodule.sub.url)" = foo/subrepo
886 test_expect_success '../subrepo works with relative local path - ./foo' '
888 cd reltest &&
889 cp pristine-.git-config .git/config &&
890 cp pristine-.gitmodules .gitmodules &&
891 git config remote.origin.url ./foo &&
892 git submodule init &&
893 test "$(git config submodule.sub.url)" = subrepo
897 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
899 cd reltest &&
900 cp pristine-.git-config .git/config &&
901 cp pristine-.gitmodules .gitmodules &&
902 git config remote.origin.url ./foo/bar &&
903 git submodule init &&
904 test "$(git config submodule.sub.url)" = foo/subrepo
908 test_expect_success '../subrepo works with relative local path - ../foo' '
910 cd reltest &&
911 cp pristine-.git-config .git/config &&
912 cp pristine-.gitmodules .gitmodules &&
913 git config remote.origin.url ../foo &&
914 git submodule init &&
915 test "$(git config submodule.sub.url)" = ../subrepo
919 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
921 cd reltest &&
922 cp pristine-.git-config .git/config &&
923 cp pristine-.gitmodules .gitmodules &&
924 git config remote.origin.url ../foo/bar &&
925 git submodule init &&
926 test "$(git config submodule.sub.url)" = ../foo/subrepo
930 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
932 cd reltest &&
933 cp pristine-.git-config .git/config &&
934 cp pristine-.gitmodules .gitmodules &&
935 mkdir -p a/b/c &&
936 (cd a/b/c && git init && test_commit msg) &&
937 git config remote.origin.url ../foo/bar.git &&
938 git submodule add ../bar/a/b/c ./a/b/c &&
939 git submodule init &&
940 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
944 test_expect_success 'moving the superproject does not break submodules' '
946 cd addtest &&
947 git submodule status >expect
948 ) &&
949 mv addtest addtest2 &&
951 cd addtest2 &&
952 git submodule status >actual &&
953 test_cmp expect actual
957 test_expect_success 'moving the submodule does not break the superproject' '
959 cd addtest2 &&
960 git submodule status
961 ) >actual &&
962 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
963 mv addtest2/repo addtest2/repo.bak &&
964 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
966 cd addtest2 &&
967 git submodule status
968 ) >actual &&
969 test_cmp expect actual
972 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
974 cd addtest2 &&
976 cd repo &&
977 echo "$submodurl/repo" >expect &&
978 git config remote.origin.url >actual &&
979 test_cmp expect actual &&
980 echo "gitdir: ../.git/modules/repo" >expect &&
981 test_cmp expect .git
982 ) &&
983 rm -rf repo &&
984 git rm repo &&
985 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
986 test_must_be_empty actual &&
987 echo "gitdir: ../.git/modules/submod" >expect &&
988 test_cmp expect submod/.git &&
990 cd repo &&
991 echo "$submodurl/bare.git" >expect &&
992 git config remote.origin.url >actual &&
993 test_cmp expect actual &&
994 echo "gitdir: ../.git/modules/repo_new" >expect &&
995 test_cmp expect .git
996 ) &&
997 echo "repo" >expect &&
998 test_must_fail git config -f .gitmodules submodule.repo.path &&
999 git config -f .gitmodules submodule.repo_new.path >actual &&
1000 test_cmp expect actual &&
1001 echo "$submodurl/repo" >expect &&
1002 test_must_fail git config -f .gitmodules submodule.repo.url &&
1003 echo "$submodurl/bare.git" >expect &&
1004 git config -f .gitmodules submodule.repo_new.url >actual &&
1005 test_cmp expect actual &&
1006 echo "$submodurl/repo" >expect &&
1007 git config submodule.repo.url >actual &&
1008 test_cmp expect actual &&
1009 echo "$submodurl/bare.git" >expect &&
1010 git config submodule.repo_new.url >actual &&
1011 test_cmp expect actual
1015 test_expect_success 'recursive relative submodules stay relative' '
1016 test_when_finished "rm -rf super clone2 subsub sub3" &&
1017 mkdir subsub &&
1019 cd subsub &&
1020 git init &&
1021 >t &&
1022 git add t &&
1023 git commit -m "initial commit"
1024 ) &&
1025 mkdir sub3 &&
1027 cd sub3 &&
1028 git init &&
1029 >t &&
1030 git add t &&
1031 git commit -m "initial commit" &&
1032 git submodule add ../subsub dirdir/subsub &&
1033 git commit -m "add submodule subsub"
1034 ) &&
1035 mkdir super &&
1037 cd super &&
1038 git init &&
1039 >t &&
1040 git add t &&
1041 git commit -m "initial commit" &&
1042 git submodule add ../sub3 &&
1043 git commit -m "add submodule sub"
1044 ) &&
1045 git clone super clone2 &&
1047 cd clone2 &&
1048 git submodule update --init --recursive &&
1049 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
1050 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
1051 ) &&
1052 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
1053 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
1056 test_expect_success 'submodule add with an existing name fails unless forced' '
1058 cd addtest2 &&
1059 rm -rf repo &&
1060 git rm repo &&
1061 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
1062 test ! -d repo &&
1063 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
1064 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
1065 echo "$submodurl/bare.git" >expect &&
1066 git config submodule.repo_new.url >actual &&
1067 test_cmp expect actual &&
1068 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
1069 test -d repo &&
1070 echo "repo" >expect &&
1071 git config -f .gitmodules submodule.repo_new.path >actual &&
1072 test_cmp expect actual &&
1073 echo "$submodurl/repo.git" >expect &&
1074 git config -f .gitmodules submodule.repo_new.url >actual &&
1075 test_cmp expect actual &&
1076 echo "$submodurl/repo.git" >expect &&
1077 git config submodule.repo_new.url >actual &&
1078 test_cmp expect actual
1082 test_expect_success 'set up a second submodule' '
1083 git submodule add ./init2 example2 &&
1084 git commit -m "submodule example2 added"
1087 test_expect_success 'submodule deinit works on repository without submodules' '
1088 test_when_finished "rm -rf newdirectory" &&
1089 mkdir newdirectory &&
1091 cd newdirectory &&
1092 git init &&
1093 >file &&
1094 git add file &&
1095 git commit -m "repo should not be empty" &&
1096 git submodule deinit . &&
1097 git submodule deinit --all
1101 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
1102 git config submodule.example.foo bar &&
1103 git config submodule.example2.frotz nitfol &&
1104 git submodule deinit init &&
1105 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1106 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1107 test -f example2/.git &&
1108 rmdir init
1111 test_expect_success 'submodule deinit should unset core.worktree' '
1112 test_path_is_file .git/modules/example/config &&
1113 test_must_fail git config -f .git/modules/example/config core.worktree
1116 test_expect_success 'submodule deinit from subdirectory' '
1117 git submodule update --init &&
1118 git config submodule.example.foo bar &&
1119 mkdir -p sub &&
1121 cd sub &&
1122 git submodule deinit ../init >../output
1123 ) &&
1124 test_grep "\\.\\./init" output &&
1125 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1126 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1127 test -f example2/.git &&
1128 rmdir init
1131 test_expect_success 'submodule deinit . deinits all initialized submodules' '
1132 git submodule update --init &&
1133 git config submodule.example.foo bar &&
1134 git config submodule.example2.frotz nitfol &&
1135 test_must_fail git submodule deinit &&
1136 git submodule deinit . >actual &&
1137 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1138 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1139 test_grep "Cleared directory .init" actual &&
1140 test_grep "Cleared directory .example2" actual &&
1141 rmdir init example2
1144 test_expect_success 'submodule deinit --all deinits all initialized submodules' '
1145 git submodule update --init &&
1146 git config submodule.example.foo bar &&
1147 git config submodule.example2.frotz nitfol &&
1148 test_must_fail git submodule deinit &&
1149 git submodule deinit --all >actual &&
1150 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1151 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1152 test_grep "Cleared directory .init" actual &&
1153 test_grep "Cleared directory .example2" actual &&
1154 rmdir init example2
1157 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
1158 git submodule update --init &&
1159 rm -rf init example2/* example2/.git &&
1160 git submodule deinit init example2 >actual &&
1161 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1162 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1163 test_grep ! "Cleared directory .init" actual &&
1164 test_grep "Cleared directory .example2" actual &&
1165 rmdir init
1168 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
1169 git submodule update --init &&
1170 echo X >>init/s &&
1171 test_must_fail git submodule deinit init &&
1172 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1173 test -f example2/.git &&
1174 git submodule deinit -f init >actual &&
1175 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1176 test_grep "Cleared directory .init" actual &&
1177 rmdir init
1180 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1181 git submodule update --init &&
1182 echo X >>init/untracked &&
1183 test_must_fail git submodule deinit init &&
1184 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1185 test -f example2/.git &&
1186 git submodule deinit -f init >actual &&
1187 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1188 test_grep "Cleared directory .init" actual &&
1189 rmdir init
1192 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1193 git submodule update --init &&
1195 cd init &&
1196 git checkout HEAD^
1197 ) &&
1198 test_must_fail git submodule deinit init &&
1199 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1200 test -f example2/.git &&
1201 git submodule deinit -f init >actual &&
1202 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1203 test_grep "Cleared directory .init" actual &&
1204 rmdir init
1207 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1208 git submodule update --init &&
1209 git submodule deinit init >actual &&
1210 test_grep "Submodule .example. (.*) unregistered for path .init" actual &&
1211 test_grep "Cleared directory .init" actual &&
1212 git submodule deinit init >actual &&
1213 test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1214 test_grep "Cleared directory .init" actual &&
1215 git submodule deinit . >actual &&
1216 test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1217 test_grep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1218 test_grep "Cleared directory .init" actual &&
1219 git submodule deinit . >actual &&
1220 test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1221 test_grep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1222 test_grep "Cleared directory .init" actual &&
1223 git submodule deinit --all >actual &&
1224 test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1225 test_grep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1226 test_grep "Cleared directory .init" actual &&
1227 rmdir init example2
1230 test_expect_success 'submodule deinit absorbs .git directory if .git is a directory' '
1231 git submodule update --init &&
1233 cd init &&
1234 rm .git &&
1235 mv ../.git/modules/example .git &&
1236 GIT_WORK_TREE=. git config --unset core.worktree
1237 ) &&
1238 git submodule deinit init &&
1239 test_path_is_missing init/.git &&
1240 test -z "$(git config --get-regexp "submodule\.example\.")"
1243 test_expect_success 'submodule with UTF-8 name' '
1244 svname=$(printf "\303\245 \303\244\303\266") &&
1245 mkdir "$svname" &&
1247 cd "$svname" &&
1248 git init &&
1249 >sub &&
1250 git add sub &&
1251 git commit -m "init sub"
1252 ) &&
1253 git submodule add ./"$svname" &&
1254 git submodule >&2 &&
1255 test -n "$(git submodule | grep "$svname")"
1258 test_expect_success 'submodule add clone shallow submodule' '
1259 mkdir super &&
1260 pwd=$(pwd) &&
1262 cd super &&
1263 git init &&
1264 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1266 cd submodule &&
1267 test 1 = $(git log --oneline | wc -l)
1272 test_expect_success 'setup superproject with submodules' '
1273 git init sub1 &&
1274 test_commit -C sub1 test &&
1275 test_commit -C sub1 test2 &&
1276 git init multisuper &&
1277 git -C multisuper submodule add ../sub1 sub0 &&
1278 git -C multisuper submodule add ../sub1 sub1 &&
1279 git -C multisuper submodule add ../sub1 sub2 &&
1280 git -C multisuper submodule add ../sub1 sub3 &&
1281 git -C multisuper commit -m "add some submodules"
1284 cat >expect <<-EOF
1285 -sub0
1286 sub1 (test2)
1287 sub2 (test2)
1288 sub3 (test2)
1291 test_expect_success 'submodule update --init with a specification' '
1292 test_when_finished "rm -rf multisuper_clone" &&
1293 pwd=$(pwd) &&
1294 git clone file://"$pwd"/multisuper multisuper_clone &&
1295 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1296 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1297 test_cmp expect actual
1300 test_expect_success 'submodule update --init with submodule.active set' '
1301 test_when_finished "rm -rf multisuper_clone" &&
1302 pwd=$(pwd) &&
1303 git clone file://"$pwd"/multisuper multisuper_clone &&
1304 git -C multisuper_clone config submodule.active "." &&
1305 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1306 git -C multisuper_clone submodule update --init &&
1307 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1308 test_cmp expect actual
1311 test_expect_success 'submodule update and setting submodule.<name>.active' '
1312 test_when_finished "rm -rf multisuper_clone" &&
1313 pwd=$(pwd) &&
1314 git clone file://"$pwd"/multisuper multisuper_clone &&
1315 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1316 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1317 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1319 cat >expect <<-\EOF &&
1320 sub0 (test2)
1321 -sub1
1322 sub2 (test2)
1323 -sub3
1325 git -C multisuper_clone submodule update &&
1326 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1327 test_cmp expect actual
1330 test_expect_success 'clone active submodule without submodule url set' '
1331 test_when_finished "rm -rf test/test" &&
1332 mkdir test &&
1333 # another dir breaks accidental relative paths still being correct
1334 git clone file://"$pwd"/multisuper test/test &&
1336 cd test/test &&
1337 git config submodule.active "." &&
1339 # do not pass --init flag, as the submodule is already active:
1340 git submodule update &&
1341 git submodule status >actual_raw &&
1343 cut -d" " -f3- actual_raw >actual &&
1344 cat >expect <<-\EOF &&
1345 sub0 (test2)
1346 sub1 (test2)
1347 sub2 (test2)
1348 sub3 (test2)
1350 test_cmp expect actual
1354 test_expect_success 'update submodules without url set in .gitconfig' '
1355 test_when_finished "rm -rf multisuper_clone" &&
1356 git clone file://"$pwd"/multisuper multisuper_clone &&
1358 git -C multisuper_clone submodule init &&
1359 for s in sub0 sub1 sub2 sub3
1361 key=submodule.$s.url &&
1362 git -C multisuper_clone config --local --unset $key &&
1363 git -C multisuper_clone config --file .gitmodules --unset $key || return 1
1364 done &&
1366 test_must_fail git -C multisuper_clone submodule update 2>err &&
1367 grep "cannot clone submodule .sub[0-3]. without a URL" err
1370 test_expect_success 'clone --recurse-submodules with a pathspec works' '
1371 test_when_finished "rm -rf multisuper_clone" &&
1372 cat >expected <<-\EOF &&
1373 sub0 (test2)
1374 -sub1
1375 -sub2
1376 -sub3
1379 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1380 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1381 test_cmp expected actual
1384 test_expect_success 'clone with multiple --recurse-submodules options' '
1385 test_when_finished "rm -rf multisuper_clone" &&
1386 cat >expect <<-\EOF &&
1387 -sub0
1388 sub1 (test2)
1389 -sub2
1390 sub3 (test2)
1393 git clone --recurse-submodules="." \
1394 --recurse-submodules=":(exclude)sub0" \
1395 --recurse-submodules=":(exclude)sub2" \
1396 multisuper multisuper_clone &&
1397 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1398 test_cmp expect actual
1401 test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
1402 test_when_finished "rm -rf multisuper_clone" &&
1403 cat <<-\EOF >expect &&
1404 -sub0
1405 sub1 (test2)
1406 -sub2
1407 sub3 (test2)
1410 cat <<-\EOF >expect2 &&
1411 -sub0
1412 sub1 (test2)
1413 -sub2
1414 sub3 (test2)
1415 -sub4
1416 sub5 (test2)
1419 git clone --recurse-submodules="." \
1420 --recurse-submodules=":(exclude)sub0" \
1421 --recurse-submodules=":(exclude)sub2" \
1422 --recurse-submodules=":(exclude)sub4" \
1423 multisuper multisuper_clone &&
1425 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1426 test_cmp expect actual &&
1428 git -C multisuper submodule add ../sub1 sub4 &&
1429 git -C multisuper submodule add ../sub1 sub5 &&
1430 git -C multisuper commit -m "add more submodules" &&
1431 # obtain the new superproject
1432 git -C multisuper_clone pull &&
1433 git -C multisuper_clone submodule update --init &&
1434 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1435 test_cmp expect2 actual
1438 test_expect_success 'init properly sets the config' '
1439 test_when_finished "rm -rf multisuper_clone" &&
1440 git clone --recurse-submodules="." \
1441 --recurse-submodules=":(exclude)sub0" \
1442 multisuper multisuper_clone &&
1444 git -C multisuper_clone submodule init -- sub0 sub1 &&
1445 git -C multisuper_clone config --get submodule.sub0.active &&
1446 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1449 test_expect_success 'recursive clone respects -q' '
1450 test_when_finished "rm -rf multisuper_clone" &&
1451 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1452 test_must_be_empty actual
1455 test_expect_success '`submodule init` and `init.templateDir`' '
1456 mkdir -p tmpl/hooks &&
1457 write_script tmpl/hooks/post-checkout <<-EOF &&
1458 echo HOOK-RUN >&2
1459 echo I was here >hook.run
1460 exit 1
1463 test_config init.templateDir "$(pwd)/tmpl" &&
1464 test_when_finished \
1465 "git config --global --unset init.templateDir || true" &&
1467 sane_unset GIT_TEMPLATE_DIR &&
1468 NO_SET_GIT_TEMPLATE_DIR=t &&
1469 export NO_SET_GIT_TEMPLATE_DIR &&
1471 git config --global init.templateDir "$(pwd)/tmpl" &&
1472 test_must_fail git submodule \
1473 add "$submodurl" sub-global 2>err &&
1474 git config --global --unset init.templateDir &&
1475 test_grep HOOK-RUN err &&
1476 test_path_is_file sub-global/hook.run &&
1478 git config init.templateDir "$(pwd)/tmpl" &&
1479 git submodule add "$submodurl" sub-local 2>err &&
1480 git config --unset init.templateDir &&
1481 test_grep ! HOOK-RUN err &&
1482 test_path_is_missing sub-local/hook.run
1486 test_done