apply: update line lengths for --inaccurate-eof
[git/raj.git] / t / t7400-submodule-basic.sh
blobc09ce0d4c1dbd8bc49595be4e3928032fbdc3c8c
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 . ./test-lib.sh
14 test_expect_success 'submodule deinit works on empty repository' '
15 git submodule deinit --all
18 test_expect_success 'setup - initial commit' '
19 >t &&
20 git add t &&
21 git commit -m "initial commit" &&
22 git branch initial
25 test_expect_success 'submodule init aborts on missing .gitmodules file' '
26 test_when_finished "git update-index --remove sub" &&
27 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
28 # missing the .gitmodules file here
29 test_must_fail git submodule init 2>actual &&
30 test_i18ngrep "No url found for submodule path" actual
33 test_expect_success 'submodule update aborts on missing .gitmodules file' '
34 test_when_finished "git update-index --remove sub" &&
35 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
36 # missing the .gitmodules file here
37 git submodule update sub 2>actual &&
38 test_i18ngrep "Submodule path .sub. not initialized" actual
41 test_expect_success 'configuration parsing' '
42 test_when_finished "rm -f .gitmodules" &&
43 cat >.gitmodules <<-\EOF &&
44 [submodule "s"]
45 path
46 ignore
47 EOF
48 test_must_fail git status
51 test_expect_success 'setup - repository in init subdirectory' '
52 mkdir init &&
54 cd init &&
55 git init &&
56 echo a >a &&
57 git add a &&
58 git commit -m "submodule commit 1" &&
59 git tag -a -m "rev-1" rev-1
63 test_expect_success 'setup - commit with gitlink' '
64 echo a >a &&
65 echo z >z &&
66 git add a init z &&
67 git commit -m "super commit 1"
70 test_expect_success 'setup - hide init subdirectory' '
71 mv init .subrepo
74 test_expect_success 'setup - repository to add submodules to' '
75 git init addtest &&
76 git init addtest-ignore
79 # The 'submodule add' tests need some repository to add as a submodule.
80 # The trash directory is a good one as any. We need to canonicalize
81 # the name, though, as some tests compare it to the absolute path git
82 # generates, which will expand symbolic links.
83 submodurl=$(pwd -P)
85 listbranches() {
86 git for-each-ref --format='%(refname)' 'refs/heads/*'
89 inspect() {
90 dir=$1 &&
91 dotdot="${2:-..}" &&
94 cd "$dir" &&
95 listbranches >"$dotdot/heads" &&
96 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
97 git rev-parse HEAD >"$dotdot/head-sha1" &&
98 git update-index --refresh &&
99 git diff-files --exit-code &&
100 git clean -n -d -x >"$dotdot/untracked"
104 test_expect_success 'submodule add' '
105 echo "refs/heads/master" >expect &&
106 >empty &&
109 cd addtest &&
110 git submodule add -q "$submodurl" submod >actual &&
111 test_must_be_empty actual &&
112 echo "gitdir: ../.git/modules/submod" >expect &&
113 test_cmp expect submod/.git &&
115 cd submod &&
116 git config core.worktree >actual &&
117 echo "../../../submod" >expect &&
118 test_cmp expect actual &&
119 rm -f actual expect
120 ) &&
121 git submodule init
122 ) &&
124 rm -f heads head untracked &&
125 inspect addtest/submod ../.. &&
126 test_cmp expect heads &&
127 test_cmp expect head &&
128 test_cmp empty untracked
131 test_expect_success 'submodule add to .gitignored path fails' '
133 cd addtest-ignore &&
134 cat <<-\EOF >expect &&
135 The following path is ignored by one of your .gitignore files:
136 submod
137 Use -f if you really want to add it.
139 # Does not use test_commit due to the ignore
140 echo "*" > .gitignore &&
141 git add --force .gitignore &&
142 git commit -m"Ignore everything" &&
143 ! git submodule add "$submodurl" submod >actual 2>&1 &&
144 test_i18ncmp expect actual
148 test_expect_success 'submodule add to .gitignored path with --force' '
150 cd addtest-ignore &&
151 git submodule add --force "$submodurl" submod
155 test_expect_success 'submodule add to reconfigure existing submodule with --force' '
157 cd addtest-ignore &&
158 git submodule add --force bogus-url submod &&
159 git submodule add -b initial "$submodurl" submod-branch &&
160 test "bogus-url" = "$(git config -f .gitmodules submodule.submod.url)" &&
161 test "bogus-url" = "$(git config submodule.submod.url)" &&
162 # Restore the url
163 git submodule add --force "$submodurl" submod
164 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
165 test "$submodurl" = "$(git config submodule.submod.url)"
169 test_expect_success 'submodule add --branch' '
170 echo "refs/heads/initial" >expect-head &&
171 cat <<-\EOF >expect-heads &&
172 refs/heads/initial
173 refs/heads/master
175 >empty &&
178 cd addtest &&
179 git submodule add -b initial "$submodurl" submod-branch &&
180 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
181 git submodule init
182 ) &&
184 rm -f heads head untracked &&
185 inspect addtest/submod-branch ../.. &&
186 test_cmp expect-heads heads &&
187 test_cmp expect-head head &&
188 test_cmp empty untracked
191 test_expect_success 'submodule add with ./ in path' '
192 echo "refs/heads/master" >expect &&
193 >empty &&
196 cd addtest &&
197 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
198 git submodule init
199 ) &&
201 rm -f heads head untracked &&
202 inspect addtest/dotsubmod/frotz ../../.. &&
203 test_cmp expect heads &&
204 test_cmp expect head &&
205 test_cmp empty untracked
208 test_expect_success 'submodule add with /././ in path' '
209 echo "refs/heads/master" >expect &&
210 >empty &&
213 cd addtest &&
214 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
215 git submodule init
216 ) &&
218 rm -f heads head untracked &&
219 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
220 test_cmp expect heads &&
221 test_cmp expect head &&
222 test_cmp empty untracked
225 test_expect_success 'submodule add with // in path' '
226 echo "refs/heads/master" >expect &&
227 >empty &&
230 cd addtest &&
231 git submodule add "$submodurl" slashslashsubmod///frotz// &&
232 git submodule init
233 ) &&
235 rm -f heads head untracked &&
236 inspect addtest/slashslashsubmod/frotz ../../.. &&
237 test_cmp expect heads &&
238 test_cmp expect head &&
239 test_cmp empty untracked
242 test_expect_success 'submodule add with /.. in path' '
243 echo "refs/heads/master" >expect &&
244 >empty &&
247 cd addtest &&
248 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
249 git submodule init
250 ) &&
252 rm -f heads head untracked &&
253 inspect addtest/realsubmod ../.. &&
254 test_cmp expect heads &&
255 test_cmp expect head &&
256 test_cmp empty untracked
259 test_expect_success 'submodule add with ./, /.. and // in path' '
260 echo "refs/heads/master" >expect &&
261 >empty &&
264 cd addtest &&
265 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
266 git submodule init
267 ) &&
269 rm -f heads head untracked &&
270 inspect addtest/realsubmod2 ../.. &&
271 test_cmp expect heads &&
272 test_cmp expect head &&
273 test_cmp empty untracked
276 test_expect_success 'submodule add in subdirectory' '
277 echo "refs/heads/master" >expect &&
278 >empty &&
280 mkdir addtest/sub &&
282 cd addtest/sub &&
283 git submodule add "$submodurl" ../realsubmod3 &&
284 git submodule init
285 ) &&
287 rm -f heads head untracked &&
288 inspect addtest/realsubmod3 ../.. &&
289 test_cmp expect heads &&
290 test_cmp expect head &&
291 test_cmp empty untracked
294 test_expect_success 'submodule add in subdirectory with relative path should fail' '
296 cd addtest/sub &&
297 test_must_fail git submodule add ../../ submod3 2>../../output.err
298 ) &&
299 test_i18ngrep toplevel output.err
302 test_expect_success 'setup - add an example entry to .gitmodules' '
303 git config --file=.gitmodules submodule.example.url git://example.com/init.git
306 test_expect_success 'status should fail for unmapped paths' '
307 test_must_fail git submodule status
310 test_expect_success 'setup - map path in .gitmodules' '
311 cat <<\EOF >expect &&
312 [submodule "example"]
313 url = git://example.com/init.git
314 path = init
317 git config --file=.gitmodules submodule.example.path init &&
319 test_cmp expect .gitmodules
322 test_expect_success 'status should only print one line' '
323 git submodule status >lines &&
324 test_line_count = 1 lines
327 test_expect_success 'setup - fetch commit name from submodule' '
328 rev1=$(cd .subrepo && git rev-parse HEAD) &&
329 printf "rev1: %s\n" "$rev1" &&
330 test -n "$rev1"
333 test_expect_success 'status should initially be "missing"' '
334 git submodule status >lines &&
335 grep "^-$rev1" lines
338 test_expect_success 'init should register submodule url in .git/config' '
339 echo git://example.com/init.git >expect &&
341 git submodule init &&
342 git config submodule.example.url >url &&
343 git config submodule.example.url ./.subrepo &&
345 test_cmp expect url
348 test_failure_with_unknown_submodule () {
349 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
350 grep "^error: .*no-such-submodule" output.err
353 test_expect_success 'init should fail with unknown submodule' '
354 test_failure_with_unknown_submodule init
357 test_expect_success 'update should fail with unknown submodule' '
358 test_failure_with_unknown_submodule update
361 test_expect_success 'status should fail with unknown submodule' '
362 test_failure_with_unknown_submodule status
365 test_expect_success 'sync should fail with unknown submodule' '
366 test_failure_with_unknown_submodule sync
369 test_expect_success 'update should fail when path is used by a file' '
370 echo hello >expect &&
372 echo "hello" >init &&
373 test_must_fail git submodule update &&
375 test_cmp expect init
378 test_expect_success 'update should fail when path is used by a nonempty directory' '
379 echo hello >expect &&
381 rm -fr init &&
382 mkdir init &&
383 echo "hello" >init/a &&
385 test_must_fail git submodule update &&
387 test_cmp expect init/a
390 test_expect_success 'update should work when path is an empty dir' '
391 rm -fr init &&
392 rm -f head-sha1 &&
393 echo "$rev1" >expect &&
395 mkdir init &&
396 git submodule update -q >update.out &&
397 test_must_be_empty update.out &&
399 inspect init &&
400 test_cmp expect head-sha1
403 test_expect_success 'status should be "up-to-date" after update' '
404 git submodule status >list &&
405 grep "^ $rev1" list
408 test_expect_success 'status "up-to-date" from subdirectory' '
409 mkdir -p sub &&
411 cd sub &&
412 git submodule status >../list
413 ) &&
414 grep "^ $rev1" list &&
415 grep "\\.\\./init" list
418 test_expect_success 'status "up-to-date" from subdirectory with path' '
419 mkdir -p sub &&
421 cd sub &&
422 git submodule status ../init >../list
423 ) &&
424 grep "^ $rev1" list &&
425 grep "\\.\\./init" list
428 test_expect_success 'status should be "modified" after submodule commit' '
430 cd init &&
431 echo b >b &&
432 git add b &&
433 git commit -m "submodule commit 2"
434 ) &&
436 rev2=$(cd init && git rev-parse HEAD) &&
437 test -n "$rev2" &&
438 git submodule status >list &&
440 grep "^+$rev2" list
443 test_expect_success 'the --cached sha1 should be rev1' '
444 git submodule --cached status >list &&
445 grep "^+$rev1" list
448 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
449 git diff >diff &&
450 grep "^+Subproject commit $rev2" diff
453 test_expect_success 'update should checkout rev1' '
454 rm -f head-sha1 &&
455 echo "$rev1" >expect &&
457 git submodule update init &&
458 inspect init &&
460 test_cmp expect head-sha1
463 test_expect_success 'status should be "up-to-date" after update' '
464 git submodule status >list &&
465 grep "^ $rev1" list
468 test_expect_success 'checkout superproject with subproject already present' '
469 git checkout initial &&
470 git checkout master
473 test_expect_success 'apply submodule diff' '
474 >empty &&
476 git branch second &&
478 cd init &&
479 echo s >s &&
480 git add s &&
481 git commit -m "change subproject"
482 ) &&
483 git update-index --add init &&
484 git commit -m "change init" &&
485 git format-patch -1 --stdout >P.diff &&
486 git checkout second &&
487 git apply --index P.diff &&
489 git diff --cached master >staged &&
490 test_cmp empty staged
493 test_expect_success 'update --init' '
494 mv init init2 &&
495 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
496 git config --remove-section submodule.example &&
497 test_must_fail git config submodule.example.url &&
499 git submodule update init 2> update.out &&
500 cat update.out &&
501 test_i18ngrep "not initialized" update.out &&
502 test_must_fail git rev-parse --resolve-git-dir init/.git &&
504 git submodule update --init init &&
505 git rev-parse --resolve-git-dir init/.git
508 test_expect_success 'update --init from subdirectory' '
509 mv init init2 &&
510 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
511 git config --remove-section submodule.example &&
512 test_must_fail git config submodule.example.url &&
514 mkdir -p sub &&
516 cd sub &&
517 git submodule update ../init 2>update.out &&
518 cat update.out &&
519 test_i18ngrep "not initialized" update.out &&
520 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
522 git submodule update --init ../init
523 ) &&
524 git rev-parse --resolve-git-dir init/.git
527 test_expect_success 'do not add files from a submodule' '
529 git reset --hard &&
530 test_must_fail git add init/a
534 test_expect_success 'gracefully add/reset submodule with a trailing slash' '
536 git reset --hard &&
537 git commit -m "commit subproject" init &&
538 (cd init &&
539 echo b > a) &&
540 git add init/ &&
541 git diff --exit-code --cached init &&
542 commit=$(cd init &&
543 git commit -m update a >/dev/null &&
544 git rev-parse HEAD) &&
545 git add init/ &&
546 test_must_fail git diff --exit-code --cached init &&
547 test $commit = $(git ls-files --stage |
548 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
549 git reset init/ &&
550 git diff --exit-code --cached init
554 test_expect_success 'ls-files gracefully handles trailing slash' '
556 test "init" = "$(git ls-files init/)"
560 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
561 rm -rf init &&
562 mkdir init &&
563 git reset --hard &&
564 git checkout initial &&
565 test ! -d init &&
566 git checkout second
569 test_expect_success 'submodule <invalid-subcommand> fails' '
570 test_must_fail git submodule no-such-subcommand
573 test_expect_success 'add submodules without specifying an explicit path' '
574 mkdir repo &&
576 cd repo &&
577 git init &&
578 echo r >r &&
579 git add r &&
580 git commit -m "repo commit 1"
581 ) &&
582 git clone --bare repo/ bare.git &&
584 cd addtest &&
585 git submodule add "$submodurl/repo" &&
586 git config -f .gitmodules submodule.repo.path repo &&
587 git submodule add "$submodurl/bare.git" &&
588 git config -f .gitmodules submodule.bare.path bare
592 test_expect_success 'add should fail when path is used by a file' '
594 cd addtest &&
595 touch file &&
596 test_must_fail git submodule add "$submodurl/repo" file
600 test_expect_success 'add should fail when path is used by an existing directory' '
602 cd addtest &&
603 mkdir empty-dir &&
604 test_must_fail git submodule add "$submodurl/repo" empty-dir
608 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
610 cd addtest &&
611 git submodule add ../repo relative &&
612 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
613 git submodule sync relative &&
614 test "$(git config submodule.relative.url)" = "$submodurl/repo"
618 test_expect_success 'set up for relative path tests' '
619 mkdir reltest &&
621 cd reltest &&
622 git init &&
623 mkdir sub &&
625 cd sub &&
626 git init &&
627 test_commit foo
628 ) &&
629 git add sub &&
630 git config -f .gitmodules submodule.sub.path sub &&
631 git config -f .gitmodules submodule.sub.url ../subrepo &&
632 cp .git/config pristine-.git-config &&
633 cp .gitmodules pristine-.gitmodules
637 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
639 cd reltest &&
640 cp pristine-.git-config .git/config &&
641 cp pristine-.gitmodules .gitmodules &&
642 git config remote.origin.url ssh://hostname/repo &&
643 git submodule init &&
644 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
648 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
650 cd reltest &&
651 cp pristine-.git-config .git/config &&
652 cp pristine-.gitmodules .gitmodules &&
653 git config remote.origin.url ssh://hostname:22/repo &&
654 git submodule init &&
655 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
659 # About the choice of the path in the next test:
660 # - double-slash side-steps path mangling issues on Windows
661 # - it is still an absolute local path
662 # - there cannot be a server with a blank in its name just in case the
663 # path is used erroneously to access a //server/share style path
664 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
666 cd reltest &&
667 cp pristine-.git-config .git/config &&
668 cp pristine-.gitmodules .gitmodules &&
669 git config remote.origin.url "//somewhere else/repo" &&
670 git submodule init &&
671 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
675 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
677 cd reltest &&
678 cp pristine-.git-config .git/config &&
679 cp pristine-.gitmodules .gitmodules &&
680 git config remote.origin.url file:///tmp/repo &&
681 git submodule init &&
682 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
686 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
688 cd reltest &&
689 cp pristine-.git-config .git/config &&
690 cp pristine-.gitmodules .gitmodules &&
691 git config remote.origin.url helper:://hostname/repo &&
692 git submodule init &&
693 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
697 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
699 cd reltest &&
700 cp pristine-.git-config .git/config &&
701 git config remote.origin.url user@host:repo &&
702 git submodule init &&
703 test "$(git config submodule.sub.url)" = user@host:subrepo
707 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
709 cd reltest &&
710 cp pristine-.git-config .git/config &&
711 cp pristine-.gitmodules .gitmodules &&
712 git config remote.origin.url user@host:path/to/repo &&
713 git submodule init &&
714 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
718 test_expect_success '../subrepo works with relative local path - foo' '
720 cd reltest &&
721 cp pristine-.git-config .git/config &&
722 cp pristine-.gitmodules .gitmodules &&
723 git config remote.origin.url foo &&
724 # actual: fails with an error
725 git submodule init &&
726 test "$(git config submodule.sub.url)" = subrepo
730 test_expect_success '../subrepo works with relative local path - foo/bar' '
732 cd reltest &&
733 cp pristine-.git-config .git/config &&
734 cp pristine-.gitmodules .gitmodules &&
735 git config remote.origin.url foo/bar &&
736 git submodule init &&
737 test "$(git config submodule.sub.url)" = foo/subrepo
741 test_expect_success '../subrepo works with relative local path - ./foo' '
743 cd reltest &&
744 cp pristine-.git-config .git/config &&
745 cp pristine-.gitmodules .gitmodules &&
746 git config remote.origin.url ./foo &&
747 git submodule init &&
748 test "$(git config submodule.sub.url)" = subrepo
752 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
754 cd reltest &&
755 cp pristine-.git-config .git/config &&
756 cp pristine-.gitmodules .gitmodules &&
757 git config remote.origin.url ./foo/bar &&
758 git submodule init &&
759 test "$(git config submodule.sub.url)" = foo/subrepo
763 test_expect_success '../subrepo works with relative local path - ../foo' '
765 cd reltest &&
766 cp pristine-.git-config .git/config &&
767 cp pristine-.gitmodules .gitmodules &&
768 git config remote.origin.url ../foo &&
769 git submodule init &&
770 test "$(git config submodule.sub.url)" = ../subrepo
774 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
776 cd reltest &&
777 cp pristine-.git-config .git/config &&
778 cp pristine-.gitmodules .gitmodules &&
779 git config remote.origin.url ../foo/bar &&
780 git submodule init &&
781 test "$(git config submodule.sub.url)" = ../foo/subrepo
785 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
787 cd reltest &&
788 cp pristine-.git-config .git/config &&
789 cp pristine-.gitmodules .gitmodules &&
790 mkdir -p a/b/c &&
791 (cd a/b/c; git init) &&
792 git config remote.origin.url ../foo/bar.git &&
793 git submodule add ../bar/a/b/c ./a/b/c &&
794 git submodule init &&
795 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
799 test_expect_success 'moving the superproject does not break submodules' '
801 cd addtest &&
802 git submodule status >expect
803 ) &&
804 mv addtest addtest2 &&
806 cd addtest2 &&
807 git submodule status >actual &&
808 test_cmp expect actual
812 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
814 cd addtest2 &&
816 cd repo &&
817 echo "$submodurl/repo" >expect &&
818 git config remote.origin.url >actual &&
819 test_cmp expect actual &&
820 echo "gitdir: ../.git/modules/repo" >expect &&
821 test_cmp expect .git
822 ) &&
823 rm -rf repo &&
824 git rm repo &&
825 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
826 test_must_be_empty actual &&
827 echo "gitdir: ../.git/modules/submod" >expect &&
828 test_cmp expect submod/.git &&
830 cd repo &&
831 echo "$submodurl/bare.git" >expect &&
832 git config remote.origin.url >actual &&
833 test_cmp expect actual &&
834 echo "gitdir: ../.git/modules/repo_new" >expect &&
835 test_cmp expect .git
836 ) &&
837 echo "repo" >expect &&
838 test_must_fail git config -f .gitmodules submodule.repo.path &&
839 git config -f .gitmodules submodule.repo_new.path >actual &&
840 test_cmp expect actual&&
841 echo "$submodurl/repo" >expect &&
842 test_must_fail git config -f .gitmodules submodule.repo.url &&
843 echo "$submodurl/bare.git" >expect &&
844 git config -f .gitmodules submodule.repo_new.url >actual &&
845 test_cmp expect actual &&
846 echo "$submodurl/repo" >expect &&
847 git config submodule.repo.url >actual &&
848 test_cmp expect actual &&
849 echo "$submodurl/bare.git" >expect &&
850 git config submodule.repo_new.url >actual &&
851 test_cmp expect actual
855 test_expect_success 'recursive relative submodules stay relative' '
856 test_when_finished "rm -rf super clone2 subsub sub3" &&
857 mkdir subsub &&
859 cd subsub &&
860 git init &&
861 >t &&
862 git add t &&
863 git commit -m "initial commit"
864 ) &&
865 mkdir sub3 &&
867 cd sub3 &&
868 git init &&
869 >t &&
870 git add t &&
871 git commit -m "initial commit" &&
872 git submodule add ../subsub dirdir/subsub &&
873 git commit -m "add submodule subsub"
874 ) &&
875 mkdir super &&
877 cd super &&
878 git init &&
879 >t &&
880 git add t &&
881 git commit -m "initial commit" &&
882 git submodule add ../sub3 &&
883 git commit -m "add submodule sub"
884 ) &&
885 git clone super clone2 &&
887 cd clone2 &&
888 git submodule update --init --recursive &&
889 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
890 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
891 ) &&
892 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
893 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
896 test_expect_success 'submodule add with an existing name fails unless forced' '
898 cd addtest2 &&
899 rm -rf repo &&
900 git rm repo &&
901 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
902 test ! -d repo &&
903 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
904 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
905 echo "$submodurl/bare.git" >expect &&
906 git config submodule.repo_new.url >actual &&
907 test_cmp expect actual &&
908 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
909 test -d repo &&
910 echo "repo" >expect &&
911 git config -f .gitmodules submodule.repo_new.path >actual &&
912 test_cmp expect actual&&
913 echo "$submodurl/repo.git" >expect &&
914 git config -f .gitmodules submodule.repo_new.url >actual &&
915 test_cmp expect actual &&
916 echo "$submodurl/repo.git" >expect &&
917 git config submodule.repo_new.url >actual &&
918 test_cmp expect actual
922 test_expect_success 'set up a second submodule' '
923 git submodule add ./init2 example2 &&
924 git commit -m "submodule example2 added"
927 test_expect_success 'submodule deinit works on repository without submodules' '
928 test_when_finished "rm -rf newdirectory" &&
929 mkdir newdirectory &&
931 cd newdirectory &&
932 git init &&
933 >file &&
934 git add file &&
935 git commit -m "repo should not be empty" &&
936 git submodule deinit . &&
937 git submodule deinit --all
941 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
942 git config submodule.example.foo bar &&
943 git config submodule.example2.frotz nitfol &&
944 git submodule deinit init &&
945 test -z "$(git config --get-regexp "submodule\.example\.")" &&
946 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
947 test -f example2/.git &&
948 rmdir init
951 test_expect_success 'submodule deinit from subdirectory' '
952 git submodule update --init &&
953 git config submodule.example.foo bar &&
954 mkdir -p sub &&
956 cd sub &&
957 git submodule deinit ../init >../output
958 ) &&
959 test_i18ngrep "\\.\\./init" output &&
960 test -z "$(git config --get-regexp "submodule\.example\.")" &&
961 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
962 test -f example2/.git &&
963 rmdir init
966 test_expect_success 'submodule deinit . deinits all initialized submodules' '
967 git submodule update --init &&
968 git config submodule.example.foo bar &&
969 git config submodule.example2.frotz nitfol &&
970 test_must_fail git submodule deinit &&
971 git submodule deinit . >actual &&
972 test -z "$(git config --get-regexp "submodule\.example\.")" &&
973 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
974 test_i18ngrep "Cleared directory .init" actual &&
975 test_i18ngrep "Cleared directory .example2" actual &&
976 rmdir init example2
979 test_expect_success 'submodule deinit --all deinits all initialized submodules' '
980 git submodule update --init &&
981 git config submodule.example.foo bar &&
982 git config submodule.example2.frotz nitfol &&
983 test_must_fail git submodule deinit &&
984 git submodule deinit --all >actual &&
985 test -z "$(git config --get-regexp "submodule\.example\.")" &&
986 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
987 test_i18ngrep "Cleared directory .init" actual &&
988 test_i18ngrep "Cleared directory .example2" actual &&
989 rmdir init example2
992 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
993 git submodule update --init &&
994 rm -rf init example2/* example2/.git &&
995 git submodule deinit init example2 >actual &&
996 test -z "$(git config --get-regexp "submodule\.example\.")" &&
997 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
998 test_i18ngrep ! "Cleared directory .init" actual &&
999 test_i18ngrep "Cleared directory .example2" actual &&
1000 rmdir init
1003 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
1004 git submodule update --init &&
1005 echo X >>init/s &&
1006 test_must_fail git submodule deinit init &&
1007 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1008 test -f example2/.git &&
1009 git submodule deinit -f init >actual &&
1010 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1011 test_i18ngrep "Cleared directory .init" actual &&
1012 rmdir init
1015 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1016 git submodule update --init &&
1017 echo X >>init/untracked &&
1018 test_must_fail git submodule deinit init &&
1019 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1020 test -f example2/.git &&
1021 git submodule deinit -f init >actual &&
1022 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1023 test_i18ngrep "Cleared directory .init" actual &&
1024 rmdir init
1027 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1028 git submodule update --init &&
1030 cd init &&
1031 git checkout HEAD^
1032 ) &&
1033 test_must_fail git submodule deinit init &&
1034 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1035 test -f example2/.git &&
1036 git submodule deinit -f init >actual &&
1037 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1038 test_i18ngrep "Cleared directory .init" actual &&
1039 rmdir init
1042 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1043 git submodule update --init &&
1044 git submodule deinit init >actual &&
1045 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1046 test_i18ngrep "Cleared directory .init" actual &&
1047 git submodule deinit init >actual &&
1048 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1049 test_i18ngrep "Cleared directory .init" actual &&
1050 git submodule deinit . >actual &&
1051 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1052 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1053 test_i18ngrep "Cleared directory .init" actual &&
1054 git submodule deinit . >actual &&
1055 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1056 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1057 test_i18ngrep "Cleared directory .init" actual &&
1058 git submodule deinit --all >actual &&
1059 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1060 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1061 test_i18ngrep "Cleared directory .init" actual &&
1062 rmdir init example2
1065 test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
1066 git submodule update --init &&
1068 cd init &&
1069 rm .git &&
1070 cp -R ../.git/modules/example .git &&
1071 GIT_WORK_TREE=. git config --unset core.worktree
1072 ) &&
1073 test_must_fail git submodule deinit init &&
1074 test_must_fail git submodule deinit -f init &&
1075 test -d init/.git &&
1076 test -n "$(git config --get-regexp "submodule\.example\.")"
1079 test_expect_success 'submodule with UTF-8 name' '
1080 svname=$(printf "\303\245 \303\244\303\266") &&
1081 mkdir "$svname" &&
1083 cd "$svname" &&
1084 git init &&
1085 >sub &&
1086 git add sub &&
1087 git commit -m "init sub"
1088 ) &&
1089 git submodule add ./"$svname" &&
1090 git submodule >&2 &&
1091 test -n "$(git submodule | grep "$svname")"
1094 test_expect_success 'submodule add clone shallow submodule' '
1095 mkdir super &&
1096 pwd=$(pwd) &&
1098 cd super &&
1099 git init &&
1100 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1102 cd submodule &&
1103 test 1 = $(git log --oneline | wc -l)
1108 test_expect_success 'submodule helper list is not confused by common prefixes' '
1109 mkdir -p dir1/b &&
1111 cd dir1/b &&
1112 git init &&
1113 echo hi >testfile2 &&
1114 git add . &&
1115 git commit -m "test1"
1116 ) &&
1117 mkdir -p dir2/b &&
1119 cd dir2/b &&
1120 git init &&
1121 echo hello >testfile1 &&
1122 git add . &&
1123 git commit -m "test2"
1124 ) &&
1125 git submodule add /dir1/b dir1/b &&
1126 git submodule add /dir2/b dir2/b &&
1127 git commit -m "first submodule commit" &&
1128 git submodule--helper list dir1/b |cut -c51- >actual &&
1129 echo "dir1/b" >expect &&
1130 test_cmp expect actual
1134 test_done