]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7408-submodule-reference.sh
Git 2.35.8
[thirdparty/git.git] / t / t7408-submodule-reference.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2009, Red Hat Inc, Author: Michael S. Tsirkin (mst@redhat.com)
4 #
5
6 test_description='test clone --reference'
7 . ./test-lib.sh
8
9 base_dir=$(pwd)
10
11 test_alternate_is_used () {
12 alternates_file="$1" &&
13 working_dir="$2" &&
14 test_line_count = 1 "$alternates_file" &&
15 echo "0 objects, 0 kilobytes" >expect &&
16 git -C "$working_dir" count-objects >actual &&
17 test_cmp expect actual
18 }
19
20 test_expect_success 'setup' '
21 git config --global protocol.file.allow always
22 '
23
24 test_expect_success 'preparing first repository' '
25 test_create_repo A &&
26 (
27 cd A &&
28 echo first >file1 &&
29 git add file1 &&
30 git commit -m A-initial
31 )
32 '
33
34 test_expect_success 'preparing second repository' '
35 git clone A B &&
36 (
37 cd B &&
38 echo second >file2 &&
39 git add file2 &&
40 git commit -m B-addition &&
41 git repack -a -d &&
42 git prune
43 )
44 '
45
46 test_expect_success 'preparing superproject' '
47 test_create_repo super &&
48 (
49 cd super &&
50 echo file >file &&
51 git add file &&
52 git commit -m B-super-initial
53 )
54 '
55
56 test_expect_success 'submodule add --reference uses alternates' '
57 (
58 cd super &&
59 git submodule add --reference ../B "file://$base_dir/A" sub &&
60 git commit -m B-super-added &&
61 git repack -ad
62 ) &&
63 test_alternate_is_used super/.git/modules/sub/objects/info/alternates super/sub
64 '
65
66 test_expect_success 'submodule add --reference with --dissociate does not use alternates' '
67 (
68 cd super &&
69 git submodule add --reference ../B --dissociate "file://$base_dir/A" sub-dissociate &&
70 git commit -m B-super-added &&
71 git repack -ad
72 ) &&
73 test_path_is_missing super/.git/modules/sub-dissociate/objects/info/alternates
74 '
75
76 test_expect_success 'that reference gets used with add' '
77 (
78 cd super/sub &&
79 echo "0 objects, 0 kilobytes" >expected &&
80 git count-objects >current &&
81 diff expected current
82 )
83 '
84
85 # The tests up to this point, and repositories created by them
86 # (A, B, super and super/sub), are about setting up the stage
87 # for subsequent tests and meant to be kept throughout the
88 # remainder of the test.
89 # Tests from here on, if they create their own test repository,
90 # are expected to clean after themselves.
91
92 test_expect_success 'updating superproject keeps alternates' '
93 test_when_finished "rm -rf super-clone" &&
94 git clone super super-clone &&
95 git -C super-clone submodule update --init --reference ../B &&
96 test_alternate_is_used super-clone/.git/modules/sub/objects/info/alternates super-clone/sub
97 '
98
99 test_expect_success 'updating superproject with --dissociate does not keep alternates' '
100 test_when_finished "rm -rf super-clone" &&
101 git clone super super-clone &&
102 git -C super-clone submodule update --init --reference ../B --dissociate &&
103 test_path_is_missing super-clone/.git/modules/sub/objects/info/alternates
104 '
105
106 test_expect_success 'submodules use alternates when cloning a superproject' '
107 test_when_finished "rm -rf super-clone" &&
108 git clone --reference super --recursive super super-clone &&
109 (
110 cd super-clone &&
111 # test superproject has alternates setup correctly
112 test_alternate_is_used .git/objects/info/alternates . &&
113 # test submodule has correct setup
114 test_alternate_is_used .git/modules/sub/objects/info/alternates sub
115 )
116 '
117
118 test_expect_success 'missing submodule alternate fails clone and submodule update' '
119 test_when_finished "rm -rf super-clone" &&
120 git clone super super2 &&
121 test_must_fail git clone --recursive --reference super2 super2 super-clone &&
122 (
123 cd super-clone &&
124 # test superproject has alternates setup correctly
125 test_alternate_is_used .git/objects/info/alternates . &&
126 # update of the submodule succeeds
127 test_must_fail git submodule update --init &&
128 # and we have no alternates:
129 test_path_is_missing .git/modules/sub/objects/info/alternates &&
130 test_path_is_missing sub/file1
131 )
132 '
133
134 test_expect_success 'ignoring missing submodule alternates passes clone and submodule update' '
135 test_when_finished "rm -rf super-clone" &&
136 git clone --reference-if-able super2 --recursive super2 super-clone &&
137 (
138 cd super-clone &&
139 # test superproject has alternates setup correctly
140 test_alternate_is_used .git/objects/info/alternates . &&
141 # update of the submodule succeeds
142 git submodule update --init &&
143 # and we have no alternates:
144 test_path_is_missing .git/modules/sub/objects/info/alternates &&
145 test_path_is_file sub/file1
146 )
147 '
148
149 test_expect_success 'preparing second superproject with a nested submodule plus partial clone' '
150 test_create_repo supersuper &&
151 (
152 cd supersuper &&
153 echo "I am super super." >file &&
154 git add file &&
155 git commit -m B-super-super-initial &&
156 git submodule add "file://$base_dir/super" subwithsub &&
157 git commit -m B-super-super-added &&
158 git submodule update --init --recursive &&
159 git repack -ad
160 ) &&
161 git clone supersuper supersuper2 &&
162 (
163 cd supersuper2 &&
164 git submodule update --init
165 )
166 '
167
168 # At this point there are three root-level positories: A, B, super and super2
169
170 test_expect_success 'nested submodule alternate in works and is actually used' '
171 test_when_finished "rm -rf supersuper-clone" &&
172 git clone --recursive --reference supersuper supersuper supersuper-clone &&
173 (
174 cd supersuper-clone &&
175 # test superproject has alternates setup correctly
176 test_alternate_is_used .git/objects/info/alternates . &&
177 # immediate submodule has alternate:
178 test_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&
179 # nested submodule also has alternate:
180 test_alternate_is_used .git/modules/subwithsub/modules/sub/objects/info/alternates subwithsub/sub
181 )
182 '
183
184 check_that_two_of_three_alternates_are_used() {
185 test_alternate_is_used .git/objects/info/alternates . &&
186 # immediate submodule has alternate:
187 test_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&
188 # but nested submodule has no alternate:
189 test_path_is_missing .git/modules/subwithsub/modules/sub/objects/info/alternates
190 }
191
192
193 test_expect_success 'missing nested submodule alternate fails clone and submodule update' '
194 test_when_finished "rm -rf supersuper-clone" &&
195 test_must_fail git clone --recursive --reference supersuper2 supersuper2 supersuper-clone &&
196 (
197 cd supersuper-clone &&
198 check_that_two_of_three_alternates_are_used &&
199 # update of the submodule fails
200 test_must_fail git submodule update --init --recursive
201 )
202 '
203
204 test_expect_success 'missing nested submodule alternate in --reference-if-able mode' '
205 test_when_finished "rm -rf supersuper-clone" &&
206 git clone --recursive --reference-if-able supersuper2 supersuper2 supersuper-clone &&
207 (
208 cd supersuper-clone &&
209 check_that_two_of_three_alternates_are_used &&
210 # update of the submodule succeeds
211 git submodule update --init --recursive
212 )
213 '
214
215 test_done