The eleventh batch
[git/gitster.git] / t / t7406-submodule-update.sh
blob0f0c86f9cb20cefb4c33ff01750a5d30790fbaf9
1 #!/bin/sh
3 # Copyright (c) 2009 Red Hat, Inc.
6 test_description='Test updating submodules
8 This test verifies that "git submodule update" detaches the HEAD of the
9 submodule and "git submodule update --rebase/--merge" does not detach the HEAD.
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
15 TEST_PASSES_SANITIZE_LEAK=true
16 . ./test-lib.sh
19 compare_head()
21 sha_main=$(git rev-list --max-count=1 main)
22 sha_head=$(git rev-list --max-count=1 HEAD)
24 test "$sha_main" = "$sha_head"
28 test_expect_success 'setup a submodule tree' '
29 git config --global protocol.file.allow always &&
30 echo file > file &&
31 git add file &&
32 test_tick &&
33 git commit -m upstream &&
34 git clone . super &&
35 git clone super submodule &&
36 git clone super rebasing &&
37 git clone super merging &&
38 git clone super none &&
39 (cd super &&
40 git submodule add ../submodule submodule &&
41 test_tick &&
42 git commit -m "submodule" &&
43 git submodule init submodule
44 ) &&
45 (cd submodule &&
46 echo "line2" > file &&
47 git add file &&
48 git commit -m "Commit 2"
49 ) &&
50 (cd super &&
51 (cd submodule &&
52 git pull --rebase origin
53 ) &&
54 git add submodule &&
55 git commit -m "submodule update"
56 ) &&
57 (cd super &&
58 git submodule add ../rebasing rebasing &&
59 test_tick &&
60 git commit -m "rebasing"
61 ) &&
62 (cd super &&
63 git submodule add ../merging merging &&
64 test_tick &&
65 git commit -m "rebasing"
66 ) &&
67 (cd super &&
68 git submodule add ../none none &&
69 test_tick &&
70 git commit -m "none"
71 ) &&
72 git clone . recursivesuper &&
73 ( cd recursivesuper &&
74 git submodule add ../super super
78 test_expect_success 'update --remote falls back to using HEAD' '
79 test_create_repo main-branch-submodule &&
80 test_commit -C main-branch-submodule initial &&
82 test_create_repo main-branch &&
83 git -C main-branch submodule add ../main-branch-submodule &&
84 git -C main-branch commit -m add-submodule &&
86 git -C main-branch-submodule switch -c hello &&
87 test_commit -C main-branch-submodule world &&
89 git clone --recursive main-branch main-branch-clone &&
90 git -C main-branch-clone submodule update --remote main-branch-submodule &&
91 test_path_exists main-branch-clone/main-branch-submodule/world.t
94 test_expect_success 'submodule update detaching the HEAD ' '
95 (cd super/submodule &&
96 git reset --hard HEAD~1
97 ) &&
98 (cd super &&
99 (cd submodule &&
100 compare_head
101 ) &&
102 git submodule update submodule &&
103 cd submodule &&
104 ! compare_head
108 test_expect_success 'submodule update from subdirectory' '
109 (cd super/submodule &&
110 git reset --hard HEAD~1
111 ) &&
112 mkdir super/sub &&
113 (cd super/sub &&
114 (cd ../submodule &&
115 compare_head
116 ) &&
117 git submodule update ../submodule &&
118 cd ../submodule &&
119 ! compare_head
123 supersha1=$(git -C super rev-parse HEAD)
124 mergingsha1=$(git -C super/merging rev-parse HEAD)
125 nonesha1=$(git -C super/none rev-parse HEAD)
126 rebasingsha1=$(git -C super/rebasing rev-parse HEAD)
127 submodulesha1=$(git -C super/submodule rev-parse HEAD)
128 pwd=$(pwd)
130 cat <<EOF >expect
131 Submodule path '../super': checked out '$supersha1'
132 Submodule path '../super/merging': checked out '$mergingsha1'
133 Submodule path '../super/none': checked out '$nonesha1'
134 Submodule path '../super/rebasing': checked out '$rebasingsha1'
135 Submodule path '../super/submodule': checked out '$submodulesha1'
138 cat <<EOF >expect2
139 Cloning into '$pwd/recursivesuper/super/merging'...
140 Cloning into '$pwd/recursivesuper/super/none'...
141 Cloning into '$pwd/recursivesuper/super/rebasing'...
142 Cloning into '$pwd/recursivesuper/super/submodule'...
143 Submodule 'merging' ($pwd/merging) registered for path '../super/merging'
144 Submodule 'none' ($pwd/none) registered for path '../super/none'
145 Submodule 'rebasing' ($pwd/rebasing) registered for path '../super/rebasing'
146 Submodule 'submodule' ($pwd/submodule) registered for path '../super/submodule'
147 done.
148 done.
149 done.
150 done.
153 test_expect_success 'submodule update --init --recursive from subdirectory' '
154 git -C recursivesuper/super reset --hard HEAD^ &&
155 (cd recursivesuper &&
156 mkdir tmp &&
157 cd tmp &&
158 git submodule update --init --recursive ../super >../../actual 2>../../actual2
159 ) &&
160 test_cmp expect actual &&
161 sort actual2 >actual2.sorted &&
162 test_cmp expect2 actual2.sorted
165 cat <<EOF >expect2
166 Submodule 'foo/sub' ($pwd/withsubs/../rebasing) registered for path 'sub'
169 test_expect_success 'submodule update --init from and of subdirectory' '
170 git init withsubs &&
171 (cd withsubs &&
172 mkdir foo &&
173 git submodule add "$(pwd)/../rebasing" foo/sub &&
174 (cd foo &&
175 git submodule deinit -f sub &&
176 git submodule update --init sub 2>../../actual2
178 ) &&
179 test_cmp expect2 actual2
182 test_expect_success 'submodule update does not fetch already present commits' '
183 (cd submodule &&
184 echo line3 >> file &&
185 git add file &&
186 test_tick &&
187 git commit -m "upstream line3"
188 ) &&
189 (cd super/submodule &&
190 head=$(git rev-parse --verify HEAD) &&
191 echo "Submodule path ${SQ}submodule$SQ: checked out $SQ$head$SQ" > ../../expected &&
192 git reset --hard HEAD~1
193 ) &&
194 (cd super &&
195 git submodule update > ../actual 2> ../actual.err
196 ) &&
197 test_cmp expected actual &&
198 test_must_be_empty actual.err
201 test_expect_success 'submodule update should fail due to local changes' '
202 (cd super/submodule &&
203 git reset --hard HEAD~1 &&
204 echo "local change" > file
205 ) &&
206 (cd super &&
207 (cd submodule &&
208 compare_head
209 ) &&
210 test_must_fail git submodule update submodule 2>../actual.raw
211 ) &&
212 sed "s/^> //" >expect <<-\EOF &&
213 > error: Your local changes to the following files would be overwritten by checkout:
214 > file
215 > Please commit your changes or stash them before you switch branches.
216 > Aborting
217 > fatal: Unable to checkout OID in submodule path '\''submodule'\''
219 sed -e "s/checkout $SQ[^$SQ]*$SQ/checkout OID/" <actual.raw >actual &&
220 test_cmp expect actual
223 test_expect_success 'submodule update should throw away changes with --force ' '
224 (cd super &&
225 (cd submodule &&
226 compare_head
227 ) &&
228 git submodule update --force submodule &&
229 cd submodule &&
230 ! compare_head
234 test_expect_success 'submodule update --force forcibly checks out submodules' '
235 (cd super &&
236 (cd submodule &&
237 rm -f file
238 ) &&
239 git submodule update --force submodule &&
240 (cd submodule &&
241 test "$(git status -s file)" = ""
246 test_expect_success 'submodule update --remote should fetch upstream changes' '
247 (cd submodule &&
248 echo line4 >> file &&
249 git add file &&
250 test_tick &&
251 git commit -m "upstream line4"
252 ) &&
253 (cd super &&
254 git submodule update --remote --force submodule &&
255 cd submodule &&
256 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
260 test_expect_success 'submodule update --remote should fetch upstream changes with .' '
262 cd super &&
263 git config -f .gitmodules submodule."submodule".branch "." &&
264 git add .gitmodules &&
265 git commit -m "submodules: update from the respective superproject branch"
266 ) &&
268 cd submodule &&
269 echo line4a >> file &&
270 git add file &&
271 test_tick &&
272 git commit -m "upstream line4a" &&
273 git checkout -b test-branch &&
274 test_commit on-test-branch
275 ) &&
277 cd super &&
278 git submodule update --remote --force submodule &&
279 git -C submodule log -1 --oneline >actual &&
280 git -C ../submodule log -1 --oneline main >expect &&
281 test_cmp expect actual &&
282 git checkout -b test-branch &&
283 git submodule update --remote --force submodule &&
284 git -C submodule log -1 --oneline >actual &&
285 git -C ../submodule log -1 --oneline test-branch >expect &&
286 test_cmp expect actual &&
287 git checkout main &&
288 git branch -d test-branch &&
289 git reset --hard HEAD^
293 test_expect_success 'local config should override .gitmodules branch' '
294 (cd submodule &&
295 git checkout test-branch &&
296 echo line5 >> file &&
297 git add file &&
298 test_tick &&
299 git commit -m "upstream line5" &&
300 git checkout main
301 ) &&
302 (cd super &&
303 git config submodule.submodule.branch test-branch &&
304 git submodule update --remote --force submodule &&
305 cd submodule &&
306 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
310 test_expect_success 'submodule update --rebase staying on main' '
311 (cd super/submodule &&
312 git checkout main
313 ) &&
314 (cd super &&
315 (cd submodule &&
316 compare_head
317 ) &&
318 git submodule update --rebase submodule &&
319 cd submodule &&
320 compare_head
324 test_expect_success 'submodule update --merge staying on main' '
325 (cd super/submodule &&
326 git reset --hard HEAD~1
327 ) &&
328 (cd super &&
329 (cd submodule &&
330 compare_head
331 ) &&
332 git submodule update --merge submodule &&
333 cd submodule &&
334 compare_head
338 test_expect_success 'submodule update - rebase in .git/config' '
339 (cd super &&
340 git config submodule.submodule.update rebase
341 ) &&
342 (cd super/submodule &&
343 git reset --hard HEAD~1
344 ) &&
345 (cd super &&
346 (cd submodule &&
347 compare_head
348 ) &&
349 git submodule update submodule &&
350 cd submodule &&
351 compare_head
355 test_expect_success 'submodule update - checkout in .git/config but --rebase given' '
356 (cd super &&
357 git config submodule.submodule.update checkout
358 ) &&
359 (cd super/submodule &&
360 git reset --hard HEAD~1
361 ) &&
362 (cd super &&
363 (cd submodule &&
364 compare_head
365 ) &&
366 git submodule update --rebase submodule &&
367 cd submodule &&
368 compare_head
372 test_expect_success 'submodule update - merge in .git/config' '
373 (cd super &&
374 git config submodule.submodule.update merge
375 ) &&
376 (cd super/submodule &&
377 git reset --hard HEAD~1
378 ) &&
379 (cd super &&
380 (cd submodule &&
381 compare_head
382 ) &&
383 git submodule update submodule &&
384 cd submodule &&
385 compare_head
389 test_expect_success 'submodule update - checkout in .git/config but --merge given' '
390 (cd super &&
391 git config submodule.submodule.update checkout
392 ) &&
393 (cd super/submodule &&
394 git reset --hard HEAD~1
395 ) &&
396 (cd super &&
397 (cd submodule &&
398 compare_head
399 ) &&
400 git submodule update --merge submodule &&
401 cd submodule &&
402 compare_head
406 test_expect_success 'submodule update - checkout in .git/config' '
407 (cd super &&
408 git config submodule.submodule.update checkout
409 ) &&
410 (cd super/submodule &&
411 git reset --hard HEAD^
412 ) &&
413 (cd super &&
414 (cd submodule &&
415 compare_head
416 ) &&
417 git submodule update submodule &&
418 cd submodule &&
419 ! compare_head
423 test_expect_success 'submodule update - command in .git/config' '
424 (cd super &&
425 git config submodule.submodule.update "!git checkout"
426 ) &&
427 (cd super/submodule &&
428 git reset --hard HEAD^
429 ) &&
430 (cd super &&
431 (cd submodule &&
432 compare_head
433 ) &&
434 git submodule update submodule &&
435 cd submodule &&
436 ! compare_head
440 test_expect_success 'submodule update - command in .gitmodules is rejected' '
441 test_when_finished "git -C super reset --hard HEAD^" &&
442 git -C super config -f .gitmodules submodule.submodule.update "!false" &&
443 git -C super commit -a -m "add command to .gitmodules file" &&
444 git -C super/submodule reset --hard $submodulesha1^ &&
445 test_must_fail git -C super submodule update submodule
448 test_expect_success 'fsck detects command in .gitmodules' '
449 git init command-in-gitmodules &&
451 cd command-in-gitmodules &&
452 git submodule add ../submodule submodule &&
453 test_commit adding-submodule &&
455 git config -f .gitmodules submodule.submodule.update "!false" &&
456 git add .gitmodules &&
457 test_commit configuring-update &&
458 test_must_fail git fsck
462 cat << EOF >expect
463 fatal: Execution of 'false $submodulesha1' failed in submodule path 'submodule'
466 test_expect_success 'submodule update - command in .git/config catches failure' '
467 (cd super &&
468 git config submodule.submodule.update "!false"
469 ) &&
470 (cd super/submodule &&
471 git reset --hard $submodulesha1^
472 ) &&
473 (cd super &&
474 test_must_fail git submodule update submodule 2>../actual
475 ) &&
476 test_cmp actual expect
479 cat << EOF >expect
480 fatal: Execution of 'false $submodulesha1' failed in submodule path '../submodule'
483 test_expect_success 'submodule update - command in .git/config catches failure -- subdirectory' '
484 (cd super &&
485 git config submodule.submodule.update "!false"
486 ) &&
487 (cd super/submodule &&
488 git reset --hard $submodulesha1^
489 ) &&
490 (cd super &&
491 mkdir tmp && cd tmp &&
492 test_must_fail git submodule update ../submodule 2>../../actual
493 ) &&
494 test_cmp actual expect
497 test_expect_success 'submodule update - command run for initial population of submodule' '
498 cat >expect <<-EOF &&
499 fatal: Execution of '\''false $submodulesha1'\'' failed in submodule path '\''submodule'\''
501 rm -rf super/submodule &&
502 test_must_fail git -C super submodule update 2>actual &&
503 test_cmp expect actual &&
504 git -C super submodule update --checkout
507 cat << EOF >expect
508 fatal: Execution of 'false $submodulesha1' failed in submodule path '../super/submodule'
509 fatal: Failed to recurse into submodule path '../super'
512 test_expect_success 'recursive submodule update - command in .git/config catches failure -- subdirectory' '
513 (cd recursivesuper &&
514 git submodule update --remote super &&
515 git add super &&
516 git commit -m "update to latest to have more than one commit in submodules"
517 ) &&
518 git -C recursivesuper/super config submodule.submodule.update "!false" &&
519 git -C recursivesuper/super/submodule reset --hard $submodulesha1^ &&
520 (cd recursivesuper &&
521 mkdir -p tmp && cd tmp &&
522 test_must_fail git submodule update --recursive ../super 2>../../actual
523 ) &&
524 test_cmp actual expect
527 test_expect_success 'submodule init does not copy command into .git/config' '
528 test_when_finished "git -C super update-index --force-remove submodule1" &&
529 test_when_finished git config -f super/.gitmodules \
530 --remove-section submodule.submodule1 &&
531 (cd super &&
532 git ls-files -s submodule >out &&
533 H=$(cut -d" " -f2 out) &&
534 mkdir submodule1 &&
535 git update-index --add --cacheinfo 160000 $H submodule1 &&
536 git config -f .gitmodules submodule.submodule1.path submodule1 &&
537 git config -f .gitmodules submodule.submodule1.url ../submodule &&
538 git config -f .gitmodules submodule.submodule1.update !false &&
539 test_must_fail git submodule init submodule1 &&
540 test_expect_code 1 git config submodule.submodule1.update >actual &&
541 test_must_be_empty actual
545 test_expect_success 'submodule init picks up rebase' '
546 (cd super &&
547 git config -f .gitmodules submodule.rebasing.update rebase &&
548 git submodule init rebasing &&
549 test "rebase" = "$(git config submodule.rebasing.update)"
553 test_expect_success 'submodule init picks up merge' '
554 (cd super &&
555 git config -f .gitmodules submodule.merging.update merge &&
556 git submodule init merging &&
557 test "merge" = "$(git config submodule.merging.update)"
561 test_expect_success 'submodule update --merge - ignores --merge for new submodules' '
562 test_config -C super submodule.submodule.update checkout &&
563 (cd super &&
564 rm -rf submodule &&
565 git submodule update submodule &&
566 git status -s submodule >expect &&
567 rm -rf submodule &&
568 git submodule update --merge submodule &&
569 git status -s submodule >actual &&
570 test_cmp expect actual
574 test_expect_success 'submodule update --rebase - ignores --rebase for new submodules' '
575 test_config -C super submodule.submodule.update checkout &&
576 (cd super &&
577 rm -rf submodule &&
578 git submodule update submodule &&
579 git status -s submodule >expect &&
580 rm -rf submodule &&
581 git submodule update --rebase submodule &&
582 git status -s submodule >actual &&
583 test_cmp expect actual
587 test_expect_success 'submodule update ignores update=merge config for new submodules' '
588 (cd super &&
589 rm -rf submodule &&
590 git submodule update submodule &&
591 git status -s submodule >expect &&
592 rm -rf submodule &&
593 git config submodule.submodule.update merge &&
594 git submodule update submodule &&
595 git status -s submodule >actual &&
596 git config --unset submodule.submodule.update &&
597 test_cmp expect actual
601 test_expect_success 'submodule update ignores update=rebase config for new submodules' '
602 (cd super &&
603 rm -rf submodule &&
604 git submodule update submodule &&
605 git status -s submodule >expect &&
606 rm -rf submodule &&
607 git config submodule.submodule.update rebase &&
608 git submodule update submodule &&
609 git status -s submodule >actual &&
610 git config --unset submodule.submodule.update &&
611 test_cmp expect actual
615 test_expect_success 'submodule init picks up update=none' '
616 (cd super &&
617 git config -f .gitmodules submodule.none.update none &&
618 git submodule init none &&
619 test "none" = "$(git config submodule.none.update)"
623 test_expect_success 'submodule update - update=none in .git/config' '
624 (cd super &&
625 git config submodule.submodule.update none &&
626 (cd submodule &&
627 git checkout main &&
628 compare_head
629 ) &&
630 git diff --name-only >out &&
631 grep ^submodule$ out &&
632 git submodule update &&
633 git diff --name-only >out &&
634 grep ^submodule$ out &&
635 (cd submodule &&
636 compare_head
637 ) &&
638 git config --unset submodule.submodule.update &&
639 git submodule update submodule
643 test_expect_success 'submodule update - update=none in .git/config but --checkout given' '
644 (cd super &&
645 git config submodule.submodule.update none &&
646 (cd submodule &&
647 git checkout main &&
648 compare_head
649 ) &&
650 git diff --name-only >out &&
651 grep ^submodule$ out &&
652 git submodule update --checkout &&
653 git diff --name-only >out &&
654 ! grep ^submodule$ out &&
655 (cd submodule &&
656 ! compare_head
657 ) &&
658 git config --unset submodule.submodule.update
662 test_expect_success 'submodule update --init skips submodule with update=none' '
663 (cd super &&
664 git add .gitmodules &&
665 git commit -m ".gitmodules"
666 ) &&
667 git clone super cloned &&
668 (cd cloned &&
669 git submodule update --init &&
670 test_path_exists submodule/.git &&
671 test_path_is_missing none/.git
675 test_expect_success 'submodule update with pathspec warns against uninitialized ones' '
676 test_when_finished "rm -fr selective" &&
677 git clone super selective &&
679 cd selective &&
680 git submodule init submodule &&
682 git submodule update submodule 2>err &&
683 ! grep "Submodule path .* not initialized" err &&
685 git submodule update rebasing 2>err &&
686 grep "Submodule path .rebasing. not initialized" err &&
688 test_path_exists submodule/.git &&
689 test_path_is_missing rebasing/.git
694 test_expect_success 'submodule update without pathspec updates only initialized ones' '
695 test_when_finished "rm -fr selective" &&
696 git clone super selective &&
698 cd selective &&
699 git submodule init submodule &&
700 git submodule update 2>err &&
701 test_path_exists submodule/.git &&
702 test_path_is_missing rebasing/.git &&
703 ! grep "Submodule path .* not initialized" err
708 test_expect_success 'submodule update continues after checkout error' '
709 (cd super &&
710 git reset --hard HEAD &&
711 git submodule add ../submodule submodule2 &&
712 git submodule init &&
713 git commit -am "new_submodule" &&
714 (cd submodule2 &&
715 git rev-parse --verify HEAD >../expect
716 ) &&
717 (cd submodule &&
718 test_commit "update_submodule" file
719 ) &&
720 (cd submodule2 &&
721 test_commit "update_submodule2" file
722 ) &&
723 git add submodule &&
724 git add submodule2 &&
725 git commit -m "two_new_submodule_commits" &&
726 (cd submodule &&
727 echo "" > file
728 ) &&
729 git checkout HEAD^ &&
730 test_must_fail git submodule update &&
731 (cd submodule2 &&
732 git rev-parse --verify HEAD >../actual
733 ) &&
734 test_cmp expect actual
737 test_expect_success 'submodule update continues after recursive checkout error' '
738 (cd super &&
739 git reset --hard HEAD &&
740 git checkout main &&
741 git submodule update &&
742 (cd submodule &&
743 git submodule add ../submodule subsubmodule &&
744 git submodule init &&
745 git commit -m "new_subsubmodule"
746 ) &&
747 git add submodule &&
748 git commit -m "update_submodule" &&
749 (cd submodule &&
750 (cd subsubmodule &&
751 test_commit "update_subsubmodule" file
752 ) &&
753 git add subsubmodule &&
754 test_commit "update_submodule_again" file &&
755 (cd subsubmodule &&
756 test_commit "update_subsubmodule_again" file
757 ) &&
758 test_commit "update_submodule_again_again" file
759 ) &&
760 (cd submodule2 &&
761 git rev-parse --verify HEAD >../expect &&
762 test_commit "update_submodule2_again" file
763 ) &&
764 git add submodule &&
765 git add submodule2 &&
766 git commit -m "new_commits" &&
767 git checkout HEAD^ &&
768 (cd submodule &&
769 git checkout HEAD^ &&
770 (cd subsubmodule &&
771 echo "" > file
773 ) &&
774 test_expect_code 1 git submodule update --recursive &&
775 (cd submodule2 &&
776 git rev-parse --verify HEAD >../actual
777 ) &&
778 test_cmp expect actual
782 test_expect_success 'submodule update exit immediately in case of merge conflict' '
783 (cd super &&
784 git checkout main &&
785 git reset --hard HEAD &&
786 (cd submodule &&
787 (cd subsubmodule &&
788 git reset --hard HEAD
790 ) &&
791 git submodule update --recursive &&
792 (cd submodule &&
793 test_commit "update_submodule_2" file
794 ) &&
795 (cd submodule2 &&
796 test_commit "update_submodule2_2" file
797 ) &&
798 git add submodule &&
799 git add submodule2 &&
800 git commit -m "two_new_submodule_commits" &&
801 (cd submodule &&
802 git checkout main &&
803 test_commit "conflict" file &&
804 echo "conflict" > file
805 ) &&
806 git checkout HEAD^ &&
807 (cd submodule2 &&
808 git rev-parse --verify HEAD >../expect
809 ) &&
810 git config submodule.submodule.update merge &&
811 test_must_fail git submodule update &&
812 (cd submodule2 &&
813 git rev-parse --verify HEAD >../actual
814 ) &&
815 test_cmp expect actual
819 test_expect_success 'submodule update exit immediately after recursive rebase error' '
820 (cd super &&
821 git checkout main &&
822 git reset --hard HEAD &&
823 (cd submodule &&
824 git reset --hard HEAD &&
825 git submodule update --recursive
826 ) &&
827 (cd submodule &&
828 test_commit "update_submodule_3" file
829 ) &&
830 (cd submodule2 &&
831 test_commit "update_submodule2_3" file
832 ) &&
833 git add submodule &&
834 git add submodule2 &&
835 git commit -m "two_new_submodule_commits" &&
836 (cd submodule &&
837 git checkout main &&
838 test_commit "conflict2" file &&
839 echo "conflict" > file
840 ) &&
841 git checkout HEAD^ &&
842 (cd submodule2 &&
843 git rev-parse --verify HEAD >../expect
844 ) &&
845 git config submodule.submodule.update rebase &&
846 test_must_fail git submodule update &&
847 (cd submodule2 &&
848 git rev-parse --verify HEAD >../actual
849 ) &&
850 test_cmp expect actual
854 test_expect_success 'add different submodules to the same path' '
855 (cd super &&
856 git submodule add ../submodule s1 &&
857 test_must_fail git submodule add ../merging s1
861 test_expect_success 'submodule add places git-dir in superprojects git-dir' '
862 (cd super &&
863 mkdir deeper &&
864 git submodule add ../submodule deeper/submodule &&
865 (cd deeper/submodule &&
866 git log > ../../expected
867 ) &&
868 (cd .git/modules/deeper/submodule &&
869 git log > ../../../../actual
870 ) &&
871 test_cmp expected actual
875 test_expect_success 'submodule update places git-dir in superprojects git-dir' '
876 (cd super &&
877 git commit -m "added submodule"
878 ) &&
879 git clone super super2 &&
880 (cd super2 &&
881 git submodule init deeper/submodule &&
882 git submodule update &&
883 (cd deeper/submodule &&
884 git log > ../../expected
885 ) &&
886 (cd .git/modules/deeper/submodule &&
887 git log > ../../../../actual
888 ) &&
889 test_cmp expected actual
893 test_expect_success 'submodule add places git-dir in superprojects git-dir recursive' '
894 (cd super2 &&
895 (cd deeper/submodule &&
896 git submodule add ../submodule subsubmodule &&
897 (cd subsubmodule &&
898 git log > ../../../expected
899 ) &&
900 git commit -m "added subsubmodule" &&
901 git push origin :
902 ) &&
903 (cd .git/modules/deeper/submodule/modules/subsubmodule &&
904 git log > ../../../../../actual
905 ) &&
906 git add deeper/submodule &&
907 git commit -m "update submodule" &&
908 git push origin : &&
909 test_cmp expected actual
913 test_expect_success 'submodule update places git-dir in superprojects git-dir recursive' '
914 mkdir super_update_r &&
915 (cd super_update_r &&
916 git init --bare
917 ) &&
918 mkdir subsuper_update_r &&
919 (cd subsuper_update_r &&
920 git init --bare
921 ) &&
922 mkdir subsubsuper_update_r &&
923 (cd subsubsuper_update_r &&
924 git init --bare
925 ) &&
926 git clone subsubsuper_update_r subsubsuper_update_r2 &&
927 (cd subsubsuper_update_r2 &&
928 test_commit "update_subsubsuper" file &&
929 git push origin main
930 ) &&
931 git clone subsuper_update_r subsuper_update_r2 &&
932 (cd subsuper_update_r2 &&
933 test_commit "update_subsuper" file &&
934 git submodule add ../subsubsuper_update_r subsubmodule &&
935 git commit -am "subsubmodule" &&
936 git push origin main
937 ) &&
938 git clone super_update_r super_update_r2 &&
939 (cd super_update_r2 &&
940 test_commit "update_super" file &&
941 git submodule add ../subsuper_update_r submodule &&
942 git commit -am "submodule" &&
943 git push origin main
944 ) &&
945 rm -rf super_update_r2 &&
946 git clone super_update_r super_update_r2 &&
947 (cd super_update_r2 &&
948 git submodule update --init --recursive >actual &&
949 test_grep "Submodule path .submodule/subsubmodule.: checked out" actual &&
950 (cd submodule/subsubmodule &&
951 git log > ../../expected
952 ) &&
953 (cd .git/modules/submodule/modules/subsubmodule &&
954 git log > ../../../../../actual
955 ) &&
956 test_cmp expected actual
960 test_expect_success 'submodule add properly re-creates deeper level submodules' '
961 (cd super &&
962 git reset --hard main &&
963 rm -rf deeper/ &&
964 git submodule add --force ../submodule deeper/submodule
968 test_expect_success 'submodule update properly revives a moved submodule' '
969 (cd super &&
970 H=$(git rev-parse --short HEAD) &&
971 git commit -am "pre move" &&
972 H2=$(git rev-parse --short HEAD) &&
973 git status >out &&
974 sed "s/$H/XXX/" out >expect &&
975 H=$(cd submodule2 && git rev-parse HEAD) &&
976 git rm --cached submodule2 &&
977 rm -rf submodule2 &&
978 mkdir -p "moved/sub module" &&
979 git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
980 git config -f .gitmodules submodule.submodule2.path "moved/sub module" &&
981 git commit -am "post move" &&
982 git submodule update &&
983 git status > out &&
984 sed "s/$H2/XXX/" out >actual &&
985 test_cmp expect actual
989 test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
990 mkdir -p linked/dir &&
991 ln -s linked/dir linkto &&
992 (cd linkto &&
993 git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
994 (cd super &&
995 git submodule update --init --recursive
1000 test_expect_success 'submodule update clone shallow submodule' '
1001 test_when_finished "rm -rf super3" &&
1002 first=$(git -C cloned rev-parse HEAD:submodule) &&
1003 second=$(git -C submodule rev-parse HEAD) &&
1004 commit_count=$(git -C submodule rev-list --count $first^..$second) &&
1005 git clone cloned super3 &&
1006 pwd=$(pwd) &&
1008 cd super3 &&
1009 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
1010 mv -f .gitmodules.tmp .gitmodules &&
1011 git submodule update --init --depth=$commit_count &&
1012 git -C submodule log --oneline >out &&
1013 test_line_count = 1 out
1017 test_expect_success 'submodule update clone shallow submodule outside of depth' '
1018 test_when_finished "rm -rf super3" &&
1019 git clone cloned super3 &&
1020 pwd=$(pwd) &&
1022 cd super3 &&
1023 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
1024 mv -f .gitmodules.tmp .gitmodules &&
1025 # Some protocol versions (e.g. 2) support fetching
1026 # unadvertised objects, so restrict this test to v0.
1027 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
1028 git submodule update --init --depth=1 2>actual &&
1029 test_grep "Direct fetching of that commit failed." actual &&
1030 git -C ../submodule config uploadpack.allowReachableSHA1InWant true &&
1031 git submodule update --init --depth=1 >actual &&
1032 git -C submodule log --oneline >out &&
1033 test_line_count = 1 out
1037 test_expect_success 'submodule update --recursive drops module name before recursing' '
1038 (cd super2 &&
1039 (cd deeper/submodule/subsubmodule &&
1040 git checkout HEAD^
1041 ) &&
1042 git submodule update --recursive deeper/submodule >actual &&
1043 test_grep "Submodule path .deeper/submodule/subsubmodule.: checked out" actual
1047 test_expect_success 'submodule update can be run in parallel' '
1048 (cd super2 &&
1049 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 7 &&
1050 grep "7 tasks" trace.out &&
1051 git config submodule.fetchJobs 8 &&
1052 GIT_TRACE=$(pwd)/trace.out git submodule update &&
1053 grep "8 tasks" trace.out &&
1054 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 9 &&
1055 grep "9 tasks" trace.out
1059 test_expect_success 'git clone passes the parallel jobs config on to submodules' '
1060 test_when_finished "rm -rf super4" &&
1061 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 7 . super4 &&
1062 grep "7 tasks" trace.out &&
1063 rm -rf super4 &&
1064 git config --global submodule.fetchJobs 8 &&
1065 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules . super4 &&
1066 grep "8 tasks" trace.out &&
1067 rm -rf super4 &&
1068 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 9 . super4 &&
1069 grep "9 tasks" trace.out &&
1070 rm -rf super4
1073 test_expect_success 'submodule update --quiet passes quietness to merge/rebase' '
1074 (cd super &&
1075 test_commit -C rebasing message &&
1076 git submodule update --rebase --quiet >out 2>err &&
1077 test_must_be_empty out &&
1078 test_must_be_empty err &&
1079 git submodule update --rebase >out 2>err &&
1080 test_file_not_empty out &&
1081 test_must_be_empty err
1085 test_expect_success 'submodule update --quiet passes quietness to fetch with a shallow clone' '
1086 test_when_finished "rm -rf super4 super5 super6" &&
1087 git clone . super4 &&
1088 (cd super4 &&
1089 git submodule add --quiet file://"$TRASH_DIRECTORY"/submodule submodule3 &&
1090 git commit -am "setup submodule3"
1091 ) &&
1092 (cd submodule &&
1093 test_commit line6 file
1094 ) &&
1095 git clone super4 super5 &&
1096 (cd super5 &&
1097 git submodule update --quiet --init --depth=1 submodule3 >out 2>err &&
1098 test_must_be_empty out &&
1099 test_must_be_empty err
1100 ) &&
1101 git clone super4 super6 &&
1102 (cd super6 &&
1103 git submodule update --init --depth=1 submodule3 >out 2>err &&
1104 test_file_not_empty out &&
1105 test_file_not_empty err
1109 test_expect_success 'submodule update --filter requires --init' '
1110 test_expect_code 129 git -C super submodule update --filter blob:none
1113 test_expect_success 'submodule update --filter sets partial clone settings' '
1114 test_when_finished "rm -rf super-filter" &&
1115 git clone cloned super-filter &&
1116 git -C super-filter submodule update --init --filter blob:none &&
1117 test_cmp_config -C super-filter/submodule true remote.origin.promisor &&
1118 test_cmp_config -C super-filter/submodule blob:none remote.origin.partialclonefilter
1121 # NEEDSWORK: Clean up the tests so that we can reuse the test setup.
1122 # Don't reuse the existing repos because the earlier tests have
1123 # intentionally disruptive configurations.
1124 test_expect_success 'setup clean recursive superproject' '
1125 git init bottom &&
1126 test_commit -C bottom "bottom" &&
1127 git init middle &&
1128 git -C middle submodule add ../bottom bottom &&
1129 git -C middle commit -m "middle" &&
1130 git init top &&
1131 git -C top submodule add ../middle middle &&
1132 git -C top commit -m "top" &&
1133 git clone --recurse-submodules top top-clean
1136 test_expect_success 'submodule update should skip unmerged submodules' '
1137 test_when_finished "rm -fr top-cloned" &&
1138 cp -r top-clean top-cloned &&
1140 # Create an upstream commit in each repo, starting with bottom
1141 test_commit -C bottom upstream_commit &&
1142 # Create middle commit
1143 git -C middle/bottom fetch &&
1144 git -C middle/bottom checkout -f FETCH_HEAD &&
1145 git -C middle add bottom &&
1146 git -C middle commit -m "upstream_commit" &&
1147 # Create top commit
1148 git -C top/middle fetch &&
1149 git -C top/middle checkout -f FETCH_HEAD &&
1150 git -C top add middle &&
1151 git -C top commit -m "upstream_commit" &&
1153 # Create a downstream conflict
1154 test_commit -C top-cloned/middle/bottom downstream_commit &&
1155 git -C top-cloned/middle add bottom &&
1156 git -C top-cloned/middle commit -m "downstream_commit" &&
1157 git -C top-cloned/middle fetch --recurse-submodules origin &&
1158 test_must_fail git -C top-cloned/middle merge origin/main &&
1160 # Make the update of "middle" a no-op, otherwise we error out
1161 # because of its unmerged state
1162 test_config -C top-cloned submodule.middle.update !true &&
1163 git -C top-cloned submodule update --recursive 2>actual.err &&
1164 cat >expect.err <<-\EOF &&
1165 Skipping unmerged submodule middle/bottom
1167 test_cmp expect.err actual.err
1170 test_expect_success 'submodule update --recursive skip submodules with strategy=none' '
1171 test_when_finished "rm -fr top-cloned" &&
1172 cp -r top-clean top-cloned &&
1174 test_commit -C top-cloned/middle/bottom downstream_commit &&
1175 git -C top-cloned/middle config submodule.bottom.update none &&
1176 git -C top-cloned submodule update --recursive 2>actual.err &&
1177 cat >expect.err <<-\EOF &&
1178 Skipping submodule '\''middle/bottom'\''
1180 test_cmp expect.err actual.err
1183 add_submodule_commit_and_validate () {
1184 HASH=$(git rev-parse HEAD) &&
1185 git update-index --add --cacheinfo 160000,$HASH,sub &&
1186 git commit -m "create submodule" &&
1187 echo "160000 commit $HASH sub" >expect &&
1188 git ls-tree HEAD -- sub >actual &&
1189 test_cmp expect actual
1192 test_expect_success 'commit with staged submodule change' '
1193 add_submodule_commit_and_validate
1196 test_expect_success 'commit with staged submodule change with ignoreSubmodules dirty' '
1197 test_config diff.ignoreSubmodules dirty &&
1198 add_submodule_commit_and_validate
1201 test_expect_success 'commit with staged submodule change with ignoreSubmodules all' '
1202 test_config diff.ignoreSubmodules all &&
1203 add_submodule_commit_and_validate
1206 test_expect_success CASE_INSENSITIVE_FS,SYMLINKS \
1207 'submodule paths must not follow symlinks' '
1209 # This is only needed because we want to run this in a self-contained
1210 # test without having to spin up an HTTP server; However, it would not
1211 # be needed in a real-world scenario where the submodule is simply
1212 # hosted on a public site.
1213 test_config_global protocol.file.allow always &&
1215 # Make sure that Git tries to use symlinks on Windows
1216 test_config_global core.symlinks true &&
1218 tell_tale_path="$PWD/tell.tale" &&
1219 git init hook &&
1221 cd hook &&
1222 mkdir -p y/hooks &&
1223 write_script y/hooks/post-checkout <<-EOF &&
1224 echo HOOK-RUN >&2
1225 echo hook-run >"$tell_tale_path"
1227 git add y/hooks/post-checkout &&
1228 test_tick &&
1229 git commit -m post-checkout
1230 ) &&
1232 hook_repo_path="$(pwd)/hook" &&
1233 git init captain &&
1235 cd captain &&
1236 git submodule add --name x/y "$hook_repo_path" A/modules/x &&
1237 test_tick &&
1238 git commit -m add-submodule &&
1240 printf .git >dotgit.txt &&
1241 git hash-object -w --stdin <dotgit.txt >dot-git.hash &&
1242 printf "120000 %s 0\ta\n" "$(cat dot-git.hash)" >index.info &&
1243 git update-index --index-info <index.info &&
1244 test_tick &&
1245 git commit -m add-symlink
1246 ) &&
1248 test_path_is_missing "$tell_tale_path" &&
1249 git clone --recursive captain hooked 2>err &&
1250 test_grep ! HOOK-RUN err &&
1251 test_path_is_missing "$tell_tale_path"
1254 test_done