Imported Upstream version 2.19.0
[platform/upstream/git.git] / t / t0410-partial-clone.sh
1 #!/bin/sh
2
3 test_description='partial clone'
4
5 . ./test-lib.sh
6
7 delete_object () {
8         rm $1/.git/objects/$(echo $2 | sed -e 's|^..|&/|')
9 }
10
11 pack_as_from_promisor () {
12         HASH=$(git -C repo pack-objects .git/objects/pack/pack) &&
13         >repo/.git/objects/pack/pack-$HASH.promisor &&
14         echo $HASH
15 }
16
17 promise_and_delete () {
18         HASH=$(git -C repo rev-parse "$1") &&
19         git -C repo tag -a -m message my_annotated_tag "$HASH" &&
20         git -C repo rev-parse my_annotated_tag | pack_as_from_promisor &&
21         # tag -d prints a message to stdout, so redirect it
22         git -C repo tag -d my_annotated_tag >/dev/null &&
23         delete_object repo "$HASH"
24 }
25
26 test_expect_success 'extensions.partialclone without filter' '
27         test_create_repo server &&
28         git clone --filter="blob:none" "file://$(pwd)/server" client &&
29         git -C client config --unset core.partialclonefilter &&
30         git -C client fetch origin
31 '
32
33 test_expect_success 'missing reflog object, but promised by a commit, passes fsck' '
34         rm -rf repo &&
35         test_create_repo repo &&
36         test_commit -C repo my_commit &&
37
38         A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
39         C=$(git -C repo commit-tree -m c -p $A HEAD^{tree}) &&
40
41         # Reference $A only from reflog, and delete it
42         git -C repo branch my_branch "$A" &&
43         git -C repo branch -f my_branch my_commit &&
44         delete_object repo "$A" &&
45
46         # State that we got $C, which refers to $A, from promisor
47         printf "$C\n" | pack_as_from_promisor &&
48
49         # Normally, it fails
50         test_must_fail git -C repo fsck &&
51
52         # But with the extension, it succeeds
53         git -C repo config core.repositoryformatversion 1 &&
54         git -C repo config extensions.partialclone "arbitrary string" &&
55         git -C repo fsck
56 '
57
58 test_expect_success 'missing reflog object, but promised by a tag, passes fsck' '
59         rm -rf repo &&
60         test_create_repo repo &&
61         test_commit -C repo my_commit &&
62
63         A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
64         git -C repo tag -a -m d my_tag_name $A &&
65         T=$(git -C repo rev-parse my_tag_name) &&
66         git -C repo tag -d my_tag_name &&
67
68         # Reference $A only from reflog, and delete it
69         git -C repo branch my_branch "$A" &&
70         git -C repo branch -f my_branch my_commit &&
71         delete_object repo "$A" &&
72
73         # State that we got $T, which refers to $A, from promisor
74         printf "$T\n" | pack_as_from_promisor &&
75
76         git -C repo config core.repositoryformatversion 1 &&
77         git -C repo config extensions.partialclone "arbitrary string" &&
78         git -C repo fsck
79 '
80
81 test_expect_success 'missing reflog object alone fails fsck, even with extension set' '
82         rm -rf repo &&
83         test_create_repo repo &&
84         test_commit -C repo my_commit &&
85
86         A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
87         B=$(git -C repo commit-tree -m b HEAD^{tree}) &&
88
89         # Reference $A only from reflog, and delete it
90         git -C repo branch my_branch "$A" &&
91         git -C repo branch -f my_branch my_commit &&
92         delete_object repo "$A" &&
93
94         git -C repo config core.repositoryformatversion 1 &&
95         git -C repo config extensions.partialclone "arbitrary string" &&
96         test_must_fail git -C repo fsck
97 '
98
99 test_expect_success 'missing ref object, but promised, passes fsck' '
100         rm -rf repo &&
101         test_create_repo repo &&
102         test_commit -C repo my_commit &&
103
104         A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
105
106         # Reference $A only from ref
107         git -C repo branch my_branch "$A" &&
108         promise_and_delete "$A" &&
109
110         git -C repo config core.repositoryformatversion 1 &&
111         git -C repo config extensions.partialclone "arbitrary string" &&
112         git -C repo fsck
113 '
114
115 test_expect_success 'missing object, but promised, passes fsck' '
116         rm -rf repo &&
117         test_create_repo repo &&
118         test_commit -C repo 1 &&
119         test_commit -C repo 2 &&
120         test_commit -C repo 3 &&
121         git -C repo tag -a annotated_tag -m "annotated tag" &&
122
123         C=$(git -C repo rev-parse 1) &&
124         T=$(git -C repo rev-parse 2^{tree}) &&
125         B=$(git hash-object repo/3.t) &&
126         AT=$(git -C repo rev-parse annotated_tag) &&
127
128         promise_and_delete "$C" &&
129         promise_and_delete "$T" &&
130         promise_and_delete "$B" &&
131         promise_and_delete "$AT" &&
132
133         git -C repo config core.repositoryformatversion 1 &&
134         git -C repo config extensions.partialclone "arbitrary string" &&
135         git -C repo fsck
136 '
137
138 test_expect_success 'missing CLI object, but promised, passes fsck' '
139         rm -rf repo &&
140         test_create_repo repo &&
141         test_commit -C repo my_commit &&
142
143         A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
144         promise_and_delete "$A" &&
145
146         git -C repo config core.repositoryformatversion 1 &&
147         git -C repo config extensions.partialclone "arbitrary string" &&
148         git -C repo fsck "$A"
149 '
150
151 test_expect_success 'fetching of missing objects' '
152         rm -rf repo &&
153         test_create_repo server &&
154         test_commit -C server foo &&
155         git -C server repack -a -d --write-bitmap-index &&
156
157         git clone "file://$(pwd)/server" repo &&
158         HASH=$(git -C repo rev-parse foo) &&
159         rm -rf repo/.git/objects/* &&
160
161         git -C repo config core.repositoryformatversion 1 &&
162         git -C repo config extensions.partialclone "origin" &&
163         git -C repo cat-file -p "$HASH" &&
164
165         # Ensure that the .promisor file is written, and check that its
166         # associated packfile contains the object
167         ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
168         test_line_count = 1 promisorlist &&
169         IDX=$(cat promisorlist | sed "s/promisor$/idx/") &&
170         git verify-pack --verbose "$IDX" | grep "$HASH"
171 '
172
173 test_expect_success 'rev-list stops traversal at missing and promised commit' '
174         rm -rf repo &&
175         test_create_repo repo &&
176         test_commit -C repo foo &&
177         test_commit -C repo bar &&
178
179         FOO=$(git -C repo rev-parse foo) &&
180         promise_and_delete "$FOO" &&
181
182         git -C repo config core.repositoryformatversion 1 &&
183         git -C repo config extensions.partialclone "arbitrary string" &&
184         git -C repo rev-list --exclude-promisor-objects --objects bar >out &&
185         grep $(git -C repo rev-parse bar) out &&
186         ! grep $FOO out
187 '
188
189 test_expect_success 'rev-list stops traversal at missing and promised tree' '
190         rm -rf repo &&
191         test_create_repo repo &&
192         test_commit -C repo foo &&
193         mkdir repo/a_dir &&
194         echo something >repo/a_dir/something &&
195         git -C repo add a_dir/something &&
196         git -C repo commit -m bar &&
197
198         # foo^{tree} (tree referenced from commit)
199         TREE=$(git -C repo rev-parse foo^{tree}) &&
200
201         # a tree referenced by HEAD^{tree} (tree referenced from tree)
202         TREE2=$(git -C repo ls-tree HEAD^{tree} | grep " tree " | head -1 | cut -b13-52) &&
203
204         promise_and_delete "$TREE" &&
205         promise_and_delete "$TREE2" &&
206
207         git -C repo config core.repositoryformatversion 1 &&
208         git -C repo config extensions.partialclone "arbitrary string" &&
209         git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
210         grep $(git -C repo rev-parse foo) out &&
211         ! grep $TREE out &&
212         grep $(git -C repo rev-parse HEAD) out &&
213         ! grep $TREE2 out
214 '
215
216 test_expect_success 'rev-list stops traversal at missing and promised blob' '
217         rm -rf repo &&
218         test_create_repo repo &&
219         echo something >repo/something &&
220         git -C repo add something &&
221         git -C repo commit -m foo &&
222
223         BLOB=$(git -C repo hash-object -w something) &&
224         promise_and_delete "$BLOB" &&
225
226         git -C repo config core.repositoryformatversion 1 &&
227         git -C repo config extensions.partialclone "arbitrary string" &&
228         git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
229         grep $(git -C repo rev-parse HEAD) out &&
230         ! grep $BLOB out
231 '
232
233 test_expect_success 'rev-list stops traversal at promisor commit, tree, and blob' '
234         rm -rf repo &&
235         test_create_repo repo &&
236         test_commit -C repo foo &&
237         test_commit -C repo bar &&
238         test_commit -C repo baz &&
239
240         COMMIT=$(git -C repo rev-parse foo) &&
241         TREE=$(git -C repo rev-parse bar^{tree}) &&
242         BLOB=$(git hash-object repo/baz.t) &&
243         printf "%s\n%s\n%s\n" $COMMIT $TREE $BLOB | pack_as_from_promisor &&
244
245         git -C repo config core.repositoryformatversion 1 &&
246         git -C repo config extensions.partialclone "arbitrary string" &&
247         git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
248         ! grep $COMMIT out &&
249         ! grep $TREE out &&
250         ! grep $BLOB out &&
251         grep $(git -C repo rev-parse bar) out  # sanity check that some walking was done
252 '
253
254 test_expect_success 'rev-list accepts missing and promised objects on command line' '
255         rm -rf repo &&
256         test_create_repo repo &&
257         test_commit -C repo foo &&
258         test_commit -C repo bar &&
259         test_commit -C repo baz &&
260
261         COMMIT=$(git -C repo rev-parse foo) &&
262         TREE=$(git -C repo rev-parse bar^{tree}) &&
263         BLOB=$(git hash-object repo/baz.t) &&
264
265         promise_and_delete $COMMIT &&
266         promise_and_delete $TREE &&
267         promise_and_delete $BLOB &&
268
269         git -C repo config core.repositoryformatversion 1 &&
270         git -C repo config extensions.partialclone "arbitrary string" &&
271         git -C repo rev-list --exclude-promisor-objects --objects "$COMMIT" "$TREE" "$BLOB"
272 '
273
274 test_expect_success 'gc repacks promisor objects separately from non-promisor objects' '
275         rm -rf repo &&
276         test_create_repo repo &&
277         test_commit -C repo one &&
278         test_commit -C repo two &&
279
280         TREE_ONE=$(git -C repo rev-parse one^{tree}) &&
281         printf "$TREE_ONE\n" | pack_as_from_promisor &&
282         TREE_TWO=$(git -C repo rev-parse two^{tree}) &&
283         printf "$TREE_TWO\n" | pack_as_from_promisor &&
284
285         git -C repo config core.repositoryformatversion 1 &&
286         git -C repo config extensions.partialclone "arbitrary string" &&
287         git -C repo gc &&
288
289         # Ensure that exactly one promisor packfile exists, and that it
290         # contains the trees but not the commits
291         ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
292         test_line_count = 1 promisorlist &&
293         PROMISOR_PACKFILE=$(sed "s/.promisor/.pack/" <promisorlist) &&
294         git verify-pack $PROMISOR_PACKFILE -v >out &&
295         grep "$TREE_ONE" out &&
296         grep "$TREE_TWO" out &&
297         ! grep "$(git -C repo rev-parse one)" out &&
298         ! grep "$(git -C repo rev-parse two)" out &&
299
300         # Remove the promisor packfile and associated files
301         rm $(sed "s/.promisor//" <promisorlist).* &&
302
303         # Ensure that the single other pack contains the commits, but not the
304         # trees
305         ls repo/.git/objects/pack/pack-*.pack >packlist &&
306         test_line_count = 1 packlist &&
307         git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
308         grep "$(git -C repo rev-parse one)" out &&
309         grep "$(git -C repo rev-parse two)" out &&
310         ! grep "$TREE_ONE" out &&
311         ! grep "$TREE_TWO" out
312 '
313
314 test_expect_success 'gc does not repack promisor objects if there are none' '
315         rm -rf repo &&
316         test_create_repo repo &&
317         test_commit -C repo one &&
318
319         git -C repo config core.repositoryformatversion 1 &&
320         git -C repo config extensions.partialclone "arbitrary string" &&
321         git -C repo gc &&
322
323         # Ensure that only one pack exists
324         ls repo/.git/objects/pack/pack-*.pack >packlist &&
325         test_line_count = 1 packlist
326 '
327
328 repack_and_check () {
329         rm -rf repo2 &&
330         cp -r repo repo2 &&
331         git -C repo2 repack $1 -d &&
332         git -C repo2 fsck &&
333
334         git -C repo2 cat-file -e $2 &&
335         git -C repo2 cat-file -e $3
336 }
337
338 test_expect_success 'repack -d does not irreversibly delete promisor objects' '
339         rm -rf repo &&
340         test_create_repo repo &&
341         git -C repo config core.repositoryformatversion 1 &&
342         git -C repo config extensions.partialclone "arbitrary string" &&
343
344         git -C repo commit --allow-empty -m one &&
345         git -C repo commit --allow-empty -m two &&
346         git -C repo commit --allow-empty -m three &&
347         git -C repo commit --allow-empty -m four &&
348         ONE=$(git -C repo rev-parse HEAD^^^) &&
349         TWO=$(git -C repo rev-parse HEAD^^) &&
350         THREE=$(git -C repo rev-parse HEAD^) &&
351
352         printf "$TWO\n" | pack_as_from_promisor &&
353         printf "$THREE\n" | pack_as_from_promisor &&
354         delete_object repo "$ONE" &&
355
356         repack_and_check -a "$TWO" "$THREE" &&
357         repack_and_check -A "$TWO" "$THREE" &&
358         repack_and_check -l "$TWO" "$THREE"
359 '
360
361 test_expect_success 'gc stops traversal when a missing but promised object is reached' '
362         rm -rf repo &&
363         test_create_repo repo &&
364         test_commit -C repo my_commit &&
365
366         TREE_HASH=$(git -C repo rev-parse HEAD^{tree}) &&
367         HASH=$(promise_and_delete $TREE_HASH) &&
368
369         git -C repo config core.repositoryformatversion 1 &&
370         git -C repo config extensions.partialclone "arbitrary string" &&
371         git -C repo gc &&
372
373         # Ensure that the promisor packfile still exists, and remove it
374         test -e repo/.git/objects/pack/pack-$HASH.pack &&
375         rm repo/.git/objects/pack/pack-$HASH.* &&
376
377         # Ensure that the single other pack contains the commit, but not the tree
378         ls repo/.git/objects/pack/pack-*.pack >packlist &&
379         test_line_count = 1 packlist &&
380         git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
381         grep "$(git -C repo rev-parse HEAD)" out &&
382         ! grep "$TREE_HASH" out
383 '
384
385 LIB_HTTPD_PORT=12345  # default port, 410, cannot be used as non-root
386 . "$TEST_DIRECTORY"/lib-httpd.sh
387 start_httpd
388
389 test_expect_success 'fetching of missing objects from an HTTP server' '
390         rm -rf repo &&
391         SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
392         test_create_repo "$SERVER" &&
393         test_commit -C "$SERVER" foo &&
394         git -C "$SERVER" repack -a -d --write-bitmap-index &&
395
396         git clone $HTTPD_URL/smart/server repo &&
397         HASH=$(git -C repo rev-parse foo) &&
398         rm -rf repo/.git/objects/* &&
399
400         git -C repo config core.repositoryformatversion 1 &&
401         git -C repo config extensions.partialclone "origin" &&
402         git -C repo cat-file -p "$HASH" &&
403
404         # Ensure that the .promisor file is written, and check that its
405         # associated packfile contains the object
406         ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
407         test_line_count = 1 promisorlist &&
408         IDX=$(cat promisorlist | sed "s/promisor$/idx/") &&
409         git verify-pack --verbose "$IDX" | grep "$HASH"
410 '
411
412 stop_httpd
413
414 test_done