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