]>
Commit | Line | Data |
---|---|---|
88961ef2 LH |
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 | |
47a528ad | 9 | subcommands of git submodule. |
88961ef2 LH |
10 | ' |
11 | ||
12 | . ./test-lib.sh | |
13 | ||
fe454b13 JN |
14 | test_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 | ||
21 | test_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 | ||
33 | test_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 | ||
40 | test_expect_success 'setup - hide init subdirectory' ' | |
41 | mv init .subrepo | |
42 | ' | |
43 | ||
a76c944b | 44 | test_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. | |
53 | submodurl=$(pwd -P) | |
a76c944b JN |
54 | |
55 | listbranches() { | |
56 | git for-each-ref --format='%(refname)' 'refs/heads/*' | |
57 | } | |
58 | ||
59 | inspect() { | |
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 | |
74 | test_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 | 101 | test_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 |
118 | test_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 | 125 | test_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 | 146 | test_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 | 163 | test_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 | 180 | test_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 | 197 | test_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 |
214 | test_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 | 219 | test_expect_success 'status should fail for unmapped paths' ' |
ce8b54d6 JN |
220 | test_must_fail git submodule status |
221 | ' | |
222 | ||
223 | test_expect_success 'setup - map path in .gitmodules' ' | |
224 | cat <<\EOF >expect && | |
225 | [submodule "example"] | |
226 | url = git://example.com/init.git | |
227 | path = init | |
228 | EOF | |
229 | ||
230 | GIT_CONFIG=.gitmodules git config submodule.example.path init && | |
231 | ||
232 | test_cmp expect .gitmodules | |
88961ef2 LH |
233 | ' |
234 | ||
235 | test_expect_success 'status should only print one line' ' | |
ce8b54d6 JN |
236 | git submodule status >lines && |
237 | test $(wc -l <lines) = 1 | |
238 | ' | |
239 | ||
240 | test_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 | ||
246 | test_expect_success 'status should initially be "missing"' ' | |
ce8b54d6 JN |
247 | git submodule status >lines && |
248 | grep "^-$rev1" lines | |
88961ef2 LH |
249 | ' |
250 | ||
211b7f19 | 251 | test_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 | ||
261 | test_expect_success 'update should fail when path is used by a file' ' | |
ce8b54d6 JN |
262 | echo hello >expect && |
263 | ||
a2d93aea | 264 | echo "hello" >init && |
ce8b54d6 JN |
265 | test_must_fail git submodule update && |
266 | ||
267 | test_cmp expect init | |
88961ef2 LH |
268 | ' |
269 | ||
211b7f19 | 270 | test_expect_success 'update should fail when path is used by a nonempty directory' ' |
ce8b54d6 JN |
271 | echo hello >expect && |
272 | ||
273 | rm -fr init && | |
a2d93aea JH |
274 | mkdir init && |
275 | echo "hello" >init/a && | |
ce8b54d6 JN |
276 | |
277 | test_must_fail git submodule update && | |
278 | ||
279 | test_cmp expect init/a | |
88961ef2 LH |
280 | ' |
281 | ||
211b7f19 | 282 | test_expect_success 'update should work when path is an empty dir' ' |
ce8b54d6 JN |
283 | rm -fr init && |
284 | rm -f head-sha1 && | |
285 | echo "$rev1" >expect && | |
286 | ||
a2d93aea | 287 | mkdir init && |
7e60407f JL |
288 | git submodule update -q >update.out && |
289 | test ! -s update.out && | |
ce8b54d6 JN |
290 | |
291 | inspect init && | |
292 | test_cmp expect head-sha1 | |
88961ef2 LH |
293 | ' |
294 | ||
211b7f19 | 295 | test_expect_success 'status should be "up-to-date" after update' ' |
ce8b54d6 JN |
296 | git submodule status >list && |
297 | grep "^ $rev1" list | |
88961ef2 LH |
298 | ' |
299 | ||
300 | test_expect_success 'status should be "modified" after submodule commit' ' | |
ce8b54d6 JN |
301 | ( |
302 | cd init && | |
303 | echo b >b && | |
304 | git add b && | |
305 | git commit -m "submodule commit 2" | |
306 | ) && | |
307 | ||
308 | rev2=$(cd init && git rev-parse HEAD) && | |
309 | test -n "$rev2" && | |
310 | git submodule status >list && | |
311 | ||
312 | grep "^+$rev2" list | |
88961ef2 LH |
313 | ' |
314 | ||
315 | test_expect_success 'the --cached sha1 should be rev1' ' | |
ce8b54d6 JN |
316 | git submodule --cached status >list && |
317 | grep "^+$rev1" list | |
88961ef2 LH |
318 | ' |
319 | ||
5701115a | 320 | test_expect_success 'git diff should report the SHA1 of the new submodule commit' ' |
ce8b54d6 JN |
321 | git diff >diff && |
322 | grep "^+Subproject commit $rev2" diff | |
5701115a SV |
323 | ' |
324 | ||
88961ef2 | 325 | test_expect_success 'update should checkout rev1' ' |
ce8b54d6 JN |
326 | rm -f head-sha1 && |
327 | echo "$rev1" >expect && | |
328 | ||
47a528ad | 329 | git submodule update init && |
ce8b54d6 JN |
330 | inspect init && |
331 | ||
332 | test_cmp expect head-sha1 | |
88961ef2 LH |
333 | ' |
334 | ||
335 | test_expect_success 'status should be "up-to-date" after update' ' | |
ce8b54d6 JN |
336 | git submodule status >list && |
337 | grep "^ $rev1" list | |
88961ef2 LH |
338 | ' |
339 | ||
0cf73755 | 340 | test_expect_success 'checkout superproject with subproject already present' ' |
47a528ad NS |
341 | git checkout initial && |
342 | git checkout master | |
0cf73755 SV |
343 | ' |
344 | ||
e06c5a6c | 345 | test_expect_success 'apply submodule diff' ' |
ce8b54d6 JN |
346 | >empty && |
347 | ||
e06c5a6c SV |
348 | git branch second && |
349 | ( | |
a2d93aea | 350 | cd init && |
e06c5a6c SV |
351 | echo s >s && |
352 | git add s && | |
353 | git commit -m "change subproject" | |
354 | ) && | |
a2d93aea | 355 | git update-index --add init && |
47a528ad NS |
356 | git commit -m "change init" && |
357 | git format-patch -1 --stdout >P.diff && | |
e06c5a6c SV |
358 | git checkout second && |
359 | git apply --index P.diff && | |
ce8b54d6 JN |
360 | |
361 | git diff --cached master >staged && | |
362 | test_cmp empty staged | |
e06c5a6c SV |
363 | ' |
364 | ||
be4d2c83 | 365 | test_expect_success 'update --init' ' |
be4d2c83 JS |
366 | mv init init2 && |
367 | git config -f .gitmodules submodule.example.url "$(pwd)/init2" && | |
ce8b54d6 JN |
368 | git config --remove-section submodule.example && |
369 | test_must_fail git config submodule.example.url && | |
370 | ||
be4d2c83 | 371 | git submodule update init > update.out && |
ce8b54d6 | 372 | cat update.out && |
1c2ef66f | 373 | test_i18ngrep "not initialized" update.out && |
abc06822 | 374 | test_must_fail git rev-parse --resolve-git-dir init/.git && |
ce8b54d6 | 375 | |
be4d2c83 | 376 | git submodule update --init init && |
abc06822 | 377 | git rev-parse --resolve-git-dir init/.git |
be4d2c83 JS |
378 | ' |
379 | ||
2ce53f9b JS |
380 | test_expect_success 'do not add files from a submodule' ' |
381 | ||
382 | git reset --hard && | |
383 | test_must_fail git add init/a | |
384 | ||
385 | ' | |
386 | ||
387 | test_expect_success 'gracefully add submodule with a trailing slash' ' | |
388 | ||
389 | git reset --hard && | |
390 | git commit -m "commit subproject" init && | |
391 | (cd init && | |
392 | echo b > a) && | |
393 | git add init/ && | |
394 | git diff --exit-code --cached init && | |
395 | commit=$(cd init && | |
396 | git commit -m update a >/dev/null && | |
397 | git rev-parse HEAD) && | |
398 | git add init/ && | |
399 | test_must_fail git diff --exit-code --cached init && | |
400 | test $commit = $(git ls-files --stage | | |
401 | sed -n "s/^160000 \([^ ]*\).*/\1/p") | |
402 | ||
403 | ' | |
404 | ||
f3670a57 JS |
405 | test_expect_success 'ls-files gracefully handles trailing slash' ' |
406 | ||
407 | test "init" = "$(git ls-files init/)" | |
408 | ||
409 | ' | |
410 | ||
c5e558a8 PC |
411 | test_expect_success 'moving to a commit without submodule does not leave empty dir' ' |
412 | rm -rf init && | |
413 | mkdir init && | |
414 | git reset --hard && | |
415 | git checkout initial && | |
416 | test ! -d init && | |
417 | git checkout second | |
418 | ' | |
419 | ||
496917b7 JS |
420 | test_expect_success 'submodule <invalid-path> warns' ' |
421 | ||
422 | git submodule no-such-submodule 2> output.err && | |
423 | grep "^error: .*no-such-submodule" output.err | |
424 | ||
425 | ' | |
426 | ||
1414e578 JL |
427 | test_expect_success 'add submodules without specifying an explicit path' ' |
428 | mkdir repo && | |
18a82692 JN |
429 | ( |
430 | cd repo && | |
431 | git init && | |
432 | echo r >r && | |
433 | git add r && | |
434 | git commit -m "repo commit 1" | |
fd4ec4f2 | 435 | ) && |
1414e578 | 436 | git clone --bare repo/ bare.git && |
69e7236c JL |
437 | ( |
438 | cd addtest && | |
439 | git submodule add "$submodurl/repo" && | |
440 | git config -f .gitmodules submodule.repo.path repo && | |
441 | git submodule add "$submodurl/bare.git" && | |
442 | git config -f .gitmodules submodule.bare.path bare | |
443 | ) | |
444 | ' | |
445 | ||
446 | test_expect_success 'add should fail when path is used by a file' ' | |
447 | ( | |
448 | cd addtest && | |
449 | touch file && | |
450 | test_must_fail git submodule add "$submodurl/repo" file | |
451 | ) | |
452 | ' | |
453 | ||
454 | test_expect_success 'add should fail when path is used by an existing directory' ' | |
455 | ( | |
456 | cd addtest && | |
457 | mkdir empty-dir && | |
458 | test_must_fail git submodule add "$submodurl/repo" empty-dir | |
459 | ) | |
1414e578 JL |
460 | ' |
461 | ||
4d689320 | 462 | test_expect_success 'use superproject as upstream when path is relative and no url is set there' ' |
8537f0ef JL |
463 | ( |
464 | cd addtest && | |
4d689320 JL |
465 | git submodule add ../repo relative && |
466 | test "$(git config -f .gitmodules submodule.relative.url)" = ../repo && | |
467 | git submodule sync relative && | |
468 | test "$(git config submodule.relative.url)" = "$submodurl/repo" | |
8537f0ef JL |
469 | ) |
470 | ' | |
471 | ||
ea640cc6 TR |
472 | test_expect_success 'set up for relative path tests' ' |
473 | mkdir reltest && | |
474 | ( | |
475 | cd reltest && | |
476 | git init && | |
477 | mkdir sub && | |
478 | ( | |
479 | cd sub && | |
480 | git init && | |
481 | test_commit foo | |
482 | ) && | |
483 | git add sub && | |
484 | git config -f .gitmodules submodule.sub.path sub && | |
485 | git config -f .gitmodules submodule.sub.url ../subrepo && | |
486 | cp .git/config pristine-.git-config | |
487 | ) | |
488 | ' | |
489 | ||
490 | test_expect_success 'relative path works with URL' ' | |
491 | ( | |
492 | cd reltest && | |
493 | cp pristine-.git-config .git/config && | |
494 | git config remote.origin.url ssh://hostname/repo && | |
495 | git submodule init && | |
496 | test "$(git config submodule.sub.url)" = ssh://hostname/subrepo | |
497 | ) | |
498 | ' | |
499 | ||
500 | test_expect_success 'relative path works with user@host:path' ' | |
501 | ( | |
502 | cd reltest && | |
503 | cp pristine-.git-config .git/config && | |
504 | git config remote.origin.url user@host:repo && | |
505 | git submodule init && | |
506 | test "$(git config submodule.sub.url)" = user@host:subrepo | |
507 | ) | |
508 | ' | |
509 | ||
d75219b4 JL |
510 | test_expect_success 'moving the superproject does not break submodules' ' |
511 | ( | |
512 | cd addtest && | |
513 | git submodule status >expect | |
514 | ) | |
515 | mv addtest addtest2 && | |
516 | ( | |
517 | cd addtest2 && | |
518 | git submodule status >actual && | |
519 | test_cmp expect actual | |
520 | ) | |
521 | ' | |
522 | ||
88961ef2 | 523 | test_done |