builtin/apply: move 'root' global into 'struct apply_state'
[alt-git.git] / t / t7400-submodule-basic.sh
blobf99f674ac795b7b55c5fc678257bd7365c71e62d
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 'setup - initial commit' '
15 >t &&
16 git add t &&
17 git commit -m "initial commit" &&
18 git branch initial
21 test_expect_success 'configuration parsing' '
22 test_when_finished "rm -f .gitmodules" &&
23 cat >.gitmodules <<-\EOF &&
24 [submodule "s"]
25 path
26 ignore
27 EOF
28 test_must_fail git status
31 test_expect_success 'setup - repository in init subdirectory' '
32 mkdir init &&
34 cd init &&
35 git init &&
36 echo a >a &&
37 git add a &&
38 git commit -m "submodule commit 1" &&
39 git tag -a -m "rev-1" rev-1
43 test_expect_success 'setup - commit with gitlink' '
44 echo a >a &&
45 echo z >z &&
46 git add a init z &&
47 git commit -m "super commit 1"
50 test_expect_success 'setup - hide init subdirectory' '
51 mv init .subrepo
54 test_expect_success 'setup - repository to add submodules to' '
55 git init addtest &&
56 git init addtest-ignore
59 # The 'submodule add' tests need some repository to add as a submodule.
60 # The trash directory is a good one as any. We need to canonicalize
61 # the name, though, as some tests compare it to the absolute path git
62 # generates, which will expand symbolic links.
63 submodurl=$(pwd -P)
65 listbranches() {
66 git for-each-ref --format='%(refname)' 'refs/heads/*'
69 inspect() {
70 dir=$1 &&
71 dotdot="${2:-..}" &&
74 cd "$dir" &&
75 listbranches >"$dotdot/heads" &&
76 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
77 git rev-parse HEAD >"$dotdot/head-sha1" &&
78 git update-index --refresh &&
79 git diff-files --exit-code &&
80 git clean -n -d -x >"$dotdot/untracked"
84 test_expect_success 'submodule add' '
85 echo "refs/heads/master" >expect &&
86 >empty &&
89 cd addtest &&
90 git submodule add -q "$submodurl" submod >actual &&
91 test_must_be_empty actual &&
92 echo "gitdir: ../.git/modules/submod" >expect &&
93 test_cmp expect submod/.git &&
95 cd submod &&
96 git config core.worktree >actual &&
97 echo "../../../submod" >expect &&
98 test_cmp expect actual &&
99 rm -f actual expect
100 ) &&
101 git submodule init
102 ) &&
104 rm -f heads head untracked &&
105 inspect addtest/submod ../.. &&
106 test_cmp expect heads &&
107 test_cmp expect head &&
108 test_cmp empty untracked
111 test_expect_success 'submodule add to .gitignored path fails' '
113 cd addtest-ignore &&
114 cat <<-\EOF >expect &&
115 The following path is ignored by one of your .gitignore files:
116 submod
117 Use -f if you really want to add it.
119 # Does not use test_commit due to the ignore
120 echo "*" > .gitignore &&
121 git add --force .gitignore &&
122 git commit -m"Ignore everything" &&
123 ! git submodule add "$submodurl" submod >actual 2>&1 &&
124 test_i18ncmp expect actual
128 test_expect_success 'submodule add to .gitignored path with --force' '
130 cd addtest-ignore &&
131 git submodule add --force "$submodurl" submod
135 test_expect_success 'submodule add --branch' '
136 echo "refs/heads/initial" >expect-head &&
137 cat <<-\EOF >expect-heads &&
138 refs/heads/initial
139 refs/heads/master
141 >empty &&
144 cd addtest &&
145 git submodule add -b initial "$submodurl" submod-branch &&
146 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
147 git submodule init
148 ) &&
150 rm -f heads head untracked &&
151 inspect addtest/submod-branch ../.. &&
152 test_cmp expect-heads heads &&
153 test_cmp expect-head head &&
154 test_cmp empty untracked
157 test_expect_success 'submodule add with ./ in path' '
158 echo "refs/heads/master" >expect &&
159 >empty &&
162 cd addtest &&
163 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
164 git submodule init
165 ) &&
167 rm -f heads head untracked &&
168 inspect addtest/dotsubmod/frotz ../../.. &&
169 test_cmp expect heads &&
170 test_cmp expect head &&
171 test_cmp empty untracked
174 test_expect_success 'submodule add with /././ in path' '
175 echo "refs/heads/master" >expect &&
176 >empty &&
179 cd addtest &&
180 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
181 git submodule init
182 ) &&
184 rm -f heads head untracked &&
185 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
186 test_cmp expect heads &&
187 test_cmp expect 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" slashslashsubmod///frotz// &&
198 git submodule init
199 ) &&
201 rm -f heads head untracked &&
202 inspect addtest/slashslashsubmod/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" dotdotsubmod/../realsubmod/frotz/.. &&
215 git submodule init
216 ) &&
218 rm -f heads head untracked &&
219 inspect addtest/realsubmod ../.. &&
220 test_cmp expect heads &&
221 test_cmp expect head &&
222 test_cmp empty untracked
225 test_expect_success 'submodule add with ./, /.. and // in path' '
226 echo "refs/heads/master" >expect &&
227 >empty &&
230 cd addtest &&
231 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
232 git submodule init
233 ) &&
235 rm -f heads head untracked &&
236 inspect addtest/realsubmod2 ../.. &&
237 test_cmp expect heads &&
238 test_cmp expect head &&
239 test_cmp empty untracked
242 test_expect_success 'submodule add in subdirectory' '
243 echo "refs/heads/master" >expect &&
244 >empty &&
246 mkdir addtest/sub &&
248 cd addtest/sub &&
249 git submodule add "$submodurl" ../realsubmod3 &&
250 git submodule init
251 ) &&
253 rm -f heads head untracked &&
254 inspect addtest/realsubmod3 ../.. &&
255 test_cmp expect heads &&
256 test_cmp expect head &&
257 test_cmp empty untracked
260 test_expect_success 'submodule add in subdirectory with relative path should fail' '
262 cd addtest/sub &&
263 test_must_fail git submodule add ../../ submod3 2>../../output.err
264 ) &&
265 test_i18ngrep toplevel output.err
268 test_expect_success 'setup - add an example entry to .gitmodules' '
269 git config --file=.gitmodules submodule.example.url git://example.com/init.git
272 test_expect_success 'status should fail for unmapped paths' '
273 test_must_fail git submodule status
276 test_expect_success 'setup - map path in .gitmodules' '
277 cat <<\EOF >expect &&
278 [submodule "example"]
279 url = git://example.com/init.git
280 path = init
283 git config --file=.gitmodules submodule.example.path init &&
285 test_cmp expect .gitmodules
288 test_expect_success 'status should only print one line' '
289 git submodule status >lines &&
290 test_line_count = 1 lines
293 test_expect_success 'setup - fetch commit name from submodule' '
294 rev1=$(cd .subrepo && git rev-parse HEAD) &&
295 printf "rev1: %s\n" "$rev1" &&
296 test -n "$rev1"
299 test_expect_success 'status should initially be "missing"' '
300 git submodule status >lines &&
301 grep "^-$rev1" lines
304 test_expect_success 'init should register submodule url in .git/config' '
305 echo git://example.com/init.git >expect &&
307 git submodule init &&
308 git config submodule.example.url >url &&
309 git config submodule.example.url ./.subrepo &&
311 test_cmp expect url
314 test_failure_with_unknown_submodule () {
315 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
316 grep "^error: .*no-such-submodule" output.err
319 test_expect_success 'init should fail with unknown submodule' '
320 test_failure_with_unknown_submodule init
323 test_expect_success 'update should fail with unknown submodule' '
324 test_failure_with_unknown_submodule update
327 test_expect_success 'status should fail with unknown submodule' '
328 test_failure_with_unknown_submodule status
331 test_expect_success 'sync should fail with unknown submodule' '
332 test_failure_with_unknown_submodule sync
335 test_expect_success 'update should fail when path is used by a file' '
336 echo hello >expect &&
338 echo "hello" >init &&
339 test_must_fail git submodule update &&
341 test_cmp expect init
344 test_expect_success 'update should fail when path is used by a nonempty directory' '
345 echo hello >expect &&
347 rm -fr init &&
348 mkdir init &&
349 echo "hello" >init/a &&
351 test_must_fail git submodule update &&
353 test_cmp expect init/a
356 test_expect_success 'update should work when path is an empty dir' '
357 rm -fr init &&
358 rm -f head-sha1 &&
359 echo "$rev1" >expect &&
361 mkdir init &&
362 git submodule update -q >update.out &&
363 test_must_be_empty update.out &&
365 inspect init &&
366 test_cmp expect head-sha1
369 test_expect_success 'status should be "up-to-date" after update' '
370 git submodule status >list &&
371 grep "^ $rev1" list
374 test_expect_success 'status "up-to-date" from subdirectory' '
375 mkdir -p sub &&
377 cd sub &&
378 git submodule status >../list
379 ) &&
380 grep "^ $rev1" list &&
381 grep "\\.\\./init" list
384 test_expect_success 'status "up-to-date" from subdirectory with path' '
385 mkdir -p sub &&
387 cd sub &&
388 git submodule status ../init >../list
389 ) &&
390 grep "^ $rev1" list &&
391 grep "\\.\\./init" list
394 test_expect_success 'status should be "modified" after submodule commit' '
396 cd init &&
397 echo b >b &&
398 git add b &&
399 git commit -m "submodule commit 2"
400 ) &&
402 rev2=$(cd init && git rev-parse HEAD) &&
403 test -n "$rev2" &&
404 git submodule status >list &&
406 grep "^+$rev2" list
409 test_expect_success 'the --cached sha1 should be rev1' '
410 git submodule --cached status >list &&
411 grep "^+$rev1" list
414 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
415 git diff >diff &&
416 grep "^+Subproject commit $rev2" diff
419 test_expect_success 'update should checkout rev1' '
420 rm -f head-sha1 &&
421 echo "$rev1" >expect &&
423 git submodule update init &&
424 inspect init &&
426 test_cmp expect head-sha1
429 test_expect_success 'status should be "up-to-date" after update' '
430 git submodule status >list &&
431 grep "^ $rev1" list
434 test_expect_success 'checkout superproject with subproject already present' '
435 git checkout initial &&
436 git checkout master
439 test_expect_success 'apply submodule diff' '
440 >empty &&
442 git branch second &&
444 cd init &&
445 echo s >s &&
446 git add s &&
447 git commit -m "change subproject"
448 ) &&
449 git update-index --add init &&
450 git commit -m "change init" &&
451 git format-patch -1 --stdout >P.diff &&
452 git checkout second &&
453 git apply --index P.diff &&
455 git diff --cached master >staged &&
456 test_cmp empty staged
459 test_expect_success 'update --init' '
460 mv init init2 &&
461 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
462 git config --remove-section submodule.example &&
463 test_must_fail git config submodule.example.url &&
465 git submodule update init 2> update.out &&
466 cat update.out &&
467 test_i18ngrep "not initialized" update.out &&
468 test_must_fail git rev-parse --resolve-git-dir init/.git &&
470 git submodule update --init init &&
471 git rev-parse --resolve-git-dir init/.git
474 test_expect_success 'update --init from subdirectory' '
475 mv init init2 &&
476 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
477 git config --remove-section submodule.example &&
478 test_must_fail git config submodule.example.url &&
480 mkdir -p sub &&
482 cd sub &&
483 git submodule update ../init 2>update.out &&
484 cat update.out &&
485 test_i18ngrep "not initialized" update.out &&
486 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
488 git submodule update --init ../init
489 ) &&
490 git rev-parse --resolve-git-dir init/.git
493 test_expect_success 'do not add files from a submodule' '
495 git reset --hard &&
496 test_must_fail git add init/a
500 test_expect_success 'gracefully add/reset submodule with a trailing slash' '
502 git reset --hard &&
503 git commit -m "commit subproject" init &&
504 (cd init &&
505 echo b > a) &&
506 git add init/ &&
507 git diff --exit-code --cached init &&
508 commit=$(cd init &&
509 git commit -m update a >/dev/null &&
510 git rev-parse HEAD) &&
511 git add init/ &&
512 test_must_fail git diff --exit-code --cached init &&
513 test $commit = $(git ls-files --stage |
514 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
515 git reset init/ &&
516 git diff --exit-code --cached init
520 test_expect_success 'ls-files gracefully handles trailing slash' '
522 test "init" = "$(git ls-files init/)"
526 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
527 rm -rf init &&
528 mkdir init &&
529 git reset --hard &&
530 git checkout initial &&
531 test ! -d init &&
532 git checkout second
535 test_expect_success 'submodule <invalid-subcommand> fails' '
536 test_must_fail git submodule no-such-subcommand
539 test_expect_success 'add submodules without specifying an explicit path' '
540 mkdir repo &&
542 cd repo &&
543 git init &&
544 echo r >r &&
545 git add r &&
546 git commit -m "repo commit 1"
547 ) &&
548 git clone --bare repo/ bare.git &&
550 cd addtest &&
551 git submodule add "$submodurl/repo" &&
552 git config -f .gitmodules submodule.repo.path repo &&
553 git submodule add "$submodurl/bare.git" &&
554 git config -f .gitmodules submodule.bare.path bare
558 test_expect_success 'add should fail when path is used by a file' '
560 cd addtest &&
561 touch file &&
562 test_must_fail git submodule add "$submodurl/repo" file
566 test_expect_success 'add should fail when path is used by an existing directory' '
568 cd addtest &&
569 mkdir empty-dir &&
570 test_must_fail git submodule add "$submodurl/repo" empty-dir
574 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
576 cd addtest &&
577 git submodule add ../repo relative &&
578 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
579 git submodule sync relative &&
580 test "$(git config submodule.relative.url)" = "$submodurl/repo"
584 test_expect_success 'set up for relative path tests' '
585 mkdir reltest &&
587 cd reltest &&
588 git init &&
589 mkdir sub &&
591 cd sub &&
592 git init &&
593 test_commit foo
594 ) &&
595 git add sub &&
596 git config -f .gitmodules submodule.sub.path sub &&
597 git config -f .gitmodules submodule.sub.url ../subrepo &&
598 cp .git/config pristine-.git-config &&
599 cp .gitmodules pristine-.gitmodules
603 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
605 cd reltest &&
606 cp pristine-.git-config .git/config &&
607 cp pristine-.gitmodules .gitmodules &&
608 git config remote.origin.url ssh://hostname/repo &&
609 git submodule init &&
610 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
614 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
616 cd reltest &&
617 cp pristine-.git-config .git/config &&
618 cp pristine-.gitmodules .gitmodules &&
619 git config remote.origin.url ssh://hostname:22/repo &&
620 git submodule init &&
621 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
625 # About the choice of the path in the next test:
626 # - double-slash side-steps path mangling issues on Windows
627 # - it is still an absolute local path
628 # - there cannot be a server with a blank in its name just in case the
629 # path is used erroneously to access a //server/share style path
630 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
632 cd reltest &&
633 cp pristine-.git-config .git/config &&
634 cp pristine-.gitmodules .gitmodules &&
635 git config remote.origin.url "//somewhere else/repo" &&
636 git submodule init &&
637 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
641 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
643 cd reltest &&
644 cp pristine-.git-config .git/config &&
645 cp pristine-.gitmodules .gitmodules &&
646 git config remote.origin.url file:///tmp/repo &&
647 git submodule init &&
648 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
652 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
654 cd reltest &&
655 cp pristine-.git-config .git/config &&
656 cp pristine-.gitmodules .gitmodules &&
657 git config remote.origin.url helper:://hostname/repo &&
658 git submodule init &&
659 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
663 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
665 cd reltest &&
666 cp pristine-.git-config .git/config &&
667 git config remote.origin.url user@host:repo &&
668 git submodule init &&
669 test "$(git config submodule.sub.url)" = user@host:subrepo
673 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
675 cd reltest &&
676 cp pristine-.git-config .git/config &&
677 cp pristine-.gitmodules .gitmodules &&
678 git config remote.origin.url user@host:path/to/repo &&
679 git submodule init &&
680 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
684 test_expect_success '../subrepo works with relative local path - foo' '
686 cd reltest &&
687 cp pristine-.git-config .git/config &&
688 cp pristine-.gitmodules .gitmodules &&
689 git config remote.origin.url foo &&
690 # actual: fails with an error
691 git submodule init &&
692 test "$(git config submodule.sub.url)" = subrepo
696 test_expect_success '../subrepo works with relative local path - foo/bar' '
698 cd reltest &&
699 cp pristine-.git-config .git/config &&
700 cp pristine-.gitmodules .gitmodules &&
701 git config remote.origin.url foo/bar &&
702 git submodule init &&
703 test "$(git config submodule.sub.url)" = foo/subrepo
707 test_expect_success '../subrepo works with relative local path - ./foo' '
709 cd reltest &&
710 cp pristine-.git-config .git/config &&
711 cp pristine-.gitmodules .gitmodules &&
712 git config remote.origin.url ./foo &&
713 git submodule init &&
714 test "$(git config submodule.sub.url)" = subrepo
718 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
720 cd reltest &&
721 cp pristine-.git-config .git/config &&
722 cp pristine-.gitmodules .gitmodules &&
723 git config remote.origin.url ./foo/bar &&
724 git submodule init &&
725 test "$(git config submodule.sub.url)" = foo/subrepo
729 test_expect_success '../subrepo works with relative local path - ../foo' '
731 cd reltest &&
732 cp pristine-.git-config .git/config &&
733 cp pristine-.gitmodules .gitmodules &&
734 git config remote.origin.url ../foo &&
735 git submodule init &&
736 test "$(git config submodule.sub.url)" = ../subrepo
740 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
742 cd reltest &&
743 cp pristine-.git-config .git/config &&
744 cp pristine-.gitmodules .gitmodules &&
745 git config remote.origin.url ../foo/bar &&
746 git submodule init &&
747 test "$(git config submodule.sub.url)" = ../foo/subrepo
751 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
753 cd reltest &&
754 cp pristine-.git-config .git/config &&
755 cp pristine-.gitmodules .gitmodules &&
756 mkdir -p a/b/c &&
757 (cd a/b/c; git init) &&
758 git config remote.origin.url ../foo/bar.git &&
759 git submodule add ../bar/a/b/c ./a/b/c &&
760 git submodule init &&
761 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
765 test_expect_success 'moving the superproject does not break submodules' '
767 cd addtest &&
768 git submodule status >expect
769 ) &&
770 mv addtest addtest2 &&
772 cd addtest2 &&
773 git submodule status >actual &&
774 test_cmp expect actual
778 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
780 cd addtest2 &&
782 cd repo &&
783 echo "$submodurl/repo" >expect &&
784 git config remote.origin.url >actual &&
785 test_cmp expect actual &&
786 echo "gitdir: ../.git/modules/repo" >expect &&
787 test_cmp expect .git
788 ) &&
789 rm -rf repo &&
790 git rm repo &&
791 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
792 test_must_be_empty actual &&
793 echo "gitdir: ../.git/modules/submod" >expect &&
794 test_cmp expect submod/.git &&
796 cd repo &&
797 echo "$submodurl/bare.git" >expect &&
798 git config remote.origin.url >actual &&
799 test_cmp expect actual &&
800 echo "gitdir: ../.git/modules/repo_new" >expect &&
801 test_cmp expect .git
802 ) &&
803 echo "repo" >expect &&
804 test_must_fail git config -f .gitmodules submodule.repo.path &&
805 git config -f .gitmodules submodule.repo_new.path >actual &&
806 test_cmp expect actual&&
807 echo "$submodurl/repo" >expect &&
808 test_must_fail git config -f .gitmodules submodule.repo.url &&
809 echo "$submodurl/bare.git" >expect &&
810 git config -f .gitmodules submodule.repo_new.url >actual &&
811 test_cmp expect actual &&
812 echo "$submodurl/repo" >expect &&
813 git config submodule.repo.url >actual &&
814 test_cmp expect actual &&
815 echo "$submodurl/bare.git" >expect &&
816 git config submodule.repo_new.url >actual &&
817 test_cmp expect actual
821 test_expect_success 'recursive relative submodules stay relative' '
822 test_when_finished "rm -rf super clone2 subsub sub3" &&
823 mkdir subsub &&
825 cd subsub &&
826 git init &&
827 >t &&
828 git add t &&
829 git commit -m "initial commit"
830 ) &&
831 mkdir sub3 &&
833 cd sub3 &&
834 git init &&
835 >t &&
836 git add t &&
837 git commit -m "initial commit" &&
838 git submodule add ../subsub dirdir/subsub &&
839 git commit -m "add submodule subsub"
840 ) &&
841 mkdir super &&
843 cd super &&
844 git init &&
845 >t &&
846 git add t &&
847 git commit -m "initial commit" &&
848 git submodule add ../sub3 &&
849 git commit -m "add submodule sub"
850 ) &&
851 git clone super clone2 &&
853 cd clone2 &&
854 git submodule update --init --recursive &&
855 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
856 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
857 ) &&
858 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
859 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
862 test_expect_success 'submodule add with an existing name fails unless forced' '
864 cd addtest2 &&
865 rm -rf repo &&
866 git rm repo &&
867 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
868 test ! -d repo &&
869 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
870 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
871 echo "$submodurl/bare.git" >expect &&
872 git config submodule.repo_new.url >actual &&
873 test_cmp expect actual &&
874 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
875 test -d repo &&
876 echo "repo" >expect &&
877 git config -f .gitmodules submodule.repo_new.path >actual &&
878 test_cmp expect actual&&
879 echo "$submodurl/repo.git" >expect &&
880 git config -f .gitmodules submodule.repo_new.url >actual &&
881 test_cmp expect actual &&
882 echo "$submodurl/repo.git" >expect &&
883 git config submodule.repo_new.url >actual &&
884 test_cmp expect actual
888 test_expect_success 'set up a second submodule' '
889 git submodule add ./init2 example2 &&
890 git commit -m "submodule example2 added"
893 test_expect_success 'submodule deinit works on repository without submodules' '
894 test_when_finished "rm -rf newdirectory" &&
895 mkdir newdirectory &&
897 cd newdirectory &&
898 git init &&
899 >file &&
900 git add file &&
901 git commit -m "repo should not be empty"
902 git submodule deinit .
906 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
907 git config submodule.example.foo bar &&
908 git config submodule.example2.frotz nitfol &&
909 git submodule deinit init &&
910 test -z "$(git config --get-regexp "submodule\.example\.")" &&
911 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
912 test -f example2/.git &&
913 rmdir init
916 test_expect_success 'submodule deinit from subdirectory' '
917 git submodule update --init &&
918 git config submodule.example.foo bar &&
919 mkdir -p sub &&
921 cd sub &&
922 git submodule deinit ../init >../output
923 ) &&
924 grep "\\.\\./init" output &&
925 test -z "$(git config --get-regexp "submodule\.example\.")" &&
926 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
927 test -f example2/.git &&
928 rmdir init
931 test_expect_success 'submodule deinit . deinits all initialized submodules' '
932 git submodule update --init &&
933 git config submodule.example.foo bar &&
934 git config submodule.example2.frotz nitfol &&
935 test_must_fail git submodule deinit &&
936 git submodule deinit . >actual &&
937 test -z "$(git config --get-regexp "submodule\.example\.")" &&
938 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
939 test_i18ngrep "Cleared directory .init" actual &&
940 test_i18ngrep "Cleared directory .example2" actual &&
941 rmdir init example2
944 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
945 git submodule update --init &&
946 rm -rf init example2/* example2/.git &&
947 git submodule deinit init example2 >actual &&
948 test -z "$(git config --get-regexp "submodule\.example\.")" &&
949 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
950 test_i18ngrep ! "Cleared directory .init" actual &&
951 test_i18ngrep "Cleared directory .example2" actual &&
952 rmdir init
955 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
956 git submodule update --init &&
957 echo X >>init/s &&
958 test_must_fail git submodule deinit init &&
959 test -n "$(git config --get-regexp "submodule\.example\.")" &&
960 test -f example2/.git &&
961 git submodule deinit -f init >actual &&
962 test -z "$(git config --get-regexp "submodule\.example\.")" &&
963 test_i18ngrep "Cleared directory .init" actual &&
964 rmdir init
967 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
968 git submodule update --init &&
969 echo X >>init/untracked &&
970 test_must_fail git submodule deinit init &&
971 test -n "$(git config --get-regexp "submodule\.example\.")" &&
972 test -f example2/.git &&
973 git submodule deinit -f init >actual &&
974 test -z "$(git config --get-regexp "submodule\.example\.")" &&
975 test_i18ngrep "Cleared directory .init" actual &&
976 rmdir init
979 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
980 git submodule update --init &&
982 cd init &&
983 git checkout HEAD^
984 ) &&
985 test_must_fail git submodule deinit init &&
986 test -n "$(git config --get-regexp "submodule\.example\.")" &&
987 test -f example2/.git &&
988 git submodule deinit -f init >actual &&
989 test -z "$(git config --get-regexp "submodule\.example\.")" &&
990 test_i18ngrep "Cleared directory .init" actual &&
991 rmdir init
994 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
995 git submodule update --init &&
996 git submodule deinit init >actual &&
997 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
998 test_i18ngrep "Cleared directory .init" actual &&
999 git submodule deinit init >actual &&
1000 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1001 test_i18ngrep "Cleared directory .init" actual &&
1002 git submodule deinit . >actual &&
1003 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1004 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1005 test_i18ngrep "Cleared directory .init" actual &&
1006 git submodule deinit . >actual &&
1007 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1008 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1009 test_i18ngrep "Cleared directory .init" actual &&
1010 rmdir init example2
1013 test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
1014 git submodule update --init &&
1016 cd init &&
1017 rm .git &&
1018 cp -R ../.git/modules/example .git &&
1019 GIT_WORK_TREE=. git config --unset core.worktree
1020 ) &&
1021 test_must_fail git submodule deinit init &&
1022 test_must_fail git submodule deinit -f init &&
1023 test -d init/.git &&
1024 test -n "$(git config --get-regexp "submodule\.example\.")"
1027 test_expect_success 'submodule with UTF-8 name' '
1028 svname=$(printf "\303\245 \303\244\303\266") &&
1029 mkdir "$svname" &&
1031 cd "$svname" &&
1032 git init &&
1033 >sub &&
1034 git add sub &&
1035 git commit -m "init sub"
1036 ) &&
1037 git submodule add ./"$svname" &&
1038 git submodule >&2 &&
1039 test -n "$(git submodule | grep "$svname")"
1042 test_expect_success 'submodule add clone shallow submodule' '
1043 mkdir super &&
1044 pwd=$(pwd) &&
1046 cd super &&
1047 git init &&
1048 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1050 cd submodule &&
1051 test 1 = $(git log --oneline | wc -l)
1056 test_expect_success 'submodule helper list is not confused by common prefixes' '
1057 mkdir -p dir1/b &&
1059 cd dir1/b &&
1060 git init &&
1061 echo hi >testfile2 &&
1062 git add . &&
1063 git commit -m "test1"
1064 ) &&
1065 mkdir -p dir2/b &&
1067 cd dir2/b &&
1068 git init &&
1069 echo hello >testfile1 &&
1070 git add . &&
1071 git commit -m "test2"
1072 ) &&
1073 git submodule add /dir1/b dir1/b &&
1074 git submodule add /dir2/b dir2/b &&
1075 git commit -m "first submodule commit" &&
1076 git submodule--helper list dir1/b |cut -c51- >actual &&
1077 echo "dir1/b" >expect &&
1078 test_cmp expect actual
1082 test_done