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.
14 test_expect_success 'setup - initial commit' '
17 git commit -m "initial commit" &&
21 test_expect_success 'setup - repository in init subdirectory' '
28 git commit -m "submodule commit 1" &&
29 git tag -a -m "rev-1" rev-1
33 test_expect_success 'setup - commit with gitlink' '
37 git commit -m "super commit 1"
40 test_expect_success 'setup - hide init subdirectory' '
44 test_expect_success 'setup - repository to add submodules to' '
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.
51 submodurl=$TRASH_DIRECTORY
54 git for-each-ref --format='%(refname)' 'refs/heads/*'
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"
72 test_expect_success 'submodule add' '
73 echo "refs/heads/master" >expect &&
78 git submodule add "$submodurl" submod &&
82 rm -f heads head untracked &&
83 inspect addtest/submod ../.. &&
84 test_cmp expect heads &&
85 test_cmp expect head &&
86 test_cmp empty untracked
89 test_expect_success 'submodule add to .gitignored path fails' '
92 cat <<-\EOF >expect &&
93 The following path is ignored by one of your .gitignore files:
95 Use -f if you really want to add it.
97 # Does not use test_commit due to the ignore
98 echo "*" > .gitignore &&
99 git add --force .gitignore &&
100 git commit -m"Ignore everything" &&
101 ! git submodule add "$submodurl" submod >actual 2>&1 &&
102 test_cmp expect actual
106 test_expect_success 'submodule add to .gitignored path with --force' '
109 git submodule add --force "$submodurl" submod
113 test_expect_success 'submodule add --branch' '
114 echo "refs/heads/initial" >expect-head &&
115 cat <<-\EOF >expect-heads &&
123 git submodule add -b initial "$submodurl" submod-branch &&
127 rm -f heads head untracked &&
128 inspect addtest/submod-branch ../.. &&
129 test_cmp expect-heads heads &&
130 test_cmp expect-head head &&
131 test_cmp empty untracked
134 test_expect_success 'submodule add with ./ in path' '
135 echo "refs/heads/master" >expect &&
140 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
144 rm -f heads head untracked &&
145 inspect addtest/dotsubmod/frotz ../../.. &&
146 test_cmp expect heads &&
147 test_cmp expect head &&
148 test_cmp empty untracked
151 test_expect_success 'submodule add with // in path' '
152 echo "refs/heads/master" >expect &&
157 git submodule add "$submodurl" slashslashsubmod///frotz// &&
161 rm -f heads head untracked &&
162 inspect addtest/slashslashsubmod/frotz ../../.. &&
163 test_cmp expect heads &&
164 test_cmp expect head &&
165 test_cmp empty untracked
168 test_expect_success 'submodule add with /.. in path' '
169 echo "refs/heads/master" >expect &&
174 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
178 rm -f heads head untracked &&
179 inspect addtest/realsubmod ../.. &&
180 test_cmp expect heads &&
181 test_cmp expect head &&
182 test_cmp empty untracked
185 test_expect_success 'submodule add with ./, /.. and // in path' '
186 echo "refs/heads/master" >expect &&
191 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
195 rm -f heads head untracked &&
196 inspect addtest/realsubmod2 ../.. &&
197 test_cmp expect heads &&
198 test_cmp expect head &&
199 test_cmp empty untracked
202 test_expect_success 'setup - add an example entry to .gitmodules' '
203 GIT_CONFIG=.gitmodules \
204 git config submodule.example.url git://example.com/init.git
207 test_expect_success 'status should fail for unmapped paths' '
208 test_must_fail git submodule status
211 test_expect_success 'setup - map path in .gitmodules' '
212 cat <<\EOF >expect &&
213 [submodule "example"]
214 url = git://example.com/init.git
218 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
220 test_cmp expect .gitmodules
223 test_expect_success 'status should only print one line' '
224 git submodule status >lines &&
225 test $(wc -l <lines) = 1
228 test_expect_success 'setup - fetch commit name from submodule' '
229 rev1=$(cd .subrepo && git rev-parse HEAD) &&
230 printf "rev1: %s\n" "$rev1" &&
234 test_expect_success 'status should initially be "missing"' '
235 git submodule status >lines &&
239 test_expect_success 'init should register submodule url in .git/config' '
240 echo git://example.com/init.git >expect &&
242 git submodule init &&
243 git config submodule.example.url >url &&
244 git config submodule.example.url ./.subrepo &&
249 test_expect_success 'update should fail when path is used by a file' '
250 echo hello >expect &&
252 echo "hello" >init &&
253 test_must_fail git submodule update &&
258 test_expect_success 'update should fail when path is used by a nonempty directory' '
259 echo hello >expect &&
263 echo "hello" >init/a &&
265 test_must_fail git submodule update &&
267 test_cmp expect init/a
270 test_expect_success 'update should work when path is an empty dir' '
273 echo "$rev1" >expect &&
276 git submodule update &&
279 test_cmp expect head-sha1
282 test_expect_success 'status should be "up-to-date" after update' '
283 git submodule status >list &&
287 test_expect_success 'status should be "modified" after submodule commit' '
292 git commit -m "submodule commit 2"
295 rev2=$(cd init && git rev-parse HEAD) &&
297 git submodule status >list &&
302 test_expect_success 'the --cached sha1 should be rev1' '
303 git submodule --cached status >list &&
307 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
309 grep "^+Subproject commit $rev2" diff
312 test_expect_success 'update should checkout rev1' '
314 echo "$rev1" >expect &&
316 git submodule update init &&
319 test_cmp expect head-sha1
322 test_expect_success 'status should be "up-to-date" after update' '
323 git submodule status >list &&
327 test_expect_success 'checkout superproject with subproject already present' '
328 git checkout initial &&
332 test_expect_success 'apply submodule diff' '
340 git commit -m "change subproject"
342 git update-index --add init &&
343 git commit -m "change init" &&
344 git format-patch -1 --stdout >P.diff &&
345 git checkout second &&
346 git apply --index P.diff &&
348 git diff --cached master >staged &&
349 test_cmp empty staged
352 test_expect_success 'update --init' '
354 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
355 git config --remove-section submodule.example &&
356 test_must_fail git config submodule.example.url &&
358 git submodule update init > update.out &&
360 grep "not initialized" update.out &&
361 ! test -d init/.git &&
363 git submodule update --init init &&
367 test_expect_success 'do not add files from a submodule' '
370 test_must_fail git add init/a
374 test_expect_success 'gracefully add submodule with a trailing slash' '
377 git commit -m "commit subproject" init &&
381 git diff --exit-code --cached init &&
383 git commit -m update a >/dev/null &&
384 git rev-parse HEAD) &&
386 test_must_fail git diff --exit-code --cached init &&
387 test $commit = $(git ls-files --stage |
388 sed -n "s/^160000 \([^ ]*\).*/\1/p")
392 test_expect_success 'ls-files gracefully handles trailing slash' '
394 test "init" = "$(git ls-files init/)"
398 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
402 git checkout initial &&
407 test_expect_success 'submodule <invalid-path> warns' '
409 git submodule no-such-submodule 2> output.err &&
410 grep "^error: .*no-such-submodule" output.err
414 test_expect_success 'add submodules without specifying an explicit path' '
421 git commit -m "repo commit 1"
423 git clone --bare repo/ bare.git &&
426 git submodule add "$submodurl/repo" &&
427 git config -f .gitmodules submodule.repo.path repo &&
428 git submodule add "$submodurl/bare.git" &&
429 git config -f .gitmodules submodule.bare.path bare
433 test_expect_success 'add should fail when path is used by a file' '
437 test_must_fail git submodule add "$submodurl/repo" file
441 test_expect_success 'add should fail when path is used by an existing directory' '
445 test_must_fail git submodule add "$submodurl/repo" empty-dir
449 test_expect_success 'set up for relative path tests' '
461 git config -f .gitmodules submodule.sub.path sub &&
462 git config -f .gitmodules submodule.sub.url ../subrepo &&
463 cp .git/config pristine-.git-config
467 test_expect_success 'relative path works with URL' '
470 cp pristine-.git-config .git/config &&
471 git config remote.origin.url ssh://hostname/repo &&
472 git submodule init &&
473 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
477 test_expect_success 'relative path works with user@host:path' '
480 cp pristine-.git-config .git/config &&
481 git config remote.origin.url user@host:repo &&
482 git submodule init &&
483 test "$(git config submodule.sub.url)" = user@host:subrepo