builtin: patch-id: add --verbatim as a command mode
[git/debian.git] / t / t5329-pack-objects-cruft.sh
blob8968f7a08d8700bb22aec956e3f8e50ab2f376bd
1 #!/bin/sh
3 test_description='cruft pack related pack-objects tests'
4 . ./test-lib.sh
6 objdir=.git/objects
7 packdir=$objdir/pack
9 basic_cruft_pack_tests () {
10 expire="$1"
12 test_expect_success "unreachable loose objects are packed (expire $expire)" '
13 git init repo &&
14 test_when_finished "rm -fr repo" &&
16 cd repo &&
18 test_commit base &&
19 git repack -Ad &&
20 test_commit loose &&
22 test-tool chmtime +2000 "$objdir/$(test_oid_to_path \
23 $(git rev-parse loose:loose.t))" &&
24 test-tool chmtime +1000 "$objdir/$(test_oid_to_path \
25 $(git rev-parse loose^{tree}))" &&
28 git rev-list --objects --no-object-names base..loose |
29 while read oid
31 path="$objdir/$(test_oid_to_path "$oid")" &&
32 printf "%s %d\n" "$oid" "$(test-tool chmtime --get "$path")"
33 done |
34 sort -k1
35 ) >expect &&
37 keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
38 cruft="$(echo $keep | git pack-objects --cruft \
39 --cruft-expiration="$expire" $packdir/pack)" &&
40 test-tool pack-mtimes "pack-$cruft.mtimes" >actual &&
42 test_cmp expect actual
46 test_expect_success "unreachable packed objects are packed (expire $expire)" '
47 git init repo &&
48 test_when_finished "rm -fr repo" &&
50 cd repo &&
52 test_commit packed &&
53 git repack -Ad &&
54 test_commit other &&
56 git rev-list --objects --no-object-names packed.. >objects &&
57 keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
58 other="$(git pack-objects --delta-base-offset \
59 $packdir/pack <objects)" &&
60 git prune-packed &&
62 test-tool chmtime --get -100 "$packdir/pack-$other.pack" >expect &&
64 cruft="$(git pack-objects --cruft --cruft-expiration="$expire" $packdir/pack <<-EOF
65 $keep
66 -pack-$other.pack
67 EOF
68 )" &&
69 test-tool pack-mtimes "pack-$cruft.mtimes" >actual.raw &&
71 cut -d" " -f2 <actual.raw | sort -u >actual &&
73 test_cmp expect actual
77 test_expect_success "unreachable cruft objects are repacked (expire $expire)" '
78 git init repo &&
79 test_when_finished "rm -fr repo" &&
81 cd repo &&
83 test_commit packed &&
84 git repack -Ad &&
85 test_commit other &&
87 git rev-list --objects --no-object-names packed.. >objects &&
88 keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
90 cruft_a="$(echo $keep | git pack-objects --cruft --cruft-expiration="$expire" $packdir/pack)" &&
91 git prune-packed &&
92 cruft_b="$(git pack-objects --cruft --cruft-expiration="$expire" $packdir/pack <<-EOF
93 $keep
94 -pack-$cruft_a.pack
95 EOF
96 )" &&
98 test-tool pack-mtimes "pack-$cruft_a.mtimes" >expect.raw &&
99 test-tool pack-mtimes "pack-$cruft_b.mtimes" >actual.raw &&
101 sort <expect.raw >expect &&
102 sort <actual.raw >actual &&
104 test_cmp expect actual
108 test_expect_success "multiple cruft packs (expire $expire)" '
109 git init repo &&
110 test_when_finished "rm -fr repo" &&
112 cd repo &&
114 test_commit reachable &&
115 git repack -Ad &&
116 keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
118 test_commit cruft &&
119 loose="$objdir/$(test_oid_to_path $(git rev-parse cruft))" &&
121 # generate three copies of the cruft object in different
122 # cruft packs, each with a unique mtime:
123 # - one expired (1000 seconds ago)
124 # - two non-expired (one 1000 seconds in the future,
125 # one 1500 seconds in the future)
126 test-tool chmtime =-1000 "$loose" &&
127 git pack-objects --cruft $packdir/pack-A <<-EOF &&
128 $keep
130 test-tool chmtime =+1000 "$loose" &&
131 git pack-objects --cruft $packdir/pack-B <<-EOF &&
132 $keep
133 -$(basename $(ls $packdir/pack-A-*.pack))
135 test-tool chmtime =+1500 "$loose" &&
136 git pack-objects --cruft $packdir/pack-C <<-EOF &&
137 $keep
138 -$(basename $(ls $packdir/pack-A-*.pack))
139 -$(basename $(ls $packdir/pack-B-*.pack))
142 # ensure the resulting cruft pack takes the most recent
143 # mtime among all copies
144 cruft="$(git pack-objects --cruft \
145 --cruft-expiration="$expire" \
146 $packdir/pack <<-EOF
147 $keep
148 -$(basename $(ls $packdir/pack-A-*.pack))
149 -$(basename $(ls $packdir/pack-B-*.pack))
150 -$(basename $(ls $packdir/pack-C-*.pack))
152 )" &&
154 test-tool pack-mtimes "$(basename $(ls $packdir/pack-C-*.mtimes))" >expect.raw &&
155 test-tool pack-mtimes "pack-$cruft.mtimes" >actual.raw &&
157 sort expect.raw >expect &&
158 sort actual.raw >actual &&
159 test_cmp expect actual
163 test_expect_success "cruft packs tolerate missing trees (expire $expire)" '
164 git init repo &&
165 test_when_finished "rm -fr repo" &&
167 cd repo &&
169 test_commit reachable &&
170 test_commit cruft &&
172 tree="$(git rev-parse cruft^{tree})" &&
174 git reset --hard reachable &&
175 git tag -d cruft &&
176 git reflog expire --all --expire=all &&
178 # remove the unreachable tree, but leave the commit
179 # which has it as its root tree intact
180 rm -fr "$objdir/$(test_oid_to_path "$tree")" &&
182 git repack -Ad &&
183 basename $(ls $packdir/pack-*.pack) >in &&
184 git pack-objects --cruft --cruft-expiration="$expire" \
185 $packdir/pack <in
189 test_expect_success "cruft packs tolerate missing blobs (expire $expire)" '
190 git init repo &&
191 test_when_finished "rm -fr repo" &&
193 cd repo &&
195 test_commit reachable &&
196 test_commit cruft &&
198 blob="$(git rev-parse cruft:cruft.t)" &&
200 git reset --hard reachable &&
201 git tag -d cruft &&
202 git reflog expire --all --expire=all &&
204 # remove the unreachable blob, but leave the commit (and
205 # the root tree of that commit) intact
206 rm -fr "$objdir/$(test_oid_to_path "$blob")" &&
208 git repack -Ad &&
209 basename $(ls $packdir/pack-*.pack) >in &&
210 git pack-objects --cruft --cruft-expiration="$expire" \
211 $packdir/pack <in
216 basic_cruft_pack_tests never
217 basic_cruft_pack_tests 2.weeks.ago
219 test_expect_success 'cruft tags rescue tagged objects' '
220 git init repo &&
221 test_when_finished "rm -fr repo" &&
223 cd repo &&
225 test_commit packed &&
226 git repack -Ad &&
228 test_commit tagged &&
229 git tag -a annotated -m tag &&
231 git rev-list --objects --no-object-names packed.. >objects &&
232 while read oid
234 test-tool chmtime -1000 \
235 "$objdir/$(test_oid_to_path $oid)"
236 done <objects &&
238 test-tool chmtime -500 \
239 "$objdir/$(test_oid_to_path $(git rev-parse annotated))" &&
241 keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
242 cruft="$(echo $keep | git pack-objects --cruft \
243 --cruft-expiration=750.seconds.ago \
244 $packdir/pack)" &&
245 test-tool pack-mtimes "pack-$cruft.mtimes" >actual.raw &&
246 cut -f1 -d" " <actual.raw | sort >actual &&
249 cat objects &&
250 git rev-parse annotated
251 ) >expect.raw &&
252 sort <expect.raw >expect &&
254 test_cmp expect actual &&
255 cat actual
259 test_expect_success 'cruft commits rescue parents, trees' '
260 git init repo &&
261 test_when_finished "rm -fr repo" &&
263 cd repo &&
265 test_commit packed &&
266 git repack -Ad &&
268 test_commit old &&
269 test_commit new &&
271 git rev-list --objects --no-object-names packed..new >objects &&
272 while read object
274 test-tool chmtime -1000 \
275 "$objdir/$(test_oid_to_path $object)"
276 done <objects &&
277 test-tool chmtime +500 "$objdir/$(test_oid_to_path \
278 $(git rev-parse HEAD))" &&
280 keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
281 cruft="$(echo $keep | git pack-objects --cruft \
282 --cruft-expiration=750.seconds.ago \
283 $packdir/pack)" &&
284 test-tool pack-mtimes "pack-$cruft.mtimes" >actual.raw &&
286 cut -d" " -f1 <actual.raw | sort >actual &&
287 sort <objects >expect &&
289 test_cmp expect actual
293 test_expect_success 'cruft trees rescue sub-trees, blobs' '
294 git init repo &&
295 test_when_finished "rm -fr repo" &&
297 cd repo &&
299 test_commit packed &&
300 git repack -Ad &&
302 mkdir -p dir/sub &&
303 echo foo >foo &&
304 echo bar >dir/bar &&
305 echo baz >dir/sub/baz &&
307 test_tick &&
308 git add . &&
309 git commit -m "pruned" &&
311 test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD))" &&
312 test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD^{tree}))" &&
313 test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD:foo))" &&
314 test-tool chmtime -500 "$objdir/$(test_oid_to_path $(git rev-parse HEAD:dir))" &&
315 test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD:dir/bar))" &&
316 test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD:dir/sub))" &&
317 test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD:dir/sub/baz))" &&
319 keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
320 cruft="$(echo $keep | git pack-objects --cruft \
321 --cruft-expiration=750.seconds.ago \
322 $packdir/pack)" &&
323 test-tool pack-mtimes "pack-$cruft.mtimes" >actual.raw &&
324 cut -f1 -d" " <actual.raw | sort >actual &&
326 git rev-parse HEAD:dir HEAD:dir/bar HEAD:dir/sub HEAD:dir/sub/baz >expect.raw &&
327 sort <expect.raw >expect &&
329 test_cmp expect actual
333 test_expect_success 'expired objects are pruned' '
334 git init repo &&
335 test_when_finished "rm -fr repo" &&
337 cd repo &&
339 test_commit packed &&
340 git repack -Ad &&
342 test_commit pruned &&
344 git rev-list --objects --no-object-names packed..pruned >objects &&
345 while read object
347 test-tool chmtime -1000 \
348 "$objdir/$(test_oid_to_path $object)"
349 done <objects &&
351 keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
352 cruft="$(echo $keep | git pack-objects --cruft \
353 --cruft-expiration=750.seconds.ago \
354 $packdir/pack)" &&
356 test-tool pack-mtimes "pack-$cruft.mtimes" >actual &&
357 test_must_be_empty actual
361 test_expect_success 'repack --cruft generates a cruft pack' '
362 git init repo &&
363 test_when_finished "rm -fr repo" &&
365 cd repo &&
367 test_commit reachable &&
368 git branch -M main &&
369 git checkout --orphan other &&
370 test_commit unreachable &&
372 git checkout main &&
373 git branch -D other &&
374 git tag -d unreachable &&
375 # objects are not cruft if they are contained in the reflogs
376 git reflog expire --all --expire=all &&
378 git rev-list --objects --all --no-object-names >reachable.raw &&
379 git cat-file --batch-all-objects --batch-check="%(objectname)" >objects &&
380 sort <reachable.raw >reachable &&
381 comm -13 reachable objects >unreachable &&
383 git repack --cruft -d &&
385 cruft=$(basename $(ls $packdir/pack-*.mtimes) .mtimes) &&
386 pack=$(basename $(ls $packdir/pack-*.pack | grep -v $cruft) .pack) &&
388 git show-index <$packdir/$pack.idx >actual.raw &&
389 cut -f2 -d" " actual.raw | sort >actual &&
390 test_cmp reachable actual &&
392 git show-index <$packdir/$cruft.idx >actual.raw &&
393 cut -f2 -d" " actual.raw | sort >actual &&
394 test_cmp unreachable actual
398 test_expect_success 'loose objects mtimes upsert others' '
399 git init repo &&
400 test_when_finished "rm -fr repo" &&
402 cd repo &&
404 test_commit reachable &&
405 git repack -Ad &&
406 git branch -M main &&
408 git checkout --orphan other &&
409 test_commit cruft &&
410 # incremental repack, leaving existing objects loose (so
411 # they can be "freshened")
412 git repack &&
414 tip="$(git rev-parse cruft)" &&
415 path="$objdir/$(test_oid_to_path "$tip")" &&
416 test-tool chmtime --get +1000 "$path" >expect &&
418 git checkout main &&
419 git branch -D other &&
420 git tag -d cruft &&
421 git reflog expire --all --expire=all &&
423 git repack --cruft -d &&
425 mtimes="$(basename $(ls $packdir/pack-*.mtimes))" &&
426 test-tool pack-mtimes "$mtimes" >actual.raw &&
427 grep "$tip" actual.raw | cut -d" " -f2 >actual &&
428 test_cmp expect actual
432 test_expect_success 'expiring cruft objects with git gc' '
433 git init repo &&
434 test_when_finished "rm -fr repo" &&
436 cd repo &&
438 test_commit reachable &&
439 git branch -M main &&
440 git checkout --orphan other &&
441 test_commit unreachable &&
443 git checkout main &&
444 git branch -D other &&
445 git tag -d unreachable &&
446 # objects are not cruft if they are contained in the reflogs
447 git reflog expire --all --expire=all &&
449 git rev-list --objects --all --no-object-names >reachable.raw &&
450 git cat-file --batch-all-objects --batch-check="%(objectname)" >objects &&
451 sort <reachable.raw >reachable &&
452 comm -13 reachable objects >unreachable &&
454 # Write a cruft pack containing all unreachable objects.
455 git gc --cruft --prune="01-01-1980" &&
457 mtimes=$(ls .git/objects/pack/pack-*.mtimes) &&
458 test_path_is_file $mtimes &&
460 # Prune all unreachable objects from the cruft pack.
461 git gc --cruft --prune=now &&
463 git cat-file --batch-all-objects --batch-check="%(objectname)" >objects &&
465 comm -23 unreachable objects >removed &&
466 test_cmp unreachable removed &&
467 test_path_is_missing $mtimes
471 test_expect_success 'cruft packs are not included in geometric repack' '
472 git init repo &&
473 test_when_finished "rm -fr repo" &&
475 cd repo &&
477 test_commit reachable &&
478 git repack -Ad &&
479 git branch -M main &&
481 git checkout --orphan other &&
482 test_commit cruft &&
483 git repack -d &&
485 git checkout main &&
486 git branch -D other &&
487 git tag -d cruft &&
488 git reflog expire --all --expire=all &&
490 git repack --cruft &&
492 find $packdir -type f | sort >before &&
493 git repack --geometric=2 -d &&
494 find $packdir -type f | sort >after &&
496 test_cmp before after
500 test_expect_success 'repack --geometric collects once-cruft objects' '
501 git init repo &&
502 test_when_finished "rm -fr repo" &&
504 cd repo &&
506 test_commit reachable &&
507 git repack -Ad &&
508 git branch -M main &&
510 git checkout --orphan other &&
511 git rm -rf . &&
512 test_commit --no-tag cruft &&
513 cruft="$(git rev-parse HEAD)" &&
515 git checkout main &&
516 git branch -D other &&
517 git reflog expire --all --expire=all &&
519 # Pack the objects created in the previous step into a cruft
520 # pack. Intentionally leave loose copies of those objects
521 # around so we can pick them up in a subsequent --geometric
522 # reapack.
523 git repack --cruft &&
525 # Now make those objects reachable, and ensure that they are
526 # packed into the new pack created via a --geometric repack.
527 git update-ref refs/heads/other $cruft &&
529 # Without this object, the set of unpacked objects is exactly
530 # the set of objects already in the cruft pack. Tweak that set
531 # to ensure we do not overwrite the cruft pack entirely.
532 test_commit reachable2 &&
534 find $packdir -name "pack-*.idx" | sort >before &&
535 git repack --geometric=2 -d &&
536 find $packdir -name "pack-*.idx" | sort >after &&
539 git rev-list --objects --no-object-names $cruft &&
540 git rev-list --objects --no-object-names reachable..reachable2
541 } >want.raw &&
542 sort want.raw >want &&
544 pack=$(comm -13 before after) &&
545 git show-index <$pack >objects.raw &&
547 cut -d" " -f2 objects.raw | sort >got &&
549 test_cmp want got
553 test_expect_success 'cruft repack with no reachable objects' '
554 git init repo &&
555 test_when_finished "rm -fr repo" &&
557 cd repo &&
559 test_commit base &&
560 git repack -ad &&
562 base="$(git rev-parse base)" &&
564 git for-each-ref --format="delete %(refname)" >in &&
565 git update-ref --stdin <in &&
566 git reflog expire --all --expire=all &&
567 rm -fr .git/index &&
569 git repack --cruft -d &&
571 git cat-file -t $base
575 test_expect_success 'cruft repack ignores --max-pack-size' '
576 git init max-pack-size &&
578 cd max-pack-size &&
579 test_commit base &&
580 # two cruft objects which exceed the maximum pack size
581 test-tool genrandom foo 1048576 | git hash-object --stdin -w &&
582 test-tool genrandom bar 1048576 | git hash-object --stdin -w &&
583 git repack --cruft --max-pack-size=1M &&
584 find $packdir -name "*.mtimes" >cruft &&
585 test_line_count = 1 cruft &&
586 test-tool pack-mtimes "$(basename "$(cat cruft)")" >objects &&
587 test_line_count = 2 objects
591 test_expect_success 'cruft repack ignores pack.packSizeLimit' '
593 cd max-pack-size &&
594 # repack everything back together to remove the existing cruft
595 # pack (but to keep its objects)
596 git repack -adk &&
597 git -c pack.packSizeLimit=1M repack --cruft &&
598 # ensure the same post condition is met when --max-pack-size
599 # would otherwise be inferred from the configuration
600 find $packdir -name "*.mtimes" >cruft &&
601 test_line_count = 1 cruft &&
602 test-tool pack-mtimes "$(basename "$(cat cruft)")" >objects &&
603 test_line_count = 2 objects
607 test_expect_success 'cruft repack respects repack.cruftWindow' '
608 git init repo &&
609 test_when_finished "rm -fr repo" &&
611 cd repo &&
613 test_commit base &&
615 GIT_TRACE2_EVENT=$(pwd)/event.trace \
616 git -c pack.window=1 -c repack.cruftWindow=2 repack \
617 --cruft --window=3 &&
619 grep "pack-objects.*--window=2.*--cruft" event.trace
623 test_expect_success 'cruft repack respects --window by default' '
624 git init repo &&
625 test_when_finished "rm -fr repo" &&
627 cd repo &&
629 test_commit base &&
631 GIT_TRACE2_EVENT=$(pwd)/event.trace \
632 git -c pack.window=2 repack --cruft --window=3 &&
634 grep "pack-objects.*--window=3.*--cruft" event.trace
638 test_expect_success 'cruft repack respects --quiet' '
639 git init repo &&
640 test_when_finished "rm -fr repo" &&
642 cd repo &&
644 test_commit base &&
645 GIT_PROGRESS_DELAY=0 git repack --cruft --quiet 2>err &&
646 test_must_be_empty err
650 test_expect_success 'cruft --local drops unreachable objects' '
651 git init alternate &&
652 git init repo &&
653 test_when_finished "rm -fr alternate repo" &&
655 test_commit -C alternate base &&
656 # Pack all objects in alterate so that the cruft repack in "repo" sees
657 # the object it dropped due to `--local` as packed. Otherwise this
658 # object would not appear packed anywhere (since it is not packed in
659 # alternate and likewise not part of the cruft pack in the other repo
660 # because of `--local`).
661 git -C alternate repack -ad &&
664 cd repo &&
666 object="$(git -C ../alternate rev-parse HEAD:base.t)" &&
667 git -C ../alternate cat-file -p $object >contents &&
669 # Write some reachable objects and two unreachable ones: one
670 # that the alternate has and another that is unique.
671 test_commit other &&
672 git hash-object -w -t blob contents &&
673 cruft="$(echo cruft | git hash-object -w -t blob --stdin)" &&
675 ( cd ../alternate/.git/objects && pwd ) \
676 >.git/objects/info/alternates &&
678 test_path_is_file $objdir/$(test_oid_to_path $cruft) &&
679 test_path_is_file $objdir/$(test_oid_to_path $object) &&
681 git repack -d --cruft --local &&
683 test-tool pack-mtimes "$(basename $(ls $packdir/pack-*.mtimes))" \
684 >objects &&
685 ! grep $object objects &&
686 grep $cruft objects
690 test_expect_success 'MIDX bitmaps tolerate reachable cruft objects' '
691 git init repo &&
692 test_when_finished "rm -fr repo" &&
694 cd repo &&
696 test_commit reachable &&
697 test_commit cruft &&
698 unreachable="$(git rev-parse cruft)" &&
700 git reset --hard $unreachable^ &&
701 git tag -d cruft &&
702 git reflog expire --all --expire=all &&
704 git repack --cruft -d &&
706 # resurrect the unreachable object via a new commit. the
707 # new commit will get selected for a bitmap, but be
708 # missing one of its parents from the selected packs.
709 git reset --hard $unreachable &&
710 test_commit resurrect &&
712 git repack --write-midx --write-bitmap-index --geometric=2 -d
716 test_expect_success 'cruft objects are freshend via loose' '
717 git init repo &&
718 test_when_finished "rm -fr repo" &&
720 cd repo &&
722 echo "cruft" >contents &&
723 blob="$(git hash-object -w -t blob contents)" &&
724 loose="$objdir/$(test_oid_to_path $blob)" &&
726 test_commit base &&
728 git repack --cruft -d &&
730 test_path_is_missing "$loose" &&
731 test-tool pack-mtimes "$(basename "$(ls $packdir/pack-*.mtimes)")" >cruft &&
732 grep "$blob" cruft &&
734 # write the same object again
735 git hash-object -w -t blob contents &&
737 test_path_is_file "$loose"
741 test_done