3 test_description
='git repack works correctly'
6 .
"${TEST_DIRECTORY}/lib-bitmap.sh"
7 .
"${TEST_DIRECTORY}/lib-midx.sh"
8 .
"${TEST_DIRECTORY}/lib-terminal.sh"
11 test_commit
"$@" 1>&2 &&
12 incrpackid
=$
(git pack-objects
--all --unpacked --incremental .git
/objects
/pack
/pack
</dev
/null
) &&
13 # Remove any loose object(s) created by test_commit, since they have
14 # already been packed. Leaving these around can create subtly different
15 # packs with `pack-objects`'s `--unpacked` option.
16 git prune-packed
1>&2 &&
17 echo pack-
${incrpackid}.pack
20 test_no_missing_in_packs
() {
21 myidx
=$
(ls -1 .git
/objects
/pack
/*.idx
) &&
22 test_path_is_file
"$myidx" &&
23 git verify-pack
-v alt_objects
/pack
/*.idx
>orig.raw
&&
24 sed -n -e "s/^\($OID_REGEX\).*/\1/p" orig.raw |
sort >orig
&&
25 git verify-pack
-v $myidx >dest.raw
&&
26 cut
-d" " -f1 dest.raw |
sort >dest
&&
27 comm -23 orig dest
>missing
&&
28 test_must_be_empty missing
31 # we expect $packid and $oid to be defined
32 test_has_duplicate_object
() {
33 want_duplicate_object
="$1"
34 found_duplicate_object
=false
35 for p
in .git
/objects
/pack
/*.idx
38 test "pack-$packid.idx" = "$idx" && continue
39 git verify-pack
-v $p >packlist ||
return $?
40 if grep "^$oid" packlist
42 found_duplicate_object
=true
43 echo "DUPLICATE OBJECT FOUND"
47 test "$want_duplicate_object" = "$found_duplicate_object"
50 test_expect_success
'objects in packs marked .keep are not repacked' '
51 echo content1 >file1 &&
52 echo content2 >file2 &&
55 git commit -m initial_commit &&
57 # The first pack will contain all of the objects except one
58 git rev-list --objects --all >objs &&
59 grep -v file2 objs | git pack-objects pack &&
60 # The second pack will contain the excluded object
61 packid=$(grep file2 objs | git pack-objects pack) &&
63 git verify-pack -v pack-$packid.idx >packlist &&
64 oid=$(head -n 1 packlist | sed -e "s/^\($OID_REGEX\).*/\1/") &&
65 mv pack-* .git/objects/pack/ &&
66 git repack -A -d -l &&
68 test_has_duplicate_object false
71 test_expect_success
'writing bitmaps via command-line can duplicate .keep objects' '
72 # build on $oid, $packid, and .keep state from previous
73 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 git repack -Adbl &&
74 test_has_duplicate_object true
77 test_expect_success
'writing bitmaps via config can duplicate .keep objects' '
78 # build on $oid, $packid, and .keep state from previous
79 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
80 git -c repack.writebitmaps=true repack -Adl &&
81 test_has_duplicate_object true
84 test_expect_success
'loose objects in alternate ODB are not repacked' '
86 echo $(pwd)/alt_objects >.git/objects/info/alternates &&
87 echo content3 >file3 &&
88 oid=$(GIT_OBJECT_DIRECTORY=alt_objects git hash-object -w file3) &&
91 git commit -m commit_file3 &&
92 git repack -a -d -l &&
94 test_has_duplicate_object false
97 test_expect_success SYMLINKS
'--local keeps packs when alternate is objectdir ' '
98 test_when_finished "rm -rf repo" &&
100 test_commit -C repo A &&
104 ls .git/objects/pack/*.pack >../expect &&
105 ln -s objects .git/alt_objects &&
106 echo "$(pwd)/.git/alt_objects" >.git/objects/info/alternates &&
107 git repack -a -d -l &&
108 ls .git/objects/pack/*.pack >../actual
110 test_cmp expect actual
113 test_expect_success
'--local disables writing bitmaps when connected to alternate ODB' '
114 test_when_finished "rm -rf shared member" &&
117 git clone --shared shared member &&
120 test_commit "object" &&
121 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adl --write-bitmap-index 2>err &&
122 cat >expect <<-EOF &&
123 warning: disabling bitmap writing, as some objects are not being packed
125 test_cmp expect err &&
126 test_path_is_missing .git/objects/pack-*.bitmap
130 test_expect_success
'packed obs in alt ODB are repacked even when local repo is packless' '
131 mkdir alt_objects/pack &&
132 mv .git/objects/pack/* alt_objects/pack &&
134 test_no_missing_in_packs
137 test_expect_success
'packed obs in alt ODB are repacked when local repo has packs' '
138 rm -f .git/objects/pack/* &&
139 echo new_content >>file1 &&
142 git commit -m more_content &&
145 test_no_missing_in_packs
148 test_expect_success
'packed obs in alternate ODB kept pack are repacked' '
149 # swap the .keep so the commit object is in the pack with .keep
150 for p in alt_objects/pack/*.pack
152 base_name=$(basename $p .pack) &&
153 if test_path_is_file alt_objects/pack/$base_name.keep
155 rm alt_objects/pack/$base_name.keep
157 touch alt_objects/pack/$base_name.keep
161 test_no_missing_in_packs
164 test_expect_success
'packed unreachable obs in alternate ODB are not loosened' '
165 rm -f alt_objects/pack/*.keep &&
166 mv .git/objects/pack/* alt_objects/pack/ &&
167 coid=$(git rev-parse HEAD^{commit}) &&
168 git reset --hard HEAD^ &&
170 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
171 # The pack-objects call on the next line is equivalent to
172 # git repack -A -d without the call to prune-packed
173 git pack-objects --honor-pack-keep --non-empty --all --reflog \
174 --unpack-unreachable </dev/null pack &&
175 rm -f .git/objects/pack/* &&
176 mv pack-* .git/objects/pack/ &&
177 git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
178 ! grep "^$coid " packlist &&
179 echo >.git/objects/info/alternates &&
180 test_must_fail git show $coid
183 test_expect_success
'local packed unreachable obs that exist in alternate ODB are not loosened' '
184 echo $(pwd)/alt_objects >.git/objects/info/alternates &&
185 echo "$coid" | git pack-objects --non-empty --all --reflog pack &&
186 rm -f .git/objects/pack/* &&
187 mv pack-* .git/objects/pack/ &&
188 # The pack-objects call on the next line is equivalent to
189 # git repack -A -d without the call to prune-packed
190 git pack-objects --honor-pack-keep --non-empty --all --reflog \
191 --unpack-unreachable </dev/null pack &&
192 rm -f .git/objects/pack/* &&
193 mv pack-* .git/objects/pack/ &&
194 git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
196 echo >.git/objects/info/alternates &&
197 test_must_fail git show $coid
200 test_expect_success
'objects made unreachable by grafts only are kept' '
202 git commit --allow-empty -m "commit 4" &&
203 H0=$(git rev-parse HEAD) &&
204 H1=$(git rev-parse HEAD^) &&
205 H2=$(git rev-parse HEAD^^) &&
206 echo "$H0 $H2" >.git/info/grafts &&
207 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
212 test_expect_success
'repack --keep-pack' '
213 test_create_repo keep-pack &&
216 # avoid producing difference packs to delta/base choices
217 git config pack.window 0 &&
218 P1=$(commit_and_pack 1) &&
219 P2=$(commit_and_pack 2) &&
220 P3=$(commit_and_pack 3) &&
221 P4=$(commit_and_pack 4) &&
222 ls .git/objects/pack/*.pack >old-counts &&
223 test_line_count = 4 old-counts &&
224 git repack -a -d --keep-pack $P1 --keep-pack $P4 &&
225 ls .git/objects/pack/*.pack >new-counts &&
226 grep -q $P1 new-counts &&
227 grep -q $P4 new-counts &&
228 test_line_count = 3 new-counts &&
231 P5=$(commit_and_pack --no-tag 5) &&
232 git reset --hard HEAD^ &&
233 git reflog expire --all --expire=all &&
234 rm -f ".git/objects/pack/${P5%.pack}.idx" &&
235 rm -f ".git/objects/info/commit-graph" &&
236 for from in $(find .git/objects/pack -type f -name "${P5%.pack}.*")
238 to="$(dirname "$from")/.tmp-1234-$(basename "$from")" &&
239 mv "$from" "$to" || return 1
242 git repack --cruft -d --keep-pack $P1 --keep-pack $P4 &&
244 ls .git/objects/pack/*.pack >newer-counts &&
245 test_cmp new-counts newer-counts &&
250 test_expect_success
'bitmaps are created by default in bare repos' '
251 git clone --bare .git bare.git &&
252 rm -f bare.git/objects/pack/*.bitmap &&
253 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
254 git -C bare.git repack -ad &&
255 bitmap=$(ls bare.git/objects/pack/*.bitmap) &&
256 test_path_is_file "$bitmap"
259 test_expect_success
'incremental repack does not complain' '
260 git -C bare.git repack -q 2>repack.err &&
261 test_must_be_empty repack.err
264 test_expect_success
'bitmaps can be disabled on bare repos' '
265 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
266 git -c repack.writeBitmaps=false -C bare.git repack -ad &&
267 bitmap=$(ls bare.git/objects/pack/*.bitmap || :) &&
271 test_expect_success
'no bitmaps created if .keep files present' '
272 pack=$(ls bare.git/objects/pack/*.pack) &&
273 test_path_is_file "$pack" &&
274 keep=${pack%.pack}.keep &&
275 test_when_finished "rm -f \"\$keep\"" &&
277 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
278 git -C bare.git repack -ad 2>stderr &&
279 test_must_be_empty stderr &&
280 find bare.git/objects/pack/ -type f -name "*.bitmap" >actual &&
281 test_must_be_empty actual
284 test_expect_success
'auto-bitmaps do not complain if unavailable' '
285 test_config -C bare.git pack.packSizeLimit 1M &&
286 blob=$(test-tool genrandom big $((1024*1024)) |
287 git -C bare.git hash-object -w --stdin) &&
288 git -C bare.git update-ref refs/tags/big $blob &&
289 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
290 git -C bare.git repack -ad 2>stderr &&
291 test_must_be_empty stderr &&
292 find bare.git/objects/pack -type f -name "*.bitmap" >actual &&
293 test_must_be_empty actual
297 midx
=$objdir/pack
/multi-pack-index
299 test_expect_success
'setup for --write-midx tests' '
303 git config core.multiPackIndex true &&
309 test_expect_success
'--write-midx unchanged' '
312 GIT_TEST_MULTI_PACK_INDEX=0 git repack &&
313 test_path_is_missing $midx &&
314 test_path_is_missing $midx-*.bitmap &&
316 GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
318 test_path_is_file $midx &&
319 test_path_is_missing $midx-*.bitmap &&
320 test_midx_consistent $objdir
324 test_expect_success
'--write-midx with a new pack' '
329 GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
331 test_path_is_file $midx &&
332 test_path_is_missing $midx-*.bitmap &&
333 test_midx_consistent $objdir
337 test_expect_success
'--write-midx with -b' '
340 GIT_TEST_MULTI_PACK_INDEX=0 git repack -mb &&
342 test_path_is_file $midx &&
343 test_path_is_file $midx-*.bitmap &&
344 test_midx_consistent $objdir
348 test_expect_success
'--write-midx with -d' '
351 test_commit repack &&
353 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Ad --write-midx &&
355 test_path_is_file $midx &&
356 test_path_is_missing $midx-*.bitmap &&
357 test_midx_consistent $objdir
361 test_expect_success
'cleans up MIDX when appropriate' '
365 test_commit repack-2 &&
366 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
368 checksum=$(midx_checksum $objdir) &&
369 test_path_is_file $midx &&
370 test_path_is_file $midx-$checksum.bitmap &&
372 test_commit repack-3 &&
373 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
375 test_path_is_file $midx &&
376 test_path_is_missing $midx-$checksum.bitmap &&
377 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
379 test_commit repack-4 &&
380 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb &&
382 find $objdir/pack -type f -name "multi-pack-index*" >files &&
383 test_must_be_empty files
387 test_expect_success
'--write-midx with preferred bitmap tips' '
388 git init midx-preferred-tips &&
389 test_when_finished "rm -fr midx-preferred-tips" &&
391 cd midx-preferred-tips &&
393 test_commit_bulk --message="%s" 103 &&
395 git log --format="%H" >commits.raw &&
396 sort <commits.raw >commits &&
398 git log --format="create refs/tags/%s/%s %H" HEAD >refs &&
399 git update-ref --stdin <refs &&
401 git repack --write-midx --write-bitmap-index &&
402 test_path_is_file $midx &&
403 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
405 test-tool bitmap list-commits | sort >bitmaps &&
406 comm -13 bitmaps commits >before &&
407 test_line_count = 1 before &&
409 rm -fr $midx-$(midx_checksum $objdir).bitmap &&
412 # instead of constructing the snapshot ourselves (c.f., the test
413 # "write a bitmap with --refs-snapshot (preferred tips)" in
414 # t5326), mark the missing commit as preferred by adding it to
415 # the pack.preferBitmapTips configuration.
416 git for-each-ref --format="%(refname:rstrip=1)" \
417 --points-at="$(cat before)" >missing &&
418 git config pack.preferBitmapTips "$(cat missing)" &&
419 git repack --write-midx --write-bitmap-index &&
421 test-tool bitmap list-commits | sort >bitmaps &&
422 comm -13 bitmaps commits >after &&
424 ! test_cmp before after
428 # The first argument is expected to be a filename
429 # and that file should contain the name of a .idx
430 # file. Send the list of objects in that .idx file
432 get_sorted_objects_from_pack
() {
433 git show-index
<$
(cat "$1") >raw
&&
437 test_expect_success
'--write-midx -b packs non-kept objects' '
439 test_when_finished "rm -fr repo" &&
443 # Create a kept pack-file
446 find $objdir/pack -name "*.idx" >before &&
447 test_line_count = 1 before &&
448 before_name=$(cat before) &&
449 >${before_name%.idx}.keep &&
451 # Create a non-kept pack-file
455 # Create loose objects
459 git repack --write-midx -a -b -d &&
461 # There should be two pack-files now, the
462 # old, kept pack and the new, non-kept pack.
463 find $objdir/pack -name "*.idx" | sort >after &&
464 test_line_count = 2 after &&
465 find $objdir/pack -name "*.keep" >kept &&
466 kept_name=$(cat kept) &&
467 echo ${kept_name%.keep}.idx >kept-idx &&
468 test_cmp before kept-idx &&
470 # Get object list from the kept pack.
471 get_sorted_objects_from_pack before >old.objects &&
473 # Get object list from the one non-kept pack-file
474 comm -13 before after >new-pack &&
475 test_line_count = 1 new-pack &&
476 get_sorted_objects_from_pack new-pack >new.objects &&
478 # None of the objects in the new pack should
479 # exist within the kept pack.
480 comm -12 old.objects new.objects >shared.objects &&
481 test_must_be_empty shared.objects
485 test_expect_success
'--write-midx removes stale pack-based bitmaps' '
488 test_when_finished "rm -fr repo" &&
492 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Ab &&
494 pack_bitmap=$(ls $objdir/pack/pack-*.bitmap) &&
495 test_path_is_file "$pack_bitmap" &&
498 GIT_TEST_MULTI_PACK_INDEX=0 git repack -bm &&
500 test_path_is_file $midx &&
501 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
502 test_path_is_missing $pack_bitmap
506 test_expect_success
'--write-midx with --pack-kept-objects' '
508 test_when_finished "rm -fr repo" &&
515 one="$(echo "one" | git pack-objects --revs $objdir/pack/pack)" &&
516 two="$(echo "one..two" | git pack-objects --revs $objdir/pack/pack)" &&
518 keep="$objdir/pack/pack-$one.keep" &&
521 git repack --write-midx --write-bitmap-index --geometric=2 -d \
522 --pack-kept-objects &&
524 test_path_is_file $keep &&
525 test_path_is_file $midx &&
526 test_path_is_file $midx-$(midx_checksum $objdir).bitmap
530 test_expect_success TTY
'--quiet disables progress' '
531 test_terminal env GIT_PROGRESS_DELAY=0 \
532 git -C midx repack -ad --quiet --write-midx 2>stderr &&
533 test_must_be_empty stderr
536 test_expect_success
'clean up .tmp-* packs on error' '
537 test_must_fail ok=sigpipe git \
538 -c repack.cruftwindow=bogus \
539 repack -ad --cruft &&
540 find $objdir/pack -name '.tmp-
*' >tmpfiles &&
541 test_must_be_empty tmpfiles
544 test_expect_success
'repack -ad cleans up old .tmp-* packs' '
545 git rev-parse HEAD >input &&
546 git pack-objects $objdir/pack/.tmp-1234 <input &&
548 find $objdir/pack -name '.tmp-
*' >tmpfiles &&
549 test_must_be_empty tmpfiles
552 test_expect_success
'setup for update-server-info' '
553 git init update-server-info &&
554 test_commit -C update-server-info message
557 test_server_info_present
() {
558 test_path_is_file update-server-info
/.git
/objects
/info
/packs
&&
559 test_path_is_file update-server-info
/.git
/info
/refs
562 test_server_info_missing
() {
563 test_path_is_missing update-server-info
/.git
/objects
/info
/packs
&&
564 test_path_is_missing update-server-info
/.git
/info
/refs
567 test_server_info_cleanup
() {
568 rm -f update-server-info
/.git
/objects
/info
/packs update-server-info
/.git
/info
/refs
&&
569 test_server_info_missing
572 test_expect_success
'updates server info by default' '
573 test_server_info_cleanup &&
574 git -C update-server-info repack &&
575 test_server_info_present
578 test_expect_success
'-n skips updating server info' '
579 test_server_info_cleanup &&
580 git -C update-server-info repack -n &&
581 test_server_info_missing
584 test_expect_success
'repack.updateServerInfo=true updates server info' '
585 test_server_info_cleanup &&
586 git -C update-server-info -c repack.updateServerInfo=true repack &&
587 test_server_info_present
590 test_expect_success
'repack.updateServerInfo=false skips updating server info' '
591 test_server_info_cleanup &&
592 git -C update-server-info -c repack.updateServerInfo=false repack &&
593 test_server_info_missing
596 test_expect_success
'-n overrides repack.updateServerInfo=true' '
597 test_server_info_cleanup &&
598 git -C update-server-info -c repack.updateServerInfo=true repack -n &&
599 test_server_info_missing
602 test_expect_success
'--expire-to stores pruned objects (now)' '
603 git init expire-to-now &&
607 git branch -M main &&
611 git checkout -b cruft &&
612 test_commit --no-tag cruft &&
614 git rev-list --objects --no-object-names main..cruft >moved.raw &&
615 sort moved.raw >moved.want &&
617 git rev-list --all --objects --no-object-names >expect.raw &&
618 sort expect.raw >expect &&
621 git branch -D cruft &&
622 git reflog expire --all --expire=all &&
624 git init --bare expired.git &&
626 --cruft --cruft-expiration="now" \
627 --expire-to="expired.git/objects/pack/pack" &&
629 expired="$(ls expired.git/objects/pack/pack-*.idx)" &&
630 test_path_is_file "${expired%.idx}.mtimes" &&
632 # Since the `--cruft-expiration` is "now", the effective
633 # behavior is to move _all_ unreachable objects out to
634 # the location in `--expire-to`.
635 git show-index <$expired >expired.raw &&
636 cut -d" " -f2 expired.raw | sort >expired.objects &&
637 git rev-list --all --objects --no-object-names \
638 >remaining.objects &&
640 # ...in other words, the combined contents of this
641 # repository and expired.git should be the same as the
642 # set of objects we started with.
643 cat expired.objects remaining.objects | sort >actual &&
644 test_cmp expect actual &&
646 # The "moved" objects (i.e., those in expired.git)
647 # should be the same as the cruft objects which were
648 # expired in the previous step.
649 test_cmp moved.want expired.objects
653 test_expect_success
'--expire-to stores pruned objects (5.minutes.ago)' '
654 git init expire-to-5.minutes.ago &&
656 cd expire-to-5.minutes.ago &&
658 git branch -M main &&
662 # Create two classes of unreachable objects, one which
663 # is older than 5 minutes (stale), and another which is
665 for kind in stale recent
667 git checkout -b $kind main &&
668 test_commit --no-tag $kind || return 1
671 git rev-list --objects --no-object-names main..stale >in &&
672 stale="$(git pack-objects $objdir/pack/pack <in)" &&
673 mtime="$(test-tool chmtime --get =-600 $objdir/pack/pack-$stale.pack)" &&
675 # expect holds the set of objects we expect to find in
676 # this repository after repacking
677 git rev-list --objects --no-object-names recent >expect.raw &&
678 sort expect.raw >expect &&
680 # moved.want holds the set of objects we expect to find
682 git rev-list --objects --no-object-names main..stale >out &&
683 sort out >moved.want &&
686 git branch -D stale recent &&
687 git reflog expire --all --expire=all &&
690 git init --bare expired.git &&
692 --cruft --cruft-expiration=5.minutes.ago \
693 --expire-to="expired.git/objects/pack/pack" &&
695 # Some of the remaining objects in this repository are
696 # unreachable, so use `cat-file --batch-all-objects`
697 # instead of `rev-list` to get their names
698 git cat-file --batch-all-objects --batch-check="%(objectname)" \
699 >remaining.objects &&
700 sort remaining.objects >actual &&
701 test_cmp expect actual &&
706 expired="$(ls objects/pack/pack-*.mtimes)" &&
707 test-tool pack-mtimes $(basename $expired) >out &&
708 cut -d" " -f1 out | sort >../moved.got &&
710 # Ensure that there are as many objects with the
711 # expected mtime as were moved to expired.git.
713 # In other words, ensure that the recorded
714 # mtimes of any moved objects was written
716 grep " $mtime$" out >matching &&
717 test_line_count = $(wc -l <../moved.want) matching
719 test_cmp moved.want moved.got