Merge branch 'sb/mailmap-updates'
[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 'setup - initial commit' '
15         >t &&
16         git add t &&
17         git commit -m "initial commit" &&
18         git branch initial
19 '
20
21 test_expect_success 'setup - repository in init subdirectory' '
22         mkdir init &&
23         (
24                 cd init &&
25                 git init &&
26                 echo a >a &&
27                 git add a &&
28                 git commit -m "submodule commit 1" &&
29                 git tag -a -m "rev-1" rev-1
30         )
31 '
32
33 test_expect_success 'setup - commit with gitlink' '
34         echo a >a &&
35         echo z >z &&
36         git add a init z &&
37         git commit -m "super commit 1"
38 '
39
40 test_expect_success 'setup - hide init subdirectory' '
41         mv init .subrepo
42 '
43
44 test_expect_success 'setup - repository to add submodules to' '
45         git init addtest &&
46         git init addtest-ignore
47 '
48
49 # The 'submodule add' tests need some repository to add as a submodule.
50 # The trash directory is a good one as any. We need to canonicalize
51 # the name, though, as some tests compare it to the absolute path git
52 # generates, which will expand symbolic links.
53 submodurl=$(pwd -P)
54
55 listbranches() {
56         git for-each-ref --format='%(refname)' 'refs/heads/*'
57 }
58
59 inspect() {
60         dir=$1 &&
61         dotdot="${2:-..}" &&
62
63         (
64                 cd "$dir" &&
65                 listbranches >"$dotdot/heads" &&
66                 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
67                 git rev-parse HEAD >"$dotdot/head-sha1" &&
68                 git update-index --refresh &&
69                 git diff-files --exit-code &&
70                 git clean -n -d -x >"$dotdot/untracked"
71         )
72 }
73
74 test_expect_success 'submodule add' '
75         echo "refs/heads/master" >expect &&
76         >empty &&
77
78         (
79                 cd addtest &&
80                 git submodule add -q "$submodurl" submod >actual &&
81                 test_must_be_empty actual &&
82                 echo "gitdir: ../.git/modules/submod" >expect &&
83                 test_cmp expect submod/.git &&
84                 (
85                         cd submod &&
86                         git config core.worktree >actual &&
87                         echo "../../../submod" >expect &&
88                         test_cmp expect actual &&
89                         rm -f actual expect
90                 ) &&
91                 git submodule init
92         ) &&
93
94         rm -f heads head untracked &&
95         inspect addtest/submod ../.. &&
96         test_cmp expect heads &&
97         test_cmp expect head &&
98         test_cmp empty untracked
99 '
100
101 test_expect_success 'submodule add to .gitignored path fails' '
102         (
103                 cd addtest-ignore &&
104                 cat <<-\EOF >expect &&
105                 The following path is ignored by one of your .gitignore files:
106                 submod
107                 Use -f if you really want to add it.
108                 EOF
109                 # Does not use test_commit due to the ignore
110                 echo "*" > .gitignore &&
111                 git add --force .gitignore &&
112                 git commit -m"Ignore everything" &&
113                 ! git submodule add "$submodurl" submod >actual 2>&1 &&
114                 test_i18ncmp expect actual
115         )
116 '
117
118 test_expect_success 'submodule add to .gitignored path with --force' '
119         (
120                 cd addtest-ignore &&
121                 git submodule add --force "$submodurl" submod
122         )
123 '
124
125 test_expect_success 'submodule add --branch' '
126         echo "refs/heads/initial" >expect-head &&
127         cat <<-\EOF >expect-heads &&
128         refs/heads/initial
129         refs/heads/master
130         EOF
131         >empty &&
132
133         (
134                 cd addtest &&
135                 git submodule add -b initial "$submodurl" submod-branch &&
136                 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
137                 git submodule init
138         ) &&
139
140         rm -f heads head untracked &&
141         inspect addtest/submod-branch ../.. &&
142         test_cmp expect-heads heads &&
143         test_cmp expect-head head &&
144         test_cmp empty untracked
145 '
146
147 test_expect_success 'submodule add with ./ in path' '
148         echo "refs/heads/master" >expect &&
149         >empty &&
150
151         (
152                 cd addtest &&
153                 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
154                 git submodule init
155         ) &&
156
157         rm -f heads head untracked &&
158         inspect addtest/dotsubmod/frotz ../../.. &&
159         test_cmp expect heads &&
160         test_cmp expect head &&
161         test_cmp empty untracked
162 '
163
164 test_expect_success 'submodule add with // in path' '
165         echo "refs/heads/master" >expect &&
166         >empty &&
167
168         (
169                 cd addtest &&
170                 git submodule add "$submodurl" slashslashsubmod///frotz// &&
171                 git submodule init
172         ) &&
173
174         rm -f heads head untracked &&
175         inspect addtest/slashslashsubmod/frotz ../../.. &&
176         test_cmp expect heads &&
177         test_cmp expect head &&
178         test_cmp empty untracked
179 '
180
181 test_expect_success 'submodule add with /.. in path' '
182         echo "refs/heads/master" >expect &&
183         >empty &&
184
185         (
186                 cd addtest &&
187                 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
188                 git submodule init
189         ) &&
190
191         rm -f heads head untracked &&
192         inspect addtest/realsubmod ../.. &&
193         test_cmp expect heads &&
194         test_cmp expect head &&
195         test_cmp empty untracked
196 '
197
198 test_expect_success 'submodule add with ./, /.. and // in path' '
199         echo "refs/heads/master" >expect &&
200         >empty &&
201
202         (
203                 cd addtest &&
204                 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
205                 git submodule init
206         ) &&
207
208         rm -f heads head untracked &&
209         inspect addtest/realsubmod2 ../.. &&
210         test_cmp expect heads &&
211         test_cmp expect head &&
212         test_cmp empty untracked
213 '
214
215 test_expect_success 'submodule add in subdirectory' '
216         echo "refs/heads/master" >expect &&
217         >empty &&
218
219         mkdir addtest/sub &&
220         (
221                 cd addtest/sub &&
222                 git submodule add "$submodurl" ../realsubmod3 &&
223                 git submodule init
224         ) &&
225
226         rm -f heads head untracked &&
227         inspect addtest/realsubmod3 ../.. &&
228         test_cmp expect heads &&
229         test_cmp expect head &&
230         test_cmp empty untracked
231 '
232
233 test_expect_success 'submodule add in subdirectory with relative path should fail' '
234         (
235                 cd addtest/sub &&
236                 test_must_fail git submodule add ../../ submod3 2>../../output.err
237         ) &&
238         test_i18ngrep toplevel output.err
239 '
240
241 test_expect_success 'setup - add an example entry to .gitmodules' '
242         GIT_CONFIG=.gitmodules \
243         git config submodule.example.url git://example.com/init.git
244 '
245
246 test_expect_success 'status should fail for unmapped paths' '
247         test_must_fail git submodule status
248 '
249
250 test_expect_success 'setup - map path in .gitmodules' '
251         cat <<\EOF >expect &&
252 [submodule "example"]
253         url = git://example.com/init.git
254         path = init
255 EOF
256
257         GIT_CONFIG=.gitmodules git config submodule.example.path init &&
258
259         test_cmp expect .gitmodules
260 '
261
262 test_expect_success 'status should only print one line' '
263         git submodule status >lines &&
264         test_line_count = 1 lines
265 '
266
267 test_expect_success 'setup - fetch commit name from submodule' '
268         rev1=$(cd .subrepo && git rev-parse HEAD) &&
269         printf "rev1: %s\n" "$rev1" &&
270         test -n "$rev1"
271 '
272
273 test_expect_success 'status should initially be "missing"' '
274         git submodule status >lines &&
275         grep "^-$rev1" lines
276 '
277
278 test_expect_success 'init should register submodule url in .git/config' '
279         echo git://example.com/init.git >expect &&
280
281         git submodule init &&
282         git config submodule.example.url >url &&
283         git config submodule.example.url ./.subrepo &&
284
285         test_cmp expect url
286 '
287
288 test_failure_with_unknown_submodule () {
289         test_must_fail git submodule $1 no-such-submodule 2>output.err &&
290         grep "^error: .*no-such-submodule" output.err
291 }
292
293 test_expect_success 'init should fail with unknown submodule' '
294         test_failure_with_unknown_submodule init
295 '
296
297 test_expect_success 'update should fail with unknown submodule' '
298         test_failure_with_unknown_submodule update
299 '
300
301 test_expect_success 'status should fail with unknown submodule' '
302         test_failure_with_unknown_submodule status
303 '
304
305 test_expect_success 'sync should fail with unknown submodule' '
306         test_failure_with_unknown_submodule sync
307 '
308
309 test_expect_success 'update should fail when path is used by a file' '
310         echo hello >expect &&
311
312         echo "hello" >init &&
313         test_must_fail git submodule update &&
314
315         test_cmp expect init
316 '
317
318 test_expect_success 'update should fail when path is used by a nonempty directory' '
319         echo hello >expect &&
320
321         rm -fr init &&
322         mkdir init &&
323         echo "hello" >init/a &&
324
325         test_must_fail git submodule update &&
326
327         test_cmp expect init/a
328 '
329
330 test_expect_success 'update should work when path is an empty dir' '
331         rm -fr init &&
332         rm -f head-sha1 &&
333         echo "$rev1" >expect &&
334
335         mkdir init &&
336         git submodule update -q >update.out &&
337         test_must_be_empty update.out &&
338
339         inspect init &&
340         test_cmp expect head-sha1
341 '
342
343 test_expect_success 'status should be "up-to-date" after update' '
344         git submodule status >list &&
345         grep "^ $rev1" list
346 '
347
348 test_expect_success 'status "up-to-date" from subdirectory' '
349         mkdir -p sub &&
350         (
351                 cd sub &&
352                 git submodule status >../list
353         ) &&
354         grep "^ $rev1" list &&
355         grep "\\.\\./init" list
356 '
357
358 test_expect_success 'status "up-to-date" from subdirectory with path' '
359         mkdir -p sub &&
360         (
361                 cd sub &&
362                 git submodule status ../init >../list
363         ) &&
364         grep "^ $rev1" list &&
365         grep "\\.\\./init" list
366 '
367
368 test_expect_success 'status should be "modified" after submodule commit' '
369         (
370                 cd init &&
371                 echo b >b &&
372                 git add b &&
373                 git commit -m "submodule commit 2"
374         ) &&
375
376         rev2=$(cd init && git rev-parse HEAD) &&
377         test -n "$rev2" &&
378         git submodule status >list &&
379
380         grep "^+$rev2" list
381 '
382
383 test_expect_success 'the --cached sha1 should be rev1' '
384         git submodule --cached status >list &&
385         grep "^+$rev1" list
386 '
387
388 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
389         git diff >diff &&
390         grep "^+Subproject commit $rev2" diff
391 '
392
393 test_expect_success 'update should checkout rev1' '
394         rm -f head-sha1 &&
395         echo "$rev1" >expect &&
396
397         git submodule update init &&
398         inspect init &&
399
400         test_cmp expect head-sha1
401 '
402
403 test_expect_success 'status should be "up-to-date" after update' '
404         git submodule status >list &&
405         grep "^ $rev1" list
406 '
407
408 test_expect_success 'checkout superproject with subproject already present' '
409         git checkout initial &&
410         git checkout master
411 '
412
413 test_expect_success 'apply submodule diff' '
414         >empty &&
415
416         git branch second &&
417         (
418                 cd init &&
419                 echo s >s &&
420                 git add s &&
421                 git commit -m "change subproject"
422         ) &&
423         git update-index --add init &&
424         git commit -m "change init" &&
425         git format-patch -1 --stdout >P.diff &&
426         git checkout second &&
427         git apply --index P.diff &&
428
429         git diff --cached master >staged &&
430         test_cmp empty staged
431 '
432
433 test_expect_success 'update --init' '
434         mv init init2 &&
435         git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
436         git config --remove-section submodule.example &&
437         test_must_fail git config submodule.example.url &&
438
439         git submodule update init > update.out &&
440         cat update.out &&
441         test_i18ngrep "not initialized" update.out &&
442         test_must_fail git rev-parse --resolve-git-dir init/.git &&
443
444         git submodule update --init init &&
445         git rev-parse --resolve-git-dir init/.git
446 '
447
448 test_expect_success 'update --init from subdirectory' '
449         mv init init2 &&
450         git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
451         git config --remove-section submodule.example &&
452         test_must_fail git config submodule.example.url &&
453
454         mkdir -p sub &&
455         (
456                 cd sub &&
457                 git submodule update ../init >update.out &&
458                 cat update.out &&
459                 test_i18ngrep "not initialized" update.out &&
460                 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
461
462                 git submodule update --init ../init
463         ) &&
464         git rev-parse --resolve-git-dir init/.git
465 '
466
467 test_expect_success 'do not add files from a submodule' '
468
469         git reset --hard &&
470         test_must_fail git add init/a
471
472 '
473
474 test_expect_success 'gracefully add submodule with a trailing slash' '
475
476         git reset --hard &&
477         git commit -m "commit subproject" init &&
478         (cd init &&
479          echo b > a) &&
480         git add init/ &&
481         git diff --exit-code --cached init &&
482         commit=$(cd init &&
483          git commit -m update a >/dev/null &&
484          git rev-parse HEAD) &&
485         git add init/ &&
486         test_must_fail git diff --exit-code --cached init &&
487         test $commit = $(git ls-files --stage |
488                 sed -n "s/^160000 \([^ ]*\).*/\1/p")
489
490 '
491
492 test_expect_success 'ls-files gracefully handles trailing slash' '
493
494         test "init" = "$(git ls-files init/)"
495
496 '
497
498 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
499         rm -rf init &&
500         mkdir init &&
501         git reset --hard &&
502         git checkout initial &&
503         test ! -d init &&
504         git checkout second
505 '
506
507 test_expect_success 'submodule <invalid-subcommand> fails' '
508         test_must_fail git submodule no-such-subcommand
509 '
510
511 test_expect_success 'add submodules without specifying an explicit path' '
512         mkdir repo &&
513         (
514                 cd repo &&
515                 git init &&
516                 echo r >r &&
517                 git add r &&
518                 git commit -m "repo commit 1"
519         ) &&
520         git clone --bare repo/ bare.git &&
521         (
522                 cd addtest &&
523                 git submodule add "$submodurl/repo" &&
524                 git config -f .gitmodules submodule.repo.path repo &&
525                 git submodule add "$submodurl/bare.git" &&
526                 git config -f .gitmodules submodule.bare.path bare
527         )
528 '
529
530 test_expect_success 'add should fail when path is used by a file' '
531         (
532                 cd addtest &&
533                 touch file &&
534                 test_must_fail  git submodule add "$submodurl/repo" file
535         )
536 '
537
538 test_expect_success 'add should fail when path is used by an existing directory' '
539         (
540                 cd addtest &&
541                 mkdir empty-dir &&
542                 test_must_fail git submodule add "$submodurl/repo" empty-dir
543         )
544 '
545
546 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
547         (
548                 cd addtest &&
549                 git submodule add ../repo relative &&
550                 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
551                 git submodule sync relative &&
552                 test "$(git config submodule.relative.url)" = "$submodurl/repo"
553         )
554 '
555
556 test_expect_success 'set up for relative path tests' '
557         mkdir reltest &&
558         (
559                 cd reltest &&
560                 git init &&
561                 mkdir sub &&
562                 (
563                         cd sub &&
564                         git init &&
565                         test_commit foo
566                 ) &&
567                 git add sub &&
568                 git config -f .gitmodules submodule.sub.path sub &&
569                 git config -f .gitmodules submodule.sub.url ../subrepo &&
570                 cp .git/config pristine-.git-config &&
571                 cp .gitmodules pristine-.gitmodules
572         )
573 '
574
575 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
576         (
577                 cd reltest &&
578                 cp pristine-.git-config .git/config &&
579                 cp pristine-.gitmodules .gitmodules &&
580                 git config remote.origin.url ssh://hostname/repo &&
581                 git submodule init &&
582                 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
583         )
584 '
585
586 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
587         (
588                 cd reltest &&
589                 cp pristine-.git-config .git/config &&
590                 cp pristine-.gitmodules .gitmodules &&
591                 git config remote.origin.url ssh://hostname:22/repo &&
592                 git submodule init &&
593                 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
594         )
595 '
596
597 # About the choice of the path in the next test:
598 # - double-slash side-steps path mangling issues on Windows
599 # - it is still an absolute local path
600 # - there cannot be a server with a blank in its name just in case the
601 #   path is used erroneously to access a //server/share style path
602 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
603         (
604                 cd reltest &&
605                 cp pristine-.git-config .git/config &&
606                 cp pristine-.gitmodules .gitmodules &&
607                 git config remote.origin.url "//somewhere else/repo" &&
608                 git submodule init &&
609                 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
610         )
611 '
612
613 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
614         (
615                 cd reltest &&
616                 cp pristine-.git-config .git/config &&
617                 cp pristine-.gitmodules .gitmodules &&
618                 git config remote.origin.url file:///tmp/repo &&
619                 git submodule init &&
620                 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
621         )
622 '
623
624 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
625         (
626                 cd reltest &&
627                 cp pristine-.git-config .git/config &&
628                 cp pristine-.gitmodules .gitmodules &&
629                 git config remote.origin.url helper:://hostname/repo &&
630                 git submodule init &&
631                 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
632         )
633 '
634
635 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
636         (
637                 cd reltest &&
638                 cp pristine-.git-config .git/config &&
639                 git config remote.origin.url user@host:repo &&
640                 git submodule init &&
641                 test "$(git config submodule.sub.url)" = user@host:subrepo
642         )
643 '
644
645 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
646         (
647                 cd reltest &&
648                 cp pristine-.git-config .git/config &&
649                 cp pristine-.gitmodules .gitmodules &&
650                 git config remote.origin.url user@host:path/to/repo &&
651                 git submodule init &&
652                 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
653         )
654 '
655
656 test_expect_success '../subrepo works with relative local path - foo' '
657         (
658                 cd reltest &&
659                 cp pristine-.git-config .git/config &&
660                 cp pristine-.gitmodules .gitmodules &&
661                 git config remote.origin.url foo &&
662                 # actual: fails with an error
663                 git submodule init &&
664                 test "$(git config submodule.sub.url)" = subrepo
665         )
666 '
667
668 test_expect_success '../subrepo works with relative local path - foo/bar' '
669         (
670                 cd reltest &&
671                 cp pristine-.git-config .git/config &&
672                 cp pristine-.gitmodules .gitmodules &&
673                 git config remote.origin.url foo/bar &&
674                 git submodule init &&
675                 test "$(git config submodule.sub.url)" = foo/subrepo
676         )
677 '
678
679 test_expect_success '../subrepo works with relative local path - ./foo' '
680         (
681                 cd reltest &&
682                 cp pristine-.git-config .git/config &&
683                 cp pristine-.gitmodules .gitmodules &&
684                 git config remote.origin.url ./foo &&
685                 git submodule init &&
686                 test "$(git config submodule.sub.url)" = subrepo
687         )
688 '
689
690 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
691         (
692                 cd reltest &&
693                 cp pristine-.git-config .git/config &&
694                 cp pristine-.gitmodules .gitmodules &&
695                 git config remote.origin.url ./foo/bar &&
696                 git submodule init &&
697                 test "$(git config submodule.sub.url)" = foo/subrepo
698         )
699 '
700
701 test_expect_success '../subrepo works with relative local path - ../foo' '
702         (
703                 cd reltest &&
704                 cp pristine-.git-config .git/config &&
705                 cp pristine-.gitmodules .gitmodules &&
706                 git config remote.origin.url ../foo &&
707                 git submodule init &&
708                 test "$(git config submodule.sub.url)" = ../subrepo
709         )
710 '
711
712 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
713         (
714                 cd reltest &&
715                 cp pristine-.git-config .git/config &&
716                 cp pristine-.gitmodules .gitmodules &&
717                 git config remote.origin.url ../foo/bar &&
718                 git submodule init &&
719                 test "$(git config submodule.sub.url)" = ../foo/subrepo
720         )
721 '
722
723 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
724         (
725                 cd reltest &&
726                 cp pristine-.git-config .git/config &&
727                 cp pristine-.gitmodules .gitmodules &&
728                 mkdir -p a/b/c &&
729                 (cd a/b/c; git init) &&
730                 git config remote.origin.url ../foo/bar.git &&
731                 git submodule add ../bar/a/b/c ./a/b/c &&
732                 git submodule init &&
733                 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
734         )
735 '
736
737 test_expect_success 'moving the superproject does not break submodules' '
738         (
739                 cd addtest &&
740                 git submodule status >expect
741         )
742         mv addtest addtest2 &&
743         (
744                 cd addtest2 &&
745                 git submodule status >actual &&
746                 test_cmp expect actual
747         )
748 '
749
750 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
751         (
752                 cd addtest2 &&
753                 (
754                         cd repo &&
755                         echo "$submodurl/repo" >expect &&
756                         git config remote.origin.url >actual &&
757                         test_cmp expect actual &&
758                         echo "gitdir: ../.git/modules/repo" >expect &&
759                         test_cmp expect .git
760                 ) &&
761                 rm -rf repo &&
762                 git rm repo &&
763                 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
764                 test_must_be_empty actual &&
765                 echo "gitdir: ../.git/modules/submod" >expect &&
766                 test_cmp expect submod/.git &&
767                 (
768                         cd repo &&
769                         echo "$submodurl/bare.git" >expect &&
770                         git config remote.origin.url >actual &&
771                         test_cmp expect actual &&
772                         echo "gitdir: ../.git/modules/repo_new" >expect &&
773                         test_cmp expect .git
774                 ) &&
775                 echo "repo" >expect &&
776                 git config -f .gitmodules submodule.repo.path >actual &&
777                 test_cmp expect actual &&
778                 git config -f .gitmodules submodule.repo_new.path >actual &&
779                 test_cmp expect actual&&
780                 echo "$submodurl/repo" >expect &&
781                 git config -f .gitmodules submodule.repo.url >actual &&
782                 test_cmp expect actual &&
783                 echo "$submodurl/bare.git" >expect &&
784                 git config -f .gitmodules submodule.repo_new.url >actual &&
785                 test_cmp expect actual &&
786                 echo "$submodurl/repo" >expect &&
787                 git config submodule.repo.url >actual &&
788                 test_cmp expect actual &&
789                 echo "$submodurl/bare.git" >expect &&
790                 git config submodule.repo_new.url >actual &&
791                 test_cmp expect actual
792         )
793 '
794
795 test_expect_success 'submodule add with an existing name fails unless forced' '
796         (
797                 cd addtest2 &&
798                 rm -rf repo &&
799                 git rm repo &&
800                 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
801                 test ! -d repo &&
802                 echo "repo" >expect &&
803                 git config -f .gitmodules submodule.repo_new.path >actual &&
804                 test_cmp expect actual&&
805                 echo "$submodurl/bare.git" >expect &&
806                 git config -f .gitmodules submodule.repo_new.url >actual &&
807                 test_cmp expect actual &&
808                 echo "$submodurl/bare.git" >expect &&
809                 git config submodule.repo_new.url >actual &&
810                 test_cmp expect actual &&
811                 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
812                 test -d repo &&
813                 echo "repo" >expect &&
814                 git config -f .gitmodules submodule.repo_new.path >actual &&
815                 test_cmp expect actual&&
816                 echo "$submodurl/repo.git" >expect &&
817                 git config -f .gitmodules submodule.repo_new.url >actual &&
818                 test_cmp expect actual &&
819                 echo "$submodurl/repo.git" >expect &&
820                 git config submodule.repo_new.url >actual &&
821                 test_cmp expect actual
822         )
823 '
824
825 test_expect_success 'set up a second submodule' '
826         git submodule add ./init2 example2 &&
827         git commit -m "submodule example2 added"
828 '
829
830 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
831         git config submodule.example.foo bar &&
832         git config submodule.example2.frotz nitfol &&
833         git submodule deinit init &&
834         test -z "$(git config --get-regexp "submodule\.example\.")" &&
835         test -n "$(git config --get-regexp "submodule\.example2\.")" &&
836         test -f example2/.git &&
837         rmdir init
838 '
839
840 test_expect_success 'submodule deinit from subdirectory' '
841         git submodule update --init &&
842         git config submodule.example.foo bar &&
843         mkdir -p sub &&
844         (
845                 cd sub &&
846                 git submodule deinit ../init >../output
847         ) &&
848         grep "\\.\\./init" output &&
849         test -z "$(git config --get-regexp "submodule\.example\.")" &&
850         test -n "$(git config --get-regexp "submodule\.example2\.")" &&
851         test -f example2/.git &&
852         rmdir init
853 '
854
855 test_expect_success 'submodule deinit . deinits all initialized submodules' '
856         git submodule update --init &&
857         git config submodule.example.foo bar &&
858         git config submodule.example2.frotz nitfol &&
859         test_must_fail git submodule deinit &&
860         git submodule deinit . >actual &&
861         test -z "$(git config --get-regexp "submodule\.example\.")" &&
862         test -z "$(git config --get-regexp "submodule\.example2\.")" &&
863         test_i18ngrep "Cleared directory .init" actual &&
864         test_i18ngrep "Cleared directory .example2" actual &&
865         rmdir init example2
866 '
867
868 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
869         git submodule update --init &&
870         rm -rf init example2/* example2/.git &&
871         git submodule deinit init example2 >actual &&
872         test -z "$(git config --get-regexp "submodule\.example\.")" &&
873         test -z "$(git config --get-regexp "submodule\.example2\.")" &&
874         test_i18ngrep ! "Cleared directory .init" actual &&
875         test_i18ngrep "Cleared directory .example2" actual &&
876         rmdir init
877 '
878
879 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
880         git submodule update --init &&
881         echo X >>init/s &&
882         test_must_fail git submodule deinit init &&
883         test -n "$(git config --get-regexp "submodule\.example\.")" &&
884         test -f example2/.git &&
885         git submodule deinit -f init >actual &&
886         test -z "$(git config --get-regexp "submodule\.example\.")" &&
887         test_i18ngrep "Cleared directory .init" actual &&
888         rmdir init
889 '
890
891 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
892         git submodule update --init &&
893         echo X >>init/untracked &&
894         test_must_fail git submodule deinit init &&
895         test -n "$(git config --get-regexp "submodule\.example\.")" &&
896         test -f example2/.git &&
897         git submodule deinit -f init >actual &&
898         test -z "$(git config --get-regexp "submodule\.example\.")" &&
899         test_i18ngrep "Cleared directory .init" actual &&
900         rmdir init
901 '
902
903 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
904         git submodule update --init &&
905         (
906                 cd init &&
907                 git checkout HEAD^
908         ) &&
909         test_must_fail git submodule deinit init &&
910         test -n "$(git config --get-regexp "submodule\.example\.")" &&
911         test -f example2/.git &&
912         git submodule deinit -f init >actual &&
913         test -z "$(git config --get-regexp "submodule\.example\.")" &&
914         test_i18ngrep "Cleared directory .init" actual &&
915         rmdir init
916 '
917
918 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
919         git submodule update --init &&
920         git submodule deinit init >actual &&
921         test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
922         test_i18ngrep "Cleared directory .init" actual &&
923         git submodule deinit init >actual &&
924         test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
925         test_i18ngrep "Cleared directory .init" actual &&
926         git submodule deinit . >actual &&
927         test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
928         test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
929         test_i18ngrep "Cleared directory .init" actual &&
930         git submodule deinit . >actual &&
931         test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
932         test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
933         test_i18ngrep "Cleared directory .init" actual &&
934         rmdir init example2
935 '
936
937 test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
938         git submodule update --init &&
939         (
940                 cd init &&
941                 rm .git &&
942                 cp -R ../.git/modules/example .git &&
943                 GIT_WORK_TREE=. git config --unset core.worktree
944         ) &&
945         test_must_fail git submodule deinit init &&
946         test_must_fail git submodule deinit -f init &&
947         test -d init/.git &&
948         test -n "$(git config --get-regexp "submodule\.example\.")"
949 '
950
951 test_expect_success 'submodule with UTF-8 name' '
952         svname=$(printf "\303\245 \303\244\303\266") &&
953         mkdir "$svname" &&
954         (
955                 cd "$svname" &&
956                 git init &&
957                 >sub &&
958                 git add sub &&
959                 git commit -m "init sub"
960         ) &&
961         test_config core.precomposeunicode true &&
962         git submodule add ./"$svname" &&
963         git submodule >&2 &&
964         test -n "$(git submodule | grep "$svname")"
965 '
966
967 test_expect_success 'submodule add clone shallow submodule' '
968         mkdir super &&
969         pwd=$(pwd)
970         (
971                 cd super &&
972                 git init &&
973                 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
974                 (
975                         cd submodule &&
976                         test 1 = $(git log --oneline | wc -l)
977                 )
978         )
979 '
980
981
982 test_done