]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7418-submodule-sparse-gitmodules.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / t / t7418-submodule-sparse-gitmodules.sh
1 #!/bin/sh
2 #
3 # Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
4 #
5
6 test_description='Test reading/writing .gitmodules when not in the working tree
7
8 This test verifies that, when .gitmodules is in the current branch but is not
9 in the working tree reading from it still works but writing to it does not.
10
11 The test setup uses a sparse checkout, however the same scenario can be set up
12 also by committing .gitmodules and then just removing it from the filesystem.
13 '
14
15 GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
16 export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
17
18 . ./test-lib.sh
19
20 test_expect_success 'sparse checkout setup which hides .gitmodules' '
21 git init upstream &&
22 git init submodule &&
23 (cd submodule &&
24 echo file >file &&
25 git add file &&
26 test_tick &&
27 git commit -m "Add file"
28 ) &&
29 (cd upstream &&
30 git submodule add ../submodule &&
31 test_tick &&
32 git commit -m "Add submodule"
33 ) &&
34 git clone --template= upstream super &&
35 (cd super &&
36 mkdir .git/info &&
37 cat >.git/info/sparse-checkout <<-\EOF &&
38 /*
39 !/.gitmodules
40 EOF
41 git config core.sparsecheckout true &&
42 git read-tree -m -u HEAD &&
43 test_path_is_missing .gitmodules
44 )
45 '
46
47 test_expect_success 'reading gitmodules config file when it is not checked out' '
48 echo "../submodule" >expect &&
49 git -C super submodule--helper config submodule.submodule.url >actual &&
50 test_cmp expect actual
51 '
52
53 test_expect_success 'not writing gitmodules config file when it is not checked out' '
54 test_must_fail git -C super submodule--helper config submodule.submodule.url newurl &&
55 test_path_is_missing super/.gitmodules
56 '
57
58 test_expect_success 'initialising submodule when the gitmodules config is not checked out' '
59 test_must_fail git -C super config submodule.submodule.url &&
60 git -C super submodule init &&
61 git -C super config submodule.submodule.url >actual &&
62 echo "$(pwd)/submodule" >expect &&
63 test_cmp expect actual
64 '
65
66 test_expect_success 'updating submodule when the gitmodules config is not checked out' '
67 test_path_is_missing super/submodule/file &&
68 git -C super submodule update &&
69 test_cmp submodule/file super/submodule/file
70 '
71
72 ORIG_SUBMODULE=$(git -C submodule rev-parse HEAD)
73 ORIG_UPSTREAM=$(git -C upstream rev-parse HEAD)
74 ORIG_SUPER=$(git -C super rev-parse HEAD)
75
76 test_expect_success 're-updating submodule when the gitmodules config is not checked out' '
77 test_when_finished "git -C submodule reset --hard $ORIG_SUBMODULE;
78 git -C upstream reset --hard $ORIG_UPSTREAM;
79 git -C super reset --hard $ORIG_SUPER;
80 git -C upstream submodule update --remote;
81 git -C super pull;
82 git -C super submodule update --remote" &&
83 (cd submodule &&
84 echo file2 >file2 &&
85 git add file2 &&
86 test_tick &&
87 git commit -m "Add file2 to submodule"
88 ) &&
89 (cd upstream &&
90 git submodule update --remote &&
91 git add submodule &&
92 test_tick &&
93 git commit -m "Update submodule"
94 ) &&
95 git -C super pull &&
96 # The --for-status options reads the gitmodules config
97 git -C super submodule summary --for-status >actual &&
98 rev1=$(git -C submodule rev-parse --short HEAD) &&
99 rev2=$(git -C submodule rev-parse --short HEAD^) &&
100 cat >expect <<-EOF &&
101 * submodule ${rev1}...${rev2} (1):
102 < Add file2 to submodule
103
104 EOF
105 test_cmp expect actual &&
106 # Test that the update actually succeeds
107 test_path_is_missing super/submodule/file2 &&
108 git -C super submodule update &&
109 test_cmp submodule/file2 super/submodule/file2 &&
110 git -C super status --short >output &&
111 test_must_be_empty output
112 '
113
114 test_expect_success 'not adding submodules when the gitmodules config is not checked out' '
115 git clone submodule new_submodule &&
116 test_must_fail git -C super submodule add ../new_submodule &&
117 test_path_is_missing .gitmodules
118 '
119
120 # This test checks that the previous "git submodule add" did not leave the
121 # repository in a spurious state when it failed.
122 test_expect_success 'init submodule still works even after the previous add failed' '
123 git -C super submodule init
124 '
125
126 test_done