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