fetch-pack: exclude blobs when lazy-fetching trees
[git/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 'fetching of missing blobs works' '
174 rm -rf server repo &&
175 test_create_repo server &&
176 test_commit -C server foo &&
177 git -C server repack -a -d --write-bitmap-index &&
178
179 git clone "file://$(pwd)/server" repo &&
180 git hash-object repo/foo.t >blobhash &&
181 rm -rf repo/.git/objects/* &&
182
183 git -C server config uploadpack.allowanysha1inwant 1 &&
184 git -C server config uploadpack.allowfilter 1 &&
185 git -C repo config core.repositoryformatversion 1 &&
186 git -C repo config extensions.partialclone "origin" &&
187
188 git -C repo cat-file -p $(cat blobhash)
189 '
190
191 test_expect_success 'fetching of missing trees does not fetch blobs' '
192 rm -rf server repo &&
193 test_create_repo server &&
194 test_commit -C server foo &&
195 git -C server repack -a -d --write-bitmap-index &&
196
197 git clone "file://$(pwd)/server" repo &&
198 git -C repo rev-parse foo^{tree} >treehash &&
199 git hash-object repo/foo.t >blobhash &&
200 rm -rf repo/.git/objects/* &&
201
202 git -C server config uploadpack.allowanysha1inwant 1 &&
203 git -C server config uploadpack.allowfilter 1 &&
204 git -C repo config core.repositoryformatversion 1 &&
205 git -C repo config extensions.partialclone "origin" &&
206 git -C repo cat-file -p $(cat treehash) &&
207
208 # Ensure that the tree, but not the blob, is fetched
209 git -C repo rev-list --objects --missing=print $(cat treehash) >objects &&
210 grep "^$(cat treehash)" objects &&
211 grep "^[?]$(cat blobhash)" objects
212 '
213
214 test_expect_success 'rev-list stops traversal at missing and promised commit' '
215 rm -rf repo &&
216 test_create_repo repo &&
217 test_commit -C repo foo &&
218 test_commit -C repo bar &&
219
220 FOO=$(git -C repo rev-parse foo) &&
221 promise_and_delete "$FOO" &&
222
223 git -C repo config core.repositoryformatversion 1 &&
224 git -C repo config extensions.partialclone "arbitrary string" &&
225 git -C repo rev-list --exclude-promisor-objects --objects bar >out &&
226 grep $(git -C repo rev-parse bar) out &&
227 ! grep $FOO out
228 '
229
230 test_expect_success 'rev-list stops traversal at missing and promised tree' '
231 rm -rf repo &&
232 test_create_repo repo &&
233 test_commit -C repo foo &&
234 mkdir repo/a_dir &&
235 echo something >repo/a_dir/something &&
236 git -C repo add a_dir/something &&
237 git -C repo commit -m bar &&
238
239 # foo^{tree} (tree referenced from commit)
240 TREE=$(git -C repo rev-parse foo^{tree}) &&
241
242 # a tree referenced by HEAD^{tree} (tree referenced from tree)
243 TREE2=$(git -C repo ls-tree HEAD^{tree} | grep " tree " | head -1 | cut -b13-52) &&
244
245 promise_and_delete "$TREE" &&
246 promise_and_delete "$TREE2" &&
247
248 git -C repo config core.repositoryformatversion 1 &&
249 git -C repo config extensions.partialclone "arbitrary string" &&
250 git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
251 grep $(git -C repo rev-parse foo) out &&
252 ! grep $TREE out &&
253 grep $(git -C repo rev-parse HEAD) out &&
254 ! grep $TREE2 out
255 '
256
257 test_expect_success 'rev-list stops traversal at missing and promised blob' '
258 rm -rf repo &&
259 test_create_repo repo &&
260 echo something >repo/something &&
261 git -C repo add something &&
262 git -C repo commit -m foo &&
263
264 BLOB=$(git -C repo hash-object -w something) &&
265 promise_and_delete "$BLOB" &&
266
267 git -C repo config core.repositoryformatversion 1 &&
268 git -C repo config extensions.partialclone "arbitrary string" &&
269 git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
270 grep $(git -C repo rev-parse HEAD) out &&
271 ! grep $BLOB out
272 '
273
274 test_expect_success 'rev-list stops traversal at promisor commit, tree, and blob' '
275 rm -rf repo &&
276 test_create_repo repo &&
277 test_commit -C repo foo &&
278 test_commit -C repo bar &&
279 test_commit -C repo baz &&
280
281 COMMIT=$(git -C repo rev-parse foo) &&
282 TREE=$(git -C repo rev-parse bar^{tree}) &&
283 BLOB=$(git hash-object repo/baz.t) &&
284 printf "%s\n%s\n%s\n" $COMMIT $TREE $BLOB | pack_as_from_promisor &&
285
286 git -C repo config core.repositoryformatversion 1 &&
287 git -C repo config extensions.partialclone "arbitrary string" &&
288 git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
289 ! grep $COMMIT out &&
290 ! grep $TREE out &&
291 ! grep $BLOB out &&
292 grep $(git -C repo rev-parse bar) out # sanity check that some walking was done
293 '
294
295 test_expect_success 'rev-list accepts missing and promised objects on command line' '
296 rm -rf repo &&
297 test_create_repo repo &&
298 test_commit -C repo foo &&
299 test_commit -C repo bar &&
300 test_commit -C repo baz &&
301
302 COMMIT=$(git -C repo rev-parse foo) &&
303 TREE=$(git -C repo rev-parse bar^{tree}) &&
304 BLOB=$(git hash-object repo/baz.t) &&
305
306 promise_and_delete $COMMIT &&
307 promise_and_delete $TREE &&
308 promise_and_delete $BLOB &&
309
310 git -C repo config core.repositoryformatversion 1 &&
311 git -C repo config extensions.partialclone "arbitrary string" &&
312 git -C repo rev-list --exclude-promisor-objects --objects "$COMMIT" "$TREE" "$BLOB"
313 '
314
315 test_expect_success 'gc repacks promisor objects separately from non-promisor objects' '
316 rm -rf repo &&
317 test_create_repo repo &&
318 test_commit -C repo one &&
319 test_commit -C repo two &&
320
321 TREE_ONE=$(git -C repo rev-parse one^{tree}) &&
322 printf "$TREE_ONE\n" | pack_as_from_promisor &&
323 TREE_TWO=$(git -C repo rev-parse two^{tree}) &&
324 printf "$TREE_TWO\n" | pack_as_from_promisor &&
325
326 git -C repo config core.repositoryformatversion 1 &&
327 git -C repo config extensions.partialclone "arbitrary string" &&
328 git -C repo gc &&
329
330 # Ensure that exactly one promisor packfile exists, and that it
331 # contains the trees but not the commits
332 ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
333 test_line_count = 1 promisorlist &&
334 PROMISOR_PACKFILE=$(sed "s/.promisor/.pack/" <promisorlist) &&
335 git verify-pack $PROMISOR_PACKFILE -v >out &&
336 grep "$TREE_ONE" out &&
337 grep "$TREE_TWO" out &&
338 ! grep "$(git -C repo rev-parse one)" out &&
339 ! grep "$(git -C repo rev-parse two)" out &&
340
341 # Remove the promisor packfile and associated files
342 rm $(sed "s/.promisor//" <promisorlist).* &&
343
344 # Ensure that the single other pack contains the commits, but not the
345 # trees
346 ls repo/.git/objects/pack/pack-*.pack >packlist &&
347 test_line_count = 1 packlist &&
348 git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
349 grep "$(git -C repo rev-parse one)" out &&
350 grep "$(git -C repo rev-parse two)" out &&
351 ! grep "$TREE_ONE" out &&
352 ! grep "$TREE_TWO" out
353 '
354
355 test_expect_success 'gc does not repack promisor objects if there are none' '
356 rm -rf repo &&
357 test_create_repo repo &&
358 test_commit -C repo one &&
359
360 git -C repo config core.repositoryformatversion 1 &&
361 git -C repo config extensions.partialclone "arbitrary string" &&
362 git -C repo gc &&
363
364 # Ensure that only one pack exists
365 ls repo/.git/objects/pack/pack-*.pack >packlist &&
366 test_line_count = 1 packlist
367 '
368
369 repack_and_check () {
370 rm -rf repo2 &&
371 cp -r repo repo2 &&
372 git -C repo2 repack $1 -d &&
373 git -C repo2 fsck &&
374
375 git -C repo2 cat-file -e $2 &&
376 git -C repo2 cat-file -e $3
377 }
378
379 test_expect_success 'repack -d does not irreversibly delete promisor objects' '
380 rm -rf repo &&
381 test_create_repo repo &&
382 git -C repo config core.repositoryformatversion 1 &&
383 git -C repo config extensions.partialclone "arbitrary string" &&
384
385 git -C repo commit --allow-empty -m one &&
386 git -C repo commit --allow-empty -m two &&
387 git -C repo commit --allow-empty -m three &&
388 git -C repo commit --allow-empty -m four &&
389 ONE=$(git -C repo rev-parse HEAD^^^) &&
390 TWO=$(git -C repo rev-parse HEAD^^) &&
391 THREE=$(git -C repo rev-parse HEAD^) &&
392
393 printf "$TWO\n" | pack_as_from_promisor &&
394 printf "$THREE\n" | pack_as_from_promisor &&
395 delete_object repo "$ONE" &&
396
397 repack_and_check -a "$TWO" "$THREE" &&
398 repack_and_check -A "$TWO" "$THREE" &&
399 repack_and_check -l "$TWO" "$THREE"
400 '
401
402 test_expect_success 'gc stops traversal when a missing but promised object is reached' '
403 rm -rf repo &&
404 test_create_repo repo &&
405 test_commit -C repo my_commit &&
406
407 TREE_HASH=$(git -C repo rev-parse HEAD^{tree}) &&
408 HASH=$(promise_and_delete $TREE_HASH) &&
409
410 git -C repo config core.repositoryformatversion 1 &&
411 git -C repo config extensions.partialclone "arbitrary string" &&
412 git -C repo gc &&
413
414 # Ensure that the promisor packfile still exists, and remove it
415 test -e repo/.git/objects/pack/pack-$HASH.pack &&
416 rm repo/.git/objects/pack/pack-$HASH.* &&
417
418 # Ensure that the single other pack contains the commit, but not the tree
419 ls repo/.git/objects/pack/pack-*.pack >packlist &&
420 test_line_count = 1 packlist &&
421 git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
422 grep "$(git -C repo rev-parse HEAD)" out &&
423 ! grep "$TREE_HASH" out
424 '
425
426 LIB_HTTPD_PORT=12345 # default port, 410, cannot be used as non-root
427 . "$TEST_DIRECTORY"/lib-httpd.sh
428 start_httpd
429
430 test_expect_success 'fetching of missing objects from an HTTP server' '
431 rm -rf repo &&
432 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
433 test_create_repo "$SERVER" &&
434 test_commit -C "$SERVER" foo &&
435 git -C "$SERVER" repack -a -d --write-bitmap-index &&
436
437 git clone $HTTPD_URL/smart/server repo &&
438 HASH=$(git -C repo rev-parse foo) &&
439 rm -rf repo/.git/objects/* &&
440
441 git -C repo config core.repositoryformatversion 1 &&
442 git -C repo config extensions.partialclone "origin" &&
443 git -C repo cat-file -p "$HASH" &&
444
445 # Ensure that the .promisor file is written, and check that its
446 # associated packfile contains the object
447 ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
448 test_line_count = 1 promisorlist &&
449 IDX=$(cat promisorlist | sed "s/promisor$/idx/") &&
450 git verify-pack --verbose "$IDX" | grep "$HASH"
451 '
452
453 stop_httpd
454
455 test_done