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