]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7400-submodule-basic.sh
Merge branch 'jk/dotgit-case-maint-1.8.5' into maint-1.8.5
[thirdparty/git.git] / t / t7400-submodule-basic.sh
CommitLineData
88961ef2
LH
1#!/bin/sh
2#
3# Copyright (c) 2007 Lars Hjemli
4#
5
6test_description='Basic porcelain support for submodules
7
8This test tries to verify basic sanity of the init, update and status
47a528ad 9subcommands of git submodule.
88961ef2
LH
10'
11
12. ./test-lib.sh
13
fe454b13
JN
14test_expect_success 'setup - initial commit' '
15 >t &&
47a528ad
NS
16 git add t &&
17 git commit -m "initial commit" &&
fe454b13
JN
18 git branch initial
19'
20
4b054402
JL
21test_expect_success 'configuration parsing' '
22 test_when_finished "rm -f .gitmodules" &&
23 cat >.gitmodules <<-\EOF &&
24 [submodule "s"]
25 path
26 ignore
27 EOF
28 test_must_fail git status
29'
30
fe454b13 31test_expect_success 'setup - repository in init subdirectory' '
a2d93aea 32 mkdir init &&
fe454b13
JN
33 (
34 cd init &&
35 git init &&
36 echo a >a &&
37 git add a &&
38 git commit -m "submodule commit 1" &&
39 git tag -a -m "rev-1" rev-1
40 )
fe454b13
JN
41'
42
43test_expect_success 'setup - commit with gitlink' '
88961ef2
LH
44 echo a >a &&
45 echo z >z &&
a2d93aea 46 git add a init z &&
fe454b13
JN
47 git commit -m "super commit 1"
48'
49
50test_expect_success 'setup - hide init subdirectory' '
51 mv init .subrepo
52'
53
a76c944b 54test_expect_success 'setup - repository to add submodules to' '
31991b02
ÆAB
55 git init addtest &&
56 git init addtest-ignore
a76c944b
JN
57'
58
59# The 'submodule add' tests need some repository to add as a submodule.
dd008b3b
JK
60# The trash directory is a good one as any. We need to canonicalize
61# the name, though, as some tests compare it to the absolute path git
62# generates, which will expand symbolic links.
63submodurl=$(pwd -P)
a76c944b
JN
64
65listbranches() {
66 git for-each-ref --format='%(refname)' 'refs/heads/*'
67}
68
69inspect() {
70 dir=$1 &&
71 dotdot="${2:-..}" &&
72
ac8463d2 73 (
a76c944b
JN
74 cd "$dir" &&
75 listbranches >"$dotdot/heads" &&
76 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
ce8b54d6 77 git rev-parse HEAD >"$dotdot/head-sha1" &&
a76c944b
JN
78 git update-index --refresh &&
79 git diff-files --exit-code &&
80 git clean -n -d -x >"$dotdot/untracked"
ac8463d2 81 )
a76c944b 82}
ac8463d2
MG
83
84test_expect_success 'submodule add' '
a76c944b
JN
85 echo "refs/heads/master" >expect &&
86 >empty &&
87
ac8463d2
MG
88 (
89 cd addtest &&
7e60407f 90 git submodule add -q "$submodurl" submod >actual &&
ca8d148d 91 test_must_be_empty actual &&
ea115a0d
JL
92 echo "gitdir: ../.git/modules/submod" >expect &&
93 test_cmp expect submod/.git &&
d75219b4
JL
94 (
95 cd submod &&
96 git config core.worktree >actual &&
97 echo "../../../submod" >expect &&
98 test_cmp expect actual &&
99 rm -f actual expect
100 ) &&
ac8463d2 101 git submodule init
a76c944b
JN
102 ) &&
103
104 rm -f heads head untracked &&
105 inspect addtest/submod ../.. &&
106 test_cmp expect heads &&
107 test_cmp expect head &&
108 test_cmp empty untracked
ac8463d2
MG
109'
110
d27b876b 111test_expect_success 'submodule add to .gitignored path fails' '
31991b02
ÆAB
112 (
113 cd addtest-ignore &&
d27b876b
JL
114 cat <<-\EOF >expect &&
115 The following path is ignored by one of your .gitignore files:
116 submod
117 Use -f if you really want to add it.
118 EOF
31991b02
ÆAB
119 # Does not use test_commit due to the ignore
120 echo "*" > .gitignore &&
121 git add --force .gitignore &&
122 git commit -m"Ignore everything" &&
d27b876b 123 ! git submodule add "$submodurl" submod >actual 2>&1 &&
3a4c3ed7 124 test_i18ncmp expect actual
d27b876b
JL
125 )
126'
31991b02 127
d27b876b
JL
128test_expect_success 'submodule add to .gitignored path with --force' '
129 (
130 cd addtest-ignore &&
131 git submodule add --force "$submodurl" submod
132 )
31991b02
ÆAB
133'
134
ea10b60c 135test_expect_success 'submodule add --branch' '
a76c944b
JN
136 echo "refs/heads/initial" >expect-head &&
137 cat <<-\EOF >expect-heads &&
138 refs/heads/initial
139 refs/heads/master
140 EOF
141 >empty &&
142
ea10b60c
BJ
143 (
144 cd addtest &&
145 git submodule add -b initial "$submodurl" submod-branch &&
b9289227 146 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
a76c944b
JN
147 git submodule init
148 ) &&
149
150 rm -f heads head untracked &&
151 inspect addtest/submod-branch ../.. &&
152 test_cmp expect-heads heads &&
153 test_cmp expect-head head &&
154 test_cmp empty untracked
ea10b60c
BJ
155'
156
db75ada5 157test_expect_success 'submodule add with ./ in path' '
a76c944b
JN
158 echo "refs/heads/master" >expect &&
159 >empty &&
160
ac8463d2
MG
161 (
162 cd addtest &&
163 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
164 git submodule init
a76c944b
JN
165 ) &&
166
167 rm -f heads head untracked &&
168 inspect addtest/dotsubmod/frotz ../../.. &&
169 test_cmp expect heads &&
170 test_cmp expect head &&
171 test_cmp empty untracked
ac8463d2
MG
172'
173
db75ada5 174test_expect_success 'submodule add with // in path' '
a76c944b
JN
175 echo "refs/heads/master" >expect &&
176 >empty &&
177
ac8463d2
MG
178 (
179 cd addtest &&
180 git submodule add "$submodurl" slashslashsubmod///frotz// &&
181 git submodule init
a76c944b
JN
182 ) &&
183
184 rm -f heads head untracked &&
185 inspect addtest/slashslashsubmod/frotz ../../.. &&
186 test_cmp expect heads &&
187 test_cmp expect head &&
188 test_cmp empty untracked
ac8463d2
MG
189'
190
db75ada5 191test_expect_success 'submodule add with /.. in path' '
a76c944b
JN
192 echo "refs/heads/master" >expect &&
193 >empty &&
194
ac8463d2
MG
195 (
196 cd addtest &&
197 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
198 git submodule init
a76c944b
JN
199 ) &&
200
201 rm -f heads head untracked &&
202 inspect addtest/realsubmod ../.. &&
203 test_cmp expect heads &&
204 test_cmp expect head &&
205 test_cmp empty untracked
ac8463d2
MG
206'
207
db75ada5 208test_expect_success 'submodule add with ./, /.. and // in path' '
a76c944b
JN
209 echo "refs/heads/master" >expect &&
210 >empty &&
211
ac8463d2
MG
212 (
213 cd addtest &&
214 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
215 git submodule init
a76c944b
JN
216 ) &&
217
218 rm -f heads head untracked &&
219 inspect addtest/realsubmod2 ../.. &&
220 test_cmp expect heads &&
221 test_cmp expect head &&
222 test_cmp empty untracked
ac8463d2
MG
223'
224
091a6eb0
JK
225test_expect_success 'submodule add in subdirectory' '
226 echo "refs/heads/master" >expect &&
227 >empty &&
228
229 mkdir addtest/sub &&
230 (
231 cd addtest/sub &&
232 git submodule add "$submodurl" ../realsubmod3 &&
233 git submodule init
234 ) &&
235
236 rm -f heads head untracked &&
237 inspect addtest/realsubmod3 ../.. &&
238 test_cmp expect heads &&
239 test_cmp expect head &&
240 test_cmp empty untracked
241'
242
243test_expect_success 'submodule add in subdirectory with relative path should fail' '
244 (
245 cd addtest/sub &&
246 test_must_fail git submodule add ../../ submod3 2>../../output.err
247 ) &&
248 test_i18ngrep toplevel output.err
249'
250
ce8b54d6
JN
251test_expect_success 'setup - add an example entry to .gitmodules' '
252 GIT_CONFIG=.gitmodules \
253 git config submodule.example.url git://example.com/init.git
254'
255
941987a5 256test_expect_success 'status should fail for unmapped paths' '
ce8b54d6
JN
257 test_must_fail git submodule status
258'
259
260test_expect_success 'setup - map path in .gitmodules' '
261 cat <<\EOF >expect &&
262[submodule "example"]
263 url = git://example.com/init.git
264 path = init
265EOF
266
267 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
268
269 test_cmp expect .gitmodules
88961ef2
LH
270'
271
272test_expect_success 'status should only print one line' '
ce8b54d6 273 git submodule status >lines &&
3fb0459b 274 test_line_count = 1 lines
ce8b54d6
JN
275'
276
277test_expect_success 'setup - fetch commit name from submodule' '
278 rev1=$(cd .subrepo && git rev-parse HEAD) &&
279 printf "rev1: %s\n" "$rev1" &&
280 test -n "$rev1"
88961ef2
LH
281'
282
283test_expect_success 'status should initially be "missing"' '
ce8b54d6
JN
284 git submodule status >lines &&
285 grep "^-$rev1" lines
88961ef2
LH
286'
287
211b7f19 288test_expect_success 'init should register submodule url in .git/config' '
ce8b54d6
JN
289 echo git://example.com/init.git >expect &&
290
47a528ad 291 git submodule init &&
ce8b54d6
JN
292 git config submodule.example.url >url &&
293 git config submodule.example.url ./.subrepo &&
294
295 test_cmp expect url
211b7f19
LH
296'
297
be9d0a3a
HV
298test_failure_with_unknown_submodule () {
299 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
300 grep "^error: .*no-such-submodule" output.err
301}
302
303test_expect_success 'init should fail with unknown submodule' '
304 test_failure_with_unknown_submodule init
305'
306
307test_expect_success 'update should fail with unknown submodule' '
308 test_failure_with_unknown_submodule update
309'
310
311test_expect_success 'status should fail with unknown submodule' '
312 test_failure_with_unknown_submodule status
313'
314
315test_expect_success 'sync should fail with unknown submodule' '
316 test_failure_with_unknown_submodule sync
317'
318
211b7f19 319test_expect_success 'update should fail when path is used by a file' '
ce8b54d6
JN
320 echo hello >expect &&
321
a2d93aea 322 echo "hello" >init &&
ce8b54d6
JN
323 test_must_fail git submodule update &&
324
325 test_cmp expect init
88961ef2
LH
326'
327
211b7f19 328test_expect_success 'update should fail when path is used by a nonempty directory' '
ce8b54d6
JN
329 echo hello >expect &&
330
331 rm -fr init &&
a2d93aea
JH
332 mkdir init &&
333 echo "hello" >init/a &&
ce8b54d6
JN
334
335 test_must_fail git submodule update &&
336
337 test_cmp expect init/a
88961ef2
LH
338'
339
211b7f19 340test_expect_success 'update should work when path is an empty dir' '
ce8b54d6
JN
341 rm -fr init &&
342 rm -f head-sha1 &&
343 echo "$rev1" >expect &&
344
a2d93aea 345 mkdir init &&
7e60407f 346 git submodule update -q >update.out &&
ca8d148d 347 test_must_be_empty update.out &&
ce8b54d6
JN
348
349 inspect init &&
350 test_cmp expect head-sha1
88961ef2
LH
351'
352
211b7f19 353test_expect_success 'status should be "up-to-date" after update' '
ce8b54d6
JN
354 git submodule status >list &&
355 grep "^ $rev1" list
88961ef2
LH
356'
357
091a6eb0
JK
358test_expect_success 'status "up-to-date" from subdirectory' '
359 mkdir -p sub &&
360 (
361 cd sub &&
362 git submodule status >../list
363 ) &&
364 grep "^ $rev1" list &&
365 grep "\\.\\./init" list
366'
367
368test_expect_success 'status "up-to-date" from subdirectory with path' '
369 mkdir -p sub &&
370 (
371 cd sub &&
372 git submodule status ../init >../list
373 ) &&
374 grep "^ $rev1" list &&
375 grep "\\.\\./init" list
376'
377
88961ef2 378test_expect_success 'status should be "modified" after submodule commit' '
ce8b54d6
JN
379 (
380 cd init &&
381 echo b >b &&
382 git add b &&
383 git commit -m "submodule commit 2"
384 ) &&
385
386 rev2=$(cd init && git rev-parse HEAD) &&
387 test -n "$rev2" &&
388 git submodule status >list &&
389
390 grep "^+$rev2" list
88961ef2
LH
391'
392
393test_expect_success 'the --cached sha1 should be rev1' '
ce8b54d6
JN
394 git submodule --cached status >list &&
395 grep "^+$rev1" list
88961ef2
LH
396'
397
5701115a 398test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
ce8b54d6
JN
399 git diff >diff &&
400 grep "^+Subproject commit $rev2" diff
5701115a
SV
401'
402
88961ef2 403test_expect_success 'update should checkout rev1' '
ce8b54d6
JN
404 rm -f head-sha1 &&
405 echo "$rev1" >expect &&
406
47a528ad 407 git submodule update init &&
ce8b54d6
JN
408 inspect init &&
409
410 test_cmp expect head-sha1
88961ef2
LH
411'
412
413test_expect_success 'status should be "up-to-date" after update' '
ce8b54d6
JN
414 git submodule status >list &&
415 grep "^ $rev1" list
88961ef2
LH
416'
417
0cf73755 418test_expect_success 'checkout superproject with subproject already present' '
47a528ad
NS
419 git checkout initial &&
420 git checkout master
0cf73755
SV
421'
422
e06c5a6c 423test_expect_success 'apply submodule diff' '
ce8b54d6
JN
424 >empty &&
425
e06c5a6c
SV
426 git branch second &&
427 (
a2d93aea 428 cd init &&
e06c5a6c
SV
429 echo s >s &&
430 git add s &&
431 git commit -m "change subproject"
432 ) &&
a2d93aea 433 git update-index --add init &&
47a528ad
NS
434 git commit -m "change init" &&
435 git format-patch -1 --stdout >P.diff &&
e06c5a6c
SV
436 git checkout second &&
437 git apply --index P.diff &&
ce8b54d6
JN
438
439 git diff --cached master >staged &&
440 test_cmp empty staged
e06c5a6c
SV
441'
442
be4d2c83 443test_expect_success 'update --init' '
be4d2c83
JS
444 mv init init2 &&
445 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
ce8b54d6
JN
446 git config --remove-section submodule.example &&
447 test_must_fail git config submodule.example.url &&
448
be4d2c83 449 git submodule update init > update.out &&
ce8b54d6 450 cat update.out &&
1c2ef66f 451 test_i18ngrep "not initialized" update.out &&
abc06822 452 test_must_fail git rev-parse --resolve-git-dir init/.git &&
ce8b54d6 453
be4d2c83 454 git submodule update --init init &&
abc06822 455 git rev-parse --resolve-git-dir init/.git
be4d2c83
JS
456'
457
091a6eb0
JK
458test_expect_success 'update --init from subdirectory' '
459 mv init init2 &&
460 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
461 git config --remove-section submodule.example &&
462 test_must_fail git config submodule.example.url &&
463
464 mkdir -p sub &&
465 (
466 cd sub &&
467 git submodule update ../init >update.out &&
468 cat update.out &&
469 test_i18ngrep "not initialized" update.out &&
470 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
471
472 git submodule update --init ../init
473 ) &&
474 git rev-parse --resolve-git-dir init/.git
475'
476
2ce53f9b
JS
477test_expect_success 'do not add files from a submodule' '
478
479 git reset --hard &&
480 test_must_fail git add init/a
481
482'
483
2c63d6eb 484test_expect_success 'gracefully add/reset submodule with a trailing slash' '
2ce53f9b
JS
485
486 git reset --hard &&
487 git commit -m "commit subproject" init &&
488 (cd init &&
489 echo b > a) &&
490 git add init/ &&
491 git diff --exit-code --cached init &&
492 commit=$(cd init &&
493 git commit -m update a >/dev/null &&
494 git rev-parse HEAD) &&
495 git add init/ &&
496 test_must_fail git diff --exit-code --cached init &&
497 test $commit = $(git ls-files --stage |
2c63d6eb
JK
498 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
499 git reset init/ &&
500 git diff --exit-code --cached init
2ce53f9b
JS
501
502'
503
f3670a57
JS
504test_expect_success 'ls-files gracefully handles trailing slash' '
505
506 test "init" = "$(git ls-files init/)"
507
508'
509
c5e558a8
PC
510test_expect_success 'moving to a commit without submodule does not leave empty dir' '
511 rm -rf init &&
512 mkdir init &&
513 git reset --hard &&
514 git checkout initial &&
515 test ! -d init &&
516 git checkout second
517'
518
af9c9f97
RR
519test_expect_success 'submodule <invalid-subcommand> fails' '
520 test_must_fail git submodule no-such-subcommand
496917b7
JS
521'
522
1414e578
JL
523test_expect_success 'add submodules without specifying an explicit path' '
524 mkdir repo &&
18a82692
JN
525 (
526 cd repo &&
527 git init &&
528 echo r >r &&
529 git add r &&
530 git commit -m "repo commit 1"
fd4ec4f2 531 ) &&
1414e578 532 git clone --bare repo/ bare.git &&
69e7236c
JL
533 (
534 cd addtest &&
535 git submodule add "$submodurl/repo" &&
536 git config -f .gitmodules submodule.repo.path repo &&
537 git submodule add "$submodurl/bare.git" &&
538 git config -f .gitmodules submodule.bare.path bare
539 )
540'
541
542test_expect_success 'add should fail when path is used by a file' '
543 (
544 cd addtest &&
545 touch file &&
546 test_must_fail git submodule add "$submodurl/repo" file
547 )
548'
549
550test_expect_success 'add should fail when path is used by an existing directory' '
551 (
552 cd addtest &&
553 mkdir empty-dir &&
554 test_must_fail git submodule add "$submodurl/repo" empty-dir
555 )
1414e578
JL
556'
557
4d689320 558test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
8537f0ef
JL
559 (
560 cd addtest &&
4d689320
JL
561 git submodule add ../repo relative &&
562 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
563 git submodule sync relative &&
564 test "$(git config submodule.relative.url)" = "$submodurl/repo"
8537f0ef
JL
565 )
566'
567
ea640cc6
TR
568test_expect_success 'set up for relative path tests' '
569 mkdir reltest &&
570 (
571 cd reltest &&
572 git init &&
573 mkdir sub &&
574 (
575 cd sub &&
576 git init &&
577 test_commit foo
578 ) &&
579 git add sub &&
580 git config -f .gitmodules submodule.sub.path sub &&
581 git config -f .gitmodules submodule.sub.url ../subrepo &&
712693e8
JS
582 cp .git/config pristine-.git-config &&
583 cp .gitmodules pristine-.gitmodules
ea640cc6
TR
584 )
585'
586
712693e8 587test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
ea640cc6
TR
588 (
589 cd reltest &&
590 cp pristine-.git-config .git/config &&
712693e8 591 cp pristine-.gitmodules .gitmodules &&
ea640cc6
TR
592 git config remote.origin.url ssh://hostname/repo &&
593 git submodule init &&
594 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
595 )
596'
597
712693e8
JS
598test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
599 (
600 cd reltest &&
601 cp pristine-.git-config .git/config &&
602 cp pristine-.gitmodules .gitmodules &&
603 git config remote.origin.url ssh://hostname:22/repo &&
604 git submodule init &&
605 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
606 )
607'
608
c517e73d
JS
609# About the choice of the path in the next test:
610# - double-slash side-steps path mangling issues on Windows
611# - it is still an absolute local path
612# - there cannot be a server with a blank in its name just in case the
613# path is used erroneously to access a //server/share style path
614test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
712693e8
JS
615 (
616 cd reltest &&
617 cp pristine-.git-config .git/config &&
618 cp pristine-.gitmodules .gitmodules &&
c517e73d 619 git config remote.origin.url "//somewhere else/repo" &&
712693e8 620 git submodule init &&
c517e73d 621 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
712693e8
JS
622 )
623'
624
625test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
626 (
627 cd reltest &&
628 cp pristine-.git-config .git/config &&
629 cp pristine-.gitmodules .gitmodules &&
630 git config remote.origin.url file:///tmp/repo &&
631 git submodule init &&
632 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
633 )
634'
635
636test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
637 (
638 cd reltest &&
639 cp pristine-.git-config .git/config &&
640 cp pristine-.gitmodules .gitmodules &&
641 git config remote.origin.url helper:://hostname/repo &&
642 git submodule init &&
643 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
644 )
645'
646
647test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
ea640cc6
TR
648 (
649 cd reltest &&
650 cp pristine-.git-config .git/config &&
651 git config remote.origin.url user@host:repo &&
652 git submodule init &&
653 test "$(git config submodule.sub.url)" = user@host:subrepo
654 )
655'
656
712693e8
JS
657test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
658 (
659 cd reltest &&
660 cp pristine-.git-config .git/config &&
661 cp pristine-.gitmodules .gitmodules &&
662 git config remote.origin.url user@host:path/to/repo &&
663 git submodule init &&
664 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
665 )
666'
667
758615e2 668test_expect_success '../subrepo works with relative local path - foo' '
49301c64
JS
669 (
670 cd reltest &&
671 cp pristine-.git-config .git/config &&
672 cp pristine-.gitmodules .gitmodules &&
673 git config remote.origin.url foo &&
674 # actual: fails with an error
675 git submodule init &&
676 test "$(git config submodule.sub.url)" = subrepo
677 )
678'
679
712693e8
JS
680test_expect_success '../subrepo works with relative local path - foo/bar' '
681 (
682 cd reltest &&
683 cp pristine-.git-config .git/config &&
684 cp pristine-.gitmodules .gitmodules &&
685 git config remote.origin.url foo/bar &&
686 git submodule init &&
687 test "$(git config submodule.sub.url)" = foo/subrepo
688 )
689'
690
758615e2 691test_expect_success '../subrepo works with relative local path - ./foo' '
49301c64
JS
692 (
693 cd reltest &&
694 cp pristine-.git-config .git/config &&
695 cp pristine-.gitmodules .gitmodules &&
696 git config remote.origin.url ./foo &&
697 git submodule init &&
698 test "$(git config submodule.sub.url)" = subrepo
699 )
700'
701
758615e2 702test_expect_success '../subrepo works with relative local path - ./foo/bar' '
49301c64
JS
703 (
704 cd reltest &&
705 cp pristine-.git-config .git/config &&
706 cp pristine-.gitmodules .gitmodules &&
707 git config remote.origin.url ./foo/bar &&
708 git submodule init &&
709 test "$(git config submodule.sub.url)" = foo/subrepo
710 )
711'
712
712693e8
JS
713test_expect_success '../subrepo works with relative local path - ../foo' '
714 (
715 cd reltest &&
716 cp pristine-.git-config .git/config &&
717 cp pristine-.gitmodules .gitmodules &&
718 git config remote.origin.url ../foo &&
719 git submodule init &&
720 test "$(git config submodule.sub.url)" = ../subrepo
721 )
722'
723
724test_expect_success '../subrepo works with relative local path - ../foo/bar' '
725 (
726 cd reltest &&
727 cp pristine-.git-config .git/config &&
728 cp pristine-.gitmodules .gitmodules &&
729 git config remote.origin.url ../foo/bar &&
730 git submodule init &&
731 test "$(git config submodule.sub.url)" = ../foo/subrepo
732 )
733'
734
735test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
736 (
737 cd reltest &&
738 cp pristine-.git-config .git/config &&
739 cp pristine-.gitmodules .gitmodules &&
740 mkdir -p a/b/c &&
741 (cd a/b/c; git init) &&
742 git config remote.origin.url ../foo/bar.git &&
743 git submodule add ../bar/a/b/c ./a/b/c &&
744 git submodule init &&
745 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
746 )
747'
748
d75219b4
JL
749test_expect_success 'moving the superproject does not break submodules' '
750 (
751 cd addtest &&
752 git submodule status >expect
753 )
754 mv addtest addtest2 &&
755 (
756 cd addtest2 &&
757 git submodule status >actual &&
758 test_cmp expect actual
759 )
760'
761
73b0898d
JL
762test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
763 (
764 cd addtest2 &&
765 (
766 cd repo &&
767 echo "$submodurl/repo" >expect &&
768 git config remote.origin.url >actual &&
769 test_cmp expect actual &&
770 echo "gitdir: ../.git/modules/repo" >expect &&
771 test_cmp expect .git
772 ) &&
773 rm -rf repo &&
774 git rm repo &&
775 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
ca8d148d 776 test_must_be_empty actual &&
73b0898d
JL
777 echo "gitdir: ../.git/modules/submod" >expect &&
778 test_cmp expect submod/.git &&
779 (
780 cd repo &&
781 echo "$submodurl/bare.git" >expect &&
782 git config remote.origin.url >actual &&
783 test_cmp expect actual &&
784 echo "gitdir: ../.git/modules/repo_new" >expect &&
785 test_cmp expect .git
786 ) &&
787 echo "repo" >expect &&
95c16418 788 test_must_fail git config -f .gitmodules submodule.repo.path &&
73b0898d
JL
789 git config -f .gitmodules submodule.repo_new.path >actual &&
790 test_cmp expect actual&&
791 echo "$submodurl/repo" >expect &&
95c16418 792 test_must_fail git config -f .gitmodules submodule.repo.url &&
73b0898d
JL
793 echo "$submodurl/bare.git" >expect &&
794 git config -f .gitmodules submodule.repo_new.url >actual &&
795 test_cmp expect actual &&
796 echo "$submodurl/repo" >expect &&
797 git config submodule.repo.url >actual &&
798 test_cmp expect actual &&
799 echo "$submodurl/bare.git" >expect &&
800 git config submodule.repo_new.url >actual &&
801 test_cmp expect actual
802 )
803'
804
4b7c286e
JL
805test_expect_success 'submodule add with an existing name fails unless forced' '
806 (
807 cd addtest2 &&
808 rm -rf repo &&
809 git rm repo &&
810 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
811 test ! -d repo &&
95c16418
JL
812 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
813 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
4b7c286e
JL
814 echo "$submodurl/bare.git" >expect &&
815 git config submodule.repo_new.url >actual &&
816 test_cmp expect actual &&
817 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
818 test -d repo &&
819 echo "repo" >expect &&
820 git config -f .gitmodules submodule.repo_new.path >actual &&
821 test_cmp expect actual&&
822 echo "$submodurl/repo.git" >expect &&
823 git config -f .gitmodules submodule.repo_new.url >actual &&
824 test_cmp expect actual &&
825 echo "$submodurl/repo.git" >expect &&
826 git config submodule.repo_new.url >actual &&
827 test_cmp expect actual
828 )
829'
830
cf419828
JL
831test_expect_success 'set up a second submodule' '
832 git submodule add ./init2 example2 &&
833 git commit -m "submodule example2 added"
834'
835
836test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
837 git config submodule.example.foo bar &&
838 git config submodule.example2.frotz nitfol &&
839 git submodule deinit init &&
840 test -z "$(git config --get-regexp "submodule\.example\.")" &&
841 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
842 test -f example2/.git &&
843 rmdir init
844'
845
091a6eb0
JK
846test_expect_success 'submodule deinit from subdirectory' '
847 git submodule update --init &&
848 git config submodule.example.foo bar &&
849 mkdir -p sub &&
850 (
851 cd sub &&
852 git submodule deinit ../init >../output
853 ) &&
854 grep "\\.\\./init" output &&
855 test -z "$(git config --get-regexp "submodule\.example\.")" &&
856 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
857 test -f example2/.git &&
858 rmdir init
859'
860
cf419828
JL
861test_expect_success 'submodule deinit . deinits all initialized submodules' '
862 git submodule update --init &&
863 git config submodule.example.foo bar &&
864 git config submodule.example2.frotz nitfol &&
865 test_must_fail git submodule deinit &&
7b294bf4 866 git submodule deinit . >actual &&
cf419828
JL
867 test -z "$(git config --get-regexp "submodule\.example\.")" &&
868 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
7b294bf4
JL
869 test_i18ngrep "Cleared directory .init" actual &&
870 test_i18ngrep "Cleared directory .example2" actual &&
cf419828
JL
871 rmdir init example2
872'
873
874test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
875 git submodule update --init &&
876 rm -rf init example2/* example2/.git &&
7b294bf4 877 git submodule deinit init example2 >actual &&
cf419828
JL
878 test -z "$(git config --get-regexp "submodule\.example\.")" &&
879 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
7b294bf4
JL
880 test_i18ngrep ! "Cleared directory .init" actual &&
881 test_i18ngrep "Cleared directory .example2" actual &&
cf419828
JL
882 rmdir init
883'
884
885test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
886 git submodule update --init &&
887 echo X >>init/s &&
888 test_must_fail git submodule deinit init &&
889 test -n "$(git config --get-regexp "submodule\.example\.")" &&
890 test -f example2/.git &&
7b294bf4 891 git submodule deinit -f init >actual &&
cf419828 892 test -z "$(git config --get-regexp "submodule\.example\.")" &&
7b294bf4 893 test_i18ngrep "Cleared directory .init" actual &&
cf419828
JL
894 rmdir init
895'
896
897test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
898 git submodule update --init &&
899 echo X >>init/untracked &&
900 test_must_fail git submodule deinit init &&
901 test -n "$(git config --get-regexp "submodule\.example\.")" &&
902 test -f example2/.git &&
7b294bf4 903 git submodule deinit -f init >actual &&
cf419828 904 test -z "$(git config --get-regexp "submodule\.example\.")" &&
7b294bf4 905 test_i18ngrep "Cleared directory .init" actual &&
cf419828
JL
906 rmdir init
907'
908
909test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
910 git submodule update --init &&
911 (
912 cd init &&
913 git checkout HEAD^
914 ) &&
915 test_must_fail git submodule deinit init &&
916 test -n "$(git config --get-regexp "submodule\.example\.")" &&
917 test -f example2/.git &&
7b294bf4 918 git submodule deinit -f init >actual &&
cf419828 919 test -z "$(git config --get-regexp "submodule\.example\.")" &&
7b294bf4 920 test_i18ngrep "Cleared directory .init" actual &&
cf419828
JL
921 rmdir init
922'
923
924test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
925 git submodule update --init &&
926 git submodule deinit init >actual &&
927 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
7b294bf4 928 test_i18ngrep "Cleared directory .init" actual &&
cf419828
JL
929 git submodule deinit init >actual &&
930 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
7b294bf4 931 test_i18ngrep "Cleared directory .init" actual &&
cf419828
JL
932 git submodule deinit . >actual &&
933 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
934 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
7b294bf4 935 test_i18ngrep "Cleared directory .init" actual &&
cf419828
JL
936 git submodule deinit . >actual &&
937 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
938 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
7b294bf4 939 test_i18ngrep "Cleared directory .init" actual &&
cf419828
JL
940 rmdir init example2
941'
942
943test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
944 git submodule update --init &&
945 (
946 cd init &&
947 rm .git &&
948 cp -R ../.git/modules/example .git &&
949 GIT_WORK_TREE=. git config --unset core.worktree
950 ) &&
951 test_must_fail git submodule deinit init &&
952 test_must_fail git submodule deinit -f init &&
953 test -d init/.git &&
954 test -n "$(git config --get-regexp "submodule\.example\.")"
955'
956
bed94704
TB
957test_expect_success 'submodule with UTF-8 name' '
958 svname=$(printf "\303\245 \303\244\303\266") &&
959 mkdir "$svname" &&
74671241 960 (
bed94704 961 cd "$svname" &&
74671241 962 git init &&
bed94704
TB
963 >sub &&
964 git add sub &&
74671241 965 git commit -m "init sub"
bed94704 966 ) &&
bed94704
TB
967 git submodule add ./"$svname" &&
968 git submodule >&2 &&
969 test -n "$(git submodule | grep "$svname")"
74671241 970'
2bb7afac 971
275cd184
FG
972test_expect_success 'submodule add clone shallow submodule' '
973 mkdir super &&
974 pwd=$(pwd)
975 (
976 cd super &&
977 git init &&
978 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
979 (
980 cd submodule &&
981 test 1 = $(git log --oneline | wc -l)
982 )
983 )
984'
985
986
88961ef2 987test_done