]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7400-submodule-basic.sh
Git 1.7.11.7
[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
21test_expect_success 'setup - repository in init subdirectory' '
a2d93aea 22 mkdir init &&
fe454b13
JN
23 (
24 cd init &&
25 git init &&
26 echo a >a &&
27 git add a &&
28 git commit -m "submodule commit 1" &&
29 git tag -a -m "rev-1" rev-1
30 )
fe454b13
JN
31'
32
33test_expect_success 'setup - commit with gitlink' '
88961ef2
LH
34 echo a >a &&
35 echo z >z &&
a2d93aea 36 git add a init z &&
fe454b13
JN
37 git commit -m "super commit 1"
38'
39
40test_expect_success 'setup - hide init subdirectory' '
41 mv init .subrepo
42'
43
a76c944b 44test_expect_success 'setup - repository to add submodules to' '
31991b02
ÆAB
45 git init addtest &&
46 git init addtest-ignore
a76c944b
JN
47'
48
49# The 'submodule add' tests need some repository to add as a submodule.
dd008b3b
JK
50# The trash directory is a good one as any. We need to canonicalize
51# the name, though, as some tests compare it to the absolute path git
52# generates, which will expand symbolic links.
53submodurl=$(pwd -P)
a76c944b
JN
54
55listbranches() {
56 git for-each-ref --format='%(refname)' 'refs/heads/*'
57}
58
59inspect() {
60 dir=$1 &&
61 dotdot="${2:-..}" &&
62
ac8463d2 63 (
a76c944b
JN
64 cd "$dir" &&
65 listbranches >"$dotdot/heads" &&
66 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
ce8b54d6 67 git rev-parse HEAD >"$dotdot/head-sha1" &&
a76c944b
JN
68 git update-index --refresh &&
69 git diff-files --exit-code &&
70 git clean -n -d -x >"$dotdot/untracked"
ac8463d2 71 )
a76c944b 72}
ac8463d2
MG
73
74test_expect_success 'submodule add' '
a76c944b
JN
75 echo "refs/heads/master" >expect &&
76 >empty &&
77
ac8463d2
MG
78 (
79 cd addtest &&
7e60407f
JL
80 git submodule add -q "$submodurl" submod >actual &&
81 test ! -s actual &&
ea115a0d
JL
82 echo "gitdir: ../.git/modules/submod" >expect &&
83 test_cmp expect submod/.git &&
d75219b4
JL
84 (
85 cd submod &&
86 git config core.worktree >actual &&
87 echo "../../../submod" >expect &&
88 test_cmp expect actual &&
89 rm -f actual expect
90 ) &&
ac8463d2 91 git submodule init
a76c944b
JN
92 ) &&
93
94 rm -f heads head untracked &&
95 inspect addtest/submod ../.. &&
96 test_cmp expect heads &&
97 test_cmp expect head &&
98 test_cmp empty untracked
ac8463d2
MG
99'
100
d27b876b 101test_expect_success 'submodule add to .gitignored path fails' '
31991b02
ÆAB
102 (
103 cd addtest-ignore &&
d27b876b
JL
104 cat <<-\EOF >expect &&
105 The following path is ignored by one of your .gitignore files:
106 submod
107 Use -f if you really want to add it.
108 EOF
31991b02
ÆAB
109 # Does not use test_commit due to the ignore
110 echo "*" > .gitignore &&
111 git add --force .gitignore &&
112 git commit -m"Ignore everything" &&
d27b876b 113 ! git submodule add "$submodurl" submod >actual 2>&1 &&
3a4c3ed7 114 test_i18ncmp expect actual
d27b876b
JL
115 )
116'
31991b02 117
d27b876b
JL
118test_expect_success 'submodule add to .gitignored path with --force' '
119 (
120 cd addtest-ignore &&
121 git submodule add --force "$submodurl" submod
122 )
31991b02
ÆAB
123'
124
ea10b60c 125test_expect_success 'submodule add --branch' '
a76c944b
JN
126 echo "refs/heads/initial" >expect-head &&
127 cat <<-\EOF >expect-heads &&
128 refs/heads/initial
129 refs/heads/master
130 EOF
131 >empty &&
132
ea10b60c
BJ
133 (
134 cd addtest &&
135 git submodule add -b initial "$submodurl" submod-branch &&
a76c944b
JN
136 git submodule init
137 ) &&
138
139 rm -f heads head untracked &&
140 inspect addtest/submod-branch ../.. &&
141 test_cmp expect-heads heads &&
142 test_cmp expect-head head &&
143 test_cmp empty untracked
ea10b60c
BJ
144'
145
db75ada5 146test_expect_success 'submodule add with ./ in path' '
a76c944b
JN
147 echo "refs/heads/master" >expect &&
148 >empty &&
149
ac8463d2
MG
150 (
151 cd addtest &&
152 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
153 git submodule init
a76c944b
JN
154 ) &&
155
156 rm -f heads head untracked &&
157 inspect addtest/dotsubmod/frotz ../../.. &&
158 test_cmp expect heads &&
159 test_cmp expect head &&
160 test_cmp empty untracked
ac8463d2
MG
161'
162
db75ada5 163test_expect_success 'submodule add with // in path' '
a76c944b
JN
164 echo "refs/heads/master" >expect &&
165 >empty &&
166
ac8463d2
MG
167 (
168 cd addtest &&
169 git submodule add "$submodurl" slashslashsubmod///frotz// &&
170 git submodule init
a76c944b
JN
171 ) &&
172
173 rm -f heads head untracked &&
174 inspect addtest/slashslashsubmod/frotz ../../.. &&
175 test_cmp expect heads &&
176 test_cmp expect head &&
177 test_cmp empty untracked
ac8463d2
MG
178'
179
db75ada5 180test_expect_success 'submodule add with /.. in path' '
a76c944b
JN
181 echo "refs/heads/master" >expect &&
182 >empty &&
183
ac8463d2
MG
184 (
185 cd addtest &&
186 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
187 git submodule init
a76c944b
JN
188 ) &&
189
190 rm -f heads head untracked &&
191 inspect addtest/realsubmod ../.. &&
192 test_cmp expect heads &&
193 test_cmp expect head &&
194 test_cmp empty untracked
ac8463d2
MG
195'
196
db75ada5 197test_expect_success 'submodule add with ./, /.. and // in path' '
a76c944b
JN
198 echo "refs/heads/master" >expect &&
199 >empty &&
200
ac8463d2
MG
201 (
202 cd addtest &&
203 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
204 git submodule init
a76c944b
JN
205 ) &&
206
207 rm -f heads head untracked &&
208 inspect addtest/realsubmod2 ../.. &&
209 test_cmp expect heads &&
210 test_cmp expect head &&
211 test_cmp empty untracked
ac8463d2
MG
212'
213
ce8b54d6
JN
214test_expect_success 'setup - add an example entry to .gitmodules' '
215 GIT_CONFIG=.gitmodules \
216 git config submodule.example.url git://example.com/init.git
217'
218
941987a5 219test_expect_success 'status should fail for unmapped paths' '
ce8b54d6
JN
220 test_must_fail git submodule status
221'
222
223test_expect_success 'setup - map path in .gitmodules' '
224 cat <<\EOF >expect &&
225[submodule "example"]
226 url = git://example.com/init.git
227 path = init
228EOF
229
230 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
231
232 test_cmp expect .gitmodules
88961ef2
LH
233'
234
235test_expect_success 'status should only print one line' '
ce8b54d6 236 git submodule status >lines &&
3fb0459b 237 test_line_count = 1 lines
ce8b54d6
JN
238'
239
240test_expect_success 'setup - fetch commit name from submodule' '
241 rev1=$(cd .subrepo && git rev-parse HEAD) &&
242 printf "rev1: %s\n" "$rev1" &&
243 test -n "$rev1"
88961ef2
LH
244'
245
246test_expect_success 'status should initially be "missing"' '
ce8b54d6
JN
247 git submodule status >lines &&
248 grep "^-$rev1" lines
88961ef2
LH
249'
250
211b7f19 251test_expect_success 'init should register submodule url in .git/config' '
ce8b54d6
JN
252 echo git://example.com/init.git >expect &&
253
47a528ad 254 git submodule init &&
ce8b54d6
JN
255 git config submodule.example.url >url &&
256 git config submodule.example.url ./.subrepo &&
257
258 test_cmp expect url
211b7f19
LH
259'
260
be9d0a3a
HV
261test_failure_with_unknown_submodule () {
262 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
263 grep "^error: .*no-such-submodule" output.err
264}
265
266test_expect_success 'init should fail with unknown submodule' '
267 test_failure_with_unknown_submodule init
268'
269
270test_expect_success 'update should fail with unknown submodule' '
271 test_failure_with_unknown_submodule update
272'
273
274test_expect_success 'status should fail with unknown submodule' '
275 test_failure_with_unknown_submodule status
276'
277
278test_expect_success 'sync should fail with unknown submodule' '
279 test_failure_with_unknown_submodule sync
280'
281
211b7f19 282test_expect_success 'update should fail when path is used by a file' '
ce8b54d6
JN
283 echo hello >expect &&
284
a2d93aea 285 echo "hello" >init &&
ce8b54d6
JN
286 test_must_fail git submodule update &&
287
288 test_cmp expect init
88961ef2
LH
289'
290
211b7f19 291test_expect_success 'update should fail when path is used by a nonempty directory' '
ce8b54d6
JN
292 echo hello >expect &&
293
294 rm -fr init &&
a2d93aea
JH
295 mkdir init &&
296 echo "hello" >init/a &&
ce8b54d6
JN
297
298 test_must_fail git submodule update &&
299
300 test_cmp expect init/a
88961ef2
LH
301'
302
211b7f19 303test_expect_success 'update should work when path is an empty dir' '
ce8b54d6
JN
304 rm -fr init &&
305 rm -f head-sha1 &&
306 echo "$rev1" >expect &&
307
a2d93aea 308 mkdir init &&
7e60407f
JL
309 git submodule update -q >update.out &&
310 test ! -s update.out &&
ce8b54d6
JN
311
312 inspect init &&
313 test_cmp expect head-sha1
88961ef2
LH
314'
315
211b7f19 316test_expect_success 'status should be "up-to-date" after update' '
ce8b54d6
JN
317 git submodule status >list &&
318 grep "^ $rev1" list
88961ef2
LH
319'
320
321test_expect_success 'status should be "modified" after submodule commit' '
ce8b54d6
JN
322 (
323 cd init &&
324 echo b >b &&
325 git add b &&
326 git commit -m "submodule commit 2"
327 ) &&
328
329 rev2=$(cd init && git rev-parse HEAD) &&
330 test -n "$rev2" &&
331 git submodule status >list &&
332
333 grep "^+$rev2" list
88961ef2
LH
334'
335
336test_expect_success 'the --cached sha1 should be rev1' '
ce8b54d6
JN
337 git submodule --cached status >list &&
338 grep "^+$rev1" list
88961ef2
LH
339'
340
5701115a 341test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
ce8b54d6
JN
342 git diff >diff &&
343 grep "^+Subproject commit $rev2" diff
5701115a
SV
344'
345
88961ef2 346test_expect_success 'update should checkout rev1' '
ce8b54d6
JN
347 rm -f head-sha1 &&
348 echo "$rev1" >expect &&
349
47a528ad 350 git submodule update init &&
ce8b54d6
JN
351 inspect init &&
352
353 test_cmp expect head-sha1
88961ef2
LH
354'
355
356test_expect_success 'status should be "up-to-date" after update' '
ce8b54d6
JN
357 git submodule status >list &&
358 grep "^ $rev1" list
88961ef2
LH
359'
360
0cf73755 361test_expect_success 'checkout superproject with subproject already present' '
47a528ad
NS
362 git checkout initial &&
363 git checkout master
0cf73755
SV
364'
365
e06c5a6c 366test_expect_success 'apply submodule diff' '
ce8b54d6
JN
367 >empty &&
368
e06c5a6c
SV
369 git branch second &&
370 (
a2d93aea 371 cd init &&
e06c5a6c
SV
372 echo s >s &&
373 git add s &&
374 git commit -m "change subproject"
375 ) &&
a2d93aea 376 git update-index --add init &&
47a528ad
NS
377 git commit -m "change init" &&
378 git format-patch -1 --stdout >P.diff &&
e06c5a6c
SV
379 git checkout second &&
380 git apply --index P.diff &&
ce8b54d6
JN
381
382 git diff --cached master >staged &&
383 test_cmp empty staged
e06c5a6c
SV
384'
385
be4d2c83 386test_expect_success 'update --init' '
be4d2c83
JS
387 mv init init2 &&
388 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
ce8b54d6
JN
389 git config --remove-section submodule.example &&
390 test_must_fail git config submodule.example.url &&
391
be4d2c83 392 git submodule update init > update.out &&
ce8b54d6 393 cat update.out &&
1c2ef66f 394 test_i18ngrep "not initialized" update.out &&
abc06822 395 test_must_fail git rev-parse --resolve-git-dir init/.git &&
ce8b54d6 396
be4d2c83 397 git submodule update --init init &&
abc06822 398 git rev-parse --resolve-git-dir init/.git
be4d2c83
JS
399'
400
2ce53f9b
JS
401test_expect_success 'do not add files from a submodule' '
402
403 git reset --hard &&
404 test_must_fail git add init/a
405
406'
407
408test_expect_success 'gracefully add submodule with a trailing slash' '
409
410 git reset --hard &&
411 git commit -m "commit subproject" init &&
412 (cd init &&
413 echo b > a) &&
414 git add init/ &&
415 git diff --exit-code --cached init &&
416 commit=$(cd init &&
417 git commit -m update a >/dev/null &&
418 git rev-parse HEAD) &&
419 git add init/ &&
420 test_must_fail git diff --exit-code --cached init &&
421 test $commit = $(git ls-files --stage |
422 sed -n "s/^160000 \([^ ]*\).*/\1/p")
423
424'
425
f3670a57
JS
426test_expect_success 'ls-files gracefully handles trailing slash' '
427
428 test "init" = "$(git ls-files init/)"
429
430'
431
c5e558a8
PC
432test_expect_success 'moving to a commit without submodule does not leave empty dir' '
433 rm -rf init &&
434 mkdir init &&
435 git reset --hard &&
436 git checkout initial &&
437 test ! -d init &&
438 git checkout second
439'
440
496917b7 441test_expect_success 'submodule <invalid-path> warns' '
be9d0a3a 442 test_failure_with_unknown_submodule
496917b7
JS
443'
444
1414e578
JL
445test_expect_success 'add submodules without specifying an explicit path' '
446 mkdir repo &&
18a82692
JN
447 (
448 cd repo &&
449 git init &&
450 echo r >r &&
451 git add r &&
452 git commit -m "repo commit 1"
fd4ec4f2 453 ) &&
1414e578 454 git clone --bare repo/ bare.git &&
69e7236c
JL
455 (
456 cd addtest &&
457 git submodule add "$submodurl/repo" &&
458 git config -f .gitmodules submodule.repo.path repo &&
459 git submodule add "$submodurl/bare.git" &&
460 git config -f .gitmodules submodule.bare.path bare
461 )
462'
463
464test_expect_success 'add should fail when path is used by a file' '
465 (
466 cd addtest &&
467 touch file &&
468 test_must_fail git submodule add "$submodurl/repo" file
469 )
470'
471
472test_expect_success 'add should fail when path is used by an existing directory' '
473 (
474 cd addtest &&
475 mkdir empty-dir &&
476 test_must_fail git submodule add "$submodurl/repo" empty-dir
477 )
1414e578
JL
478'
479
4d689320 480test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
8537f0ef
JL
481 (
482 cd addtest &&
4d689320
JL
483 git submodule add ../repo relative &&
484 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
485 git submodule sync relative &&
486 test "$(git config submodule.relative.url)" = "$submodurl/repo"
8537f0ef
JL
487 )
488'
489
ea640cc6
TR
490test_expect_success 'set up for relative path tests' '
491 mkdir reltest &&
492 (
493 cd reltest &&
494 git init &&
495 mkdir sub &&
496 (
497 cd sub &&
498 git init &&
499 test_commit foo
500 ) &&
501 git add sub &&
502 git config -f .gitmodules submodule.sub.path sub &&
503 git config -f .gitmodules submodule.sub.url ../subrepo &&
504 cp .git/config pristine-.git-config
505 )
506'
507
508test_expect_success 'relative path works with URL' '
509 (
510 cd reltest &&
511 cp pristine-.git-config .git/config &&
512 git config remote.origin.url ssh://hostname/repo &&
513 git submodule init &&
514 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
515 )
516'
517
518test_expect_success 'relative path works with user@host:path' '
519 (
520 cd reltest &&
521 cp pristine-.git-config .git/config &&
522 git config remote.origin.url user@host:repo &&
523 git submodule init &&
524 test "$(git config submodule.sub.url)" = user@host:subrepo
525 )
526'
527
d75219b4
JL
528test_expect_success 'moving the superproject does not break submodules' '
529 (
530 cd addtest &&
531 git submodule status >expect
532 )
533 mv addtest addtest2 &&
534 (
535 cd addtest2 &&
536 git submodule status >actual &&
537 test_cmp expect actual
538 )
539'
540
88961ef2 541test_done