3 test_description
='git pack-objects using object filtering'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK
=true
11 # Test blob:none filter.
13 test_expect_success
'setup r1' '
17 echo "This is file: $n" > r1/file.$n &&
18 git -C r1 add file.$n &&
19 git -C r1 commit -m "$n" || return 1
23 parse_verify_pack_blob_oid
() {
27 test_expect_success
'verify blob count in normal packfile' '
28 git -C r1 ls-files -s file.1 file.2 file.3 file.4 file.5 \
30 test_parse_ls_files_stage_oids <ls_files_result |
33 git -C r1 pack-objects --revs --stdout >all.pack <<-EOF &&
36 git -C r1 index-pack ../all.pack &&
38 git -C r1 verify-pack -v ../all.pack >verify_result &&
39 grep blob verify_result |
40 parse_verify_pack_blob_oid |
43 test_cmp expected observed
46 test_expect_success
'verify blob:none packfile has no blobs' '
47 git -C r1 pack-objects --revs --stdout --filter=blob:none >filter.pack <<-EOF &&
50 git -C r1 index-pack ../filter.pack &&
52 git -C r1 verify-pack -v ../filter.pack >verify_result &&
53 ! grep blob verify_result
56 test_expect_success
'verify blob:none packfile without --stdout' '
57 git -C r1 pack-objects --revs --filter=blob:none mypackname >packhash <<-EOF &&
60 git -C r1 verify-pack -v "mypackname-$(cat packhash).pack" >verify_result &&
61 ! grep blob verify_result
64 test_expect_success
'verify normal and blob:none packfiles have same commits/trees' '
65 git -C r1 verify-pack -v ../all.pack >verify_result &&
66 grep -E "commit|tree" verify_result |
67 parse_verify_pack_blob_oid |
70 git -C r1 verify-pack -v ../filter.pack >verify_result &&
71 grep -E "commit|tree" verify_result |
72 parse_verify_pack_blob_oid |
75 test_cmp expected observed
78 test_expect_success
'get an error for missing tree object' '
82 git -C r5 commit -m "foo" &&
83 git -C r5 rev-parse HEAD^{tree} >tree &&
84 del=$(sed "s|..|&/|" tree) &&
85 rm r5/.git/objects/$del &&
86 test_must_fail git -C r5 pack-objects --revs --stdout 2>bad_tree <<-EOF &&
89 grep "bad tree object" bad_tree
92 test_expect_success
'setup for tests of tree:0' '
94 echo "This is a file in a subtree" >r1/subtree/file &&
95 git -C r1 add subtree/file &&
96 git -C r1 commit -m subtree
99 test_expect_success
'verify tree:0 packfile has no blobs or trees' '
100 git -C r1 pack-objects --revs --stdout --filter=tree:0 >commitsonly.pack <<-EOF &&
103 git -C r1 index-pack ../commitsonly.pack &&
104 git -C r1 verify-pack -v ../commitsonly.pack >objs &&
105 ! grep -E "tree|blob" objs
108 test_expect_success
'grab tree directly when using tree:0' '
109 # We should get the tree specified directly but not its blobs or subtrees.
110 git -C r1 pack-objects --revs --stdout --filter=tree:0 >commitsonly.pack <<-EOF &&
113 git -C r1 index-pack ../commitsonly.pack &&
114 git -C r1 verify-pack -v ../commitsonly.pack >objs &&
115 awk "/tree|blob/{print \$1}" objs >trees_and_blobs &&
116 git -C r1 rev-parse HEAD: >expected &&
117 test_cmp expected trees_and_blobs
120 # Test blob:limit=<n>[kmg] filter.
121 # We boundary test around the size parameter. The filter is strictly less than
122 # the value, so size 500 and 1000 should have the same results, but 1001 should
125 test_expect_success
'setup r2' '
129 printf "%"$n"s" X > r2/large.$n &&
130 git -C r2 add large.$n &&
131 git -C r2 commit -m "$n" || return 1
135 test_expect_success
'verify blob count in normal packfile' '
136 git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
137 test_parse_ls_files_stage_oids <ls_files_result |
140 git -C r2 pack-objects --revs --stdout >all.pack <<-EOF &&
143 git -C r2 index-pack ../all.pack &&
145 git -C r2 verify-pack -v ../all.pack >verify_result &&
146 grep blob verify_result |
147 parse_verify_pack_blob_oid |
150 test_cmp expected observed
153 test_expect_success
'verify blob:limit=500 omits all blobs' '
154 git -C r2 pack-objects --revs --stdout --filter=blob:limit=500 >filter.pack <<-EOF &&
157 git -C r2 index-pack ../filter.pack &&
159 git -C r2 verify-pack -v ../filter.pack >verify_result &&
160 ! grep blob verify_result
163 test_expect_success
'verify blob:limit=1000' '
164 git -C r2 pack-objects --revs --stdout --filter=blob:limit=1000 >filter.pack <<-EOF &&
167 git -C r2 index-pack ../filter.pack &&
169 git -C r2 verify-pack -v ../filter.pack >verify_result &&
170 ! grep blob verify_result
173 test_expect_success
'verify blob:limit=1001' '
174 git -C r2 ls-files -s large.1000 >ls_files_result &&
175 test_parse_ls_files_stage_oids <ls_files_result |
178 git -C r2 pack-objects --revs --stdout --filter=blob:limit=1001 >filter.pack <<-EOF &&
181 git -C r2 index-pack ../filter.pack &&
183 git -C r2 verify-pack -v ../filter.pack >verify_result &&
184 grep blob verify_result |
185 parse_verify_pack_blob_oid |
188 test_cmp expected observed
191 test_expect_success
'verify blob:limit=10001' '
192 git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
193 test_parse_ls_files_stage_oids <ls_files_result |
196 git -C r2 pack-objects --revs --stdout --filter=blob:limit=10001 >filter.pack <<-EOF &&
199 git -C r2 index-pack ../filter.pack &&
201 git -C r2 verify-pack -v ../filter.pack >verify_result &&
202 grep blob verify_result |
203 parse_verify_pack_blob_oid |
206 test_cmp expected observed
209 test_expect_success
'verify blob:limit=1k' '
210 git -C r2 ls-files -s large.1000 >ls_files_result &&
211 test_parse_ls_files_stage_oids <ls_files_result |
214 git -C r2 pack-objects --revs --stdout --filter=blob:limit=1k >filter.pack <<-EOF &&
217 git -C r2 index-pack ../filter.pack &&
219 git -C r2 verify-pack -v ../filter.pack >verify_result &&
220 grep blob verify_result |
221 parse_verify_pack_blob_oid |
224 test_cmp expected observed
227 test_expect_success
'verify explicitly specifying oversized blob in input' '
228 git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
229 test_parse_ls_files_stage_oids <ls_files_result |
232 echo HEAD >objects &&
233 git -C r2 rev-parse HEAD:large.10000 >>objects &&
234 git -C r2 pack-objects --revs --stdout --filter=blob:limit=1k <objects >filter.pack &&
235 git -C r2 index-pack ../filter.pack &&
237 git -C r2 verify-pack -v ../filter.pack >verify_result &&
238 grep blob verify_result |
239 parse_verify_pack_blob_oid |
242 test_cmp expected observed
245 test_expect_success
'verify blob:limit=1m' '
246 git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
247 test_parse_ls_files_stage_oids <ls_files_result |
250 git -C r2 pack-objects --revs --stdout --filter=blob:limit=1m >filter.pack <<-EOF &&
253 git -C r2 index-pack ../filter.pack &&
255 git -C r2 verify-pack -v ../filter.pack >verify_result &&
256 grep blob verify_result |
257 parse_verify_pack_blob_oid |
260 test_cmp expected observed
263 test_expect_success
'verify normal and blob:limit packfiles have same commits/trees' '
264 git -C r2 verify-pack -v ../all.pack >verify_result &&
265 grep -E "commit|tree" verify_result |
266 parse_verify_pack_blob_oid |
269 git -C r2 verify-pack -v ../filter.pack >verify_result &&
270 grep -E "commit|tree" verify_result |
271 parse_verify_pack_blob_oid |
274 test_cmp expected observed
277 test_expect_success
'verify small limit and big limit results in small limit' '
278 git -C r2 ls-files -s large.1000 >ls_files_result &&
279 test_parse_ls_files_stage_oids <ls_files_result |
282 git -C r2 pack-objects --revs --stdout --filter=blob:limit=1001 \
283 --filter=blob:limit=10001 >filter.pack <<-EOF &&
286 git -C r2 index-pack ../filter.pack &&
288 git -C r2 verify-pack -v ../filter.pack >verify_result &&
289 grep blob verify_result |
290 parse_verify_pack_blob_oid |
293 test_cmp expected observed
296 test_expect_success
'verify big limit and small limit results in small limit' '
297 git -C r2 ls-files -s large.1000 >ls_files_result &&
298 test_parse_ls_files_stage_oids <ls_files_result |
301 git -C r2 pack-objects --revs --stdout --filter=blob:limit=10001 \
302 --filter=blob:limit=1001 >filter.pack <<-EOF &&
305 git -C r2 index-pack ../filter.pack &&
307 git -C r2 verify-pack -v ../filter.pack >verify_result &&
308 grep blob verify_result |
309 parse_verify_pack_blob_oid |
312 test_cmp expected observed
315 # Test sparse:path=<path> filter.
317 # NOTE: sparse:path filter support has been dropped for security reasons,
318 # so the tests have been changed to make sure that using it fails.
320 # Use a local file containing a sparse-checkout specification to filter
321 # out blobs not required for the corresponding sparse-checkout. We do not
322 # require sparse-checkout to actually be enabled.
324 test_expect_success
'setup r3' '
327 for n in sparse1 sparse2
329 echo "This is file: $n" > r3/$n &&
331 echo "This is file: dir1/$n" > r3/dir1/$n &&
332 git -C r3 add dir1/$n || return 1
334 git -C r3 commit -m "sparse" &&
335 echo dir1/ >pattern1 &&
336 echo sparse1 >pattern2
339 test_expect_success
'verify blob count in normal packfile' '
340 git -C r3 ls-files -s sparse1 sparse2 dir1/sparse1 dir1/sparse2 \
342 test_parse_ls_files_stage_oids <ls_files_result |
345 git -C r3 pack-objects --revs --stdout >all.pack <<-EOF &&
348 git -C r3 index-pack ../all.pack &&
350 git -C r3 verify-pack -v ../all.pack >verify_result &&
351 grep blob verify_result |
352 parse_verify_pack_blob_oid |
355 test_cmp expected observed
358 test_expect_success
'verify sparse:path=pattern1 fails' '
359 test_must_fail git -C r3 pack-objects --revs --stdout \
360 --filter=sparse:path=../pattern1 <<-EOF
365 test_expect_success
'verify sparse:path=pattern2 fails' '
366 test_must_fail git -C r3 pack-objects --revs --stdout \
367 --filter=sparse:path=../pattern2 <<-EOF
372 # Test sparse:oid=<oid-ish> filter.
373 # Use a blob containing a sparse-checkout specification to filter
374 # out blobs not required for the corresponding sparse-checkout. We do not
375 # require sparse-checkout to actually be enabled.
377 test_expect_success
'setup r4' '
380 for n in sparse1 sparse2
382 echo "This is file: $n" > r4/$n &&
384 echo "This is file: dir1/$n" > r4/dir1/$n &&
385 git -C r4 add dir1/$n || return 1
387 echo dir1/ >r4/pattern &&
388 git -C r4 add pattern &&
389 git -C r4 commit -m "pattern"
392 test_expect_success
'verify blob count in normal packfile' '
393 git -C r4 ls-files -s pattern sparse1 sparse2 dir1/sparse1 dir1/sparse2 \
395 test_parse_ls_files_stage_oids <ls_files_result |
398 git -C r4 pack-objects --revs --stdout >all.pack <<-EOF &&
401 git -C r4 index-pack ../all.pack &&
403 git -C r4 verify-pack -v ../all.pack >verify_result &&
404 grep blob verify_result |
405 parse_verify_pack_blob_oid |
408 test_cmp expected observed
411 test_expect_success
'verify sparse:oid=OID' '
412 git -C r4 ls-files -s dir1/sparse1 dir1/sparse2 >ls_files_result &&
413 test_parse_ls_files_stage_oids <ls_files_result |
416 git -C r4 ls-files -s pattern >staged &&
417 oid=$(test_parse_ls_files_stage_oids <staged) &&
418 git -C r4 pack-objects --revs --stdout --filter=sparse:oid=$oid >filter.pack <<-EOF &&
421 git -C r4 index-pack ../filter.pack &&
423 git -C r4 verify-pack -v ../filter.pack >verify_result &&
424 grep blob verify_result |
425 parse_verify_pack_blob_oid |
428 test_cmp expected observed
431 test_expect_success
'verify sparse:oid=oid-ish' '
432 git -C r4 ls-files -s dir1/sparse1 dir1/sparse2 >ls_files_result &&
433 test_parse_ls_files_stage_oids <ls_files_result |
436 git -C r4 pack-objects --revs --stdout --filter=sparse:oid=main:pattern >filter.pack <<-EOF &&
439 git -C r4 index-pack ../filter.pack &&
441 git -C r4 verify-pack -v ../filter.pack >verify_result &&
442 grep blob verify_result |
443 parse_verify_pack_blob_oid |
446 test_cmp expected observed
449 # Delete some loose objects and use pack-objects, but WITHOUT any filtering.
450 # This models previously omitted objects that we did not receive.
452 test_expect_success
'setup r1 - delete loose blobs' '
453 git -C r1 ls-files -s file.1 file.2 file.3 file.4 file.5 \
455 test_parse_ls_files_stage_oids <ls_files_result |
458 for id in `sed "s|..|&/|" expected`
460 rm r1/.git/objects/$id || return 1
464 test_expect_success
'verify pack-objects fails w/ missing objects' '
465 test_must_fail git -C r1 pack-objects --revs --stdout >miss.pack <<-EOF
470 test_expect_success
'verify pack-objects fails w/ --missing=error' '
471 test_must_fail git -C r1 pack-objects --revs --stdout --missing=error >miss.pack <<-EOF
476 test_expect_success
'verify pack-objects w/ --missing=allow-any' '
477 git -C r1 pack-objects --revs --stdout --missing=allow-any >miss.pack <<-EOF