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