3 test_description
='git rev-list 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 the blob:none filter.
13 test_expect_success
'setup r1' '
14 echo "{print \$1}" >print_1.awk &&
15 echo "{print \$2}" >print_2.awk &&
20 echo "This is file: $n" > r1/file.$n &&
21 git -C r1 add file.$n &&
22 git -C r1 commit -m "$n" || return 1
26 test_expect_success
'verify blob:none omits all 5 blobs' '
27 git -C r1 ls-files -s file.1 file.2 file.3 file.4 file.5 \
29 awk -f print_2.awk ls_files_result |
32 git -C r1 rev-list --quiet --objects --filter-print-omitted \
33 --filter=blob:none HEAD >revs &&
34 awk -f print_1.awk revs |
38 test_cmp expected observed
41 test_expect_success
'specify blob explicitly prevents filtering' '
42 file_3=$(git -C r1 ls-files -s file.3 |
43 awk -f print_2.awk) &&
45 file_4=$(git -C r1 ls-files -s file.4 |
46 awk -f print_2.awk) &&
48 git -C r1 rev-list --objects --filter=blob:none HEAD $file_3 >observed &&
49 grep "$file_3" observed &&
50 ! grep "$file_4" observed
53 test_expect_success
'verify emitted+omitted == all' '
54 git -C r1 rev-list --objects HEAD >revs &&
55 awk -f print_1.awk revs |
58 git -C r1 rev-list --objects --filter-print-omitted --filter=blob:none \
60 awk -f print_1.awk revs |
64 test_cmp expected observed
68 # Test blob:limit=<n>[kmg] filter.
69 # We boundary test around the size parameter. The filter is strictly less than
70 # the value, so size 500 and 1000 should have the same results, but 1001 should
73 test_expect_success
'setup r2' '
77 printf "%"$n"s" X > r2/large.$n &&
78 git -C r2 add large.$n &&
79 git -C r2 commit -m "$n" || return 1
83 test_expect_success
'verify blob:limit=500 omits all blobs' '
84 git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
85 awk -f print_2.awk ls_files_result |
88 git -C r2 rev-list --quiet --objects --filter-print-omitted \
89 --filter=blob:limit=500 HEAD >revs &&
90 awk -f print_1.awk revs |
94 test_cmp expected observed
97 test_expect_success
'verify emitted+omitted == all' '
98 git -C r2 rev-list --objects HEAD >revs &&
99 awk -f print_1.awk revs |
102 git -C r2 rev-list --objects --filter-print-omitted \
103 --filter=blob:limit=500 HEAD >revs &&
104 awk -f print_1.awk revs |
108 test_cmp expected observed
111 test_expect_success
'verify blob:limit=1000' '
112 git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
113 awk -f print_2.awk ls_files_result |
116 git -C r2 rev-list --quiet --objects --filter-print-omitted \
117 --filter=blob:limit=1000 HEAD >revs &&
118 awk -f print_1.awk revs |
122 test_cmp expected observed
125 test_expect_success
'verify blob:limit=1001' '
126 git -C r2 ls-files -s large.10000 >ls_files_result &&
127 awk -f print_2.awk ls_files_result |
130 git -C r2 rev-list --quiet --objects --filter-print-omitted \
131 --filter=blob:limit=1001 HEAD >revs &&
132 awk -f print_1.awk revs |
136 test_cmp expected observed
139 test_expect_success
'verify blob:limit=1k' '
140 git -C r2 ls-files -s large.10000 >ls_files_result &&
141 awk -f print_2.awk ls_files_result |
144 git -C r2 rev-list --quiet --objects --filter-print-omitted \
145 --filter=blob:limit=1k HEAD >revs &&
146 awk -f print_1.awk revs |
150 test_cmp expected observed
153 test_expect_success
'verify blob:limit=1m' '
154 git -C r2 rev-list --quiet --objects --filter-print-omitted \
155 --filter=blob:limit=1m HEAD >revs &&
156 awk -f print_1.awk revs |
160 test_must_be_empty observed
163 # Test object:type=<type> filter.
165 test_expect_success
'setup object-type' '
166 test_create_repo object-type &&
167 test_commit --no-tag -C object-type message blob &&
168 git -C object-type tag tag -m tag-message
171 test_expect_success
'verify object:type= fails with invalid type' '
172 test_must_fail git -C object-type rev-list --objects --filter=object:type= HEAD &&
173 test_must_fail git -C object-type rev-list --objects --filter=object:type=invalid HEAD
176 test_expect_success
'verify object:type=blob prints blob and commit' '
177 git -C object-type rev-parse HEAD >expected &&
178 printf "%s blob\n" $(git -C object-type rev-parse HEAD:blob) >>expected &&
179 git -C object-type rev-list --objects --filter=object:type=blob HEAD >actual &&
180 test_cmp expected actual
183 test_expect_success
'verify object:type=tree prints tree and commit' '
185 git -C object-type rev-parse HEAD &&
186 printf "%s \n" $(git -C object-type rev-parse HEAD^{tree})
188 git -C object-type rev-list --objects --filter=object:type=tree HEAD >actual &&
189 test_cmp expected actual
192 test_expect_success
'verify object:type=commit prints commit' '
193 git -C object-type rev-parse HEAD >expected &&
194 git -C object-type rev-list --objects --filter=object:type=commit HEAD >actual &&
195 test_cmp expected actual
198 test_expect_success
'verify object:type=tag prints tag' '
200 git -C object-type rev-parse HEAD &&
201 printf "%s tag\n" $(git -C object-type rev-parse tag)
203 git -C object-type rev-list --objects --filter=object:type=tag tag >actual &&
204 test_cmp expected actual
207 test_expect_success
'verify object:type=blob prints only blob with --filter-provided-objects' '
208 printf "%s blob\n" $(git -C object-type rev-parse HEAD:blob) >expected &&
209 git -C object-type rev-list --objects \
210 --filter=object:type=blob --filter-provided-objects HEAD >actual &&
211 test_cmp expected actual
214 test_expect_success
'verify object:type=tree prints only tree with --filter-provided-objects' '
215 printf "%s \n" $(git -C object-type rev-parse HEAD^{tree}) >expected &&
216 git -C object-type rev-list --objects \
217 --filter=object:type=tree HEAD --filter-provided-objects >actual &&
218 test_cmp expected actual
221 test_expect_success
'verify object:type=commit prints only commit with --filter-provided-objects' '
222 git -C object-type rev-parse HEAD >expected &&
223 git -C object-type rev-list --objects \
224 --filter=object:type=commit --filter-provided-objects HEAD >actual &&
225 test_cmp expected actual
228 test_expect_success
'verify object:type=tag prints only tag with --filter-provided-objects' '
229 printf "%s tag\n" $(git -C object-type rev-parse tag) >expected &&
230 git -C object-type rev-list --objects \
231 --filter=object:type=tag --filter-provided-objects tag >actual &&
232 test_cmp expected actual
235 # Test sparse:path=<path> filter.
237 # NOTE: sparse:path filter support has been dropped for security reasons,
238 # so the tests have been changed to make sure that using it fails.
240 # Use a local file containing a sparse-checkout specification to filter
241 # out blobs not required for the corresponding sparse-checkout. We do not
242 # require sparse-checkout to actually be enabled.
244 test_expect_success
'setup r3' '
247 for n in sparse1 sparse2
249 echo "This is file: $n" > r3/$n &&
251 echo "This is file: dir1/$n" > r3/dir1/$n &&
252 git -C r3 add dir1/$n || return 1
254 git -C r3 commit -m "sparse" &&
255 echo dir1/ >pattern1 &&
256 echo sparse1 >pattern2
259 test_expect_success
'verify sparse:path=pattern1 fails' '
260 test_must_fail git -C r3 rev-list --quiet --objects \
261 --filter-print-omitted --filter=sparse:path=../pattern1 HEAD
264 test_expect_success
'verify sparse:path=pattern2 fails' '
265 test_must_fail git -C r3 rev-list --quiet --objects \
266 --filter-print-omitted --filter=sparse:path=../pattern2 HEAD
269 # Test sparse:oid=<oid-ish> filter.
270 # Use a blob containing a sparse-checkout specification to filter
271 # out blobs not required for the corresponding sparse-checkout. We do not
272 # require sparse-checkout to actually be enabled.
274 test_expect_success
'setup r3 part 2' '
275 echo dir1/ >r3/pattern &&
276 git -C r3 add pattern &&
277 git -C r3 commit -m "pattern"
280 test_expect_success
'verify sparse:oid=OID omits top-level files' '
281 git -C r3 ls-files -s pattern sparse1 sparse2 >ls_files_result &&
282 awk -f print_2.awk ls_files_result |
285 oid=$(git -C r3 ls-files -s pattern | awk -f print_2.awk) &&
287 git -C r3 rev-list --quiet --objects --filter-print-omitted \
288 --filter=sparse:oid=$oid HEAD >revs &&
289 awk -f print_1.awk revs |
293 test_cmp expected observed
296 test_expect_success
'verify sparse:oid=oid-ish omits top-level files' '
297 git -C r3 ls-files -s pattern sparse1 sparse2 >ls_files_result &&
298 awk -f print_2.awk ls_files_result |
301 git -C r3 rev-list --quiet --objects --filter-print-omitted \
302 --filter=sparse:oid=main:pattern HEAD >revs &&
303 awk -f print_1.awk revs |
307 test_cmp expected observed
310 test_expect_success
'rev-list W/ --missing=print and --missing=allow-any for trees' '
311 TREE=$(git -C r3 rev-parse HEAD:dir1) &&
313 # Create a spare repo because we will be deleting objects from this one.
316 rm r3.b/.git/objects/$(echo $TREE | sed "s|^..|&/|") &&
318 git -C r3.b rev-list --quiet --missing=print --objects HEAD \
319 >missing_objs 2>rev_list_err &&
320 echo "?$TREE" >expected &&
321 test_cmp expected missing_objs &&
323 # do not complain when a missing tree cannot be parsed
324 test_must_be_empty rev_list_err &&
326 git -C r3.b rev-list --missing=allow-any --objects HEAD \
327 >objs 2>rev_list_err &&
329 test_must_be_empty rev_list_err
332 # Test tree:0 filter.
334 test_expect_success
'verify tree:0 includes trees in "filtered" output' '
335 git -C r3 rev-list --quiet --objects --filter-print-omitted \
336 --filter=tree:0 HEAD >revs &&
338 awk -f print_1.awk revs |
340 xargs -n1 git -C r3 cat-file -t >unsorted_filtered_types &&
342 sort -u unsorted_filtered_types >filtered_types &&
343 test_write_lines blob tree >expected &&
344 test_cmp expected filtered_types
347 # Make sure tree:0 does not iterate through any trees.
349 test_expect_success
'verify skipping tree iteration when not collecting omits' '
350 GIT_TRACE=1 git -C r3 rev-list \
351 --objects --filter=tree:0 HEAD 2>filter_trace &&
352 grep "Skipping contents of tree [.][.][.]" filter_trace >actual &&
353 # One line for each commit traversed.
354 test_line_count = 2 actual &&
356 # Make sure no other trees were considered besides the root.
357 ! grep "Skipping contents of tree [^.]" filter_trace &&
359 # Try this again with "combine:". If both sub-filters are skipping
360 # trees, the composite filter should also skip trees. This is not
361 # important unless the user does combine:tree:X+tree:Y or another filter
362 # besides "tree:" is implemented in the future which can skip trees.
363 GIT_TRACE=1 git -C r3 rev-list \
364 --objects --filter=combine:tree:1+tree:3 HEAD 2>filter_trace &&
366 # Only skip the dir1/ tree, which is shared between the two commits.
367 grep "Skipping contents of tree " filter_trace >actual &&
368 test_write_lines "Skipping contents of tree dir1/..." >expected &&
369 test_cmp expected actual
372 # Test tree:# filters.
378 hash=$
(git
-C r3 rev-parse
$commit:$name) &&
379 grep "^$hash $name$" actual
382 test_expect_success
'verify tree:1 includes root trees' '
383 git -C r3 rev-list --objects --filter=tree:1 HEAD >actual &&
385 # We should get two root directories and two commits.
386 expect_has HEAD "" &&
387 expect_has HEAD~1 "" &&
388 test_line_count = 4 actual
391 test_expect_success
'verify tree:2 includes root trees and immediate children' '
392 git -C r3 rev-list --objects --filter=tree:2 HEAD >actual &&
394 expect_has HEAD "" &&
395 expect_has HEAD~1 "" &&
396 expect_has HEAD dir1 &&
397 expect_has HEAD pattern &&
398 expect_has HEAD sparse1 &&
399 expect_has HEAD sparse2 &&
401 # There are also 2 commit objects
402 test_line_count = 8 actual
405 test_expect_success
'verify tree:3 includes everything expected' '
406 git -C r3 rev-list --objects --filter=tree:3 HEAD >actual &&
408 expect_has HEAD "" &&
409 expect_has HEAD~1 "" &&
410 expect_has HEAD dir1 &&
411 expect_has HEAD dir1/sparse1 &&
412 expect_has HEAD dir1/sparse2 &&
413 expect_has HEAD pattern &&
414 expect_has HEAD sparse1 &&
415 expect_has HEAD sparse2 &&
417 # There are also 2 commit objects
418 test_line_count = 10 actual
421 test_expect_success
'combine:... for a simple combination' '
422 git -C r3 rev-list --objects --filter=combine:tree:2+blob:none HEAD \
425 expect_has HEAD "" &&
426 expect_has HEAD~1 "" &&
427 expect_has HEAD dir1 &&
429 # There are also 2 commit objects
430 test_line_count = 5 actual &&
432 cp actual expected &&
434 # Try again using repeated --filter - this is equivalent to a manual
435 # combine with "combine:...+..."
436 git -C r3 rev-list --objects --filter=combine:tree:2 \
437 --filter=blob:none HEAD >actual &&
439 test_cmp expected actual
442 test_expect_success
'combine:... with URL encoding' '
443 git -C r3 rev-list --objects \
444 --filter=combine:tree%3a2+blob:%6Eon%65 HEAD >actual &&
446 expect_has HEAD "" &&
447 expect_has HEAD~1 "" &&
448 expect_has HEAD dir1 &&
450 # There are also 2 commit objects
451 test_line_count = 5 actual
454 expect_invalid_filter_spec
() {
458 test_must_fail git
-C r3 rev-list
--objects --filter="$spec" HEAD \
459 >actual
2>actual_stderr
&&
460 test_must_be_empty actual
&&
461 test_grep
"$err" actual_stderr
464 test_expect_success
'combine:... while URL-encoding things that should not be' '
465 expect_invalid_filter_spec combine%3Atree:2+blob:none \
466 "invalid filter-spec"
469 test_expect_success
'combine: with nothing after the :' '
470 expect_invalid_filter_spec combine: "expected something after combine:"
473 test_expect_success
'parse error in first sub-filter in combine:' '
474 expect_invalid_filter_spec combine:tree:asdf+blob:none \
475 "expected .tree:<depth>."
478 test_expect_success
'combine:... with non-encoded reserved chars' '
479 expect_invalid_filter_spec combine:tree:2+sparse:@xyz \
480 "must escape char in sub-filter-spec: .@." &&
481 expect_invalid_filter_spec combine:tree:2+sparse:\` \
482 "must escape char in sub-filter-spec: .\`." &&
483 expect_invalid_filter_spec combine:tree:2+sparse:~abc \
484 "must escape char in sub-filter-spec: .\~."
487 test_expect_success
'validate err msg for "combine:<valid-filter>+"' '
488 expect_invalid_filter_spec combine:tree:2+ "expected .tree:<depth>."
491 test_expect_success
'combine:... with edge-case hex digits: Ff Aa 0 9' '
492 git -C r3 rev-list --objects --filter="combine:tree:2+bl%6Fb:n%6fne" \
494 test_line_count = 5 actual &&
495 git -C r3 rev-list --objects --filter="combine:tree%3A2+blob%3anone" \
497 test_line_count = 5 actual &&
498 git -C r3 rev-list --objects --filter="combine:tree:%30" HEAD >actual &&
499 test_line_count = 2 actual &&
500 git -C r3 rev-list --objects --filter="combine:tree:%39+blob:none" \
502 test_line_count = 5 actual
505 test_expect_success
'add sparse pattern blobs whose paths have reserved chars' '
506 cp r3/pattern r3/pattern1+renamed% &&
507 cp r3/pattern "r3/p;at%ter+n" &&
508 cp r3/pattern r3/^~pattern &&
509 git -C r3 add pattern1+renamed% "p;at%ter+n" ^~pattern &&
510 git -C r3 commit -m "add sparse pattern files with reserved chars"
513 test_expect_success
'combine:... with more than two sub-filters' '
514 git -C r3 rev-list --objects \
515 --filter=combine:tree:3+blob:limit=40+sparse:oid=main:pattern \
518 expect_has HEAD "" &&
519 expect_has HEAD~1 "" &&
520 expect_has HEAD~2 "" &&
521 expect_has HEAD dir1 &&
522 expect_has HEAD dir1/sparse1 &&
523 expect_has HEAD dir1/sparse2 &&
525 # Should also have 3 commits
526 test_line_count = 9 actual &&
528 # Try again, this time making sure the last sub-filter is only
532 git -C r3 rev-list --objects \
533 --filter=combine:tree:3+blob:limit=40+sparse:oid=main:pattern1%2brenamed%25 \
535 test_cmp expect actual &&
537 # Use the same composite filter again, but with a pattern file name that
538 # requires encoding multiple characters, and use implicit filter
540 test_when_finished "rm -f trace1" &&
541 GIT_TRACE=$(pwd)/trace1 git -C r3 rev-list --objects \
542 --filter=tree:3 --filter=blob:limit=40 \
543 --filter=sparse:oid="main:p;at%ter+n" \
546 test_cmp expect actual &&
547 grep "Add to combine filter-spec: sparse:oid=main:p%3bat%25ter%2bn" \
550 # Repeat the above test, but this time, the characters to encode are in
551 # the LHS of the combined filter.
552 test_when_finished "rm -f trace2" &&
553 GIT_TRACE=$(pwd)/trace2 git -C r3 rev-list --objects \
554 --filter=sparse:oid=main:^~pattern \
555 --filter=tree:3 --filter=blob:limit=40 \
558 test_cmp expect actual &&
559 grep "Add to combine filter-spec: sparse:oid=main:%5e%7epattern" \
563 # Test provisional omit collection logic with a repo that has objects appearing
564 # at multiple depths - first deeper than the filter's threshold, then shallow.
566 test_expect_success
'setup r4' '
571 echo bar > r4/subdir/bar &&
574 cp -r r4/foo r4/subdir r4/filt &&
576 git -C r4 add foo subdir filt &&
577 git -C r4 commit -m "commit msg"
580 expect_has_with_different_name
() {
584 hash=$
(git
-C $repo rev-parse HEAD
:$name) &&
585 ! grep "^$hash $name$" actual
&&
586 grep "^$hash " actual
&&
587 ! grep "~$hash" actual
590 test_expect_success
'test tree:# filter provisional omit for blob and tree' '
591 git -C r4 rev-list --objects --filter-print-omitted --filter=tree:2 \
593 expect_has_with_different_name r4 filt/foo &&
594 expect_has_with_different_name r4 filt/subdir
597 test_expect_success
'verify skipping tree iteration when collecting omits' '
598 GIT_TRACE=1 git -C r4 rev-list --filter-print-omitted \
599 --objects --filter=tree:0 HEAD 2>filter_trace &&
600 grep "^Skipping contents of tree " filter_trace >actual &&
602 echo "Skipping contents of tree subdir/..." >expect &&
603 test_cmp expect actual
606 test_expect_success
'setup r5' '
608 mkdir -p r5/subdir &&
610 echo 1 >r5/short-root &&
611 echo 12345 >r5/long-root &&
612 echo a >r5/subdir/short-subdir &&
613 echo abcde >r5/subdir/long-subdir &&
615 git -C r5 add short-root long-root subdir &&
616 git -C r5 commit -m "commit msg"
619 test_expect_success
'verify collecting omits in combined: filter' '
620 # Note that this test guards against the naive implementation of simply
621 # giving both filters the same "omits" set and expecting it to
622 # automatically merge them.
623 git -C r5 rev-list --objects --quiet --filter-print-omitted \
624 --filter=combine:tree:2+blob:limit=3 HEAD >actual &&
626 # Expect 0 trees/commits, 3 blobs omitted (all blobs except short-root)
627 omitted_1=$(echo 12345 | git hash-object --stdin) &&
628 omitted_2=$(echo a | git hash-object --stdin) &&
629 omitted_3=$(echo abcde | git hash-object --stdin) &&
631 grep ~$omitted_1 actual &&
632 grep ~$omitted_2 actual &&
633 grep ~$omitted_3 actual &&
634 test_line_count = 3 actual
637 # Test tree:<depth> where a tree is iterated to twice - once where a subentry is
638 # too deep to be included, and again where the blob inside it is shallow enough
639 # to be included. This makes sure we don't use LOFR_MARK_SEEN incorrectly (we
640 # can't use it because a tree can be iterated over again at a lower depth).
642 test_expect_success
'tree:<depth> where we iterate over tree at two levels' '
645 mkdir -p r5/a/subdir/b &&
646 echo foo > r5/a/subdir/b/foo &&
648 mkdir -p r5/subdir/b &&
649 echo foo > r5/subdir/b/foo &&
651 git -C r5 add a subdir &&
652 git -C r5 commit -m "commit msg" &&
654 git -C r5 rev-list --objects --filter=tree:4 HEAD >actual &&
655 expect_has_with_different_name r5 a/subdir/b/foo
658 test_expect_success
'tree:<depth> which filters out blob but given as arg' '
659 blob_hash=$(git -C r4 rev-parse HEAD:subdir/bar) &&
661 git -C r4 rev-list --objects --filter=tree:1 HEAD $blob_hash >actual &&
662 grep ^$blob_hash actual
665 # Delete some loose objects and use rev-list, but WITHOUT any filtering.
666 # This models previously omitted objects that we did not receive.
668 test_expect_success
'rev-list W/ --missing=print' '
669 git -C r1 ls-files -s file.1 file.2 file.3 file.4 file.5 \
671 awk -f print_2.awk ls_files_result |
674 for id in `sed "s|..|&/|" expected`
676 rm r1/.git/objects/$id || return 1
679 git -C r1 rev-list --quiet --missing=print --objects HEAD >revs &&
680 awk -f print_1.awk revs |
684 test_cmp expected observed
687 test_expect_success
'rev-list W/O --missing fails' '
688 test_must_fail git -C r1 rev-list --quiet --objects HEAD
691 test_expect_success
'rev-list W/ missing=allow-any' '
692 git -C r1 rev-list --quiet --missing=allow-any --objects HEAD
695 # Test expansion of filter specs.
697 test_expect_success
'expand blob limit in protocol' '
698 git -C r2 config --local uploadpack.allowfilter 1 &&
699 GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 clone \
700 --filter=blob:limit=1k "file://$(pwd)/r2" limit &&
701 ! grep "blob:limit=1k" trace &&
702 grep "blob:limit=1024" trace
705 test_expect_success EXPENSIVE
'large sparse filter file ignored' '
706 blob=$(dd if=/dev/zero bs=101M count=1 |
707 git hash-object -w --stdin) &&
709 git rev-list --all --objects --filter=sparse:oid=$blob 2>err &&
710 cat >expect <<-EOF &&
711 warning: ignoring excessively large pattern blob: $blob
712 fatal: unable to parse sparse filter data in $blob