]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7400-submodule-basic.sh
Sync with 2.36.3
[thirdparty/git.git] / t / t7400-submodule-basic.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Lars Hjemli
4 #
5
6 test_description='Basic porcelain support for submodules
7
8 This test tries to verify basic sanity of the init, update and status
9 subcommands of git submodule.
10 '
11
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14
15 . ./test-lib.sh
16
17 test_expect_success 'setup - enable local submodules' '
18 git config --global protocol.file.allow always
19 '
20
21 test_expect_success 'submodule deinit works on empty repository' '
22 git submodule deinit --all
23 '
24
25 test_expect_success 'setup - initial commit' '
26 >t &&
27 git add t &&
28 git commit -m "initial commit" &&
29 git branch initial
30 '
31
32 test_expect_success 'submodule init aborts on missing .gitmodules file' '
33 test_when_finished "git update-index --remove sub" &&
34 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
35 # missing the .gitmodules file here
36 test_must_fail git submodule init 2>actual &&
37 test_i18ngrep "No url found for submodule path" actual
38 '
39
40 test_expect_success 'submodule update aborts on missing .gitmodules file' '
41 test_when_finished "git update-index --remove sub" &&
42 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
43 # missing the .gitmodules file here
44 git submodule update sub 2>actual &&
45 test_i18ngrep "Submodule path .sub. not initialized" actual
46 '
47
48 test_expect_success 'submodule update aborts on missing gitmodules url' '
49 test_when_finished "git update-index --remove sub" &&
50 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
51 test_when_finished "rm -f .gitmodules" &&
52 git config -f .gitmodules submodule.s.path sub &&
53 test_must_fail git submodule init
54 '
55
56 test_expect_success 'add aborts on repository with no commits' '
57 cat >expect <<-\EOF &&
58 fatal: '"'repo-no-commits'"' does not have a commit checked out
59 EOF
60 git init repo-no-commits &&
61 test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
62 test_cmp expect actual
63 '
64
65 test_expect_success 'status should ignore inner git repo when not added' '
66 rm -fr inner &&
67 mkdir inner &&
68 (
69 cd inner &&
70 git init &&
71 >t &&
72 git add t &&
73 git commit -m "initial"
74 ) &&
75 test_must_fail git submodule status inner 2>output.err &&
76 rm -fr inner &&
77 test_i18ngrep "^error: .*did not match any file(s) known to git" output.err
78 '
79
80 test_expect_success 'setup - repository in init subdirectory' '
81 mkdir init &&
82 (
83 cd init &&
84 git init &&
85 echo a >a &&
86 git add a &&
87 git commit -m "submodule commit 1" &&
88 git tag -a -m "rev-1" rev-1
89 )
90 '
91
92 test_expect_success 'setup - commit with gitlink' '
93 echo a >a &&
94 echo z >z &&
95 git add a init z &&
96 git commit -m "super commit 1"
97 '
98
99 test_expect_success 'setup - hide init subdirectory' '
100 mv init .subrepo
101 '
102
103 test_expect_success 'setup - repository to add submodules to' '
104 git init addtest &&
105 git init addtest-ignore
106 '
107
108 # The 'submodule add' tests need some repository to add as a submodule.
109 # The trash directory is a good one as any. We need to canonicalize
110 # the name, though, as some tests compare it to the absolute path git
111 # generates, which will expand symbolic links.
112 submodurl=$(pwd -P)
113
114 listbranches() {
115 git for-each-ref --format='%(refname)' 'refs/heads/*'
116 }
117
118 inspect() {
119 dir=$1 &&
120 dotdot="${2:-..}" &&
121
122 (
123 cd "$dir" &&
124 listbranches >"$dotdot/heads" &&
125 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
126 git rev-parse HEAD >"$dotdot/head-sha1" &&
127 git update-index --refresh &&
128 git diff-files --exit-code &&
129 git clean -n -d -x >"$dotdot/untracked"
130 )
131 }
132
133 test_expect_success 'submodule add' '
134 echo "refs/heads/main" >expect &&
135
136 (
137 cd addtest &&
138 git submodule add -q "$submodurl" submod >actual &&
139 test_must_be_empty actual &&
140 echo "gitdir: ../.git/modules/submod" >expect &&
141 test_cmp expect submod/.git &&
142 (
143 cd submod &&
144 git config core.worktree >actual &&
145 echo "../../../submod" >expect &&
146 test_cmp expect actual &&
147 rm -f actual expect
148 ) &&
149 git submodule init
150 ) &&
151
152 rm -f heads head untracked &&
153 inspect addtest/submod ../.. &&
154 test_cmp expect heads &&
155 test_cmp expect head &&
156 test_must_be_empty untracked
157 '
158
159 test_expect_success 'setup parent and one repository' '
160 test_create_repo parent &&
161 test_commit -C parent one
162 '
163
164 test_expect_success 'redirected submodule add does not show progress' '
165 git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
166 2>err &&
167 ! grep % err &&
168 test_i18ngrep ! "Checking connectivity" err
169 '
170
171 test_expect_success 'redirected submodule add --progress does show progress' '
172 git -C addtest submodule add --progress "file://$submodurl/parent" \
173 submod-redirected-progress 2>err && \
174 grep % err
175 '
176
177 test_expect_success 'submodule add to .gitignored path fails' '
178 (
179 cd addtest-ignore &&
180 cat <<-\EOF >expect &&
181 The following paths are ignored by one of your .gitignore files:
182 submod
183 hint: Use -f if you really want to add them.
184 hint: Turn this message off by running
185 hint: "git config advice.addIgnoredFile false"
186 EOF
187 # Does not use test_commit due to the ignore
188 echo "*" > .gitignore &&
189 git add --force .gitignore &&
190 git commit -m"Ignore everything" &&
191 ! git submodule add "$submodurl" submod >actual 2>&1 &&
192 test_cmp expect actual
193 )
194 '
195
196 test_expect_success 'submodule add to .gitignored path with --force' '
197 (
198 cd addtest-ignore &&
199 git submodule add --force "$submodurl" submod
200 )
201 '
202
203 test_expect_success 'submodule add to path with tracked content fails' '
204 (
205 cd addtest &&
206 echo "fatal: '\''dir-tracked'\'' already exists in the index" >expect &&
207 mkdir dir-tracked &&
208 test_commit foo dir-tracked/bar &&
209 test_must_fail git submodule add "$submodurl" dir-tracked >actual 2>&1 &&
210 test_cmp expect actual
211 )
212 '
213
214 test_expect_success 'submodule add to reconfigure existing submodule with --force' '
215 (
216 cd addtest-ignore &&
217 bogus_url="$(pwd)/bogus-url" &&
218 git submodule add --force "$bogus_url" submod &&
219 git submodule add --force -b initial "$submodurl" submod-branch &&
220 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
221 test "$bogus_url" = "$(git config submodule.submod.url)" &&
222 # Restore the url
223 git submodule add --force "$submodurl" submod &&
224 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
225 test "$submodurl" = "$(git config submodule.submod.url)"
226 )
227 '
228
229 test_expect_success 'submodule add relays add --dry-run stderr' '
230 test_when_finished "rm -rf addtest/.git/index.lock" &&
231 (
232 cd addtest &&
233 : >.git/index.lock &&
234 ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
235 test_i18ngrep "^fatal: .*index\.lock" output.err &&
236 test_path_is_missing sub-while-locked
237 )
238 '
239
240 test_expect_success 'submodule add --branch' '
241 echo "refs/heads/initial" >expect-head &&
242 cat <<-\EOF >expect-heads &&
243 refs/heads/initial
244 refs/heads/main
245 EOF
246
247 (
248 cd addtest &&
249 git submodule add -b initial "$submodurl" submod-branch &&
250 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
251 git submodule init
252 ) &&
253
254 rm -f heads head untracked &&
255 inspect addtest/submod-branch ../.. &&
256 test_cmp expect-heads heads &&
257 test_cmp expect-head head &&
258 test_must_be_empty untracked
259 '
260
261 test_expect_success 'submodule add with ./ in path' '
262 echo "refs/heads/main" >expect &&
263
264 (
265 cd addtest &&
266 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
267 git submodule init
268 ) &&
269
270 rm -f heads head untracked &&
271 inspect addtest/dotsubmod/frotz ../../.. &&
272 test_cmp expect heads &&
273 test_cmp expect head &&
274 test_must_be_empty untracked
275 '
276
277 test_expect_success 'submodule add with /././ in path' '
278 echo "refs/heads/main" >expect &&
279
280 (
281 cd addtest &&
282 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
283 git submodule init
284 ) &&
285
286 rm -f heads head untracked &&
287 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
288 test_cmp expect heads &&
289 test_cmp expect head &&
290 test_must_be_empty untracked
291 '
292
293 test_expect_success 'submodule add with // in path' '
294 echo "refs/heads/main" >expect &&
295
296 (
297 cd addtest &&
298 git submodule add "$submodurl" slashslashsubmod///frotz// &&
299 git submodule init
300 ) &&
301
302 rm -f heads head untracked &&
303 inspect addtest/slashslashsubmod/frotz ../../.. &&
304 test_cmp expect heads &&
305 test_cmp expect head &&
306 test_must_be_empty untracked
307 '
308
309 test_expect_success 'submodule add with /.. in path' '
310 echo "refs/heads/main" >expect &&
311
312 (
313 cd addtest &&
314 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
315 git submodule init
316 ) &&
317
318 rm -f heads head untracked &&
319 inspect addtest/realsubmod ../.. &&
320 test_cmp expect heads &&
321 test_cmp expect head &&
322 test_must_be_empty untracked
323 '
324
325 test_expect_success 'submodule add with ./, /.. and // in path' '
326 echo "refs/heads/main" >expect &&
327
328 (
329 cd addtest &&
330 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
331 git submodule init
332 ) &&
333
334 rm -f heads head untracked &&
335 inspect addtest/realsubmod2 ../.. &&
336 test_cmp expect heads &&
337 test_cmp expect head &&
338 test_must_be_empty untracked
339 '
340
341 test_expect_success !CYGWIN 'submodule add with \\ in path' '
342 test_when_finished "rm -rf parent sub\\with\\backslash" &&
343
344 # Initialize a repo with a backslash in its name
345 git init sub\\with\\backslash &&
346 touch sub\\with\\backslash/empty.file &&
347 git -C sub\\with\\backslash add empty.file &&
348 git -C sub\\with\\backslash commit -m "Added empty.file" &&
349
350 # Add that repository as a submodule
351 git init parent &&
352 git -C parent submodule add ../sub\\with\\backslash
353 '
354
355 test_expect_success 'submodule add in subdirectory' '
356 echo "refs/heads/main" >expect &&
357
358 mkdir addtest/sub &&
359 (
360 cd addtest/sub &&
361 git submodule add "$submodurl" ../realsubmod3 &&
362 git submodule init
363 ) &&
364
365 rm -f heads head untracked &&
366 inspect addtest/realsubmod3 ../.. &&
367 test_cmp expect heads &&
368 test_cmp expect head &&
369 test_must_be_empty untracked
370 '
371
372 test_expect_success 'submodule add in subdirectory with relative path should fail' '
373 (
374 cd addtest/sub &&
375 test_must_fail git submodule add ../../ submod3 2>../../output.err
376 ) &&
377 test_i18ngrep toplevel output.err
378 '
379
380 test_expect_success 'setup - add an example entry to .gitmodules' '
381 git config --file=.gitmodules submodule.example.url git://example.com/init.git
382 '
383
384 test_expect_success 'status should fail for unmapped paths' '
385 test_must_fail git submodule status
386 '
387
388 test_expect_success 'setup - map path in .gitmodules' '
389 cat <<\EOF >expect &&
390 [submodule "example"]
391 url = git://example.com/init.git
392 path = init
393 EOF
394
395 git config --file=.gitmodules submodule.example.path init &&
396
397 test_cmp expect .gitmodules
398 '
399
400 test_expect_success 'status should only print one line' '
401 git submodule status >lines &&
402 test_line_count = 1 lines
403 '
404
405 test_expect_success 'status from subdirectory should have the same SHA1' '
406 test_when_finished "rmdir addtest/subdir" &&
407 (
408 cd addtest &&
409 mkdir subdir &&
410 git submodule status >output &&
411 awk "{print \$1}" <output >expect &&
412 cd subdir &&
413 git submodule status >../output &&
414 awk "{print \$1}" <../output >../actual &&
415 test_cmp ../expect ../actual &&
416 git -C ../submod checkout HEAD^ &&
417 git submodule status >../output &&
418 awk "{print \$1}" <../output >../actual2 &&
419 cd .. &&
420 git submodule status >output &&
421 awk "{print \$1}" <output >expect2 &&
422 test_cmp expect2 actual2 &&
423 ! test_cmp actual actual2
424 )
425 '
426
427 test_expect_success 'setup - fetch commit name from submodule' '
428 rev1=$(cd .subrepo && git rev-parse HEAD) &&
429 printf "rev1: %s\n" "$rev1" &&
430 test -n "$rev1"
431 '
432
433 test_expect_success 'status should initially be "missing"' '
434 git submodule status >lines &&
435 grep "^-$rev1" lines
436 '
437
438 test_expect_success 'init should register submodule url in .git/config' '
439 echo git://example.com/init.git >expect &&
440
441 git submodule init &&
442 git config submodule.example.url >url &&
443 git config submodule.example.url ./.subrepo &&
444
445 test_cmp expect url
446 '
447
448 test_expect_success 'status should still be "missing" after initializing' '
449 rm -fr init &&
450 mkdir init &&
451 git submodule status >lines &&
452 rm -fr init &&
453 grep "^-$rev1" lines
454 '
455
456 test_failure_with_unknown_submodule () {
457 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
458 test_i18ngrep "^error: .*no-such-submodule" output.err
459 }
460
461 test_expect_success 'init should fail with unknown submodule' '
462 test_failure_with_unknown_submodule init
463 '
464
465 test_expect_success 'update should fail with unknown submodule' '
466 test_failure_with_unknown_submodule update
467 '
468
469 test_expect_success 'status should fail with unknown submodule' '
470 test_failure_with_unknown_submodule status
471 '
472
473 test_expect_success 'sync should fail with unknown submodule' '
474 test_failure_with_unknown_submodule sync
475 '
476
477 test_expect_success 'update should fail when path is used by a file' '
478 echo hello >expect &&
479
480 echo "hello" >init &&
481 test_must_fail git submodule update &&
482
483 test_cmp expect init
484 '
485
486 test_expect_success 'update should fail when path is used by a nonempty directory' '
487 echo hello >expect &&
488
489 rm -fr init &&
490 mkdir init &&
491 echo "hello" >init/a &&
492
493 test_must_fail git submodule update &&
494
495 test_cmp expect init/a
496 '
497
498 test_expect_success 'update should work when path is an empty dir' '
499 rm -fr init &&
500 rm -f head-sha1 &&
501 echo "$rev1" >expect &&
502
503 mkdir init &&
504 git submodule update -q >update.out &&
505 test_must_be_empty update.out &&
506
507 inspect init &&
508 test_cmp expect head-sha1
509 '
510
511 test_expect_success 'status should be "up-to-date" after update' '
512 git submodule status >list &&
513 grep "^ $rev1" list
514 '
515
516 test_expect_success 'status "up-to-date" from subdirectory' '
517 mkdir -p sub &&
518 (
519 cd sub &&
520 git submodule status >../list
521 ) &&
522 grep "^ $rev1" list &&
523 grep "\\.\\./init" list
524 '
525
526 test_expect_success 'status "up-to-date" from subdirectory with path' '
527 mkdir -p sub &&
528 (
529 cd sub &&
530 git submodule status ../init >../list
531 ) &&
532 grep "^ $rev1" list &&
533 grep "\\.\\./init" list
534 '
535
536 test_expect_success 'status should be "modified" after submodule commit' '
537 (
538 cd init &&
539 echo b >b &&
540 git add b &&
541 git commit -m "submodule commit 2"
542 ) &&
543
544 rev2=$(cd init && git rev-parse HEAD) &&
545 test -n "$rev2" &&
546 git submodule status >list &&
547
548 grep "^+$rev2" list
549 '
550
551 test_expect_success 'the --cached sha1 should be rev1' '
552 git submodule --cached status >list &&
553 grep "^+$rev1" list
554 '
555
556 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
557 git diff >diff &&
558 grep "^+Subproject commit $rev2" diff
559 '
560
561 test_expect_success 'update should checkout rev1' '
562 rm -f head-sha1 &&
563 echo "$rev1" >expect &&
564
565 git submodule update init &&
566 inspect init &&
567
568 test_cmp expect head-sha1
569 '
570
571 test_expect_success 'status should be "up-to-date" after update' '
572 git submodule status >list &&
573 grep "^ $rev1" list
574 '
575
576 test_expect_success 'checkout superproject with subproject already present' '
577 git checkout initial &&
578 git checkout main
579 '
580
581 test_expect_success 'apply submodule diff' '
582 git branch second &&
583 (
584 cd init &&
585 echo s >s &&
586 git add s &&
587 git commit -m "change subproject"
588 ) &&
589 git update-index --add init &&
590 git commit -m "change init" &&
591 git format-patch -1 --stdout >P.diff &&
592 git checkout second &&
593 git apply --index P.diff &&
594
595 git diff --cached main >staged &&
596 test_must_be_empty staged
597 '
598
599 test_expect_success 'update --init' '
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 &&
604
605 git submodule update init 2> update.out &&
606 test_i18ngrep "not initialized" update.out &&
607 test_must_fail git rev-parse --resolve-git-dir init/.git &&
608
609 git submodule update --init init &&
610 git rev-parse --resolve-git-dir init/.git
611 '
612
613 test_expect_success 'update --init from subdirectory' '
614 mv init init2 &&
615 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
616 git config --remove-section submodule.example &&
617 test_must_fail git config submodule.example.url &&
618
619 mkdir -p sub &&
620 (
621 cd sub &&
622 git submodule update ../init 2>update.out &&
623 test_i18ngrep "not initialized" update.out &&
624 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
625
626 git submodule update --init ../init
627 ) &&
628 git rev-parse --resolve-git-dir init/.git
629 '
630
631 test_expect_success 'do not add files from a submodule' '
632
633 git reset --hard &&
634 test_must_fail git add init/a
635
636 '
637
638 test_expect_success 'gracefully add/reset submodule with a trailing slash' '
639
640 git reset --hard &&
641 git commit -m "commit subproject" init &&
642 (cd init &&
643 echo b > a) &&
644 git add init/ &&
645 git diff --exit-code --cached init &&
646 commit=$(cd init &&
647 git commit -m update a >/dev/null &&
648 git rev-parse HEAD) &&
649 git add init/ &&
650 test_must_fail git diff --exit-code --cached init &&
651 test $commit = $(git ls-files --stage |
652 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
653 git reset init/ &&
654 git diff --exit-code --cached init
655
656 '
657
658 test_expect_success 'ls-files gracefully handles trailing slash' '
659
660 test "init" = "$(git ls-files init/)"
661
662 '
663
664 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
665 rm -rf init &&
666 mkdir init &&
667 git reset --hard &&
668 git checkout initial &&
669 test ! -d init &&
670 git checkout second
671 '
672
673 test_expect_success 'submodule <invalid-subcommand> fails' '
674 test_must_fail git submodule no-such-subcommand
675 '
676
677 test_expect_success 'add submodules without specifying an explicit path' '
678 mkdir repo &&
679 (
680 cd repo &&
681 git init &&
682 echo r >r &&
683 git add r &&
684 git commit -m "repo commit 1"
685 ) &&
686 git clone --bare repo/ bare.git &&
687 (
688 cd addtest &&
689 git submodule add "$submodurl/repo" &&
690 git config -f .gitmodules submodule.repo.path repo &&
691 git submodule add "$submodurl/bare.git" &&
692 git config -f .gitmodules submodule.bare.path bare
693 )
694 '
695
696 test_expect_success 'add should fail when path is used by a file' '
697 (
698 cd addtest &&
699 touch file &&
700 test_must_fail git submodule add "$submodurl/repo" file
701 )
702 '
703
704 test_expect_success 'add should fail when path is used by an existing directory' '
705 (
706 cd addtest &&
707 mkdir empty-dir &&
708 test_must_fail git submodule add "$submodurl/repo" empty-dir
709 )
710 '
711
712 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
713 (
714 cd addtest &&
715 git submodule add ../repo relative &&
716 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
717 git submodule sync relative &&
718 test "$(git config submodule.relative.url)" = "$submodurl/repo"
719 )
720 '
721
722 test_expect_success 'set up for relative path tests' '
723 mkdir reltest &&
724 (
725 cd reltest &&
726 git init &&
727 mkdir sub &&
728 (
729 cd sub &&
730 git init &&
731 test_commit foo
732 ) &&
733 git add sub &&
734 git config -f .gitmodules submodule.sub.path sub &&
735 git config -f .gitmodules submodule.sub.url ../subrepo &&
736 cp .git/config pristine-.git-config &&
737 cp .gitmodules pristine-.gitmodules
738 )
739 '
740
741 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
742 (
743 cd reltest &&
744 cp pristine-.git-config .git/config &&
745 cp pristine-.gitmodules .gitmodules &&
746 git config remote.origin.url ssh://hostname/repo &&
747 git submodule init &&
748 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
749 )
750 '
751
752 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
753 (
754 cd reltest &&
755 cp pristine-.git-config .git/config &&
756 cp pristine-.gitmodules .gitmodules &&
757 git config remote.origin.url ssh://hostname:22/repo &&
758 git submodule init &&
759 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
760 )
761 '
762
763 # About the choice of the path in the next test:
764 # - double-slash side-steps path mangling issues on Windows
765 # - it is still an absolute local path
766 # - there cannot be a server with a blank in its name just in case the
767 # path is used erroneously to access a //server/share style path
768 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
769 (
770 cd reltest &&
771 cp pristine-.git-config .git/config &&
772 cp pristine-.gitmodules .gitmodules &&
773 git config remote.origin.url "//somewhere else/repo" &&
774 git submodule init &&
775 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
776 )
777 '
778
779 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
780 (
781 cd reltest &&
782 cp pristine-.git-config .git/config &&
783 cp pristine-.gitmodules .gitmodules &&
784 git config remote.origin.url file:///tmp/repo &&
785 git submodule init &&
786 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
787 )
788 '
789
790 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
791 (
792 cd reltest &&
793 cp pristine-.git-config .git/config &&
794 cp pristine-.gitmodules .gitmodules &&
795 git config remote.origin.url helper:://hostname/repo &&
796 git submodule init &&
797 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
798 )
799 '
800
801 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
802 (
803 cd reltest &&
804 cp pristine-.git-config .git/config &&
805 git config remote.origin.url user@host:repo &&
806 git submodule init &&
807 test "$(git config submodule.sub.url)" = user@host:subrepo
808 )
809 '
810
811 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
812 (
813 cd reltest &&
814 cp pristine-.git-config .git/config &&
815 cp pristine-.gitmodules .gitmodules &&
816 git config remote.origin.url user@host:path/to/repo &&
817 git submodule init &&
818 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
819 )
820 '
821
822 test_expect_success '../subrepo works with relative local path - foo' '
823 (
824 cd reltest &&
825 cp pristine-.git-config .git/config &&
826 cp pristine-.gitmodules .gitmodules &&
827 git config remote.origin.url foo &&
828 # actual: fails with an error
829 git submodule init &&
830 test "$(git config submodule.sub.url)" = subrepo
831 )
832 '
833
834 test_expect_success '../subrepo works with relative local path - foo/bar' '
835 (
836 cd reltest &&
837 cp pristine-.git-config .git/config &&
838 cp pristine-.gitmodules .gitmodules &&
839 git config remote.origin.url foo/bar &&
840 git submodule init &&
841 test "$(git config submodule.sub.url)" = foo/subrepo
842 )
843 '
844
845 test_expect_success '../subrepo works with relative local path - ./foo' '
846 (
847 cd reltest &&
848 cp pristine-.git-config .git/config &&
849 cp pristine-.gitmodules .gitmodules &&
850 git config remote.origin.url ./foo &&
851 git submodule init &&
852 test "$(git config submodule.sub.url)" = subrepo
853 )
854 '
855
856 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
857 (
858 cd reltest &&
859 cp pristine-.git-config .git/config &&
860 cp pristine-.gitmodules .gitmodules &&
861 git config remote.origin.url ./foo/bar &&
862 git submodule init &&
863 test "$(git config submodule.sub.url)" = foo/subrepo
864 )
865 '
866
867 test_expect_success '../subrepo works with relative local path - ../foo' '
868 (
869 cd reltest &&
870 cp pristine-.git-config .git/config &&
871 cp pristine-.gitmodules .gitmodules &&
872 git config remote.origin.url ../foo &&
873 git submodule init &&
874 test "$(git config submodule.sub.url)" = ../subrepo
875 )
876 '
877
878 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
879 (
880 cd reltest &&
881 cp pristine-.git-config .git/config &&
882 cp pristine-.gitmodules .gitmodules &&
883 git config remote.origin.url ../foo/bar &&
884 git submodule init &&
885 test "$(git config submodule.sub.url)" = ../foo/subrepo
886 )
887 '
888
889 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
890 (
891 cd reltest &&
892 cp pristine-.git-config .git/config &&
893 cp pristine-.gitmodules .gitmodules &&
894 mkdir -p a/b/c &&
895 (cd a/b/c && git init && test_commit msg) &&
896 git config remote.origin.url ../foo/bar.git &&
897 git submodule add ../bar/a/b/c ./a/b/c &&
898 git submodule init &&
899 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
900 )
901 '
902
903 test_expect_success 'moving the superproject does not break submodules' '
904 (
905 cd addtest &&
906 git submodule status >expect
907 ) &&
908 mv addtest addtest2 &&
909 (
910 cd addtest2 &&
911 git submodule status >actual &&
912 test_cmp expect actual
913 )
914 '
915
916 test_expect_success 'moving the submodule does not break the superproject' '
917 (
918 cd addtest2 &&
919 git submodule status
920 ) >actual &&
921 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
922 mv addtest2/repo addtest2/repo.bak &&
923 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
924 (
925 cd addtest2 &&
926 git submodule status
927 ) >actual &&
928 test_cmp expect actual
929 '
930
931 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
932 (
933 cd addtest2 &&
934 (
935 cd repo &&
936 echo "$submodurl/repo" >expect &&
937 git config remote.origin.url >actual &&
938 test_cmp expect actual &&
939 echo "gitdir: ../.git/modules/repo" >expect &&
940 test_cmp expect .git
941 ) &&
942 rm -rf repo &&
943 git rm repo &&
944 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
945 test_must_be_empty actual &&
946 echo "gitdir: ../.git/modules/submod" >expect &&
947 test_cmp expect submod/.git &&
948 (
949 cd repo &&
950 echo "$submodurl/bare.git" >expect &&
951 git config remote.origin.url >actual &&
952 test_cmp expect actual &&
953 echo "gitdir: ../.git/modules/repo_new" >expect &&
954 test_cmp expect .git
955 ) &&
956 echo "repo" >expect &&
957 test_must_fail git config -f .gitmodules submodule.repo.path &&
958 git config -f .gitmodules submodule.repo_new.path >actual &&
959 test_cmp expect actual &&
960 echo "$submodurl/repo" >expect &&
961 test_must_fail git config -f .gitmodules submodule.repo.url &&
962 echo "$submodurl/bare.git" >expect &&
963 git config -f .gitmodules submodule.repo_new.url >actual &&
964 test_cmp expect actual &&
965 echo "$submodurl/repo" >expect &&
966 git config submodule.repo.url >actual &&
967 test_cmp expect actual &&
968 echo "$submodurl/bare.git" >expect &&
969 git config submodule.repo_new.url >actual &&
970 test_cmp expect actual
971 )
972 '
973
974 test_expect_success 'recursive relative submodules stay relative' '
975 test_when_finished "rm -rf super clone2 subsub sub3" &&
976 mkdir subsub &&
977 (
978 cd subsub &&
979 git init &&
980 >t &&
981 git add t &&
982 git commit -m "initial commit"
983 ) &&
984 mkdir sub3 &&
985 (
986 cd sub3 &&
987 git init &&
988 >t &&
989 git add t &&
990 git commit -m "initial commit" &&
991 git submodule add ../subsub dirdir/subsub &&
992 git commit -m "add submodule subsub"
993 ) &&
994 mkdir super &&
995 (
996 cd super &&
997 git init &&
998 >t &&
999 git add t &&
1000 git commit -m "initial commit" &&
1001 git submodule add ../sub3 &&
1002 git commit -m "add submodule sub"
1003 ) &&
1004 git clone super clone2 &&
1005 (
1006 cd clone2 &&
1007 git submodule update --init --recursive &&
1008 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
1009 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
1010 ) &&
1011 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
1012 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
1013 '
1014
1015 test_expect_success 'submodule add with an existing name fails unless forced' '
1016 (
1017 cd addtest2 &&
1018 rm -rf repo &&
1019 git rm repo &&
1020 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
1021 test ! -d repo &&
1022 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
1023 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
1024 echo "$submodurl/bare.git" >expect &&
1025 git config submodule.repo_new.url >actual &&
1026 test_cmp expect actual &&
1027 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
1028 test -d repo &&
1029 echo "repo" >expect &&
1030 git config -f .gitmodules submodule.repo_new.path >actual &&
1031 test_cmp expect actual &&
1032 echo "$submodurl/repo.git" >expect &&
1033 git config -f .gitmodules submodule.repo_new.url >actual &&
1034 test_cmp expect actual &&
1035 echo "$submodurl/repo.git" >expect &&
1036 git config submodule.repo_new.url >actual &&
1037 test_cmp expect actual
1038 )
1039 '
1040
1041 test_expect_success 'set up a second submodule' '
1042 git submodule add ./init2 example2 &&
1043 git commit -m "submodule example2 added"
1044 '
1045
1046 test_expect_success 'submodule deinit works on repository without submodules' '
1047 test_when_finished "rm -rf newdirectory" &&
1048 mkdir newdirectory &&
1049 (
1050 cd newdirectory &&
1051 git init &&
1052 >file &&
1053 git add file &&
1054 git commit -m "repo should not be empty" &&
1055 git submodule deinit . &&
1056 git submodule deinit --all
1057 )
1058 '
1059
1060 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
1061 git config submodule.example.foo bar &&
1062 git config submodule.example2.frotz nitfol &&
1063 git submodule deinit init &&
1064 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1065 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1066 test -f example2/.git &&
1067 rmdir init
1068 '
1069
1070 test_expect_success 'submodule deinit should unset core.worktree' '
1071 test_path_is_file .git/modules/example/config &&
1072 test_must_fail git config -f .git/modules/example/config core.worktree
1073 '
1074
1075 test_expect_success 'submodule deinit from subdirectory' '
1076 git submodule update --init &&
1077 git config submodule.example.foo bar &&
1078 mkdir -p sub &&
1079 (
1080 cd sub &&
1081 git submodule deinit ../init >../output
1082 ) &&
1083 test_i18ngrep "\\.\\./init" output &&
1084 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1085 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1086 test -f example2/.git &&
1087 rmdir init
1088 '
1089
1090 test_expect_success 'submodule deinit . deinits all initialized submodules' '
1091 git submodule update --init &&
1092 git config submodule.example.foo bar &&
1093 git config submodule.example2.frotz nitfol &&
1094 test_must_fail git submodule deinit &&
1095 git submodule deinit . >actual &&
1096 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1097 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1098 test_i18ngrep "Cleared directory .init" actual &&
1099 test_i18ngrep "Cleared directory .example2" actual &&
1100 rmdir init example2
1101 '
1102
1103 test_expect_success 'submodule deinit --all deinits all initialized submodules' '
1104 git submodule update --init &&
1105 git config submodule.example.foo bar &&
1106 git config submodule.example2.frotz nitfol &&
1107 test_must_fail git submodule deinit &&
1108 git submodule deinit --all >actual &&
1109 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1110 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1111 test_i18ngrep "Cleared directory .init" actual &&
1112 test_i18ngrep "Cleared directory .example2" actual &&
1113 rmdir init example2
1114 '
1115
1116 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
1117 git submodule update --init &&
1118 rm -rf init example2/* example2/.git &&
1119 git submodule deinit init example2 >actual &&
1120 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1121 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1122 test_i18ngrep ! "Cleared directory .init" actual &&
1123 test_i18ngrep "Cleared directory .example2" actual &&
1124 rmdir init
1125 '
1126
1127 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
1128 git submodule update --init &&
1129 echo X >>init/s &&
1130 test_must_fail git submodule deinit init &&
1131 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1132 test -f example2/.git &&
1133 git submodule deinit -f init >actual &&
1134 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1135 test_i18ngrep "Cleared directory .init" actual &&
1136 rmdir init
1137 '
1138
1139 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1140 git submodule update --init &&
1141 echo X >>init/untracked &&
1142 test_must_fail git submodule deinit init &&
1143 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1144 test -f example2/.git &&
1145 git submodule deinit -f init >actual &&
1146 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1147 test_i18ngrep "Cleared directory .init" actual &&
1148 rmdir init
1149 '
1150
1151 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1152 git submodule update --init &&
1153 (
1154 cd init &&
1155 git checkout HEAD^
1156 ) &&
1157 test_must_fail git submodule deinit init &&
1158 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1159 test -f example2/.git &&
1160 git submodule deinit -f init >actual &&
1161 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1162 test_i18ngrep "Cleared directory .init" actual &&
1163 rmdir init
1164 '
1165
1166 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1167 git submodule update --init &&
1168 git submodule deinit init >actual &&
1169 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1170 test_i18ngrep "Cleared directory .init" actual &&
1171 git submodule deinit init >actual &&
1172 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1173 test_i18ngrep "Cleared directory .init" actual &&
1174 git submodule deinit . >actual &&
1175 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1176 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1177 test_i18ngrep "Cleared directory .init" actual &&
1178 git submodule deinit . >actual &&
1179 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1180 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1181 test_i18ngrep "Cleared directory .init" actual &&
1182 git submodule deinit --all >actual &&
1183 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1184 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1185 test_i18ngrep "Cleared directory .init" actual &&
1186 rmdir init example2
1187 '
1188
1189 test_expect_success 'submodule deinit absorbs .git directory if .git is a directory' '
1190 git submodule update --init &&
1191 (
1192 cd init &&
1193 rm .git &&
1194 mv ../.git/modules/example .git &&
1195 GIT_WORK_TREE=. git config --unset core.worktree
1196 ) &&
1197 git submodule deinit init &&
1198 test_path_is_missing init/.git &&
1199 test -z "$(git config --get-regexp "submodule\.example\.")"
1200 '
1201
1202 test_expect_success 'submodule with UTF-8 name' '
1203 svname=$(printf "\303\245 \303\244\303\266") &&
1204 mkdir "$svname" &&
1205 (
1206 cd "$svname" &&
1207 git init &&
1208 >sub &&
1209 git add sub &&
1210 git commit -m "init sub"
1211 ) &&
1212 git submodule add ./"$svname" &&
1213 git submodule >&2 &&
1214 test -n "$(git submodule | grep "$svname")"
1215 '
1216
1217 test_expect_success 'submodule add clone shallow submodule' '
1218 mkdir super &&
1219 pwd=$(pwd) &&
1220 (
1221 cd super &&
1222 git init &&
1223 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1224 (
1225 cd submodule &&
1226 test 1 = $(git log --oneline | wc -l)
1227 )
1228 )
1229 '
1230
1231 test_expect_success 'submodule helper list is not confused by common prefixes' '
1232 mkdir -p dir1/b &&
1233 (
1234 cd dir1/b &&
1235 git init &&
1236 echo hi >testfile2 &&
1237 git add . &&
1238 git commit -m "test1"
1239 ) &&
1240 mkdir -p dir2/b &&
1241 (
1242 cd dir2/b &&
1243 git init &&
1244 echo hello >testfile1 &&
1245 git add . &&
1246 git commit -m "test2"
1247 ) &&
1248 git submodule add /dir1/b dir1/b &&
1249 git submodule add /dir2/b dir2/b &&
1250 git commit -m "first submodule commit" &&
1251 git submodule--helper list dir1/b | cut -f 2 >actual &&
1252 echo "dir1/b" >expect &&
1253 test_cmp expect actual
1254 '
1255
1256 test_expect_success 'setup superproject with submodules' '
1257 git init sub1 &&
1258 test_commit -C sub1 test &&
1259 test_commit -C sub1 test2 &&
1260 git init multisuper &&
1261 git -C multisuper submodule add ../sub1 sub0 &&
1262 git -C multisuper submodule add ../sub1 sub1 &&
1263 git -C multisuper submodule add ../sub1 sub2 &&
1264 git -C multisuper submodule add ../sub1 sub3 &&
1265 git -C multisuper commit -m "add some submodules"
1266 '
1267
1268 cat >expect <<-EOF
1269 -sub0
1270 sub1 (test2)
1271 sub2 (test2)
1272 sub3 (test2)
1273 EOF
1274
1275 test_expect_success 'submodule update --init with a specification' '
1276 test_when_finished "rm -rf multisuper_clone" &&
1277 pwd=$(pwd) &&
1278 git clone file://"$pwd"/multisuper multisuper_clone &&
1279 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1280 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1281 test_cmp expect actual
1282 '
1283
1284 test_expect_success 'submodule update --init with submodule.active set' '
1285 test_when_finished "rm -rf multisuper_clone" &&
1286 pwd=$(pwd) &&
1287 git clone file://"$pwd"/multisuper multisuper_clone &&
1288 git -C multisuper_clone config submodule.active "." &&
1289 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1290 git -C multisuper_clone submodule update --init &&
1291 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1292 test_cmp expect actual
1293 '
1294
1295 test_expect_success 'submodule update and setting submodule.<name>.active' '
1296 test_when_finished "rm -rf multisuper_clone" &&
1297 pwd=$(pwd) &&
1298 git clone file://"$pwd"/multisuper multisuper_clone &&
1299 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1300 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1301 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1302
1303 cat >expect <<-\EOF &&
1304 sub0 (test2)
1305 -sub1
1306 sub2 (test2)
1307 -sub3
1308 EOF
1309 git -C multisuper_clone submodule update &&
1310 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1311 test_cmp expect actual
1312 '
1313
1314 test_expect_success 'clone active submodule without submodule url set' '
1315 test_when_finished "rm -rf test/test" &&
1316 mkdir test &&
1317 # another dir breaks accidental relative paths still being correct
1318 git clone file://"$pwd"/multisuper test/test &&
1319 (
1320 cd test/test &&
1321 git config submodule.active "." &&
1322
1323 # do not pass --init flag, as the submodule is already active:
1324 git submodule update &&
1325 git submodule status >actual_raw &&
1326
1327 cut -d" " -f3- actual_raw >actual &&
1328 cat >expect <<-\EOF &&
1329 sub0 (test2)
1330 sub1 (test2)
1331 sub2 (test2)
1332 sub3 (test2)
1333 EOF
1334 test_cmp expect actual
1335 )
1336 '
1337
1338 test_expect_success 'clone --recurse-submodules with a pathspec works' '
1339 test_when_finished "rm -rf multisuper_clone" &&
1340 cat >expected <<-\EOF &&
1341 sub0 (test2)
1342 -sub1
1343 -sub2
1344 -sub3
1345 EOF
1346
1347 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1348 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1349 test_cmp expected actual
1350 '
1351
1352 test_expect_success 'clone with multiple --recurse-submodules options' '
1353 test_when_finished "rm -rf multisuper_clone" &&
1354 cat >expect <<-\EOF &&
1355 -sub0
1356 sub1 (test2)
1357 -sub2
1358 sub3 (test2)
1359 EOF
1360
1361 git clone --recurse-submodules="." \
1362 --recurse-submodules=":(exclude)sub0" \
1363 --recurse-submodules=":(exclude)sub2" \
1364 multisuper multisuper_clone &&
1365 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1366 test_cmp expect actual
1367 '
1368
1369 test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
1370 test_when_finished "rm -rf multisuper_clone" &&
1371 cat <<-\EOF >expect &&
1372 -sub0
1373 sub1 (test2)
1374 -sub2
1375 sub3 (test2)
1376 EOF
1377
1378 cat <<-\EOF >expect2 &&
1379 -sub0
1380 sub1 (test2)
1381 -sub2
1382 sub3 (test2)
1383 -sub4
1384 sub5 (test2)
1385 EOF
1386
1387 git clone --recurse-submodules="." \
1388 --recurse-submodules=":(exclude)sub0" \
1389 --recurse-submodules=":(exclude)sub2" \
1390 --recurse-submodules=":(exclude)sub4" \
1391 multisuper multisuper_clone &&
1392
1393 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1394 test_cmp expect actual &&
1395
1396 git -C multisuper submodule add ../sub1 sub4 &&
1397 git -C multisuper submodule add ../sub1 sub5 &&
1398 git -C multisuper commit -m "add more submodules" &&
1399 # obtain the new superproject
1400 git -C multisuper_clone pull &&
1401 git -C multisuper_clone submodule update --init &&
1402 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1403 test_cmp expect2 actual
1404 '
1405
1406 test_expect_success 'init properly sets the config' '
1407 test_when_finished "rm -rf multisuper_clone" &&
1408 git clone --recurse-submodules="." \
1409 --recurse-submodules=":(exclude)sub0" \
1410 multisuper multisuper_clone &&
1411
1412 git -C multisuper_clone submodule init -- sub0 sub1 &&
1413 git -C multisuper_clone config --get submodule.sub0.active &&
1414 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1415 '
1416
1417 test_expect_success 'recursive clone respects -q' '
1418 test_when_finished "rm -rf multisuper_clone" &&
1419 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1420 test_must_be_empty actual
1421 '
1422
1423 test_done