utf8: fix overflow when returning string width
[alt-git.git] / t / t7400-submodule-basic.sh
blobbf1a4dfadb16821406237225365f638353b70d97
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 - enable local submodules' '
15 git config --global protocol.file.allow always
18 test_expect_success 'submodule deinit works on empty repository' '
19 git submodule deinit --all
22 test_expect_success 'setup - initial commit' '
23 >t &&
24 git add t &&
25 git commit -m "initial commit" &&
26 git branch initial
29 test_expect_success 'submodule init aborts on missing .gitmodules file' '
30 test_when_finished "git update-index --remove sub" &&
31 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
32 # missing the .gitmodules file here
33 test_must_fail git submodule init 2>actual &&
34 test_i18ngrep "No url found for submodule path" actual
37 test_expect_success 'submodule update aborts on missing .gitmodules file' '
38 test_when_finished "git update-index --remove sub" &&
39 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
40 # missing the .gitmodules file here
41 git submodule update sub 2>actual &&
42 test_i18ngrep "Submodule path .sub. not initialized" actual
45 test_expect_success 'submodule update aborts on missing gitmodules url' '
46 test_when_finished "git update-index --remove sub" &&
47 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
48 test_when_finished "rm -f .gitmodules" &&
49 git config -f .gitmodules submodule.s.path sub &&
50 test_must_fail git submodule init
53 test_expect_success 'add aborts on repository with no commits' '
54 cat >expect <<-\EOF &&
55 '"'repo-no-commits'"' does not have a commit checked out
56 EOF
57 git init repo-no-commits &&
58 test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
59 test_i18ncmp expect actual
62 test_expect_success 'status should ignore inner git repo when not added' '
63 rm -fr inner &&
64 mkdir inner &&
66 cd inner &&
67 git init &&
68 >t &&
69 git add t &&
70 git commit -m "initial"
71 ) &&
72 test_must_fail git submodule status inner 2>output.err &&
73 rm -fr inner &&
74 test_i18ngrep "^error: .*did not match any file(s) known to git" output.err
77 test_expect_success 'setup - repository in init subdirectory' '
78 mkdir init &&
80 cd init &&
81 git init &&
82 echo a >a &&
83 git add a &&
84 git commit -m "submodule commit 1" &&
85 git tag -a -m "rev-1" rev-1
89 test_expect_success 'setup - commit with gitlink' '
90 echo a >a &&
91 echo z >z &&
92 git add a init z &&
93 git commit -m "super commit 1"
96 test_expect_success 'setup - hide init subdirectory' '
97 mv init .subrepo
100 test_expect_success 'setup - repository to add submodules to' '
101 git init addtest &&
102 git init addtest-ignore
105 # The 'submodule add' tests need some repository to add as a submodule.
106 # The trash directory is a good one as any. We need to canonicalize
107 # the name, though, as some tests compare it to the absolute path git
108 # generates, which will expand symbolic links.
109 submodurl=$(pwd -P)
111 listbranches() {
112 git for-each-ref --format='%(refname)' 'refs/heads/*'
115 inspect() {
116 dir=$1 &&
117 dotdot="${2:-..}" &&
120 cd "$dir" &&
121 listbranches >"$dotdot/heads" &&
122 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
123 git rev-parse HEAD >"$dotdot/head-sha1" &&
124 git update-index --refresh &&
125 git diff-files --exit-code &&
126 git clean -n -d -x >"$dotdot/untracked"
130 test_expect_success 'submodule add' '
131 echo "refs/heads/master" >expect &&
134 cd addtest &&
135 git submodule add -q "$submodurl" submod >actual &&
136 test_must_be_empty actual &&
137 echo "gitdir: ../.git/modules/submod" >expect &&
138 test_cmp expect submod/.git &&
140 cd submod &&
141 git config core.worktree >actual &&
142 echo "../../../submod" >expect &&
143 test_cmp expect actual &&
144 rm -f actual expect
145 ) &&
146 git submodule init
147 ) &&
149 rm -f heads head untracked &&
150 inspect addtest/submod ../.. &&
151 test_cmp expect heads &&
152 test_cmp expect head &&
153 test_must_be_empty untracked
156 test_expect_success 'setup parent and one repository' '
157 test_create_repo parent &&
158 test_commit -C parent one
161 test_expect_success 'redirected submodule add does not show progress' '
162 git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
163 2>err &&
164 ! grep % err &&
165 test_i18ngrep ! "Checking connectivity" err
168 test_expect_success 'redirected submodule add --progress does show progress' '
169 git -C addtest submodule add --progress "file://$submodurl/parent" \
170 submod-redirected-progress 2>err && \
171 grep % err
174 test_expect_success 'submodule add to .gitignored path fails' '
176 cd addtest-ignore &&
177 cat <<-\EOF >expect &&
178 The following paths are ignored by one of your .gitignore files:
179 submod
180 hint: Use -f if you really want to add them.
181 hint: Turn this message off by running
182 hint: "git config advice.addIgnoredFile false"
184 # Does not use test_commit due to the ignore
185 echo "*" > .gitignore &&
186 git add --force .gitignore &&
187 git commit -m"Ignore everything" &&
188 ! git submodule add "$submodurl" submod >actual 2>&1 &&
189 test_i18ncmp expect actual
193 test_expect_success 'submodule add to .gitignored path with --force' '
195 cd addtest-ignore &&
196 git submodule add --force "$submodurl" submod
200 test_expect_success 'submodule add to reconfigure existing submodule with --force' '
202 cd addtest-ignore &&
203 bogus_url="$(pwd)/bogus-url" &&
204 git submodule add --force "$bogus_url" submod &&
205 git submodule add --force -b initial "$submodurl" submod-branch &&
206 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
207 test "$bogus_url" = "$(git config submodule.submod.url)" &&
208 # Restore the url
209 git submodule add --force "$submodurl" submod &&
210 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
211 test "$submodurl" = "$(git config submodule.submod.url)"
215 test_expect_success 'submodule add relays add --dry-run stderr' '
216 test_when_finished "rm -rf addtest/.git/index.lock" &&
218 cd addtest &&
219 : >.git/index.lock &&
220 ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
221 test_i18ngrep "^fatal: .*index\.lock" output.err &&
222 test_path_is_missing sub-while-locked
226 test_expect_success 'submodule add --branch' '
227 echo "refs/heads/initial" >expect-head &&
228 cat <<-\EOF >expect-heads &&
229 refs/heads/initial
230 refs/heads/master
234 cd addtest &&
235 git submodule add -b initial "$submodurl" submod-branch &&
236 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
237 git submodule init
238 ) &&
240 rm -f heads head untracked &&
241 inspect addtest/submod-branch ../.. &&
242 test_cmp expect-heads heads &&
243 test_cmp expect-head head &&
244 test_must_be_empty untracked
247 test_expect_success 'submodule add with ./ in path' '
248 echo "refs/heads/master" >expect &&
251 cd addtest &&
252 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
253 git submodule init
254 ) &&
256 rm -f heads head untracked &&
257 inspect addtest/dotsubmod/frotz ../../.. &&
258 test_cmp expect heads &&
259 test_cmp expect head &&
260 test_must_be_empty untracked
263 test_expect_success 'submodule add with /././ in path' '
264 echo "refs/heads/master" >expect &&
267 cd addtest &&
268 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
269 git submodule init
270 ) &&
272 rm -f heads head untracked &&
273 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
274 test_cmp expect heads &&
275 test_cmp expect head &&
276 test_must_be_empty untracked
279 test_expect_success 'submodule add with // in path' '
280 echo "refs/heads/master" >expect &&
283 cd addtest &&
284 git submodule add "$submodurl" slashslashsubmod///frotz// &&
285 git submodule init
286 ) &&
288 rm -f heads head untracked &&
289 inspect addtest/slashslashsubmod/frotz ../../.. &&
290 test_cmp expect heads &&
291 test_cmp expect head &&
292 test_must_be_empty untracked
295 test_expect_success 'submodule add with /.. in path' '
296 echo "refs/heads/master" >expect &&
299 cd addtest &&
300 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
301 git submodule init
302 ) &&
304 rm -f heads head untracked &&
305 inspect addtest/realsubmod ../.. &&
306 test_cmp expect heads &&
307 test_cmp expect head &&
308 test_must_be_empty untracked
311 test_expect_success 'submodule add with ./, /.. and // in path' '
312 echo "refs/heads/master" >expect &&
315 cd addtest &&
316 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
317 git submodule init
318 ) &&
320 rm -f heads head untracked &&
321 inspect addtest/realsubmod2 ../.. &&
322 test_cmp expect heads &&
323 test_cmp expect head &&
324 test_must_be_empty untracked
327 test_expect_success !CYGWIN 'submodule add with \\ in path' '
328 test_when_finished "rm -rf parent sub\\with\\backslash" &&
330 # Initialize a repo with a backslash in its name
331 git init sub\\with\\backslash &&
332 touch sub\\with\\backslash/empty.file &&
333 git -C sub\\with\\backslash add empty.file &&
334 git -C sub\\with\\backslash commit -m "Added empty.file" &&
336 # Add that repository as a submodule
337 git init parent &&
338 git -C parent submodule add ../sub\\with\\backslash
341 test_expect_success 'submodule add in subdirectory' '
342 echo "refs/heads/master" >expect &&
344 mkdir addtest/sub &&
346 cd addtest/sub &&
347 git submodule add "$submodurl" ../realsubmod3 &&
348 git submodule init
349 ) &&
351 rm -f heads head untracked &&
352 inspect addtest/realsubmod3 ../.. &&
353 test_cmp expect heads &&
354 test_cmp expect head &&
355 test_must_be_empty untracked
358 test_expect_success 'submodule add in subdirectory with relative path should fail' '
360 cd addtest/sub &&
361 test_must_fail git submodule add ../../ submod3 2>../../output.err
362 ) &&
363 test_i18ngrep toplevel output.err
366 test_expect_success 'setup - add an example entry to .gitmodules' '
367 git config --file=.gitmodules submodule.example.url git://example.com/init.git
370 test_expect_success 'status should fail for unmapped paths' '
371 test_must_fail git submodule status
374 test_expect_success 'setup - map path in .gitmodules' '
375 cat <<\EOF >expect &&
376 [submodule "example"]
377 url = git://example.com/init.git
378 path = init
381 git config --file=.gitmodules submodule.example.path init &&
383 test_cmp expect .gitmodules
386 test_expect_success 'status should only print one line' '
387 git submodule status >lines &&
388 test_line_count = 1 lines
391 test_expect_success 'status from subdirectory should have the same SHA1' '
392 test_when_finished "rmdir addtest/subdir" &&
394 cd addtest &&
395 mkdir subdir &&
396 git submodule status >output &&
397 awk "{print \$1}" <output >expect &&
398 cd subdir &&
399 git submodule status >../output &&
400 awk "{print \$1}" <../output >../actual &&
401 test_cmp ../expect ../actual &&
402 git -C ../submod checkout HEAD^ &&
403 git submodule status >../output &&
404 awk "{print \$1}" <../output >../actual2 &&
405 cd .. &&
406 git submodule status >output &&
407 awk "{print \$1}" <output >expect2 &&
408 test_cmp expect2 actual2 &&
409 ! test_cmp actual actual2
413 test_expect_success 'setup - fetch commit name from submodule' '
414 rev1=$(cd .subrepo && git rev-parse HEAD) &&
415 printf "rev1: %s\n" "$rev1" &&
416 test -n "$rev1"
419 test_expect_success 'status should initially be "missing"' '
420 git submodule status >lines &&
421 grep "^-$rev1" lines
424 test_expect_success 'init should register submodule url in .git/config' '
425 echo git://example.com/init.git >expect &&
427 git submodule init &&
428 git config submodule.example.url >url &&
429 git config submodule.example.url ./.subrepo &&
431 test_cmp expect url
434 test_expect_success 'status should still be "missing" after initializing' '
435 rm -fr init &&
436 mkdir init &&
437 git submodule status >lines &&
438 rm -fr init &&
439 grep "^-$rev1" lines
442 test_failure_with_unknown_submodule () {
443 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
444 test_i18ngrep "^error: .*no-such-submodule" output.err
447 test_expect_success 'init should fail with unknown submodule' '
448 test_failure_with_unknown_submodule init
451 test_expect_success 'update should fail with unknown submodule' '
452 test_failure_with_unknown_submodule update
455 test_expect_success 'status should fail with unknown submodule' '
456 test_failure_with_unknown_submodule status
459 test_expect_success 'sync should fail with unknown submodule' '
460 test_failure_with_unknown_submodule sync
463 test_expect_success 'update should fail when path is used by a file' '
464 echo hello >expect &&
466 echo "hello" >init &&
467 test_must_fail git submodule update &&
469 test_cmp expect init
472 test_expect_success 'update should fail when path is used by a nonempty directory' '
473 echo hello >expect &&
475 rm -fr init &&
476 mkdir init &&
477 echo "hello" >init/a &&
479 test_must_fail git submodule update &&
481 test_cmp expect init/a
484 test_expect_success 'update should work when path is an empty dir' '
485 rm -fr init &&
486 rm -f head-sha1 &&
487 echo "$rev1" >expect &&
489 mkdir init &&
490 git submodule update -q >update.out &&
491 test_must_be_empty update.out &&
493 inspect init &&
494 test_cmp expect head-sha1
497 test_expect_success 'status should be "up-to-date" after update' '
498 git submodule status >list &&
499 grep "^ $rev1" list
502 test_expect_success 'status "up-to-date" from subdirectory' '
503 mkdir -p sub &&
505 cd sub &&
506 git submodule status >../list
507 ) &&
508 grep "^ $rev1" list &&
509 grep "\\.\\./init" list
512 test_expect_success 'status "up-to-date" from subdirectory with path' '
513 mkdir -p sub &&
515 cd sub &&
516 git submodule status ../init >../list
517 ) &&
518 grep "^ $rev1" list &&
519 grep "\\.\\./init" list
522 test_expect_success 'status should be "modified" after submodule commit' '
524 cd init &&
525 echo b >b &&
526 git add b &&
527 git commit -m "submodule commit 2"
528 ) &&
530 rev2=$(cd init && git rev-parse HEAD) &&
531 test -n "$rev2" &&
532 git submodule status >list &&
534 grep "^+$rev2" list
537 test_expect_success 'the --cached sha1 should be rev1' '
538 git submodule --cached status >list &&
539 grep "^+$rev1" list
542 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
543 git diff >diff &&
544 grep "^+Subproject commit $rev2" diff
547 test_expect_success 'update should checkout rev1' '
548 rm -f head-sha1 &&
549 echo "$rev1" >expect &&
551 git submodule update init &&
552 inspect init &&
554 test_cmp expect head-sha1
557 test_expect_success 'status should be "up-to-date" after update' '
558 git submodule status >list &&
559 grep "^ $rev1" list
562 test_expect_success 'checkout superproject with subproject already present' '
563 git checkout initial &&
564 git checkout master
567 test_expect_success 'apply submodule diff' '
568 git branch second &&
570 cd init &&
571 echo s >s &&
572 git add s &&
573 git commit -m "change subproject"
574 ) &&
575 git update-index --add init &&
576 git commit -m "change init" &&
577 git format-patch -1 --stdout >P.diff &&
578 git checkout second &&
579 git apply --index P.diff &&
581 git diff --cached master >staged &&
582 test_must_be_empty staged
585 test_expect_success 'update --init' '
586 mv init init2 &&
587 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
588 git config --remove-section submodule.example &&
589 test_must_fail git config submodule.example.url &&
591 git submodule update init 2> update.out &&
592 test_i18ngrep "not initialized" update.out &&
593 test_must_fail git rev-parse --resolve-git-dir init/.git &&
595 git submodule update --init init &&
596 git rev-parse --resolve-git-dir init/.git
599 test_expect_success 'update --init from subdirectory' '
600 mv init init2 &&
601 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
602 git config --remove-section submodule.example &&
603 test_must_fail git config submodule.example.url &&
605 mkdir -p sub &&
607 cd sub &&
608 git submodule update ../init 2>update.out &&
609 test_i18ngrep "not initialized" update.out &&
610 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
612 git submodule update --init ../init
613 ) &&
614 git rev-parse --resolve-git-dir init/.git
617 test_expect_success 'do not add files from a submodule' '
619 git reset --hard &&
620 test_must_fail git add init/a
624 test_expect_success 'gracefully add/reset submodule with a trailing slash' '
626 git reset --hard &&
627 git commit -m "commit subproject" init &&
628 (cd init &&
629 echo b > a) &&
630 git add init/ &&
631 git diff --exit-code --cached init &&
632 commit=$(cd init &&
633 git commit -m update a >/dev/null &&
634 git rev-parse HEAD) &&
635 git add init/ &&
636 test_must_fail git diff --exit-code --cached init &&
637 test $commit = $(git ls-files --stage |
638 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
639 git reset init/ &&
640 git diff --exit-code --cached init
644 test_expect_success 'ls-files gracefully handles trailing slash' '
646 test "init" = "$(git ls-files init/)"
650 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
651 rm -rf init &&
652 mkdir init &&
653 git reset --hard &&
654 git checkout initial &&
655 test ! -d init &&
656 git checkout second
659 test_expect_success 'submodule <invalid-subcommand> fails' '
660 test_must_fail git submodule no-such-subcommand
663 test_expect_success 'add submodules without specifying an explicit path' '
664 mkdir repo &&
666 cd repo &&
667 git init &&
668 echo r >r &&
669 git add r &&
670 git commit -m "repo commit 1"
671 ) &&
672 git clone --bare repo/ bare.git &&
674 cd addtest &&
675 git submodule add "$submodurl/repo" &&
676 git config -f .gitmodules submodule.repo.path repo &&
677 git submodule add "$submodurl/bare.git" &&
678 git config -f .gitmodules submodule.bare.path bare
682 test_expect_success 'add should fail when path is used by a file' '
684 cd addtest &&
685 touch file &&
686 test_must_fail git submodule add "$submodurl/repo" file
690 test_expect_success 'add should fail when path is used by an existing directory' '
692 cd addtest &&
693 mkdir empty-dir &&
694 test_must_fail git submodule add "$submodurl/repo" empty-dir
698 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
700 cd addtest &&
701 git submodule add ../repo relative &&
702 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
703 git submodule sync relative &&
704 test "$(git config submodule.relative.url)" = "$submodurl/repo"
708 test_expect_success 'set up for relative path tests' '
709 mkdir reltest &&
711 cd reltest &&
712 git init &&
713 mkdir sub &&
715 cd sub &&
716 git init &&
717 test_commit foo
718 ) &&
719 git add sub &&
720 git config -f .gitmodules submodule.sub.path sub &&
721 git config -f .gitmodules submodule.sub.url ../subrepo &&
722 cp .git/config pristine-.git-config &&
723 cp .gitmodules pristine-.gitmodules
727 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
729 cd reltest &&
730 cp pristine-.git-config .git/config &&
731 cp pristine-.gitmodules .gitmodules &&
732 git config remote.origin.url ssh://hostname/repo &&
733 git submodule init &&
734 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
738 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
740 cd reltest &&
741 cp pristine-.git-config .git/config &&
742 cp pristine-.gitmodules .gitmodules &&
743 git config remote.origin.url ssh://hostname:22/repo &&
744 git submodule init &&
745 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
749 # About the choice of the path in the next test:
750 # - double-slash side-steps path mangling issues on Windows
751 # - it is still an absolute local path
752 # - there cannot be a server with a blank in its name just in case the
753 # path is used erroneously to access a //server/share style path
754 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
756 cd reltest &&
757 cp pristine-.git-config .git/config &&
758 cp pristine-.gitmodules .gitmodules &&
759 git config remote.origin.url "//somewhere else/repo" &&
760 git submodule init &&
761 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
765 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
767 cd reltest &&
768 cp pristine-.git-config .git/config &&
769 cp pristine-.gitmodules .gitmodules &&
770 git config remote.origin.url file:///tmp/repo &&
771 git submodule init &&
772 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
776 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
778 cd reltest &&
779 cp pristine-.git-config .git/config &&
780 cp pristine-.gitmodules .gitmodules &&
781 git config remote.origin.url helper:://hostname/repo &&
782 git submodule init &&
783 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
787 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
789 cd reltest &&
790 cp pristine-.git-config .git/config &&
791 git config remote.origin.url user@host:repo &&
792 git submodule init &&
793 test "$(git config submodule.sub.url)" = user@host:subrepo
797 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
799 cd reltest &&
800 cp pristine-.git-config .git/config &&
801 cp pristine-.gitmodules .gitmodules &&
802 git config remote.origin.url user@host:path/to/repo &&
803 git submodule init &&
804 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
808 test_expect_success '../subrepo works with relative local path - foo' '
810 cd reltest &&
811 cp pristine-.git-config .git/config &&
812 cp pristine-.gitmodules .gitmodules &&
813 git config remote.origin.url foo &&
814 # actual: fails with an error
815 git submodule init &&
816 test "$(git config submodule.sub.url)" = subrepo
820 test_expect_success '../subrepo works with relative local path - foo/bar' '
822 cd reltest &&
823 cp pristine-.git-config .git/config &&
824 cp pristine-.gitmodules .gitmodules &&
825 git config remote.origin.url foo/bar &&
826 git submodule init &&
827 test "$(git config submodule.sub.url)" = foo/subrepo
831 test_expect_success '../subrepo works with relative local path - ./foo' '
833 cd reltest &&
834 cp pristine-.git-config .git/config &&
835 cp pristine-.gitmodules .gitmodules &&
836 git config remote.origin.url ./foo &&
837 git submodule init &&
838 test "$(git config submodule.sub.url)" = subrepo
842 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
844 cd reltest &&
845 cp pristine-.git-config .git/config &&
846 cp pristine-.gitmodules .gitmodules &&
847 git config remote.origin.url ./foo/bar &&
848 git submodule init &&
849 test "$(git config submodule.sub.url)" = foo/subrepo
853 test_expect_success '../subrepo works with relative local path - ../foo' '
855 cd reltest &&
856 cp pristine-.git-config .git/config &&
857 cp pristine-.gitmodules .gitmodules &&
858 git config remote.origin.url ../foo &&
859 git submodule init &&
860 test "$(git config submodule.sub.url)" = ../subrepo
864 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
866 cd reltest &&
867 cp pristine-.git-config .git/config &&
868 cp pristine-.gitmodules .gitmodules &&
869 git config remote.origin.url ../foo/bar &&
870 git submodule init &&
871 test "$(git config submodule.sub.url)" = ../foo/subrepo
875 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
877 cd reltest &&
878 cp pristine-.git-config .git/config &&
879 cp pristine-.gitmodules .gitmodules &&
880 mkdir -p a/b/c &&
881 (cd a/b/c && git init && test_commit msg) &&
882 git config remote.origin.url ../foo/bar.git &&
883 git submodule add ../bar/a/b/c ./a/b/c &&
884 git submodule init &&
885 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
889 test_expect_success 'moving the superproject does not break submodules' '
891 cd addtest &&
892 git submodule status >expect
893 ) &&
894 mv addtest addtest2 &&
896 cd addtest2 &&
897 git submodule status >actual &&
898 test_cmp expect actual
902 test_expect_success 'moving the submodule does not break the superproject' '
904 cd addtest2 &&
905 git submodule status
906 ) >actual &&
907 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
908 mv addtest2/repo addtest2/repo.bak &&
909 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
911 cd addtest2 &&
912 git submodule status
913 ) >actual &&
914 test_cmp expect actual
917 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
919 cd addtest2 &&
921 cd repo &&
922 echo "$submodurl/repo" >expect &&
923 git config remote.origin.url >actual &&
924 test_cmp expect actual &&
925 echo "gitdir: ../.git/modules/repo" >expect &&
926 test_cmp expect .git
927 ) &&
928 rm -rf repo &&
929 git rm repo &&
930 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
931 test_must_be_empty actual &&
932 echo "gitdir: ../.git/modules/submod" >expect &&
933 test_cmp expect submod/.git &&
935 cd repo &&
936 echo "$submodurl/bare.git" >expect &&
937 git config remote.origin.url >actual &&
938 test_cmp expect actual &&
939 echo "gitdir: ../.git/modules/repo_new" >expect &&
940 test_cmp expect .git
941 ) &&
942 echo "repo" >expect &&
943 test_must_fail git config -f .gitmodules submodule.repo.path &&
944 git config -f .gitmodules submodule.repo_new.path >actual &&
945 test_cmp expect actual &&
946 echo "$submodurl/repo" >expect &&
947 test_must_fail git config -f .gitmodules submodule.repo.url &&
948 echo "$submodurl/bare.git" >expect &&
949 git config -f .gitmodules submodule.repo_new.url >actual &&
950 test_cmp expect actual &&
951 echo "$submodurl/repo" >expect &&
952 git config submodule.repo.url >actual &&
953 test_cmp expect actual &&
954 echo "$submodurl/bare.git" >expect &&
955 git config submodule.repo_new.url >actual &&
956 test_cmp expect actual
960 test_expect_success 'recursive relative submodules stay relative' '
961 test_when_finished "rm -rf super clone2 subsub sub3" &&
962 mkdir subsub &&
964 cd subsub &&
965 git init &&
966 >t &&
967 git add t &&
968 git commit -m "initial commit"
969 ) &&
970 mkdir sub3 &&
972 cd sub3 &&
973 git init &&
974 >t &&
975 git add t &&
976 git commit -m "initial commit" &&
977 git submodule add ../subsub dirdir/subsub &&
978 git commit -m "add submodule subsub"
979 ) &&
980 mkdir super &&
982 cd super &&
983 git init &&
984 >t &&
985 git add t &&
986 git commit -m "initial commit" &&
987 git submodule add ../sub3 &&
988 git commit -m "add submodule sub"
989 ) &&
990 git clone super clone2 &&
992 cd clone2 &&
993 git submodule update --init --recursive &&
994 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
995 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
996 ) &&
997 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
998 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
1001 test_expect_success 'submodule add with an existing name fails unless forced' '
1003 cd addtest2 &&
1004 rm -rf repo &&
1005 git rm repo &&
1006 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
1007 test ! -d repo &&
1008 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
1009 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
1010 echo "$submodurl/bare.git" >expect &&
1011 git config submodule.repo_new.url >actual &&
1012 test_cmp expect actual &&
1013 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
1014 test -d repo &&
1015 echo "repo" >expect &&
1016 git config -f .gitmodules submodule.repo_new.path >actual &&
1017 test_cmp expect actual &&
1018 echo "$submodurl/repo.git" >expect &&
1019 git config -f .gitmodules submodule.repo_new.url >actual &&
1020 test_cmp expect actual &&
1021 echo "$submodurl/repo.git" >expect &&
1022 git config submodule.repo_new.url >actual &&
1023 test_cmp expect actual
1027 test_expect_success 'set up a second submodule' '
1028 git submodule add ./init2 example2 &&
1029 git commit -m "submodule example2 added"
1032 test_expect_success 'submodule deinit works on repository without submodules' '
1033 test_when_finished "rm -rf newdirectory" &&
1034 mkdir newdirectory &&
1036 cd newdirectory &&
1037 git init &&
1038 >file &&
1039 git add file &&
1040 git commit -m "repo should not be empty" &&
1041 git submodule deinit . &&
1042 git submodule deinit --all
1046 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
1047 git config submodule.example.foo bar &&
1048 git config submodule.example2.frotz nitfol &&
1049 git submodule deinit init &&
1050 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1051 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1052 test -f example2/.git &&
1053 rmdir init
1056 test_expect_success 'submodule deinit should unset core.worktree' '
1057 test_path_is_file .git/modules/example/config &&
1058 test_must_fail git config -f .git/modules/example/config core.worktree
1061 test_expect_success 'submodule deinit from subdirectory' '
1062 git submodule update --init &&
1063 git config submodule.example.foo bar &&
1064 mkdir -p sub &&
1066 cd sub &&
1067 git submodule deinit ../init >../output
1068 ) &&
1069 test_i18ngrep "\\.\\./init" output &&
1070 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1071 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1072 test -f example2/.git &&
1073 rmdir init
1076 test_expect_success 'submodule deinit . deinits all initialized submodules' '
1077 git submodule update --init &&
1078 git config submodule.example.foo bar &&
1079 git config submodule.example2.frotz nitfol &&
1080 test_must_fail git submodule deinit &&
1081 git submodule deinit . >actual &&
1082 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1083 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1084 test_i18ngrep "Cleared directory .init" actual &&
1085 test_i18ngrep "Cleared directory .example2" actual &&
1086 rmdir init example2
1089 test_expect_success 'submodule deinit --all deinits all initialized submodules' '
1090 git submodule update --init &&
1091 git config submodule.example.foo bar &&
1092 git config submodule.example2.frotz nitfol &&
1093 test_must_fail git submodule deinit &&
1094 git submodule deinit --all >actual &&
1095 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1096 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1097 test_i18ngrep "Cleared directory .init" actual &&
1098 test_i18ngrep "Cleared directory .example2" actual &&
1099 rmdir init example2
1102 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
1103 git submodule update --init &&
1104 rm -rf init example2/* example2/.git &&
1105 git submodule deinit init example2 >actual &&
1106 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1107 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1108 test_i18ngrep ! "Cleared directory .init" actual &&
1109 test_i18ngrep "Cleared directory .example2" actual &&
1110 rmdir init
1113 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
1114 git submodule update --init &&
1115 echo X >>init/s &&
1116 test_must_fail git submodule deinit init &&
1117 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1118 test -f example2/.git &&
1119 git submodule deinit -f init >actual &&
1120 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1121 test_i18ngrep "Cleared directory .init" actual &&
1122 rmdir init
1125 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1126 git submodule update --init &&
1127 echo X >>init/untracked &&
1128 test_must_fail git submodule deinit init &&
1129 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1130 test -f example2/.git &&
1131 git submodule deinit -f init >actual &&
1132 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1133 test_i18ngrep "Cleared directory .init" actual &&
1134 rmdir init
1137 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1138 git submodule update --init &&
1140 cd init &&
1141 git checkout HEAD^
1142 ) &&
1143 test_must_fail git submodule deinit init &&
1144 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1145 test -f example2/.git &&
1146 git submodule deinit -f init >actual &&
1147 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1148 test_i18ngrep "Cleared directory .init" actual &&
1149 rmdir init
1152 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1153 git submodule update --init &&
1154 git submodule deinit init >actual &&
1155 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1156 test_i18ngrep "Cleared directory .init" actual &&
1157 git submodule deinit init >actual &&
1158 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1159 test_i18ngrep "Cleared directory .init" actual &&
1160 git submodule deinit . >actual &&
1161 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1162 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1163 test_i18ngrep "Cleared directory .init" actual &&
1164 git submodule deinit . >actual &&
1165 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1166 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1167 test_i18ngrep "Cleared directory .init" actual &&
1168 git submodule deinit --all >actual &&
1169 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1170 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1171 test_i18ngrep "Cleared directory .init" actual &&
1172 rmdir init example2
1175 test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
1176 git submodule update --init &&
1178 cd init &&
1179 rm .git &&
1180 cp -R ../.git/modules/example .git &&
1181 GIT_WORK_TREE=. git config --unset core.worktree
1182 ) &&
1183 test_must_fail git submodule deinit init &&
1184 test_must_fail git submodule deinit -f init &&
1185 test -d init/.git &&
1186 test -n "$(git config --get-regexp "submodule\.example\.")"
1189 test_expect_success 'submodule with UTF-8 name' '
1190 svname=$(printf "\303\245 \303\244\303\266") &&
1191 mkdir "$svname" &&
1193 cd "$svname" &&
1194 git init &&
1195 >sub &&
1196 git add sub &&
1197 git commit -m "init sub"
1198 ) &&
1199 git submodule add ./"$svname" &&
1200 git submodule >&2 &&
1201 test -n "$(git submodule | grep "$svname")"
1204 test_expect_success 'submodule add clone shallow submodule' '
1205 mkdir super &&
1206 pwd=$(pwd) &&
1208 cd super &&
1209 git init &&
1210 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1212 cd submodule &&
1213 test 1 = $(git log --oneline | wc -l)
1218 test_expect_success 'submodule helper list is not confused by common prefixes' '
1219 mkdir -p dir1/b &&
1221 cd dir1/b &&
1222 git init &&
1223 echo hi >testfile2 &&
1224 git add . &&
1225 git commit -m "test1"
1226 ) &&
1227 mkdir -p dir2/b &&
1229 cd dir2/b &&
1230 git init &&
1231 echo hello >testfile1 &&
1232 git add . &&
1233 git commit -m "test2"
1234 ) &&
1235 git submodule add /dir1/b dir1/b &&
1236 git submodule add /dir2/b dir2/b &&
1237 git commit -m "first submodule commit" &&
1238 git submodule--helper list dir1/b | cut -f 2 >actual &&
1239 echo "dir1/b" >expect &&
1240 test_cmp expect actual
1243 test_expect_success 'setup superproject with submodules' '
1244 git init sub1 &&
1245 test_commit -C sub1 test &&
1246 test_commit -C sub1 test2 &&
1247 git init multisuper &&
1248 git -C multisuper submodule add ../sub1 sub0 &&
1249 git -C multisuper submodule add ../sub1 sub1 &&
1250 git -C multisuper submodule add ../sub1 sub2 &&
1251 git -C multisuper submodule add ../sub1 sub3 &&
1252 git -C multisuper commit -m "add some submodules"
1255 cat >expect <<-EOF
1256 -sub0
1257 sub1 (test2)
1258 sub2 (test2)
1259 sub3 (test2)
1262 test_expect_success 'submodule update --init with a specification' '
1263 test_when_finished "rm -rf multisuper_clone" &&
1264 pwd=$(pwd) &&
1265 git clone file://"$pwd"/multisuper multisuper_clone &&
1266 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1267 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1268 test_cmp expect actual
1271 test_expect_success 'submodule update --init with submodule.active set' '
1272 test_when_finished "rm -rf multisuper_clone" &&
1273 pwd=$(pwd) &&
1274 git clone file://"$pwd"/multisuper multisuper_clone &&
1275 git -C multisuper_clone config submodule.active "." &&
1276 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1277 git -C multisuper_clone submodule update --init &&
1278 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1279 test_cmp expect actual
1282 test_expect_success 'submodule update and setting submodule.<name>.active' '
1283 test_when_finished "rm -rf multisuper_clone" &&
1284 pwd=$(pwd) &&
1285 git clone file://"$pwd"/multisuper multisuper_clone &&
1286 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1287 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1288 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1290 cat >expect <<-\EOF &&
1291 sub0 (test2)
1292 -sub1
1293 sub2 (test2)
1294 -sub3
1296 git -C multisuper_clone submodule update &&
1297 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1298 test_cmp expect actual
1301 test_expect_success 'clone active submodule without submodule url set' '
1302 test_when_finished "rm -rf test/test" &&
1303 mkdir test &&
1304 # another dir breaks accidental relative paths still being correct
1305 git clone file://"$pwd"/multisuper test/test &&
1307 cd test/test &&
1308 git config submodule.active "." &&
1310 # do not pass --init flag, as the submodule is already active:
1311 git submodule update &&
1312 git submodule status >actual_raw &&
1314 cut -d" " -f3- actual_raw >actual &&
1315 cat >expect <<-\EOF &&
1316 sub0 (test2)
1317 sub1 (test2)
1318 sub2 (test2)
1319 sub3 (test2)
1321 test_cmp expect actual
1325 test_expect_success 'clone --recurse-submodules with a pathspec works' '
1326 test_when_finished "rm -rf multisuper_clone" &&
1327 cat >expected <<-\EOF &&
1328 sub0 (test2)
1329 -sub1
1330 -sub2
1331 -sub3
1334 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1335 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1336 test_cmp expected actual
1339 test_expect_success 'clone with multiple --recurse-submodules options' '
1340 test_when_finished "rm -rf multisuper_clone" &&
1341 cat >expect <<-\EOF &&
1342 -sub0
1343 sub1 (test2)
1344 -sub2
1345 sub3 (test2)
1348 git clone --recurse-submodules="." \
1349 --recurse-submodules=":(exclude)sub0" \
1350 --recurse-submodules=":(exclude)sub2" \
1351 multisuper multisuper_clone &&
1352 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1353 test_cmp expect actual
1356 test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
1357 test_when_finished "rm -rf multisuper_clone" &&
1358 cat <<-\EOF >expect &&
1359 -sub0
1360 sub1 (test2)
1361 -sub2
1362 sub3 (test2)
1365 cat <<-\EOF >expect2 &&
1366 -sub0
1367 sub1 (test2)
1368 -sub2
1369 sub3 (test2)
1370 -sub4
1371 sub5 (test2)
1374 git clone --recurse-submodules="." \
1375 --recurse-submodules=":(exclude)sub0" \
1376 --recurse-submodules=":(exclude)sub2" \
1377 --recurse-submodules=":(exclude)sub4" \
1378 multisuper multisuper_clone &&
1380 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1381 test_cmp expect actual &&
1383 git -C multisuper submodule add ../sub1 sub4 &&
1384 git -C multisuper submodule add ../sub1 sub5 &&
1385 git -C multisuper commit -m "add more submodules" &&
1386 # obtain the new superproject
1387 git -C multisuper_clone pull &&
1388 git -C multisuper_clone submodule update --init &&
1389 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1390 test_cmp expect2 actual
1393 test_expect_success 'init properly sets the config' '
1394 test_when_finished "rm -rf multisuper_clone" &&
1395 git clone --recurse-submodules="." \
1396 --recurse-submodules=":(exclude)sub0" \
1397 multisuper multisuper_clone &&
1399 git -C multisuper_clone submodule init -- sub0 sub1 &&
1400 git -C multisuper_clone config --get submodule.sub0.active &&
1401 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1404 test_expect_success 'recursive clone respects -q' '
1405 test_when_finished "rm -rf multisuper_clone" &&
1406 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1407 test_must_be_empty actual
1410 test_done