blob: 76a7cb0af7955aa618556a43cc9f2a568d8de3e8 [file] [log] [blame]
Lars Hjemli88961ef2007-06-02 03:27:42 +02001#!/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
Nanako Shiraishi47a528a2008-09-03 17:59:33 +09009subcommands of git submodule.
Lars Hjemli88961ef2007-06-02 03:27:42 +020010'
11
12. ./test-lib.sh
13
Stefan Bellerf6a52792016-05-05 12:52:32 -070014test_expect_success 'submodule deinit works on empty repository' '
15 git submodule deinit --all
16'
17
Jonathan Niederfe454b12010-04-10 00:38:37 -050018test_expect_success 'setup - initial commit' '
19 >t &&
Nanako Shiraishi47a528a2008-09-03 17:59:33 +090020 git add t &&
21 git commit -m "initial commit" &&
Jonathan Niederfe454b12010-04-10 00:38:37 -050022 git branch initial
23'
24
Stefan Bellerd92028a2016-04-28 13:02:45 -070025test_expect_success 'submodule init aborts on missing .gitmodules file' '
26 test_when_finished "git update-index --remove sub" &&
27 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
28 # missing the .gitmodules file here
29 test_must_fail git submodule init 2>actual &&
30 test_i18ngrep "No url found for submodule path" actual
31'
32
Stefan Beller08fdbdb2016-04-28 13:02:46 -070033test_expect_success 'submodule update aborts on missing .gitmodules file' '
34 test_when_finished "git update-index --remove sub" &&
35 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
36 # missing the .gitmodules file here
37 git submodule update sub 2>actual &&
38 test_i18ngrep "Submodule path .sub. not initialized" actual
39'
40
Jeff King627fde12017-04-24 20:57:47 -040041test_expect_success 'submodule update aborts on missing gitmodules url' '
42 test_when_finished "git update-index --remove sub" &&
43 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
44 test_when_finished "rm -f .gitmodules" &&
45 git config -f .gitmodules submodule.s.path sub &&
46 test_must_fail git submodule init
47'
48
Jonathan Niederfe454b12010-04-10 00:38:37 -050049test_expect_success 'setup - repository in init subdirectory' '
Junio C Hamanoa2d93ae2008-01-15 03:13:55 -080050 mkdir init &&
Jonathan Niederfe454b12010-04-10 00:38:37 -050051 (
52 cd init &&
53 git init &&
54 echo a >a &&
55 git add a &&
56 git commit -m "submodule commit 1" &&
57 git tag -a -m "rev-1" rev-1
58 )
Jonathan Niederfe454b12010-04-10 00:38:37 -050059'
60
61test_expect_success 'setup - commit with gitlink' '
Lars Hjemli88961ef2007-06-02 03:27:42 +020062 echo a >a &&
63 echo z >z &&
Junio C Hamanoa2d93ae2008-01-15 03:13:55 -080064 git add a init z &&
Jonathan Niederfe454b12010-04-10 00:38:37 -050065 git commit -m "super commit 1"
66'
67
68test_expect_success 'setup - hide init subdirectory' '
69 mv init .subrepo
70'
71
Jonathan Niedera76c9442010-04-10 00:39:04 -050072test_expect_success 'setup - repository to add submodules to' '
Ævar Arnfjörð Bjarmason31991b02010-07-05 17:33:03 +000073 git init addtest &&
74 git init addtest-ignore
Michael J Gruberac8463d2009-03-03 16:08:20 +010075'
76
Jonathan Niedera76c9442010-04-10 00:39:04 -050077# The 'submodule add' tests need some repository to add as a submodule.
Jeff Kingdd008b32011-07-30 09:05:54 -060078# The trash directory is a good one as any. We need to canonicalize
79# the name, though, as some tests compare it to the absolute path git
80# generates, which will expand symbolic links.
81submodurl=$(pwd -P)
Jonathan Niedera76c9442010-04-10 00:39:04 -050082
83listbranches() {
84 git for-each-ref --format='%(refname)' 'refs/heads/*'
85}
86
87inspect() {
88 dir=$1 &&
89 dotdot="${2:-..}" &&
90
91 (
92 cd "$dir" &&
93 listbranches >"$dotdot/heads" &&
94 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
Jonathan Niederce8b54d2010-04-10 00:39:41 -050095 git rev-parse HEAD >"$dotdot/head-sha1" &&
Jonathan Niedera76c9442010-04-10 00:39:04 -050096 git update-index --refresh &&
97 git diff-files --exit-code &&
98 git clean -n -d -x >"$dotdot/untracked"
99 )
100}
101
Michael J Gruberac8463d2009-03-03 16:08:20 +0100102test_expect_success 'submodule add' '
Jonathan Niedera76c9442010-04-10 00:39:04 -0500103 echo "refs/heads/master" >expect &&
Jonathan Niedera76c9442010-04-10 00:39:04 -0500104
Michael J Gruberac8463d2009-03-03 16:08:20 +0100105 (
106 cd addtest &&
Jens Lehmann7e604072011-07-26 23:39:03 +0200107 git submodule add -q "$submodurl" submod >actual &&
Junio C Hamanoca8d1482013-06-09 11:29:20 -0700108 test_must_be_empty actual &&
Jens Lehmannea115a02012-03-04 22:14:30 +0100109 echo "gitdir: ../.git/modules/submod" >expect &&
110 test_cmp expect submod/.git &&
Jens Lehmannd75219b2012-03-04 22:15:08 +0100111 (
112 cd submod &&
113 git config core.worktree >actual &&
114 echo "../../../submod" >expect &&
115 test_cmp expect actual &&
116 rm -f actual expect
117 ) &&
Michael J Gruberac8463d2009-03-03 16:08:20 +0100118 git submodule init
Jonathan Niedera76c9442010-04-10 00:39:04 -0500119 ) &&
120
121 rm -f heads head untracked &&
122 inspect addtest/submod ../.. &&
123 test_cmp expect heads &&
124 test_cmp expect head &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200125 test_must_be_empty untracked
Michael J Gruberac8463d2009-03-03 16:08:20 +0100126'
127
Stefan Beller83f5fa52018-06-18 16:41:48 -0700128test_expect_success 'setup parent and one repository' '
129 test_create_repo parent &&
130 test_commit -C parent one
131'
Casey Fitzpatrick6d33e1c2018-05-03 06:53:45 -0400132
133test_expect_success 'redirected submodule add does not show progress' '
134 git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
135 2>err &&
136 ! grep % err &&
137 test_i18ngrep ! "Checking connectivity" err
138'
139
140test_expect_success 'redirected submodule add --progress does show progress' '
141 git -C addtest submodule add --progress "file://$submodurl/parent" \
142 submod-redirected-progress 2>err && \
143 grep % err
144'
145
Jens Lehmannd27b8762010-07-17 17:11:43 +0200146test_expect_success 'submodule add to .gitignored path fails' '
Ævar Arnfjörð Bjarmason31991b02010-07-05 17:33:03 +0000147 (
148 cd addtest-ignore &&
Jens Lehmannd27b8762010-07-17 17:11:43 +0200149 cat <<-\EOF >expect &&
150 The following path is ignored by one of your .gitignore files:
151 submod
152 Use -f if you really want to add it.
153 EOF
Ævar Arnfjörð Bjarmason31991b02010-07-05 17:33:03 +0000154 # Does not use test_commit due to the ignore
155 echo "*" > .gitignore &&
156 git add --force .gitignore &&
157 git commit -m"Ignore everything" &&
Jens Lehmannd27b8762010-07-17 17:11:43 +0200158 ! git submodule add "$submodurl" submod >actual 2>&1 &&
Ævar Arnfjörð Bjarmason3a4c3ed2011-05-21 18:44:07 +0000159 test_i18ncmp expect actual
Jens Lehmannd27b8762010-07-17 17:11:43 +0200160 )
161'
Ævar Arnfjörð Bjarmason31991b02010-07-05 17:33:03 +0000162
Jens Lehmannd27b8762010-07-17 17:11:43 +0200163test_expect_success 'submodule add to .gitignored path with --force' '
164 (
165 cd addtest-ignore &&
166 git submodule add --force "$submodurl" submod
167 )
Ævar Arnfjörð Bjarmason31991b02010-07-05 17:33:03 +0000168'
169
Stefan Beller619acfc2016-10-06 12:37:24 -0700170test_expect_success 'submodule add to reconfigure existing submodule with --force' '
171 (
172 cd addtest-ignore &&
Eric Sunshineadc73312018-07-01 20:23:51 -0400173 bogus_url="$(pwd)/bogus-url" &&
174 git submodule add --force "$bogus_url" submod &&
175 git submodule add --force -b initial "$submodurl" submod-branch &&
176 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
177 test "$bogus_url" = "$(git config submodule.submod.url)" &&
Stefan Beller619acfc2016-10-06 12:37:24 -0700178 # Restore the url
Eric Sunshineadc73312018-07-01 20:23:51 -0400179 git submodule add --force "$submodurl" submod &&
Stefan Beller619acfc2016-10-06 12:37:24 -0700180 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
181 test "$submodurl" = "$(git config submodule.submod.url)"
182 )
183'
184
Ben Jacksonea10b602009-04-18 20:42:07 -0700185test_expect_success 'submodule add --branch' '
Jonathan Niedera76c9442010-04-10 00:39:04 -0500186 echo "refs/heads/initial" >expect-head &&
187 cat <<-\EOF >expect-heads &&
188 refs/heads/initial
189 refs/heads/master
190 EOF
Jonathan Niedera76c9442010-04-10 00:39:04 -0500191
Ben Jacksonea10b602009-04-18 20:42:07 -0700192 (
193 cd addtest &&
194 git submodule add -b initial "$submodurl" submod-branch &&
W. Trevor Kingb9289222012-12-19 11:03:33 -0500195 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
Jonathan Niedera76c9442010-04-10 00:39:04 -0500196 git submodule init
197 ) &&
198
199 rm -f heads head untracked &&
200 inspect addtest/submod-branch ../.. &&
201 test_cmp expect-heads heads &&
202 test_cmp expect-head head &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200203 test_must_be_empty untracked
Ben Jacksonea10b602009-04-18 20:42:07 -0700204'
205
Michael J Gruberdb75ada2009-03-03 16:08:21 +0100206test_expect_success 'submodule add with ./ in path' '
Jonathan Niedera76c9442010-04-10 00:39:04 -0500207 echo "refs/heads/master" >expect &&
Jonathan Niedera76c9442010-04-10 00:39:04 -0500208
Michael J Gruberac8463d2009-03-03 16:08:20 +0100209 (
210 cd addtest &&
211 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
212 git submodule init
Jonathan Niedera76c9442010-04-10 00:39:04 -0500213 ) &&
214
215 rm -f heads head untracked &&
216 inspect addtest/dotsubmod/frotz ../../.. &&
217 test_cmp expect heads &&
218 test_cmp expect head &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200219 test_must_be_empty untracked
Michael J Gruberac8463d2009-03-03 16:08:20 +0100220'
221
Patrick Steinhardt8196e722015-01-30 16:14:03 +0100222test_expect_success 'submodule add with /././ in path' '
223 echo "refs/heads/master" >expect &&
Patrick Steinhardt8196e722015-01-30 16:14:03 +0100224
225 (
226 cd addtest &&
227 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
228 git submodule init
229 ) &&
230
231 rm -f heads head untracked &&
232 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
233 test_cmp expect heads &&
234 test_cmp expect head &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200235 test_must_be_empty untracked
Patrick Steinhardt8196e722015-01-30 16:14:03 +0100236'
237
Michael J Gruberdb75ada2009-03-03 16:08:21 +0100238test_expect_success 'submodule add with // in path' '
Jonathan Niedera76c9442010-04-10 00:39:04 -0500239 echo "refs/heads/master" >expect &&
Jonathan Niedera76c9442010-04-10 00:39:04 -0500240
Michael J Gruberac8463d2009-03-03 16:08:20 +0100241 (
242 cd addtest &&
243 git submodule add "$submodurl" slashslashsubmod///frotz// &&
244 git submodule init
Jonathan Niedera76c9442010-04-10 00:39:04 -0500245 ) &&
246
247 rm -f heads head untracked &&
248 inspect addtest/slashslashsubmod/frotz ../../.. &&
249 test_cmp expect heads &&
250 test_cmp expect head &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200251 test_must_be_empty untracked
Michael J Gruberac8463d2009-03-03 16:08:20 +0100252'
253
Michael J Gruberdb75ada2009-03-03 16:08:21 +0100254test_expect_success 'submodule add with /.. in path' '
Jonathan Niedera76c9442010-04-10 00:39:04 -0500255 echo "refs/heads/master" >expect &&
Jonathan Niedera76c9442010-04-10 00:39:04 -0500256
Michael J Gruberac8463d2009-03-03 16:08:20 +0100257 (
258 cd addtest &&
259 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
260 git submodule init
Jonathan Niedera76c9442010-04-10 00:39:04 -0500261 ) &&
262
263 rm -f heads head untracked &&
264 inspect addtest/realsubmod ../.. &&
265 test_cmp expect heads &&
266 test_cmp expect head &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200267 test_must_be_empty untracked
Michael J Gruberac8463d2009-03-03 16:08:20 +0100268'
269
Michael J Gruberdb75ada2009-03-03 16:08:21 +0100270test_expect_success 'submodule add with ./, /.. and // in path' '
Jonathan Niedera76c9442010-04-10 00:39:04 -0500271 echo "refs/heads/master" >expect &&
Jonathan Niedera76c9442010-04-10 00:39:04 -0500272
Michael J Gruberac8463d2009-03-03 16:08:20 +0100273 (
274 cd addtest &&
275 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
276 git submodule init
Jonathan Niedera76c9442010-04-10 00:39:04 -0500277 ) &&
278
279 rm -f heads head untracked &&
280 inspect addtest/realsubmod2 ../.. &&
281 test_cmp expect heads &&
282 test_cmp expect head &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200283 test_must_be_empty untracked
Michael J Gruberac8463d2009-03-03 16:08:20 +0100284'
285
Ramsay Jones7f1b2252017-04-30 18:29:30 +0100286test_expect_success !CYGWIN 'submodule add with \\ in path' '
Brandon Williamscf9e55f2017-04-07 10:23:06 -0700287 test_when_finished "rm -rf parent sub\\with\\backslash" &&
288
289 # Initialize a repo with a backslash in its name
290 git init sub\\with\\backslash &&
291 touch sub\\with\\backslash/empty.file &&
292 git -C sub\\with\\backslash add empty.file &&
293 git -C sub\\with\\backslash commit -m "Added empty.file" &&
294
295 # Add that repository as a submodule
296 git init parent &&
297 git -C parent submodule add ../sub\\with\\backslash
298'
299
John Keeping091a6eb2013-06-16 15:18:18 +0100300test_expect_success 'submodule add in subdirectory' '
301 echo "refs/heads/master" >expect &&
John Keeping091a6eb2013-06-16 15:18:18 +0100302
303 mkdir addtest/sub &&
304 (
305 cd addtest/sub &&
306 git submodule add "$submodurl" ../realsubmod3 &&
307 git submodule init
308 ) &&
309
310 rm -f heads head untracked &&
311 inspect addtest/realsubmod3 ../.. &&
312 test_cmp expect heads &&
313 test_cmp expect head &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200314 test_must_be_empty untracked
John Keeping091a6eb2013-06-16 15:18:18 +0100315'
316
317test_expect_success 'submodule add in subdirectory with relative path should fail' '
318 (
319 cd addtest/sub &&
320 test_must_fail git submodule add ../../ submod3 2>../../output.err
321 ) &&
322 test_i18ngrep toplevel output.err
323'
324
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500325test_expect_success 'setup - add an example entry to .gitmodules' '
Jeff Kingf7e87142014-03-20 19:17:01 -0400326 git config --file=.gitmodules submodule.example.url git://example.com/init.git
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500327'
328
Lars Hjemli941987a2007-06-11 21:12:24 +0200329test_expect_success 'status should fail for unmapped paths' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500330 test_must_fail git submodule status
331'
332
333test_expect_success 'setup - map path in .gitmodules' '
334 cat <<\EOF >expect &&
335[submodule "example"]
336 url = git://example.com/init.git
337 path = init
338EOF
339
Jeff Kingf7e87142014-03-20 19:17:01 -0400340 git config --file=.gitmodules submodule.example.path init &&
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500341
342 test_cmp expect .gitmodules
Lars Hjemli88961ef2007-06-02 03:27:42 +0200343'
344
345test_expect_success 'status should only print one line' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500346 git submodule status >lines &&
Stefano Lattarini3fb04592012-04-11 13:24:01 +0200347 test_line_count = 1 lines
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500348'
349
350test_expect_success 'setup - fetch commit name from submodule' '
351 rev1=$(cd .subrepo && git rev-parse HEAD) &&
352 printf "rev1: %s\n" "$rev1" &&
353 test -n "$rev1"
Lars Hjemli88961ef2007-06-02 03:27:42 +0200354'
355
356test_expect_success 'status should initially be "missing"' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500357 git submodule status >lines &&
358 grep "^-$rev1" lines
Lars Hjemli88961ef2007-06-02 03:27:42 +0200359'
360
Lars Hjemli211b7f12007-06-06 11:13:02 +0200361test_expect_success 'init should register submodule url in .git/config' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500362 echo git://example.com/init.git >expect &&
363
Nanako Shiraishi47a528a2008-09-03 17:59:33 +0900364 git submodule init &&
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500365 git config submodule.example.url >url &&
366 git config submodule.example.url ./.subrepo &&
367
368 test_cmp expect url
Lars Hjemli211b7f12007-06-06 11:13:02 +0200369'
370
Heiko Voigtbe9d0a32012-08-14 22:35:27 +0200371test_failure_with_unknown_submodule () {
372 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
Nguyễn Thái Ngọc Duya80897c2018-07-21 09:49:30 +0200373 test_i18ngrep "^error: .*no-such-submodule" output.err
Heiko Voigtbe9d0a32012-08-14 22:35:27 +0200374}
375
376test_expect_success 'init should fail with unknown submodule' '
377 test_failure_with_unknown_submodule init
378'
379
380test_expect_success 'update should fail with unknown submodule' '
381 test_failure_with_unknown_submodule update
382'
383
384test_expect_success 'status should fail with unknown submodule' '
385 test_failure_with_unknown_submodule status
386'
387
388test_expect_success 'sync should fail with unknown submodule' '
389 test_failure_with_unknown_submodule sync
390'
391
Lars Hjemli211b7f12007-06-06 11:13:02 +0200392test_expect_success 'update should fail when path is used by a file' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500393 echo hello >expect &&
394
Junio C Hamanoa2d93ae2008-01-15 03:13:55 -0800395 echo "hello" >init &&
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500396 test_must_fail git submodule update &&
397
398 test_cmp expect init
Lars Hjemli88961ef2007-06-02 03:27:42 +0200399'
400
Lars Hjemli211b7f12007-06-06 11:13:02 +0200401test_expect_success 'update should fail when path is used by a nonempty directory' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500402 echo hello >expect &&
403
404 rm -fr init &&
Junio C Hamanoa2d93ae2008-01-15 03:13:55 -0800405 mkdir init &&
406 echo "hello" >init/a &&
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500407
408 test_must_fail git submodule update &&
409
410 test_cmp expect init/a
Lars Hjemli88961ef2007-06-02 03:27:42 +0200411'
412
Lars Hjemli211b7f12007-06-06 11:13:02 +0200413test_expect_success 'update should work when path is an empty dir' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500414 rm -fr init &&
415 rm -f head-sha1 &&
416 echo "$rev1" >expect &&
417
Junio C Hamanoa2d93ae2008-01-15 03:13:55 -0800418 mkdir init &&
Jens Lehmann7e604072011-07-26 23:39:03 +0200419 git submodule update -q >update.out &&
Junio C Hamanoca8d1482013-06-09 11:29:20 -0700420 test_must_be_empty update.out &&
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500421
422 inspect init &&
423 test_cmp expect head-sha1
Lars Hjemli88961ef2007-06-02 03:27:42 +0200424'
425
Lars Hjemli211b7f12007-06-06 11:13:02 +0200426test_expect_success 'status should be "up-to-date" after update' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500427 git submodule status >list &&
428 grep "^ $rev1" list
Lars Hjemli88961ef2007-06-02 03:27:42 +0200429'
430
John Keeping091a6eb2013-06-16 15:18:18 +0100431test_expect_success 'status "up-to-date" from subdirectory' '
432 mkdir -p sub &&
433 (
434 cd sub &&
435 git submodule status >../list
436 ) &&
437 grep "^ $rev1" list &&
438 grep "\\.\\./init" list
439'
440
441test_expect_success 'status "up-to-date" from subdirectory with path' '
442 mkdir -p sub &&
443 (
444 cd sub &&
445 git submodule status ../init >../list
446 ) &&
447 grep "^ $rev1" list &&
448 grep "\\.\\./init" list
449'
450
Lars Hjemli88961ef2007-06-02 03:27:42 +0200451test_expect_success 'status should be "modified" after submodule commit' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500452 (
453 cd init &&
454 echo b >b &&
455 git add b &&
456 git commit -m "submodule commit 2"
457 ) &&
458
459 rev2=$(cd init && git rev-parse HEAD) &&
460 test -n "$rev2" &&
461 git submodule status >list &&
462
463 grep "^+$rev2" list
Lars Hjemli88961ef2007-06-02 03:27:42 +0200464'
465
466test_expect_success 'the --cached sha1 should be rev1' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500467 git submodule --cached status >list &&
468 grep "^+$rev1" list
Lars Hjemli88961ef2007-06-02 03:27:42 +0200469'
470
Sven Verdoolaege57011152007-09-08 12:30:22 +0200471test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500472 git diff >diff &&
473 grep "^+Subproject commit $rev2" diff
Sven Verdoolaege57011152007-09-08 12:30:22 +0200474'
475
Lars Hjemli88961ef2007-06-02 03:27:42 +0200476test_expect_success 'update should checkout rev1' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500477 rm -f head-sha1 &&
478 echo "$rev1" >expect &&
479
Nanako Shiraishi47a528a2008-09-03 17:59:33 +0900480 git submodule update init &&
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500481 inspect init &&
482
483 test_cmp expect head-sha1
Lars Hjemli88961ef2007-06-02 03:27:42 +0200484'
485
486test_expect_success 'status should be "up-to-date" after update' '
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500487 git submodule status >list &&
488 grep "^ $rev1" list
Lars Hjemli88961ef2007-06-02 03:27:42 +0200489'
490
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200491test_expect_success 'checkout superproject with subproject already present' '
Nanako Shiraishi47a528a2008-09-03 17:59:33 +0900492 git checkout initial &&
493 git checkout master
Sven Verdoolaege0cf73752007-07-17 20:28:28 +0200494'
495
Sven Verdoolaegee06c5a62007-08-15 19:22:09 +0200496test_expect_success 'apply submodule diff' '
497 git branch second &&
498 (
Junio C Hamanoa2d93ae2008-01-15 03:13:55 -0800499 cd init &&
Sven Verdoolaegee06c5a62007-08-15 19:22:09 +0200500 echo s >s &&
501 git add s &&
502 git commit -m "change subproject"
503 ) &&
Junio C Hamanoa2d93ae2008-01-15 03:13:55 -0800504 git update-index --add init &&
Nanako Shiraishi47a528a2008-09-03 17:59:33 +0900505 git commit -m "change init" &&
506 git format-patch -1 --stdout >P.diff &&
Sven Verdoolaegee06c5a62007-08-15 19:22:09 +0200507 git checkout second &&
508 git apply --index P.diff &&
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500509
510 git diff --cached master >staged &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200511 test_must_be_empty staged
Sven Verdoolaegee06c5a62007-08-15 19:22:09 +0200512'
513
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100514test_expect_success 'update --init' '
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100515 mv init init2 &&
516 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500517 git config --remove-section submodule.example &&
518 test_must_fail git config submodule.example.url &&
519
Stefan Beller8c6b5492016-02-29 18:07:14 -0800520 git submodule update init 2> update.out &&
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500521 cat update.out &&
Ævar Arnfjörð Bjarmason1c2ef662011-05-21 18:44:08 +0000522 test_i18ngrep "not initialized" update.out &&
Fredrik Gustafssonabc06822011-08-15 23:17:46 +0200523 test_must_fail git rev-parse --resolve-git-dir init/.git &&
Jonathan Niederce8b54d2010-04-10 00:39:41 -0500524
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100525 git submodule update --init init &&
Fredrik Gustafssonabc06822011-08-15 23:17:46 +0200526 git rev-parse --resolve-git-dir init/.git
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100527'
528
John Keeping091a6eb2013-06-16 15:18:18 +0100529test_expect_success 'update --init from subdirectory' '
530 mv init init2 &&
531 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
532 git config --remove-section submodule.example &&
533 test_must_fail git config submodule.example.url &&
534
535 mkdir -p sub &&
536 (
537 cd sub &&
Stefan Beller8c6b5492016-02-29 18:07:14 -0800538 git submodule update ../init 2>update.out &&
John Keeping091a6eb2013-06-16 15:18:18 +0100539 cat update.out &&
540 test_i18ngrep "not initialized" update.out &&
541 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
542
543 git submodule update --init ../init
544 ) &&
545 git rev-parse --resolve-git-dir init/.git
546'
547
Johannes Schindelin2ce53f92009-01-02 19:08:40 +0100548test_expect_success 'do not add files from a submodule' '
549
550 git reset --hard &&
551 test_must_fail git add init/a
552
553'
554
John Keeping2c63d6e2013-09-12 20:25:01 +0100555test_expect_success 'gracefully add/reset submodule with a trailing slash' '
Johannes Schindelin2ce53f92009-01-02 19:08:40 +0100556
557 git reset --hard &&
558 git commit -m "commit subproject" init &&
559 (cd init &&
560 echo b > a) &&
561 git add init/ &&
562 git diff --exit-code --cached init &&
563 commit=$(cd init &&
564 git commit -m update a >/dev/null &&
565 git rev-parse HEAD) &&
566 git add init/ &&
567 test_must_fail git diff --exit-code --cached init &&
568 test $commit = $(git ls-files --stage |
John Keeping2c63d6e2013-09-12 20:25:01 +0100569 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
570 git reset init/ &&
571 git diff --exit-code --cached init
Johannes Schindelin2ce53f92009-01-02 19:08:40 +0100572
573'
574
Johannes Schindelinf3670a52009-02-07 14:43:03 +0100575test_expect_success 'ls-files gracefully handles trailing slash' '
576
577 test "init" = "$(git ls-files init/)"
578
579'
580
Peter Collingbournec5e558a2010-01-11 02:59:54 +0000581test_expect_success 'moving to a commit without submodule does not leave empty dir' '
582 rm -rf init &&
583 mkdir init &&
584 git reset --hard &&
585 git checkout initial &&
586 test ! -d init &&
587 git checkout second
588'
589
Ramkumar Ramachandraaf9c9f92012-09-22 16:57:59 +0530590test_expect_success 'submodule <invalid-subcommand> fails' '
591 test_must_fail git submodule no-such-subcommand
Johannes Schindelin496917b2009-02-07 14:43:15 +0100592'
593
Jens Lehmann1414e572009-09-22 17:10:12 +0200594test_expect_success 'add submodules without specifying an explicit path' '
595 mkdir repo &&
Jonathan Nieder18a82692010-09-06 20:42:54 -0500596 (
597 cd repo &&
598 git init &&
599 echo r >r &&
600 git add r &&
601 git commit -m "repo commit 1"
Jens Lehmannfd4ec4f2010-09-06 20:39:54 +0200602 ) &&
Jens Lehmann1414e572009-09-22 17:10:12 +0200603 git clone --bare repo/ bare.git &&
Jens Lehmann69e72362010-12-05 00:27:35 +0100604 (
605 cd addtest &&
606 git submodule add "$submodurl/repo" &&
607 git config -f .gitmodules submodule.repo.path repo &&
608 git submodule add "$submodurl/bare.git" &&
609 git config -f .gitmodules submodule.bare.path bare
610 )
611'
612
613test_expect_success 'add should fail when path is used by a file' '
614 (
615 cd addtest &&
616 touch file &&
617 test_must_fail git submodule add "$submodurl/repo" file
618 )
619'
620
621test_expect_success 'add should fail when path is used by an existing directory' '
622 (
623 cd addtest &&
624 mkdir empty-dir &&
625 test_must_fail git submodule add "$submodurl/repo" empty-dir
626 )
Jens Lehmann1414e572009-09-22 17:10:12 +0200627'
628
Jens Lehmann4d689322011-06-06 21:58:04 +0200629test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
Jens Lehmann8537f0e2011-06-06 21:57:01 +0200630 (
631 cd addtest &&
Jens Lehmann4d689322011-06-06 21:58:04 +0200632 git submodule add ../repo relative &&
633 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
634 git submodule sync relative &&
635 test "$(git config submodule.relative.url)" = "$submodurl/repo"
Jens Lehmann8537f0e2011-06-06 21:57:01 +0200636 )
637'
638
Thomas Rastea640cc2011-01-10 11:37:26 +0100639test_expect_success 'set up for relative path tests' '
640 mkdir reltest &&
641 (
642 cd reltest &&
643 git init &&
644 mkdir sub &&
645 (
646 cd sub &&
647 git init &&
648 test_commit foo
649 ) &&
650 git add sub &&
651 git config -f .gitmodules submodule.sub.path sub &&
652 git config -f .gitmodules submodule.sub.url ../subrepo &&
Jon Seymour712693e2012-06-03 19:46:47 +1000653 cp .git/config pristine-.git-config &&
654 cp .gitmodules pristine-.gitmodules
Thomas Rastea640cc2011-01-10 11:37:26 +0100655 )
656'
657
Jon Seymour712693e2012-06-03 19:46:47 +1000658test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
Thomas Rastea640cc2011-01-10 11:37:26 +0100659 (
660 cd reltest &&
661 cp pristine-.git-config .git/config &&
Jon Seymour712693e2012-06-03 19:46:47 +1000662 cp pristine-.gitmodules .gitmodules &&
Thomas Rastea640cc2011-01-10 11:37:26 +0100663 git config remote.origin.url ssh://hostname/repo &&
664 git submodule init &&
665 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
666 )
667'
668
Jon Seymour712693e2012-06-03 19:46:47 +1000669test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
670 (
671 cd reltest &&
672 cp pristine-.git-config .git/config &&
673 cp pristine-.gitmodules .gitmodules &&
674 git config remote.origin.url ssh://hostname:22/repo &&
675 git submodule init &&
676 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
677 )
678'
679
Johannes Sixtc517e732012-06-14 14:10:27 +0200680# About the choice of the path in the next test:
681# - double-slash side-steps path mangling issues on Windows
682# - it is still an absolute local path
683# - there cannot be a server with a blank in its name just in case the
684# path is used erroneously to access a //server/share style path
685test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
Jon Seymour712693e2012-06-03 19:46:47 +1000686 (
687 cd reltest &&
688 cp pristine-.git-config .git/config &&
689 cp pristine-.gitmodules .gitmodules &&
Johannes Sixtc517e732012-06-14 14:10:27 +0200690 git config remote.origin.url "//somewhere else/repo" &&
Jon Seymour712693e2012-06-03 19:46:47 +1000691 git submodule init &&
Johannes Sixtc517e732012-06-14 14:10:27 +0200692 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
Jon Seymour712693e2012-06-03 19:46:47 +1000693 )
694'
695
696test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
697 (
698 cd reltest &&
699 cp pristine-.git-config .git/config &&
700 cp pristine-.gitmodules .gitmodules &&
701 git config remote.origin.url file:///tmp/repo &&
702 git submodule init &&
703 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
704 )
705'
706
707test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
708 (
709 cd reltest &&
710 cp pristine-.git-config .git/config &&
711 cp pristine-.gitmodules .gitmodules &&
712 git config remote.origin.url helper:://hostname/repo &&
713 git submodule init &&
714 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
715 )
716'
717
718test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
Thomas Rastea640cc2011-01-10 11:37:26 +0100719 (
720 cd reltest &&
721 cp pristine-.git-config .git/config &&
722 git config remote.origin.url user@host:repo &&
723 git submodule init &&
724 test "$(git config submodule.sub.url)" = user@host:subrepo
725 )
726'
727
Jon Seymour712693e2012-06-03 19:46:47 +1000728test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
729 (
730 cd reltest &&
731 cp pristine-.git-config .git/config &&
732 cp pristine-.gitmodules .gitmodules &&
733 git config remote.origin.url user@host:path/to/repo &&
734 git submodule init &&
735 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
736 )
737'
738
Jon Seymour758615e2012-06-06 21:57:30 +1000739test_expect_success '../subrepo works with relative local path - foo' '
Jon Seymour49301c62012-06-03 19:46:48 +1000740 (
741 cd reltest &&
742 cp pristine-.git-config .git/config &&
743 cp pristine-.gitmodules .gitmodules &&
744 git config remote.origin.url foo &&
745 # actual: fails with an error
746 git submodule init &&
747 test "$(git config submodule.sub.url)" = subrepo
748 )
749'
750
Jon Seymour712693e2012-06-03 19:46:47 +1000751test_expect_success '../subrepo works with relative local path - foo/bar' '
752 (
753 cd reltest &&
754 cp pristine-.git-config .git/config &&
755 cp pristine-.gitmodules .gitmodules &&
756 git config remote.origin.url foo/bar &&
757 git submodule init &&
758 test "$(git config submodule.sub.url)" = foo/subrepo
759 )
760'
761
Jon Seymour758615e2012-06-06 21:57:30 +1000762test_expect_success '../subrepo works with relative local path - ./foo' '
Jon Seymour49301c62012-06-03 19:46:48 +1000763 (
764 cd reltest &&
765 cp pristine-.git-config .git/config &&
766 cp pristine-.gitmodules .gitmodules &&
767 git config remote.origin.url ./foo &&
768 git submodule init &&
769 test "$(git config submodule.sub.url)" = subrepo
770 )
771'
772
Jon Seymour758615e2012-06-06 21:57:30 +1000773test_expect_success '../subrepo works with relative local path - ./foo/bar' '
Jon Seymour49301c62012-06-03 19:46:48 +1000774 (
775 cd reltest &&
776 cp pristine-.git-config .git/config &&
777 cp pristine-.gitmodules .gitmodules &&
778 git config remote.origin.url ./foo/bar &&
779 git submodule init &&
780 test "$(git config submodule.sub.url)" = foo/subrepo
781 )
782'
783
Jon Seymour712693e2012-06-03 19:46:47 +1000784test_expect_success '../subrepo works with relative local path - ../foo' '
785 (
786 cd reltest &&
787 cp pristine-.git-config .git/config &&
788 cp pristine-.gitmodules .gitmodules &&
789 git config remote.origin.url ../foo &&
790 git submodule init &&
791 test "$(git config submodule.sub.url)" = ../subrepo
792 )
793'
794
795test_expect_success '../subrepo works with relative local path - ../foo/bar' '
796 (
797 cd reltest &&
798 cp pristine-.git-config .git/config &&
799 cp pristine-.gitmodules .gitmodules &&
800 git config remote.origin.url ../foo/bar &&
801 git submodule init &&
802 test "$(git config submodule.sub.url)" = ../foo/subrepo
803 )
804'
805
806test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
807 (
808 cd reltest &&
809 cp pristine-.git-config .git/config &&
810 cp pristine-.gitmodules .gitmodules &&
811 mkdir -p a/b/c &&
Eric Sunshinee974e062018-07-01 20:24:03 -0400812 (cd a/b/c && git init) &&
Jon Seymour712693e2012-06-03 19:46:47 +1000813 git config remote.origin.url ../foo/bar.git &&
814 git submodule add ../bar/a/b/c ./a/b/c &&
815 git submodule init &&
816 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
817 )
818'
819
Jens Lehmannd75219b2012-03-04 22:15:08 +0100820test_expect_success 'moving the superproject does not break submodules' '
821 (
822 cd addtest &&
823 git submodule status >expect
Jeff King99094a72015-03-20 06:07:15 -0400824 ) &&
Jens Lehmannd75219b2012-03-04 22:15:08 +0100825 mv addtest addtest2 &&
826 (
827 cd addtest2 &&
828 git submodule status >actual &&
829 test_cmp expect actual
830 )
831'
832
René Scharfe74b6bda2018-03-28 23:14:08 +0200833test_expect_success 'moving the submodule does not break the superproject' '
834 (
835 cd addtest2 &&
836 git submodule status
837 ) >actual &&
838 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
839 mv addtest2/repo addtest2/repo.bak &&
840 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
841 (
842 cd addtest2 &&
843 git submodule status
844 ) >actual &&
845 test_cmp expect actual
846'
847
Jens Lehmann73b08982012-09-30 01:05:58 +0200848test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
849 (
850 cd addtest2 &&
851 (
852 cd repo &&
853 echo "$submodurl/repo" >expect &&
854 git config remote.origin.url >actual &&
855 test_cmp expect actual &&
856 echo "gitdir: ../.git/modules/repo" >expect &&
857 test_cmp expect .git
858 ) &&
859 rm -rf repo &&
860 git rm repo &&
861 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
Junio C Hamanoca8d1482013-06-09 11:29:20 -0700862 test_must_be_empty actual &&
Jens Lehmann73b08982012-09-30 01:05:58 +0200863 echo "gitdir: ../.git/modules/submod" >expect &&
864 test_cmp expect submod/.git &&
865 (
866 cd repo &&
867 echo "$submodurl/bare.git" >expect &&
868 git config remote.origin.url >actual &&
869 test_cmp expect actual &&
870 echo "gitdir: ../.git/modules/repo_new" >expect &&
871 test_cmp expect .git
872 ) &&
873 echo "repo" >expect &&
Jens Lehmann95c16412013-08-06 21:15:25 +0200874 test_must_fail git config -f .gitmodules submodule.repo.path &&
Jens Lehmann73b08982012-09-30 01:05:58 +0200875 git config -f .gitmodules submodule.repo_new.path >actual &&
876 test_cmp expect actual&&
877 echo "$submodurl/repo" >expect &&
Jens Lehmann95c16412013-08-06 21:15:25 +0200878 test_must_fail git config -f .gitmodules submodule.repo.url &&
Jens Lehmann73b08982012-09-30 01:05:58 +0200879 echo "$submodurl/bare.git" >expect &&
880 git config -f .gitmodules submodule.repo_new.url >actual &&
881 test_cmp expect actual &&
882 echo "$submodurl/repo" >expect &&
883 git config submodule.repo.url >actual &&
884 test_cmp expect actual &&
885 echo "$submodurl/bare.git" >expect &&
886 git config submodule.repo_new.url >actual &&
887 test_cmp expect actual
888 )
889'
890
Stefan Bellerf8eaa0b2016-03-31 17:17:28 -0700891test_expect_success 'recursive relative submodules stay relative' '
Stefan Beller3fea1212016-03-31 14:04:36 -0700892 test_when_finished "rm -rf super clone2 subsub sub3" &&
893 mkdir subsub &&
894 (
895 cd subsub &&
896 git init &&
897 >t &&
898 git add t &&
899 git commit -m "initial commit"
900 ) &&
901 mkdir sub3 &&
902 (
903 cd sub3 &&
904 git init &&
905 >t &&
906 git add t &&
907 git commit -m "initial commit" &&
908 git submodule add ../subsub dirdir/subsub &&
909 git commit -m "add submodule subsub"
910 ) &&
911 mkdir super &&
912 (
913 cd super &&
914 git init &&
915 >t &&
916 git add t &&
917 git commit -m "initial commit" &&
918 git submodule add ../sub3 &&
919 git commit -m "add submodule sub"
920 ) &&
921 git clone super clone2 &&
922 (
923 cd clone2 &&
924 git submodule update --init --recursive &&
925 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
926 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
927 ) &&
928 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
929 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
930'
931
Jens Lehmann4b7c2862012-09-30 23:01:29 +0200932test_expect_success 'submodule add with an existing name fails unless forced' '
933 (
934 cd addtest2 &&
935 rm -rf repo &&
936 git rm repo &&
937 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
938 test ! -d repo &&
Jens Lehmann95c16412013-08-06 21:15:25 +0200939 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
940 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
Jens Lehmann4b7c2862012-09-30 23:01:29 +0200941 echo "$submodurl/bare.git" >expect &&
942 git config submodule.repo_new.url >actual &&
943 test_cmp expect actual &&
944 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
945 test -d repo &&
946 echo "repo" >expect &&
947 git config -f .gitmodules submodule.repo_new.path >actual &&
948 test_cmp expect actual&&
949 echo "$submodurl/repo.git" >expect &&
950 git config -f .gitmodules submodule.repo_new.url >actual &&
951 test_cmp expect actual &&
952 echo "$submodurl/repo.git" >expect &&
953 git config submodule.repo_new.url >actual &&
954 test_cmp expect actual
955 )
956'
957
Jens Lehmanncf419822013-03-04 22:20:24 +0100958test_expect_success 'set up a second submodule' '
959 git submodule add ./init2 example2 &&
960 git commit -m "submodule example2 added"
961'
962
Stefan Beller84ba9592016-03-22 16:42:14 -0700963test_expect_success 'submodule deinit works on repository without submodules' '
964 test_when_finished "rm -rf newdirectory" &&
965 mkdir newdirectory &&
966 (
967 cd newdirectory &&
968 git init &&
969 >file &&
970 git add file &&
Stefan Beller14544dd2016-05-02 15:24:02 -0700971 git commit -m "repo should not be empty" &&
Stefan Bellerf6a52792016-05-05 12:52:32 -0700972 git submodule deinit . &&
973 git submodule deinit --all
Stefan Beller84ba9592016-03-22 16:42:14 -0700974 )
975'
976
Jens Lehmanncf419822013-03-04 22:20:24 +0100977test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
978 git config submodule.example.foo bar &&
979 git config submodule.example2.frotz nitfol &&
980 git submodule deinit init &&
981 test -z "$(git config --get-regexp "submodule\.example\.")" &&
982 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
983 test -f example2/.git &&
984 rmdir init
985'
986
John Keeping091a6eb2013-06-16 15:18:18 +0100987test_expect_success 'submodule deinit from subdirectory' '
988 git submodule update --init &&
989 git config submodule.example.foo bar &&
990 mkdir -p sub &&
991 (
992 cd sub &&
993 git submodule deinit ../init >../output
994 ) &&
Vasco Almeida1edbaac2016-06-17 20:21:07 +0000995 test_i18ngrep "\\.\\./init" output &&
John Keeping091a6eb2013-06-16 15:18:18 +0100996 test -z "$(git config --get-regexp "submodule\.example\.")" &&
997 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
998 test -f example2/.git &&
999 rmdir init
1000'
1001
Jens Lehmanncf419822013-03-04 22:20:24 +01001002test_expect_success 'submodule deinit . deinits all initialized submodules' '
1003 git submodule update --init &&
1004 git config submodule.example.foo bar &&
1005 git config submodule.example2.frotz nitfol &&
1006 test_must_fail git submodule deinit &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001007 git submodule deinit . >actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001008 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1009 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001010 test_i18ngrep "Cleared directory .init" actual &&
1011 test_i18ngrep "Cleared directory .example2" actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001012 rmdir init example2
1013'
1014
Stefan Bellerf6a52792016-05-05 12:52:32 -07001015test_expect_success 'submodule deinit --all deinits all initialized submodules' '
1016 git submodule update --init &&
1017 git config submodule.example.foo bar &&
1018 git config submodule.example2.frotz nitfol &&
1019 test_must_fail git submodule deinit &&
1020 git submodule deinit --all >actual &&
1021 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1022 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1023 test_i18ngrep "Cleared directory .init" actual &&
1024 test_i18ngrep "Cleared directory .example2" actual &&
1025 rmdir init example2
1026'
1027
Jens Lehmanncf419822013-03-04 22:20:24 +01001028test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
1029 git submodule update --init &&
1030 rm -rf init example2/* example2/.git &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001031 git submodule deinit init example2 >actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001032 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1033 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001034 test_i18ngrep ! "Cleared directory .init" actual &&
1035 test_i18ngrep "Cleared directory .example2" actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001036 rmdir init
1037'
1038
1039test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
1040 git submodule update --init &&
1041 echo X >>init/s &&
1042 test_must_fail git submodule deinit init &&
1043 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1044 test -f example2/.git &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001045 git submodule deinit -f init >actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001046 test -z "$(git config --get-regexp "submodule\.example\.")" &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001047 test_i18ngrep "Cleared directory .init" actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001048 rmdir init
1049'
1050
1051test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1052 git submodule update --init &&
1053 echo X >>init/untracked &&
1054 test_must_fail git submodule deinit init &&
1055 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1056 test -f example2/.git &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001057 git submodule deinit -f init >actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001058 test -z "$(git config --get-regexp "submodule\.example\.")" &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001059 test_i18ngrep "Cleared directory .init" actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001060 rmdir init
1061'
1062
1063test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1064 git submodule update --init &&
1065 (
1066 cd init &&
1067 git checkout HEAD^
1068 ) &&
1069 test_must_fail git submodule deinit init &&
1070 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1071 test -f example2/.git &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001072 git submodule deinit -f init >actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001073 test -z "$(git config --get-regexp "submodule\.example\.")" &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001074 test_i18ngrep "Cleared directory .init" actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001075 rmdir init
1076'
1077
1078test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1079 git submodule update --init &&
1080 git submodule deinit init >actual &&
1081 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001082 test_i18ngrep "Cleared directory .init" actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001083 git submodule deinit init >actual &&
1084 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001085 test_i18ngrep "Cleared directory .init" actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001086 git submodule deinit . >actual &&
1087 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1088 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001089 test_i18ngrep "Cleared directory .init" actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001090 git submodule deinit . >actual &&
1091 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1092 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
Jens Lehmann7b294bf2013-04-01 21:02:00 +02001093 test_i18ngrep "Cleared directory .init" actual &&
Stefan Bellerf6a52792016-05-05 12:52:32 -07001094 git submodule deinit --all >actual &&
1095 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1096 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1097 test_i18ngrep "Cleared directory .init" actual &&
Jens Lehmanncf419822013-03-04 22:20:24 +01001098 rmdir init example2
1099'
1100
1101test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
1102 git submodule update --init &&
1103 (
1104 cd init &&
1105 rm .git &&
1106 cp -R ../.git/modules/example .git &&
1107 GIT_WORK_TREE=. git config --unset core.worktree
1108 ) &&
1109 test_must_fail git submodule deinit init &&
1110 test_must_fail git submodule deinit -f init &&
1111 test -d init/.git &&
1112 test -n "$(git config --get-regexp "submodule\.example\.")"
1113'
1114
Torsten Bögershausenbed94702013-06-20 16:58:48 +02001115test_expect_success 'submodule with UTF-8 name' '
1116 svname=$(printf "\303\245 \303\244\303\266") &&
1117 mkdir "$svname" &&
Fredrik Gustafsson74671242013-06-14 02:26:02 +02001118 (
Torsten Bögershausenbed94702013-06-20 16:58:48 +02001119 cd "$svname" &&
Fredrik Gustafsson74671242013-06-14 02:26:02 +02001120 git init &&
Torsten Bögershausenbed94702013-06-20 16:58:48 +02001121 >sub &&
1122 git add sub &&
Fredrik Gustafsson74671242013-06-14 02:26:02 +02001123 git commit -m "init sub"
Torsten Bögershausenbed94702013-06-20 16:58:48 +02001124 ) &&
Torsten Bögershausenbed94702013-06-20 16:58:48 +02001125 git submodule add ./"$svname" &&
1126 git submodule >&2 &&
1127 test -n "$(git submodule | grep "$svname")"
Fredrik Gustafsson74671242013-06-14 02:26:02 +02001128'
Junio C Hamano2bb7afa2013-07-15 10:28:48 -07001129
Fredrik Gustafsson275cd182013-07-02 23:42:56 +02001130test_expect_success 'submodule add clone shallow submodule' '
1131 mkdir super &&
Jeff King99094a72015-03-20 06:07:15 -04001132 pwd=$(pwd) &&
Fredrik Gustafsson275cd182013-07-02 23:42:56 +02001133 (
1134 cd super &&
1135 git init &&
1136 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1137 (
1138 cd submodule &&
1139 test 1 = $(git log --oneline | wc -l)
1140 )
1141 )
1142'
1143
Stefan Beller2b56bb72016-02-24 13:15:02 -08001144test_expect_success 'submodule helper list is not confused by common prefixes' '
1145 mkdir -p dir1/b &&
1146 (
1147 cd dir1/b &&
1148 git init &&
1149 echo hi >testfile2 &&
1150 git add . &&
1151 git commit -m "test1"
1152 ) &&
1153 mkdir -p dir2/b &&
1154 (
1155 cd dir2/b &&
1156 git init &&
1157 echo hello >testfile1 &&
1158 git add . &&
1159 git commit -m "test2"
1160 ) &&
1161 git submodule add /dir1/b dir1/b &&
1162 git submodule add /dir2/b dir2/b &&
1163 git commit -m "first submodule commit" &&
1164 git submodule--helper list dir1/b |cut -c51- >actual &&
1165 echo "dir1/b" >expect &&
1166 test_cmp expect actual
1167'
1168
Brandon Williams3e7eaed2017-03-17 15:38:02 -07001169test_expect_success 'setup superproject with submodules' '
1170 git init sub1 &&
1171 test_commit -C sub1 test &&
1172 test_commit -C sub1 test2 &&
1173 git init multisuper &&
1174 git -C multisuper submodule add ../sub1 sub0 &&
1175 git -C multisuper submodule add ../sub1 sub1 &&
1176 git -C multisuper submodule add ../sub1 sub2 &&
1177 git -C multisuper submodule add ../sub1 sub3 &&
1178 git -C multisuper commit -m "add some submodules"
1179'
1180
1181cat >expect <<-EOF
1182-sub0
1183 sub1 (test2)
1184 sub2 (test2)
1185 sub3 (test2)
1186EOF
1187
1188test_expect_success 'submodule update --init with a specification' '
1189 test_when_finished "rm -rf multisuper_clone" &&
1190 pwd=$(pwd) &&
1191 git clone file://"$pwd"/multisuper multisuper_clone &&
1192 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1193 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1194 test_cmp expect actual
1195'
1196
1197test_expect_success 'submodule update --init with submodule.active set' '
1198 test_when_finished "rm -rf multisuper_clone" &&
1199 pwd=$(pwd) &&
1200 git clone file://"$pwd"/multisuper multisuper_clone &&
1201 git -C multisuper_clone config submodule.active "." &&
1202 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1203 git -C multisuper_clone submodule update --init &&
1204 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1205 test_cmp expect actual
1206'
1207
1208test_expect_success 'submodule update and setting submodule.<name>.active' '
1209 test_when_finished "rm -rf multisuper_clone" &&
1210 pwd=$(pwd) &&
1211 git clone file://"$pwd"/multisuper multisuper_clone &&
1212 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1213 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1214 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1215
1216 cat >expect <<-\EOF &&
1217 sub0 (test2)
1218 -sub1
1219 sub2 (test2)
1220 -sub3
1221 EOF
1222 git -C multisuper_clone submodule update &&
1223 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1224 test_cmp expect actual
1225'
Fredrik Gustafsson275cd182013-07-02 23:42:56 +02001226
Stefan Bellere0a862f2018-10-16 10:27:03 -07001227test_expect_success 'clone active submodule without submodule url set' '
1228 test_when_finished "rm -rf test/test" &&
1229 mkdir test &&
1230 # another dir breaks accidental relative paths still being correct
1231 git clone file://"$pwd"/multisuper test/test &&
1232 (
1233 cd test/test &&
1234 git config submodule.active "." &&
1235
1236 # do not pass --init flag, as the submodule is already active:
1237 git submodule update &&
1238 git submodule status >actual_raw &&
1239
1240 cut -c 1,43- actual_raw >actual &&
1241 cat >expect <<-\EOF &&
1242 sub0 (test2)
1243 sub1 (test2)
1244 sub2 (test2)
1245 sub3 (test2)
1246 EOF
1247 test_cmp expect actual
1248 )
1249'
1250
Brandon Williamsbb62e0a2017-03-17 15:38:03 -07001251test_expect_success 'clone --recurse-submodules with a pathspec works' '
1252 test_when_finished "rm -rf multisuper_clone" &&
1253 cat >expected <<-\EOF &&
1254 sub0 (test2)
1255 -sub1
1256 -sub2
1257 -sub3
1258 EOF
1259
1260 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1261 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
Stefan Beller9c5b2fa2017-10-06 12:00:06 -07001262 test_cmp expected actual
Brandon Williamsbb62e0a2017-03-17 15:38:03 -07001263'
1264
1265test_expect_success 'clone with multiple --recurse-submodules options' '
1266 test_when_finished "rm -rf multisuper_clone" &&
1267 cat >expect <<-\EOF &&
1268 -sub0
1269 sub1 (test2)
1270 -sub2
1271 sub3 (test2)
1272 EOF
1273
1274 git clone --recurse-submodules="." \
1275 --recurse-submodules=":(exclude)sub0" \
1276 --recurse-submodules=":(exclude)sub2" \
1277 multisuper multisuper_clone &&
1278 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1279 test_cmp expect actual
1280'
1281
1282test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
1283 test_when_finished "rm -rf multisuper_clone" &&
1284 cat <<-\EOF >expect &&
1285 -sub0
1286 sub1 (test2)
1287 -sub2
1288 sub3 (test2)
1289 EOF
1290
1291 cat <<-\EOF >expect2 &&
1292 -sub0
1293 sub1 (test2)
1294 -sub2
1295 sub3 (test2)
1296 -sub4
1297 sub5 (test2)
1298 EOF
1299
1300 git clone --recurse-submodules="." \
1301 --recurse-submodules=":(exclude)sub0" \
1302 --recurse-submodules=":(exclude)sub2" \
1303 --recurse-submodules=":(exclude)sub4" \
1304 multisuper multisuper_clone &&
1305
1306 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1307 test_cmp expect actual &&
1308
1309 git -C multisuper submodule add ../sub1 sub4 &&
1310 git -C multisuper submodule add ../sub1 sub5 &&
1311 git -C multisuper commit -m "add more submodules" &&
1312 # obtain the new superproject
1313 git -C multisuper_clone pull &&
1314 git -C multisuper_clone submodule update --init &&
1315 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1316 test_cmp expect2 actual
1317'
1318
Brandon Williams1f8d7112017-03-17 15:38:04 -07001319test_expect_success 'init properly sets the config' '
1320 test_when_finished "rm -rf multisuper_clone" &&
1321 git clone --recurse-submodules="." \
1322 --recurse-submodules=":(exclude)sub0" \
1323 multisuper multisuper_clone &&
1324
1325 git -C multisuper_clone submodule init -- sub0 sub1 &&
1326 git -C multisuper_clone config --get submodule.sub0.active &&
1327 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1328'
1329
Brandon Williams03c004c2017-08-03 15:25:44 -07001330test_expect_success 'recursive clone respects -q' '
1331 test_when_finished "rm -rf multisuper_clone" &&
1332 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1333 test_must_be_empty actual
1334'
1335
Lars Hjemli88961ef2007-06-02 03:27:42 +02001336test_done