Merge branch 'jc/maint-diff-patch-header' into maint
[alt-git.git] / t / t7400-submodule-basic.sh
blob695f7afdf31cc589e9c31d764d1e713ad64240c0
1 #!/bin/sh
3 # Copyright (c) 2007 Lars Hjemli
6 test_description='Basic porcelain support for submodules
8 This test tries to verify basic sanity of the init, update and status
9 subcommands of git submodule.
12 . ./test-lib.sh
14 test_expect_success 'setup - initial commit' '
15 >t &&
16 git add t &&
17 git commit -m "initial commit" &&
18 git branch initial
21 test_expect_success 'setup - repository in init subdirectory' '
22 mkdir init &&
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
33 test_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"
40 test_expect_success 'setup - hide init subdirectory' '
41 mv init .subrepo
44 test_expect_success 'setup - repository to add submodules to' '
45 git init addtest &&
46 git init addtest-ignore
49 # The 'submodule add' tests need some repository to add as a submodule.
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)
55 listbranches() {
56 git for-each-ref --format='%(refname)' 'refs/heads/*'
59 inspect() {
60 dir=$1 &&
61 dotdot="${2:-..}" &&
64 cd "$dir" &&
65 listbranches >"$dotdot/heads" &&
66 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
67 git rev-parse HEAD >"$dotdot/head-sha1" &&
68 git update-index --refresh &&
69 git diff-files --exit-code &&
70 git clean -n -d -x >"$dotdot/untracked"
74 test_expect_success 'submodule add' '
75 echo "refs/heads/master" >expect &&
76 >empty &&
79 cd addtest &&
80 git submodule add -q "$submodurl" submod >actual &&
81 test ! -s actual &&
82 git submodule init
83 ) &&
85 rm -f heads head untracked &&
86 inspect addtest/submod ../.. &&
87 test_cmp expect heads &&
88 test_cmp expect head &&
89 test_cmp empty untracked
92 test_expect_success 'submodule add to .gitignored path fails' '
94 cd addtest-ignore &&
95 cat <<-\EOF >expect &&
96 The following path is ignored by one of your .gitignore files:
97 submod
98 Use -f if you really want to add it.
99 EOF
100 # Does not use test_commit due to the ignore
101 echo "*" > .gitignore &&
102 git add --force .gitignore &&
103 git commit -m"Ignore everything" &&
104 ! git submodule add "$submodurl" submod >actual 2>&1 &&
105 test_i18ncmp expect actual
109 test_expect_success 'submodule add to .gitignored path with --force' '
111 cd addtest-ignore &&
112 git submodule add --force "$submodurl" submod
116 test_expect_success 'submodule add --branch' '
117 echo "refs/heads/initial" >expect-head &&
118 cat <<-\EOF >expect-heads &&
119 refs/heads/initial
120 refs/heads/master
122 >empty &&
125 cd addtest &&
126 git submodule add -b initial "$submodurl" submod-branch &&
127 git submodule init
128 ) &&
130 rm -f heads head untracked &&
131 inspect addtest/submod-branch ../.. &&
132 test_cmp expect-heads heads &&
133 test_cmp expect-head head &&
134 test_cmp empty untracked
137 test_expect_success 'submodule add with ./ in path' '
138 echo "refs/heads/master" >expect &&
139 >empty &&
142 cd addtest &&
143 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
144 git submodule init
145 ) &&
147 rm -f heads head untracked &&
148 inspect addtest/dotsubmod/frotz ../../.. &&
149 test_cmp expect heads &&
150 test_cmp expect head &&
151 test_cmp empty untracked
154 test_expect_success 'submodule add with // in path' '
155 echo "refs/heads/master" >expect &&
156 >empty &&
159 cd addtest &&
160 git submodule add "$submodurl" slashslashsubmod///frotz// &&
161 git submodule init
162 ) &&
164 rm -f heads head untracked &&
165 inspect addtest/slashslashsubmod/frotz ../../.. &&
166 test_cmp expect heads &&
167 test_cmp expect head &&
168 test_cmp empty untracked
171 test_expect_success 'submodule add with /.. in path' '
172 echo "refs/heads/master" >expect &&
173 >empty &&
176 cd addtest &&
177 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
178 git submodule init
179 ) &&
181 rm -f heads head untracked &&
182 inspect addtest/realsubmod ../.. &&
183 test_cmp expect heads &&
184 test_cmp expect head &&
185 test_cmp empty untracked
188 test_expect_success 'submodule add with ./, /.. and // in path' '
189 echo "refs/heads/master" >expect &&
190 >empty &&
193 cd addtest &&
194 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
195 git submodule init
196 ) &&
198 rm -f heads head untracked &&
199 inspect addtest/realsubmod2 ../.. &&
200 test_cmp expect heads &&
201 test_cmp expect head &&
202 test_cmp empty untracked
205 test_expect_success 'setup - add an example entry to .gitmodules' '
206 GIT_CONFIG=.gitmodules \
207 git config submodule.example.url git://example.com/init.git
210 test_expect_success 'status should fail for unmapped paths' '
211 test_must_fail git submodule status
214 test_expect_success 'setup - map path in .gitmodules' '
215 cat <<\EOF >expect &&
216 [submodule "example"]
217 url = git://example.com/init.git
218 path = init
221 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
223 test_cmp expect .gitmodules
226 test_expect_success 'status should only print one line' '
227 git submodule status >lines &&
228 test $(wc -l <lines) = 1
231 test_expect_success 'setup - fetch commit name from submodule' '
232 rev1=$(cd .subrepo && git rev-parse HEAD) &&
233 printf "rev1: %s\n" "$rev1" &&
234 test -n "$rev1"
237 test_expect_success 'status should initially be "missing"' '
238 git submodule status >lines &&
239 grep "^-$rev1" lines
242 test_expect_success 'init should register submodule url in .git/config' '
243 echo git://example.com/init.git >expect &&
245 git submodule init &&
246 git config submodule.example.url >url &&
247 git config submodule.example.url ./.subrepo &&
249 test_cmp expect url
252 test_expect_success 'update should fail when path is used by a file' '
253 echo hello >expect &&
255 echo "hello" >init &&
256 test_must_fail git submodule update &&
258 test_cmp expect init
261 test_expect_success 'update should fail when path is used by a nonempty directory' '
262 echo hello >expect &&
264 rm -fr init &&
265 mkdir init &&
266 echo "hello" >init/a &&
268 test_must_fail git submodule update &&
270 test_cmp expect init/a
273 test_expect_success 'update should work when path is an empty dir' '
274 rm -fr init &&
275 rm -f head-sha1 &&
276 echo "$rev1" >expect &&
278 mkdir init &&
279 git submodule update -q >update.out &&
280 test ! -s update.out &&
282 inspect init &&
283 test_cmp expect head-sha1
286 test_expect_success 'status should be "up-to-date" after update' '
287 git submodule status >list &&
288 grep "^ $rev1" list
291 test_expect_success 'status should be "modified" after submodule commit' '
293 cd init &&
294 echo b >b &&
295 git add b &&
296 git commit -m "submodule commit 2"
297 ) &&
299 rev2=$(cd init && git rev-parse HEAD) &&
300 test -n "$rev2" &&
301 git submodule status >list &&
303 grep "^+$rev2" list
306 test_expect_success 'the --cached sha1 should be rev1' '
307 git submodule --cached status >list &&
308 grep "^+$rev1" list
311 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
312 git diff >diff &&
313 grep "^+Subproject commit $rev2" diff
316 test_expect_success 'update should checkout rev1' '
317 rm -f head-sha1 &&
318 echo "$rev1" >expect &&
320 git submodule update init &&
321 inspect init &&
323 test_cmp expect head-sha1
326 test_expect_success 'status should be "up-to-date" after update' '
327 git submodule status >list &&
328 grep "^ $rev1" list
331 test_expect_success 'checkout superproject with subproject already present' '
332 git checkout initial &&
333 git checkout master
336 test_expect_success 'apply submodule diff' '
337 >empty &&
339 git branch second &&
341 cd init &&
342 echo s >s &&
343 git add s &&
344 git commit -m "change subproject"
345 ) &&
346 git update-index --add init &&
347 git commit -m "change init" &&
348 git format-patch -1 --stdout >P.diff &&
349 git checkout second &&
350 git apply --index P.diff &&
352 git diff --cached master >staged &&
353 test_cmp empty staged
356 test_expect_success 'update --init' '
357 mv init init2 &&
358 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
359 git config --remove-section submodule.example &&
360 test_must_fail git config submodule.example.url &&
362 git submodule update init > update.out &&
363 cat update.out &&
364 test_i18ngrep "not initialized" update.out &&
365 test_must_fail git rev-parse --resolve-git-dir init/.git &&
367 git submodule update --init init &&
368 git rev-parse --resolve-git-dir init/.git
371 test_expect_success 'do not add files from a submodule' '
373 git reset --hard &&
374 test_must_fail git add init/a
378 test_expect_success 'gracefully add submodule with a trailing slash' '
380 git reset --hard &&
381 git commit -m "commit subproject" init &&
382 (cd init &&
383 echo b > a) &&
384 git add init/ &&
385 git diff --exit-code --cached init &&
386 commit=$(cd init &&
387 git commit -m update a >/dev/null &&
388 git rev-parse HEAD) &&
389 git add init/ &&
390 test_must_fail git diff --exit-code --cached init &&
391 test $commit = $(git ls-files --stage |
392 sed -n "s/^160000 \([^ ]*\).*/\1/p")
396 test_expect_success 'ls-files gracefully handles trailing slash' '
398 test "init" = "$(git ls-files init/)"
402 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
403 rm -rf init &&
404 mkdir init &&
405 git reset --hard &&
406 git checkout initial &&
407 test ! -d init &&
408 git checkout second
411 test_expect_success 'submodule <invalid-path> warns' '
413 git submodule no-such-submodule 2> output.err &&
414 grep "^error: .*no-such-submodule" output.err
418 test_expect_success 'add submodules without specifying an explicit path' '
419 mkdir repo &&
421 cd repo &&
422 git init &&
423 echo r >r &&
424 git add r &&
425 git commit -m "repo commit 1"
426 ) &&
427 git clone --bare repo/ bare.git &&
429 cd addtest &&
430 git submodule add "$submodurl/repo" &&
431 git config -f .gitmodules submodule.repo.path repo &&
432 git submodule add "$submodurl/bare.git" &&
433 git config -f .gitmodules submodule.bare.path bare
437 test_expect_success 'add should fail when path is used by a file' '
439 cd addtest &&
440 touch file &&
441 test_must_fail git submodule add "$submodurl/repo" file
445 test_expect_success 'add should fail when path is used by an existing directory' '
447 cd addtest &&
448 mkdir empty-dir &&
449 test_must_fail git submodule add "$submodurl/repo" empty-dir
453 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
455 cd addtest &&
456 git submodule add ../repo relative &&
457 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
458 git submodule sync relative &&
459 test "$(git config submodule.relative.url)" = "$submodurl/repo"
463 test_expect_success 'set up for relative path tests' '
464 mkdir reltest &&
466 cd reltest &&
467 git init &&
468 mkdir sub &&
470 cd sub &&
471 git init &&
472 test_commit foo
473 ) &&
474 git add sub &&
475 git config -f .gitmodules submodule.sub.path sub &&
476 git config -f .gitmodules submodule.sub.url ../subrepo &&
477 cp .git/config pristine-.git-config
481 test_expect_success 'relative path works with URL' '
483 cd reltest &&
484 cp pristine-.git-config .git/config &&
485 git config remote.origin.url ssh://hostname/repo &&
486 git submodule init &&
487 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
491 test_expect_success 'relative path works with user@host:path' '
493 cd reltest &&
494 cp pristine-.git-config .git/config &&
495 git config remote.origin.url user@host:repo &&
496 git submodule init &&
497 test "$(git config submodule.sub.url)" = user@host:subrepo
501 test_done