Merge branch 'dl/test-must-fail-fixes-2'
[git] / t / t7400-submodule-basic.sh
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
9 subcommands of git submodule.
10 '
11
12 . ./test-lib.sh
13
14 test_expect_success 'submodule deinit works on empty repository' '
15         git submodule deinit --all
16 '
17
18 test_expect_success 'setup - initial commit' '
19         >t &&
20         git add t &&
21         git commit -m "initial commit" &&
22         git branch initial
23 '
24
25 test_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
33 test_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
41 test_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
49 test_expect_success 'add aborts on repository with no commits' '
50         cat >expect <<-\EOF &&
51         '"'repo-no-commits'"' does not have a commit checked out
52         EOF
53         git init repo-no-commits &&
54         test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
55         test_i18ncmp expect actual
56 '
57
58 test_expect_success 'setup - repository in init subdirectory' '
59         mkdir init &&
60         (
61                 cd init &&
62                 git init &&
63                 echo a >a &&
64                 git add a &&
65                 git commit -m "submodule commit 1" &&
66                 git tag -a -m "rev-1" rev-1
67         )
68 '
69
70 test_expect_success 'setup - commit with gitlink' '
71         echo a >a &&
72         echo z >z &&
73         git add a init z &&
74         git commit -m "super commit 1"
75 '
76
77 test_expect_success 'setup - hide init subdirectory' '
78         mv init .subrepo
79 '
80
81 test_expect_success 'setup - repository to add submodules to' '
82         git init addtest &&
83         git init addtest-ignore
84 '
85
86 # The 'submodule add' tests need some repository to add as a submodule.
87 # The trash directory is a good one as any. We need to canonicalize
88 # the name, though, as some tests compare it to the absolute path git
89 # generates, which will expand symbolic links.
90 submodurl=$(pwd -P)
91
92 listbranches() {
93         git for-each-ref --format='%(refname)' 'refs/heads/*'
94 }
95
96 inspect() {
97         dir=$1 &&
98         dotdot="${2:-..}" &&
99
100         (
101                 cd "$dir" &&
102                 listbranches >"$dotdot/heads" &&
103                 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
104                 git rev-parse HEAD >"$dotdot/head-sha1" &&
105                 git update-index --refresh &&
106                 git diff-files --exit-code &&
107                 git clean -n -d -x >"$dotdot/untracked"
108         )
109 }
110
111 test_expect_success 'submodule add' '
112         echo "refs/heads/master" >expect &&
113
114         (
115                 cd addtest &&
116                 git submodule add -q "$submodurl" submod >actual &&
117                 test_must_be_empty actual &&
118                 echo "gitdir: ../.git/modules/submod" >expect &&
119                 test_cmp expect submod/.git &&
120                 (
121                         cd submod &&
122                         git config core.worktree >actual &&
123                         echo "../../../submod" >expect &&
124                         test_cmp expect actual &&
125                         rm -f actual expect
126                 ) &&
127                 git submodule init
128         ) &&
129
130         rm -f heads head untracked &&
131         inspect addtest/submod ../.. &&
132         test_cmp expect heads &&
133         test_cmp expect head &&
134         test_must_be_empty untracked
135 '
136
137 test_expect_success 'setup parent and one repository' '
138         test_create_repo parent &&
139         test_commit -C parent one
140 '
141
142 test_expect_success 'redirected submodule add does not show progress' '
143         git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
144                 2>err &&
145         ! grep % err &&
146         test_i18ngrep ! "Checking connectivity" err
147 '
148
149 test_expect_success 'redirected submodule add --progress does show progress' '
150         git -C addtest submodule add --progress "file://$submodurl/parent" \
151                 submod-redirected-progress 2>err && \
152         grep % err
153 '
154
155 test_expect_success 'submodule add to .gitignored path fails' '
156         (
157                 cd addtest-ignore &&
158                 cat <<-\EOF >expect &&
159                 The following paths are ignored by one of your .gitignore files:
160                 submod
161                 hint: Use -f if you really want to add them.
162                 hint: Turn this message off by running
163                 hint: "git config advice.addIgnoredFile false"
164                 EOF
165                 # Does not use test_commit due to the ignore
166                 echo "*" > .gitignore &&
167                 git add --force .gitignore &&
168                 git commit -m"Ignore everything" &&
169                 ! git submodule add "$submodurl" submod >actual 2>&1 &&
170                 test_i18ncmp expect actual
171         )
172 '
173
174 test_expect_success 'submodule add to .gitignored path with --force' '
175         (
176                 cd addtest-ignore &&
177                 git submodule add --force "$submodurl" submod
178         )
179 '
180
181 test_expect_success 'submodule add to reconfigure existing submodule with --force' '
182         (
183                 cd addtest-ignore &&
184                 bogus_url="$(pwd)/bogus-url" &&
185                 git submodule add --force "$bogus_url" submod &&
186                 git submodule add --force -b initial "$submodurl" submod-branch &&
187                 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
188                 test "$bogus_url" = "$(git config submodule.submod.url)" &&
189                 # Restore the url
190                 git submodule add --force "$submodurl" submod &&
191                 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
192                 test "$submodurl" = "$(git config submodule.submod.url)"
193         )
194 '
195
196 test_expect_success 'submodule add relays add --dry-run stderr' '
197         test_when_finished "rm -rf addtest/.git/index.lock" &&
198         (
199                 cd addtest &&
200                 : >.git/index.lock &&
201                 ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
202                 test_i18ngrep "^fatal: .*index\.lock" output.err &&
203                 test_path_is_missing sub-while-locked
204         )
205 '
206
207 test_expect_success 'submodule add --branch' '
208         echo "refs/heads/initial" >expect-head &&
209         cat <<-\EOF >expect-heads &&
210         refs/heads/initial
211         refs/heads/master
212         EOF
213
214         (
215                 cd addtest &&
216                 git submodule add -b initial "$submodurl" submod-branch &&
217                 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
218                 git submodule init
219         ) &&
220
221         rm -f heads head untracked &&
222         inspect addtest/submod-branch ../.. &&
223         test_cmp expect-heads heads &&
224         test_cmp expect-head head &&
225         test_must_be_empty untracked
226 '
227
228 test_expect_success 'submodule add with ./ in path' '
229         echo "refs/heads/master" >expect &&
230
231         (
232                 cd addtest &&
233                 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
234                 git submodule init
235         ) &&
236
237         rm -f heads head untracked &&
238         inspect addtest/dotsubmod/frotz ../../.. &&
239         test_cmp expect heads &&
240         test_cmp expect head &&
241         test_must_be_empty untracked
242 '
243
244 test_expect_success 'submodule add with /././ in path' '
245         echo "refs/heads/master" >expect &&
246
247         (
248                 cd addtest &&
249                 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
250                 git submodule init
251         ) &&
252
253         rm -f heads head untracked &&
254         inspect addtest/dotslashdotsubmod/frotz ../../.. &&
255         test_cmp expect heads &&
256         test_cmp expect head &&
257         test_must_be_empty untracked
258 '
259
260 test_expect_success 'submodule add with // in path' '
261         echo "refs/heads/master" >expect &&
262
263         (
264                 cd addtest &&
265                 git submodule add "$submodurl" slashslashsubmod///frotz// &&
266                 git submodule init
267         ) &&
268
269         rm -f heads head untracked &&
270         inspect addtest/slashslashsubmod/frotz ../../.. &&
271         test_cmp expect heads &&
272         test_cmp expect head &&
273         test_must_be_empty untracked
274 '
275
276 test_expect_success 'submodule add with /.. in path' '
277         echo "refs/heads/master" >expect &&
278
279         (
280                 cd addtest &&
281                 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
282                 git submodule init
283         ) &&
284
285         rm -f heads head untracked &&
286         inspect addtest/realsubmod ../.. &&
287         test_cmp expect heads &&
288         test_cmp expect head &&
289         test_must_be_empty untracked
290 '
291
292 test_expect_success 'submodule add with ./, /.. and // in path' '
293         echo "refs/heads/master" >expect &&
294
295         (
296                 cd addtest &&
297                 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
298                 git submodule init
299         ) &&
300
301         rm -f heads head untracked &&
302         inspect addtest/realsubmod2 ../.. &&
303         test_cmp expect heads &&
304         test_cmp expect head &&
305         test_must_be_empty untracked
306 '
307
308 test_expect_success !CYGWIN 'submodule add with \\ in path' '
309         test_when_finished "rm -rf parent sub\\with\\backslash" &&
310
311         # Initialize a repo with a backslash in its name
312         git init sub\\with\\backslash &&
313         touch sub\\with\\backslash/empty.file &&
314         git -C sub\\with\\backslash add empty.file &&
315         git -C sub\\with\\backslash commit -m "Added empty.file" &&
316
317         # Add that repository as a submodule
318         git init parent &&
319         git -C parent submodule add ../sub\\with\\backslash
320 '
321
322 test_expect_success 'submodule add in subdirectory' '
323         echo "refs/heads/master" >expect &&
324
325         mkdir addtest/sub &&
326         (
327                 cd addtest/sub &&
328                 git submodule add "$submodurl" ../realsubmod3 &&
329                 git submodule init
330         ) &&
331
332         rm -f heads head untracked &&
333         inspect addtest/realsubmod3 ../.. &&
334         test_cmp expect heads &&
335         test_cmp expect head &&
336         test_must_be_empty untracked
337 '
338
339 test_expect_success 'submodule add in subdirectory with relative path should fail' '
340         (
341                 cd addtest/sub &&
342                 test_must_fail git submodule add ../../ submod3 2>../../output.err
343         ) &&
344         test_i18ngrep toplevel output.err
345 '
346
347 test_expect_success 'setup - add an example entry to .gitmodules' '
348         git config --file=.gitmodules submodule.example.url git://example.com/init.git
349 '
350
351 test_expect_success 'status should fail for unmapped paths' '
352         test_must_fail git submodule status
353 '
354
355 test_expect_success 'setup - map path in .gitmodules' '
356         cat <<\EOF >expect &&
357 [submodule "example"]
358         url = git://example.com/init.git
359         path = init
360 EOF
361
362         git config --file=.gitmodules submodule.example.path init &&
363
364         test_cmp expect .gitmodules
365 '
366
367 test_expect_success 'status should only print one line' '
368         git submodule status >lines &&
369         test_line_count = 1 lines
370 '
371
372 test_expect_success 'status from subdirectory should have the same SHA1' '
373         test_when_finished "rmdir addtest/subdir" &&
374         (
375                 cd addtest &&
376                 mkdir subdir &&
377                 git submodule status >output &&
378                 awk "{print \$1}" <output >expect &&
379                 cd subdir &&
380                 git submodule status >../output &&
381                 awk "{print \$1}" <../output >../actual &&
382                 test_cmp ../expect ../actual &&
383                 git -C ../submod checkout HEAD^ &&
384                 git submodule status >../output &&
385                 awk "{print \$1}" <../output >../actual2 &&
386                 cd .. &&
387                 git submodule status >output &&
388                 awk "{print \$1}" <output >expect2 &&
389                 test_cmp expect2 actual2 &&
390                 ! test_cmp actual actual2
391         )
392 '
393
394 test_expect_success 'setup - fetch commit name from submodule' '
395         rev1=$(cd .subrepo && git rev-parse HEAD) &&
396         printf "rev1: %s\n" "$rev1" &&
397         test -n "$rev1"
398 '
399
400 test_expect_success 'status should initially be "missing"' '
401         git submodule status >lines &&
402         grep "^-$rev1" lines
403 '
404
405 test_expect_success 'init should register submodule url in .git/config' '
406         echo git://example.com/init.git >expect &&
407
408         git submodule init &&
409         git config submodule.example.url >url &&
410         git config submodule.example.url ./.subrepo &&
411
412         test_cmp expect url
413 '
414
415 test_failure_with_unknown_submodule () {
416         test_must_fail git submodule $1 no-such-submodule 2>output.err &&
417         test_i18ngrep "^error: .*no-such-submodule" output.err
418 }
419
420 test_expect_success 'init should fail with unknown submodule' '
421         test_failure_with_unknown_submodule init
422 '
423
424 test_expect_success 'update should fail with unknown submodule' '
425         test_failure_with_unknown_submodule update
426 '
427
428 test_expect_success 'status should fail with unknown submodule' '
429         test_failure_with_unknown_submodule status
430 '
431
432 test_expect_success 'sync should fail with unknown submodule' '
433         test_failure_with_unknown_submodule sync
434 '
435
436 test_expect_success 'update should fail when path is used by a file' '
437         echo hello >expect &&
438
439         echo "hello" >init &&
440         test_must_fail git submodule update &&
441
442         test_cmp expect init
443 '
444
445 test_expect_success 'update should fail when path is used by a nonempty directory' '
446         echo hello >expect &&
447
448         rm -fr init &&
449         mkdir init &&
450         echo "hello" >init/a &&
451
452         test_must_fail git submodule update &&
453
454         test_cmp expect init/a
455 '
456
457 test_expect_success 'update should work when path is an empty dir' '
458         rm -fr init &&
459         rm -f head-sha1 &&
460         echo "$rev1" >expect &&
461
462         mkdir init &&
463         git submodule update -q >update.out &&
464         test_must_be_empty update.out &&
465
466         inspect init &&
467         test_cmp expect head-sha1
468 '
469
470 test_expect_success 'status should be "up-to-date" after update' '
471         git submodule status >list &&
472         grep "^ $rev1" list
473 '
474
475 test_expect_success 'status "up-to-date" from subdirectory' '
476         mkdir -p sub &&
477         (
478                 cd sub &&
479                 git submodule status >../list
480         ) &&
481         grep "^ $rev1" list &&
482         grep "\\.\\./init" list
483 '
484
485 test_expect_success 'status "up-to-date" from subdirectory with path' '
486         mkdir -p sub &&
487         (
488                 cd sub &&
489                 git submodule status ../init >../list
490         ) &&
491         grep "^ $rev1" list &&
492         grep "\\.\\./init" list
493 '
494
495 test_expect_success 'status should be "modified" after submodule commit' '
496         (
497                 cd init &&
498                 echo b >b &&
499                 git add b &&
500                 git commit -m "submodule commit 2"
501         ) &&
502
503         rev2=$(cd init && git rev-parse HEAD) &&
504         test -n "$rev2" &&
505         git submodule status >list &&
506
507         grep "^+$rev2" list
508 '
509
510 test_expect_success 'the --cached sha1 should be rev1' '
511         git submodule --cached status >list &&
512         grep "^+$rev1" list
513 '
514
515 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
516         git diff >diff &&
517         grep "^+Subproject commit $rev2" diff
518 '
519
520 test_expect_success 'update should checkout rev1' '
521         rm -f head-sha1 &&
522         echo "$rev1" >expect &&
523
524         git submodule update init &&
525         inspect init &&
526
527         test_cmp expect head-sha1
528 '
529
530 test_expect_success 'status should be "up-to-date" after update' '
531         git submodule status >list &&
532         grep "^ $rev1" list
533 '
534
535 test_expect_success 'checkout superproject with subproject already present' '
536         git checkout initial &&
537         git checkout master
538 '
539
540 test_expect_success 'apply submodule diff' '
541         git branch second &&
542         (
543                 cd init &&
544                 echo s >s &&
545                 git add s &&
546                 git commit -m "change subproject"
547         ) &&
548         git update-index --add init &&
549         git commit -m "change init" &&
550         git format-patch -1 --stdout >P.diff &&
551         git checkout second &&
552         git apply --index P.diff &&
553
554         git diff --cached master >staged &&
555         test_must_be_empty staged
556 '
557
558 test_expect_success 'update --init' '
559         mv init init2 &&
560         git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
561         git config --remove-section submodule.example &&
562         test_must_fail git config submodule.example.url &&
563
564         git submodule update init 2> update.out &&
565         test_i18ngrep "not initialized" update.out &&
566         test_must_fail git rev-parse --resolve-git-dir init/.git &&
567
568         git submodule update --init init &&
569         git rev-parse --resolve-git-dir init/.git
570 '
571
572 test_expect_success 'update --init from subdirectory' '
573         mv init init2 &&
574         git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
575         git config --remove-section submodule.example &&
576         test_must_fail git config submodule.example.url &&
577
578         mkdir -p sub &&
579         (
580                 cd sub &&
581                 git submodule update ../init 2>update.out &&
582                 test_i18ngrep "not initialized" update.out &&
583                 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
584
585                 git submodule update --init ../init
586         ) &&
587         git rev-parse --resolve-git-dir init/.git
588 '
589
590 test_expect_success 'do not add files from a submodule' '
591
592         git reset --hard &&
593         test_must_fail git add init/a
594
595 '
596
597 test_expect_success 'gracefully add/reset submodule with a trailing slash' '
598
599         git reset --hard &&
600         git commit -m "commit subproject" init &&
601         (cd init &&
602          echo b > a) &&
603         git add init/ &&
604         git diff --exit-code --cached init &&
605         commit=$(cd init &&
606          git commit -m update a >/dev/null &&
607          git rev-parse HEAD) &&
608         git add init/ &&
609         test_must_fail git diff --exit-code --cached init &&
610         test $commit = $(git ls-files --stage |
611                 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
612         git reset init/ &&
613         git diff --exit-code --cached init
614
615 '
616
617 test_expect_success 'ls-files gracefully handles trailing slash' '
618
619         test "init" = "$(git ls-files init/)"
620
621 '
622
623 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
624         rm -rf init &&
625         mkdir init &&
626         git reset --hard &&
627         git checkout initial &&
628         test ! -d init &&
629         git checkout second
630 '
631
632 test_expect_success 'submodule <invalid-subcommand> fails' '
633         test_must_fail git submodule no-such-subcommand
634 '
635
636 test_expect_success 'add submodules without specifying an explicit path' '
637         mkdir repo &&
638         (
639                 cd repo &&
640                 git init &&
641                 echo r >r &&
642                 git add r &&
643                 git commit -m "repo commit 1"
644         ) &&
645         git clone --bare repo/ bare.git &&
646         (
647                 cd addtest &&
648                 git submodule add "$submodurl/repo" &&
649                 git config -f .gitmodules submodule.repo.path repo &&
650                 git submodule add "$submodurl/bare.git" &&
651                 git config -f .gitmodules submodule.bare.path bare
652         )
653 '
654
655 test_expect_success 'add should fail when path is used by a file' '
656         (
657                 cd addtest &&
658                 touch file &&
659                 test_must_fail  git submodule add "$submodurl/repo" file
660         )
661 '
662
663 test_expect_success 'add should fail when path is used by an existing directory' '
664         (
665                 cd addtest &&
666                 mkdir empty-dir &&
667                 test_must_fail git submodule add "$submodurl/repo" empty-dir
668         )
669 '
670
671 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
672         (
673                 cd addtest &&
674                 git submodule add ../repo relative &&
675                 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
676                 git submodule sync relative &&
677                 test "$(git config submodule.relative.url)" = "$submodurl/repo"
678         )
679 '
680
681 test_expect_success 'set up for relative path tests' '
682         mkdir reltest &&
683         (
684                 cd reltest &&
685                 git init &&
686                 mkdir sub &&
687                 (
688                         cd sub &&
689                         git init &&
690                         test_commit foo
691                 ) &&
692                 git add sub &&
693                 git config -f .gitmodules submodule.sub.path sub &&
694                 git config -f .gitmodules submodule.sub.url ../subrepo &&
695                 cp .git/config pristine-.git-config &&
696                 cp .gitmodules pristine-.gitmodules
697         )
698 '
699
700 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
701         (
702                 cd reltest &&
703                 cp pristine-.git-config .git/config &&
704                 cp pristine-.gitmodules .gitmodules &&
705                 git config remote.origin.url ssh://hostname/repo &&
706                 git submodule init &&
707                 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
708         )
709 '
710
711 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
712         (
713                 cd reltest &&
714                 cp pristine-.git-config .git/config &&
715                 cp pristine-.gitmodules .gitmodules &&
716                 git config remote.origin.url ssh://hostname:22/repo &&
717                 git submodule init &&
718                 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
719         )
720 '
721
722 # About the choice of the path in the next test:
723 # - double-slash side-steps path mangling issues on Windows
724 # - it is still an absolute local path
725 # - there cannot be a server with a blank in its name just in case the
726 #   path is used erroneously to access a //server/share style path
727 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
728         (
729                 cd reltest &&
730                 cp pristine-.git-config .git/config &&
731                 cp pristine-.gitmodules .gitmodules &&
732                 git config remote.origin.url "//somewhere else/repo" &&
733                 git submodule init &&
734                 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
735         )
736 '
737
738 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
739         (
740                 cd reltest &&
741                 cp pristine-.git-config .git/config &&
742                 cp pristine-.gitmodules .gitmodules &&
743                 git config remote.origin.url file:///tmp/repo &&
744                 git submodule init &&
745                 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
746         )
747 '
748
749 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
750         (
751                 cd reltest &&
752                 cp pristine-.git-config .git/config &&
753                 cp pristine-.gitmodules .gitmodules &&
754                 git config remote.origin.url helper:://hostname/repo &&
755                 git submodule init &&
756                 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
757         )
758 '
759
760 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
761         (
762                 cd reltest &&
763                 cp pristine-.git-config .git/config &&
764                 git config remote.origin.url user@host:repo &&
765                 git submodule init &&
766                 test "$(git config submodule.sub.url)" = user@host:subrepo
767         )
768 '
769
770 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
771         (
772                 cd reltest &&
773                 cp pristine-.git-config .git/config &&
774                 cp pristine-.gitmodules .gitmodules &&
775                 git config remote.origin.url user@host:path/to/repo &&
776                 git submodule init &&
777                 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
778         )
779 '
780
781 test_expect_success '../subrepo works with relative local path - foo' '
782         (
783                 cd reltest &&
784                 cp pristine-.git-config .git/config &&
785                 cp pristine-.gitmodules .gitmodules &&
786                 git config remote.origin.url foo &&
787                 # actual: fails with an error
788                 git submodule init &&
789                 test "$(git config submodule.sub.url)" = subrepo
790         )
791 '
792
793 test_expect_success '../subrepo works with relative local path - foo/bar' '
794         (
795                 cd reltest &&
796                 cp pristine-.git-config .git/config &&
797                 cp pristine-.gitmodules .gitmodules &&
798                 git config remote.origin.url foo/bar &&
799                 git submodule init &&
800                 test "$(git config submodule.sub.url)" = foo/subrepo
801         )
802 '
803
804 test_expect_success '../subrepo works with relative local path - ./foo' '
805         (
806                 cd reltest &&
807                 cp pristine-.git-config .git/config &&
808                 cp pristine-.gitmodules .gitmodules &&
809                 git config remote.origin.url ./foo &&
810                 git submodule init &&
811                 test "$(git config submodule.sub.url)" = subrepo
812         )
813 '
814
815 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
816         (
817                 cd reltest &&
818                 cp pristine-.git-config .git/config &&
819                 cp pristine-.gitmodules .gitmodules &&
820                 git config remote.origin.url ./foo/bar &&
821                 git submodule init &&
822                 test "$(git config submodule.sub.url)" = foo/subrepo
823         )
824 '
825
826 test_expect_success '../subrepo works with relative local path - ../foo' '
827         (
828                 cd reltest &&
829                 cp pristine-.git-config .git/config &&
830                 cp pristine-.gitmodules .gitmodules &&
831                 git config remote.origin.url ../foo &&
832                 git submodule init &&
833                 test "$(git config submodule.sub.url)" = ../subrepo
834         )
835 '
836
837 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
838         (
839                 cd reltest &&
840                 cp pristine-.git-config .git/config &&
841                 cp pristine-.gitmodules .gitmodules &&
842                 git config remote.origin.url ../foo/bar &&
843                 git submodule init &&
844                 test "$(git config submodule.sub.url)" = ../foo/subrepo
845         )
846 '
847
848 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
849         (
850                 cd reltest &&
851                 cp pristine-.git-config .git/config &&
852                 cp pristine-.gitmodules .gitmodules &&
853                 mkdir -p a/b/c &&
854                 (cd a/b/c && git init && test_commit msg) &&
855                 git config remote.origin.url ../foo/bar.git &&
856                 git submodule add ../bar/a/b/c ./a/b/c &&
857                 git submodule init &&
858                 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
859         )
860 '
861
862 test_expect_success 'moving the superproject does not break submodules' '
863         (
864                 cd addtest &&
865                 git submodule status >expect
866         ) &&
867         mv addtest addtest2 &&
868         (
869                 cd addtest2 &&
870                 git submodule status >actual &&
871                 test_cmp expect actual
872         )
873 '
874
875 test_expect_success 'moving the submodule does not break the superproject' '
876         (
877                 cd addtest2 &&
878                 git submodule status
879         ) >actual &&
880         sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
881         mv addtest2/repo addtest2/repo.bak &&
882         test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
883         (
884                 cd addtest2 &&
885                 git submodule status
886         ) >actual &&
887         test_cmp expect actual
888 '
889
890 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
891         (
892                 cd addtest2 &&
893                 (
894                         cd repo &&
895                         echo "$submodurl/repo" >expect &&
896                         git config remote.origin.url >actual &&
897                         test_cmp expect actual &&
898                         echo "gitdir: ../.git/modules/repo" >expect &&
899                         test_cmp expect .git
900                 ) &&
901                 rm -rf repo &&
902                 git rm repo &&
903                 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
904                 test_must_be_empty actual &&
905                 echo "gitdir: ../.git/modules/submod" >expect &&
906                 test_cmp expect submod/.git &&
907                 (
908                         cd repo &&
909                         echo "$submodurl/bare.git" >expect &&
910                         git config remote.origin.url >actual &&
911                         test_cmp expect actual &&
912                         echo "gitdir: ../.git/modules/repo_new" >expect &&
913                         test_cmp expect .git
914                 ) &&
915                 echo "repo" >expect &&
916                 test_must_fail git config -f .gitmodules submodule.repo.path &&
917                 git config -f .gitmodules submodule.repo_new.path >actual &&
918                 test_cmp expect actual&&
919                 echo "$submodurl/repo" >expect &&
920                 test_must_fail git config -f .gitmodules submodule.repo.url &&
921                 echo "$submodurl/bare.git" >expect &&
922                 git config -f .gitmodules submodule.repo_new.url >actual &&
923                 test_cmp expect actual &&
924                 echo "$submodurl/repo" >expect &&
925                 git config submodule.repo.url >actual &&
926                 test_cmp expect actual &&
927                 echo "$submodurl/bare.git" >expect &&
928                 git config submodule.repo_new.url >actual &&
929                 test_cmp expect actual
930         )
931 '
932
933 test_expect_success 'recursive relative submodules stay relative' '
934         test_when_finished "rm -rf super clone2 subsub sub3" &&
935         mkdir subsub &&
936         (
937                 cd subsub &&
938                 git init &&
939                 >t &&
940                 git add t &&
941                 git commit -m "initial commit"
942         ) &&
943         mkdir sub3 &&
944         (
945                 cd sub3 &&
946                 git init &&
947                 >t &&
948                 git add t &&
949                 git commit -m "initial commit" &&
950                 git submodule add ../subsub dirdir/subsub &&
951                 git commit -m "add submodule subsub"
952         ) &&
953         mkdir super &&
954         (
955                 cd super &&
956                 git init &&
957                 >t &&
958                 git add t &&
959                 git commit -m "initial commit" &&
960                 git submodule add ../sub3 &&
961                 git commit -m "add submodule sub"
962         ) &&
963         git clone super clone2 &&
964         (
965                 cd clone2 &&
966                 git submodule update --init --recursive &&
967                 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
968                 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
969         ) &&
970         test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
971         test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
972 '
973
974 test_expect_success 'submodule add with an existing name fails unless forced' '
975         (
976                 cd addtest2 &&
977                 rm -rf repo &&
978                 git rm repo &&
979                 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
980                 test ! -d repo &&
981                 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
982                 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
983                 echo "$submodurl/bare.git" >expect &&
984                 git config submodule.repo_new.url >actual &&
985                 test_cmp expect actual &&
986                 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
987                 test -d repo &&
988                 echo "repo" >expect &&
989                 git config -f .gitmodules submodule.repo_new.path >actual &&
990                 test_cmp expect actual&&
991                 echo "$submodurl/repo.git" >expect &&
992                 git config -f .gitmodules submodule.repo_new.url >actual &&
993                 test_cmp expect actual &&
994                 echo "$submodurl/repo.git" >expect &&
995                 git config submodule.repo_new.url >actual &&
996                 test_cmp expect actual
997         )
998 '
999
1000 test_expect_success 'set up a second submodule' '
1001         git submodule add ./init2 example2 &&
1002         git commit -m "submodule example2 added"
1003 '
1004
1005 test_expect_success 'submodule deinit works on repository without submodules' '
1006         test_when_finished "rm -rf newdirectory" &&
1007         mkdir newdirectory &&
1008         (
1009                 cd newdirectory &&
1010                 git init &&
1011                 >file &&
1012                 git add file &&
1013                 git commit -m "repo should not be empty" &&
1014                 git submodule deinit . &&
1015                 git submodule deinit --all
1016         )
1017 '
1018
1019 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
1020         git config submodule.example.foo bar &&
1021         git config submodule.example2.frotz nitfol &&
1022         git submodule deinit init &&
1023         test -z "$(git config --get-regexp "submodule\.example\.")" &&
1024         test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1025         test -f example2/.git &&
1026         rmdir init
1027 '
1028
1029 test_expect_success 'submodule deinit should unset core.worktree' '
1030         test_path_is_file .git/modules/example/config &&
1031         test_must_fail git config -f .git/modules/example/config core.worktree
1032 '
1033
1034 test_expect_success 'submodule deinit from subdirectory' '
1035         git submodule update --init &&
1036         git config submodule.example.foo bar &&
1037         mkdir -p sub &&
1038         (
1039                 cd sub &&
1040                 git submodule deinit ../init >../output
1041         ) &&
1042         test_i18ngrep "\\.\\./init" output &&
1043         test -z "$(git config --get-regexp "submodule\.example\.")" &&
1044         test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1045         test -f example2/.git &&
1046         rmdir init
1047 '
1048
1049 test_expect_success 'submodule deinit . deinits all initialized submodules' '
1050         git submodule update --init &&
1051         git config submodule.example.foo bar &&
1052         git config submodule.example2.frotz nitfol &&
1053         test_must_fail git submodule deinit &&
1054         git submodule deinit . >actual &&
1055         test -z "$(git config --get-regexp "submodule\.example\.")" &&
1056         test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1057         test_i18ngrep "Cleared directory .init" actual &&
1058         test_i18ngrep "Cleared directory .example2" actual &&
1059         rmdir init example2
1060 '
1061
1062 test_expect_success 'submodule deinit --all deinits all initialized submodules' '
1063         git submodule update --init &&
1064         git config submodule.example.foo bar &&
1065         git config submodule.example2.frotz nitfol &&
1066         test_must_fail git submodule deinit &&
1067         git submodule deinit --all >actual &&
1068         test -z "$(git config --get-regexp "submodule\.example\.")" &&
1069         test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1070         test_i18ngrep "Cleared directory .init" actual &&
1071         test_i18ngrep "Cleared directory .example2" actual &&
1072         rmdir init example2
1073 '
1074
1075 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
1076         git submodule update --init &&
1077         rm -rf init example2/* example2/.git &&
1078         git submodule deinit init example2 >actual &&
1079         test -z "$(git config --get-regexp "submodule\.example\.")" &&
1080         test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1081         test_i18ngrep ! "Cleared directory .init" actual &&
1082         test_i18ngrep "Cleared directory .example2" actual &&
1083         rmdir init
1084 '
1085
1086 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
1087         git submodule update --init &&
1088         echo X >>init/s &&
1089         test_must_fail git submodule deinit init &&
1090         test -n "$(git config --get-regexp "submodule\.example\.")" &&
1091         test -f example2/.git &&
1092         git submodule deinit -f init >actual &&
1093         test -z "$(git config --get-regexp "submodule\.example\.")" &&
1094         test_i18ngrep "Cleared directory .init" actual &&
1095         rmdir init
1096 '
1097
1098 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1099         git submodule update --init &&
1100         echo X >>init/untracked &&
1101         test_must_fail git submodule deinit init &&
1102         test -n "$(git config --get-regexp "submodule\.example\.")" &&
1103         test -f example2/.git &&
1104         git submodule deinit -f init >actual &&
1105         test -z "$(git config --get-regexp "submodule\.example\.")" &&
1106         test_i18ngrep "Cleared directory .init" actual &&
1107         rmdir init
1108 '
1109
1110 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1111         git submodule update --init &&
1112         (
1113                 cd init &&
1114                 git checkout HEAD^
1115         ) &&
1116         test_must_fail git submodule deinit init &&
1117         test -n "$(git config --get-regexp "submodule\.example\.")" &&
1118         test -f example2/.git &&
1119         git submodule deinit -f init >actual &&
1120         test -z "$(git config --get-regexp "submodule\.example\.")" &&
1121         test_i18ngrep "Cleared directory .init" actual &&
1122         rmdir init
1123 '
1124
1125 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1126         git submodule update --init &&
1127         git submodule deinit init >actual &&
1128         test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1129         test_i18ngrep "Cleared directory .init" actual &&
1130         git submodule deinit init >actual &&
1131         test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1132         test_i18ngrep "Cleared directory .init" actual &&
1133         git submodule deinit . >actual &&
1134         test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1135         test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1136         test_i18ngrep "Cleared directory .init" actual &&
1137         git submodule deinit . >actual &&
1138         test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1139         test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1140         test_i18ngrep "Cleared directory .init" actual &&
1141         git submodule deinit --all >actual &&
1142         test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1143         test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1144         test_i18ngrep "Cleared directory .init" actual &&
1145         rmdir init example2
1146 '
1147
1148 test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
1149         git submodule update --init &&
1150         (
1151                 cd init &&
1152                 rm .git &&
1153                 cp -R ../.git/modules/example .git &&
1154                 GIT_WORK_TREE=. git config --unset core.worktree
1155         ) &&
1156         test_must_fail git submodule deinit init &&
1157         test_must_fail git submodule deinit -f init &&
1158         test -d init/.git &&
1159         test -n "$(git config --get-regexp "submodule\.example\.")"
1160 '
1161
1162 test_expect_success 'submodule with UTF-8 name' '
1163         svname=$(printf "\303\245 \303\244\303\266") &&
1164         mkdir "$svname" &&
1165         (
1166                 cd "$svname" &&
1167                 git init &&
1168                 >sub &&
1169                 git add sub &&
1170                 git commit -m "init sub"
1171         ) &&
1172         git submodule add ./"$svname" &&
1173         git submodule >&2 &&
1174         test -n "$(git submodule | grep "$svname")"
1175 '
1176
1177 test_expect_success 'submodule add clone shallow submodule' '
1178         mkdir super &&
1179         pwd=$(pwd) &&
1180         (
1181                 cd super &&
1182                 git init &&
1183                 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1184                 (
1185                         cd submodule &&
1186                         test 1 = $(git log --oneline | wc -l)
1187                 )
1188         )
1189 '
1190
1191 test_expect_success 'submodule helper list is not confused by common prefixes' '
1192         mkdir -p dir1/b &&
1193         (
1194                 cd dir1/b &&
1195                 git init &&
1196                 echo hi >testfile2 &&
1197                 git add . &&
1198                 git commit -m "test1"
1199         ) &&
1200         mkdir -p dir2/b &&
1201         (
1202                 cd dir2/b &&
1203                 git init &&
1204                 echo hello >testfile1 &&
1205                 git add .  &&
1206                 git commit -m "test2"
1207         ) &&
1208         git submodule add /dir1/b dir1/b &&
1209         git submodule add /dir2/b dir2/b &&
1210         git commit -m "first submodule commit" &&
1211         git submodule--helper list dir1/b |cut -c51- >actual &&
1212         echo "dir1/b" >expect &&
1213         test_cmp expect actual
1214 '
1215
1216 test_expect_success 'setup superproject with submodules' '
1217         git init sub1 &&
1218         test_commit -C sub1 test &&
1219         test_commit -C sub1 test2 &&
1220         git init multisuper &&
1221         git -C multisuper submodule add ../sub1 sub0 &&
1222         git -C multisuper submodule add ../sub1 sub1 &&
1223         git -C multisuper submodule add ../sub1 sub2 &&
1224         git -C multisuper submodule add ../sub1 sub3 &&
1225         git -C multisuper commit -m "add some submodules"
1226 '
1227
1228 cat >expect <<-EOF
1229 -sub0
1230  sub1 (test2)
1231  sub2 (test2)
1232  sub3 (test2)
1233 EOF
1234
1235 test_expect_success 'submodule update --init with a specification' '
1236         test_when_finished "rm -rf multisuper_clone" &&
1237         pwd=$(pwd) &&
1238         git clone file://"$pwd"/multisuper multisuper_clone &&
1239         git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1240         git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1241         test_cmp expect actual
1242 '
1243
1244 test_expect_success 'submodule update --init with submodule.active set' '
1245         test_when_finished "rm -rf multisuper_clone" &&
1246         pwd=$(pwd) &&
1247         git clone file://"$pwd"/multisuper multisuper_clone &&
1248         git -C multisuper_clone config submodule.active "." &&
1249         git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1250         git -C multisuper_clone submodule update --init &&
1251         git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1252         test_cmp expect actual
1253 '
1254
1255 test_expect_success 'submodule update and setting submodule.<name>.active' '
1256         test_when_finished "rm -rf multisuper_clone" &&
1257         pwd=$(pwd) &&
1258         git clone file://"$pwd"/multisuper multisuper_clone &&
1259         git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1260         git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1261         git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1262
1263         cat >expect <<-\EOF &&
1264          sub0 (test2)
1265         -sub1
1266          sub2 (test2)
1267         -sub3
1268         EOF
1269         git -C multisuper_clone submodule update &&
1270         git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1271         test_cmp expect actual
1272 '
1273
1274 test_expect_success 'clone active submodule without submodule url set' '
1275         test_when_finished "rm -rf test/test" &&
1276         mkdir test &&
1277         # another dir breaks accidental relative paths still being correct
1278         git clone file://"$pwd"/multisuper test/test &&
1279         (
1280                 cd test/test &&
1281                 git config submodule.active "." &&
1282
1283                 # do not pass --init flag, as the submodule is already active:
1284                 git submodule update &&
1285                 git submodule status >actual_raw &&
1286
1287                 cut -c 1,43- actual_raw >actual &&
1288                 cat >expect <<-\EOF &&
1289                  sub0 (test2)
1290                  sub1 (test2)
1291                  sub2 (test2)
1292                  sub3 (test2)
1293                 EOF
1294                 test_cmp expect actual
1295         )
1296 '
1297
1298 test_expect_success 'clone --recurse-submodules with a pathspec works' '
1299         test_when_finished "rm -rf multisuper_clone" &&
1300         cat >expected <<-\EOF &&
1301          sub0 (test2)
1302         -sub1
1303         -sub2
1304         -sub3
1305         EOF
1306
1307         git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1308         git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1309         test_cmp expected actual
1310 '
1311
1312 test_expect_success 'clone with multiple --recurse-submodules options' '
1313         test_when_finished "rm -rf multisuper_clone" &&
1314         cat >expect <<-\EOF &&
1315         -sub0
1316          sub1 (test2)
1317         -sub2
1318          sub3 (test2)
1319         EOF
1320
1321         git clone --recurse-submodules="." \
1322                   --recurse-submodules=":(exclude)sub0" \
1323                   --recurse-submodules=":(exclude)sub2" \
1324                   multisuper multisuper_clone &&
1325         git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1326         test_cmp expect actual
1327 '
1328
1329 test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
1330         test_when_finished "rm -rf multisuper_clone" &&
1331         cat <<-\EOF >expect &&
1332         -sub0
1333          sub1 (test2)
1334         -sub2
1335          sub3 (test2)
1336         EOF
1337
1338         cat <<-\EOF >expect2 &&
1339         -sub0
1340          sub1 (test2)
1341         -sub2
1342          sub3 (test2)
1343         -sub4
1344          sub5 (test2)
1345         EOF
1346
1347         git clone --recurse-submodules="." \
1348                   --recurse-submodules=":(exclude)sub0" \
1349                   --recurse-submodules=":(exclude)sub2" \
1350                   --recurse-submodules=":(exclude)sub4" \
1351                   multisuper multisuper_clone &&
1352
1353         git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1354         test_cmp expect actual &&
1355
1356         git -C multisuper submodule add ../sub1 sub4 &&
1357         git -C multisuper submodule add ../sub1 sub5 &&
1358         git -C multisuper commit -m "add more submodules" &&
1359         # obtain the new superproject
1360         git -C multisuper_clone pull &&
1361         git -C multisuper_clone submodule update --init &&
1362         git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1363         test_cmp expect2 actual
1364 '
1365
1366 test_expect_success 'init properly sets the config' '
1367         test_when_finished "rm -rf multisuper_clone" &&
1368         git clone --recurse-submodules="." \
1369                   --recurse-submodules=":(exclude)sub0" \
1370                   multisuper multisuper_clone &&
1371
1372         git -C multisuper_clone submodule init -- sub0 sub1 &&
1373         git -C multisuper_clone config --get submodule.sub0.active &&
1374         test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1375 '
1376
1377 test_expect_success 'recursive clone respects -q' '
1378         test_when_finished "rm -rf multisuper_clone" &&
1379         git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1380         test_must_be_empty actual
1381 '
1382
1383 test_done