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