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