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 echo pack-
${incrpackid}.pack
16 test_no_missing_in_packs
() {
17 myidx
=$
(ls -1 .git
/objects
/pack
/*.idx
) &&
18 test_path_is_file
"$myidx" &&
19 git verify-pack
-v alt_objects
/pack
/*.idx
>orig.raw
&&
20 sed -n -e "s/^\($OID_REGEX\).*/\1/p" orig.raw |
sort >orig
&&
21 git verify-pack
-v $myidx >dest.raw
&&
22 cut
-d" " -f1 dest.raw |
sort >dest
&&
23 comm -23 orig dest
>missing
&&
24 test_must_be_empty missing
27 # we expect $packid and $oid to be defined
28 test_has_duplicate_object
() {
29 want_duplicate_object
="$1"
30 found_duplicate_object
=false
31 for p
in .git
/objects
/pack
/*.idx
34 test "pack-$packid.idx" = "$idx" && continue
35 git verify-pack
-v $p >packlist ||
return $?
36 if grep "^$oid" packlist
38 found_duplicate_object
=true
39 echo "DUPLICATE OBJECT FOUND"
43 test "$want_duplicate_object" = "$found_duplicate_object"
46 test_expect_success
'objects in packs marked .keep are not repacked' '
47 echo content1 >file1 &&
48 echo content2 >file2 &&
51 git commit -m initial_commit &&
53 # The first pack will contain all of the objects except one
54 git rev-list --objects --all >objs &&
55 grep -v file2 objs | git pack-objects pack &&
56 # The second pack will contain the excluded object
57 packid=$(grep file2 objs | git pack-objects pack) &&
59 git verify-pack -v pack-$packid.idx >packlist &&
60 oid=$(head -n 1 packlist | sed -e "s/^\($OID_REGEX\).*/\1/") &&
61 mv pack-* .git/objects/pack/ &&
62 git repack -A -d -l &&
64 test_has_duplicate_object false
67 test_expect_success
'writing bitmaps via command-line can duplicate .keep objects' '
68 # build on $oid, $packid, and .keep state from previous
69 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 git repack -Adbl &&
70 test_has_duplicate_object true
73 test_expect_success
'writing bitmaps via config can duplicate .keep objects' '
74 # build on $oid, $packid, and .keep state from previous
75 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
76 git -c repack.writebitmaps=true repack -Adl &&
77 test_has_duplicate_object true
80 test_expect_success
'loose objects in alternate ODB are not repacked' '
82 echo $(pwd)/alt_objects >.git/objects/info/alternates &&
83 echo content3 >file3 &&
84 oid=$(GIT_OBJECT_DIRECTORY=alt_objects git hash-object -w file3) &&
87 git commit -m commit_file3 &&
88 git repack -a -d -l &&
90 test_has_duplicate_object false
93 test_expect_success SYMLINKS
'--local keeps packs when alternate is objectdir ' '
94 test_when_finished "rm -rf repo" &&
96 test_commit -C repo A &&
100 ls .git/objects/pack/*.pack >../expect &&
101 ln -s objects .git/alt_objects &&
102 echo "$(pwd)/.git/alt_objects" >.git/objects/info/alternates &&
103 git repack -a -d -l &&
104 ls .git/objects/pack/*.pack >../actual
106 test_cmp expect actual
109 test_expect_success
'--local disables writing bitmaps when connected to alternate ODB' '
110 test_when_finished "rm -rf shared member" &&
113 git clone --shared shared member &&
116 test_commit "object" &&
117 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adl --write-bitmap-index 2>err &&
118 cat >expect <<-EOF &&
119 warning: disabling bitmap writing, as some objects are not being packed
121 test_cmp expect err &&
122 test_path_is_missing .git/objects/pack-*.bitmap
126 test_expect_success
'packed obs in alt ODB are repacked even when local repo is packless' '
127 mkdir alt_objects/pack &&
128 mv .git/objects/pack/* alt_objects/pack &&
130 test_no_missing_in_packs
133 test_expect_success
'packed obs in alt ODB are repacked when local repo has packs' '
134 rm -f .git/objects/pack/* &&
135 echo new_content >>file1 &&
138 git commit -m more_content &&
141 test_no_missing_in_packs
144 test_expect_success
'packed obs in alternate ODB kept pack are repacked' '
145 # swap the .keep so the commit object is in the pack with .keep
146 for p in alt_objects/pack/*.pack
148 base_name=$(basename $p .pack) &&
149 if test_path_is_file alt_objects/pack/$base_name.keep
151 rm alt_objects/pack/$base_name.keep
153 touch alt_objects/pack/$base_name.keep
157 test_no_missing_in_packs
160 test_expect_success
'packed unreachable obs in alternate ODB are not loosened' '
161 rm -f alt_objects/pack/*.keep &&
162 mv .git/objects/pack/* alt_objects/pack/ &&
163 coid=$(git rev-parse HEAD^{commit}) &&
164 git reset --hard HEAD^ &&
166 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
167 # The pack-objects call on the next line is equivalent to
168 # git repack -A -d without the call to prune-packed
169 git pack-objects --honor-pack-keep --non-empty --all --reflog \
170 --unpack-unreachable </dev/null pack &&
171 rm -f .git/objects/pack/* &&
172 mv pack-* .git/objects/pack/ &&
173 git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
174 ! grep "^$coid " packlist &&
175 echo >.git/objects/info/alternates &&
176 test_must_fail git show $coid
179 test_expect_success
'local packed unreachable obs that exist in alternate ODB are not loosened' '
180 echo $(pwd)/alt_objects >.git/objects/info/alternates &&
181 echo "$coid" | git pack-objects --non-empty --all --reflog pack &&
182 rm -f .git/objects/pack/* &&
183 mv pack-* .git/objects/pack/ &&
184 # The pack-objects call on the next line is equivalent to
185 # git repack -A -d without the call to prune-packed
186 git pack-objects --honor-pack-keep --non-empty --all --reflog \
187 --unpack-unreachable </dev/null pack &&
188 rm -f .git/objects/pack/* &&
189 mv pack-* .git/objects/pack/ &&
190 git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
192 echo >.git/objects/info/alternates &&
193 test_must_fail git show $coid
196 test_expect_success
'objects made unreachable by grafts only are kept' '
198 git commit --allow-empty -m "commit 4" &&
199 H0=$(git rev-parse HEAD) &&
200 H1=$(git rev-parse HEAD^) &&
201 H2=$(git rev-parse HEAD^^) &&
202 echo "$H0 $H2" >.git/info/grafts &&
203 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
208 test_expect_success
'repack --keep-pack' '
209 test_create_repo keep-pack &&
212 P1=$(commit_and_pack 1) &&
213 P2=$(commit_and_pack 2) &&
214 P3=$(commit_and_pack 3) &&
215 P4=$(commit_and_pack 4) &&
216 ls .git/objects/pack/*.pack >old-counts &&
217 test_line_count = 4 old-counts &&
218 git repack -a -d --keep-pack $P1 --keep-pack $P4 &&
219 ls .git/objects/pack/*.pack >new-counts &&
220 grep -q $P1 new-counts &&
221 grep -q $P4 new-counts &&
222 test_line_count = 3 new-counts &&
227 test_expect_success
'bitmaps are created by default in bare repos' '
228 git clone --bare .git bare.git &&
229 rm -f bare.git/objects/pack/*.bitmap &&
230 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
231 git -C bare.git repack -ad &&
232 bitmap=$(ls bare.git/objects/pack/*.bitmap) &&
233 test_path_is_file "$bitmap"
236 test_expect_success
'incremental repack does not complain' '
237 git -C bare.git repack -q 2>repack.err &&
238 test_must_be_empty repack.err
241 test_expect_success
'bitmaps can be disabled on bare repos' '
242 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
243 git -c repack.writeBitmaps=false -C bare.git repack -ad &&
244 bitmap=$(ls bare.git/objects/pack/*.bitmap || :) &&
248 test_expect_success
'no bitmaps created if .keep files present' '
249 pack=$(ls bare.git/objects/pack/*.pack) &&
250 test_path_is_file "$pack" &&
251 keep=${pack%.pack}.keep &&
252 test_when_finished "rm -f \"\$keep\"" &&
254 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
255 git -C bare.git repack -ad 2>stderr &&
256 test_must_be_empty stderr &&
257 find bare.git/objects/pack/ -type f -name "*.bitmap" >actual &&
258 test_must_be_empty actual
261 test_expect_success
'auto-bitmaps do not complain if unavailable' '
262 test_config -C bare.git pack.packSizeLimit 1M &&
263 blob=$(test-tool genrandom big $((1024*1024)) |
264 git -C bare.git hash-object -w --stdin) &&
265 git -C bare.git update-ref refs/tags/big $blob &&
266 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
267 git -C bare.git repack -ad 2>stderr &&
268 test_must_be_empty stderr &&
269 find bare.git/objects/pack -type f -name "*.bitmap" >actual &&
270 test_must_be_empty actual
274 midx
=$objdir/pack
/multi-pack-index
276 test_expect_success
'setup for --write-midx tests' '
280 git config core.multiPackIndex true &&
286 test_expect_success
'--write-midx unchanged' '
289 GIT_TEST_MULTI_PACK_INDEX=0 git repack &&
290 test_path_is_missing $midx &&
291 test_path_is_missing $midx-*.bitmap &&
293 GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
295 test_path_is_file $midx &&
296 test_path_is_missing $midx-*.bitmap &&
297 test_midx_consistent $objdir
301 test_expect_success
'--write-midx with a new pack' '
306 GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
308 test_path_is_file $midx &&
309 test_path_is_missing $midx-*.bitmap &&
310 test_midx_consistent $objdir
314 test_expect_success
'--write-midx with -b' '
317 GIT_TEST_MULTI_PACK_INDEX=0 git repack -mb &&
319 test_path_is_file $midx &&
320 test_path_is_file $midx-*.bitmap &&
321 test_midx_consistent $objdir
325 test_expect_success
'--write-midx with -d' '
328 test_commit repack &&
330 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Ad --write-midx &&
332 test_path_is_file $midx &&
333 test_path_is_missing $midx-*.bitmap &&
334 test_midx_consistent $objdir
338 test_expect_success
'cleans up MIDX when appropriate' '
342 test_commit repack-2 &&
343 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
345 checksum=$(midx_checksum $objdir) &&
346 test_path_is_file $midx &&
347 test_path_is_file $midx-$checksum.bitmap &&
349 test_commit repack-3 &&
350 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
352 test_path_is_file $midx &&
353 test_path_is_missing $midx-$checksum.bitmap &&
354 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
356 test_commit repack-4 &&
357 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb &&
359 find $objdir/pack -type f -name "multi-pack-index*" >files &&
360 test_must_be_empty files
364 test_expect_success
'--write-midx with preferred bitmap tips' '
365 git init midx-preferred-tips &&
366 test_when_finished "rm -fr midx-preferred-tips" &&
368 cd midx-preferred-tips &&
370 test_commit_bulk --message="%s" 103 &&
372 git log --format="%H" >commits.raw &&
373 sort <commits.raw >commits &&
375 git log --format="create refs/tags/%s/%s %H" HEAD >refs &&
376 git update-ref --stdin <refs &&
378 git repack --write-midx --write-bitmap-index &&
379 test_path_is_file $midx &&
380 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
382 test-tool bitmap list-commits | sort >bitmaps &&
383 comm -13 bitmaps commits >before &&
384 test_line_count = 1 before &&
386 rm -fr $midx-$(midx_checksum $objdir).bitmap &&
389 # instead of constructing the snapshot ourselves (c.f., the test
390 # "write a bitmap with --refs-snapshot (preferred tips)" in
391 # t5326), mark the missing commit as preferred by adding it to
392 # the pack.preferBitmapTips configuration.
393 git for-each-ref --format="%(refname:rstrip=1)" \
394 --points-at="$(cat before)" >missing &&
395 git config pack.preferBitmapTips "$(cat missing)" &&
396 git repack --write-midx --write-bitmap-index &&
398 test-tool bitmap list-commits | sort >bitmaps &&
399 comm -13 bitmaps commits >after &&
401 ! test_cmp before after
405 # The first argument is expected to be a filename
406 # and that file should contain the name of a .idx
407 # file. Send the list of objects in that .idx file
409 get_sorted_objects_from_pack
() {
410 git show-index
<$
(cat "$1") >raw
&&
414 test_expect_success
'--write-midx -b packs non-kept objects' '
416 test_when_finished "rm -fr repo" &&
420 # Create a kept pack-file
423 find $objdir/pack -name "*.idx" >before &&
424 test_line_count = 1 before &&
425 before_name=$(cat before) &&
426 >${before_name%.idx}.keep &&
428 # Create a non-kept pack-file
432 # Create loose objects
436 git repack --write-midx -a -b -d &&
438 # There should be two pack-files now, the
439 # old, kept pack and the new, non-kept pack.
440 find $objdir/pack -name "*.idx" | sort >after &&
441 test_line_count = 2 after &&
442 find $objdir/pack -name "*.keep" >kept &&
443 kept_name=$(cat kept) &&
444 echo ${kept_name%.keep}.idx >kept-idx &&
445 test_cmp before kept-idx &&
447 # Get object list from the kept pack.
448 get_sorted_objects_from_pack before >old.objects &&
450 # Get object list from the one non-kept pack-file
451 comm -13 before after >new-pack &&
452 test_line_count = 1 new-pack &&
453 get_sorted_objects_from_pack new-pack >new.objects &&
455 # None of the objects in the new pack should
456 # exist within the kept pack.
457 comm -12 old.objects new.objects >shared.objects &&
458 test_must_be_empty shared.objects
462 test_expect_success
'--write-midx removes stale pack-based bitmaps' '
465 test_when_finished "rm -fr repo" &&
469 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Ab &&
471 pack_bitmap=$(ls $objdir/pack/pack-*.bitmap) &&
472 test_path_is_file "$pack_bitmap" &&
475 GIT_TEST_MULTI_PACK_INDEX=0 git repack -bm &&
477 test_path_is_file $midx &&
478 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
479 test_path_is_missing $pack_bitmap
483 test_expect_success
'--write-midx with --pack-kept-objects' '
485 test_when_finished "rm -fr repo" &&
492 one="$(echo "one" | git pack-objects --revs $objdir/pack/pack)" &&
493 two="$(echo "one..two" | git pack-objects --revs $objdir/pack/pack)" &&
495 keep="$objdir/pack/pack-$one.keep" &&
498 git repack --write-midx --write-bitmap-index --geometric=2 -d \
499 --pack-kept-objects &&
501 test_path_is_file $keep &&
502 test_path_is_file $midx &&
503 test_path_is_file $midx-$(midx_checksum $objdir).bitmap
507 test_expect_success TTY
'--quiet disables progress' '
508 test_terminal env GIT_PROGRESS_DELAY=0 \
509 git -C midx repack -ad --quiet --write-midx 2>stderr &&
510 test_must_be_empty stderr
513 test_expect_success
'clean up .tmp-* packs on error' '
514 test_must_fail ok=sigpipe git \
515 -c repack.cruftwindow=bogus \
516 repack -ad --cruft &&
517 find $objdir/pack -name '.tmp-
*' >tmpfiles &&
518 test_must_be_empty tmpfiles
521 test_expect_success
'repack -ad cleans up old .tmp-* packs' '
522 git rev-parse HEAD >input &&
523 git pack-objects $objdir/pack/.tmp-1234 <input &&
525 find $objdir/pack -name '.tmp-
*' >tmpfiles &&
526 test_must_be_empty tmpfiles
529 test_expect_success
'setup for update-server-info' '
530 git init update-server-info &&
531 test_commit -C update-server-info message
534 test_server_info_present
() {
535 test_path_is_file update-server-info
/.git
/objects
/info
/packs
&&
536 test_path_is_file update-server-info
/.git
/info
/refs
539 test_server_info_missing
() {
540 test_path_is_missing update-server-info
/.git
/objects
/info
/packs
&&
541 test_path_is_missing update-server-info
/.git
/info
/refs
544 test_server_info_cleanup
() {
545 rm -f update-server-info
/.git
/objects
/info
/packs update-server-info
/.git
/info
/refs
&&
546 test_server_info_missing
549 test_expect_success
'updates server info by default' '
550 test_server_info_cleanup &&
551 git -C update-server-info repack &&
552 test_server_info_present
555 test_expect_success
'-n skips updating server info' '
556 test_server_info_cleanup &&
557 git -C update-server-info repack -n &&
558 test_server_info_missing
561 test_expect_success
'repack.updateServerInfo=true updates server info' '
562 test_server_info_cleanup &&
563 git -C update-server-info -c repack.updateServerInfo=true repack &&
564 test_server_info_present
567 test_expect_success
'repack.updateServerInfo=false skips updating server info' '
568 test_server_info_cleanup &&
569 git -C update-server-info -c repack.updateServerInfo=false repack &&
570 test_server_info_missing
573 test_expect_success
'-n overrides repack.updateServerInfo=true' '
574 test_server_info_cleanup &&
575 git -C update-server-info -c repack.updateServerInfo=true repack -n &&
576 test_server_info_missing
579 test_expect_success
'--expire-to stores pruned objects (now)' '
580 git init expire-to-now &&
584 git branch -M main &&
588 git checkout -b cruft &&
589 test_commit --no-tag cruft &&
591 git rev-list --objects --no-object-names main..cruft >moved.raw &&
592 sort moved.raw >moved.want &&
594 git rev-list --all --objects --no-object-names >expect.raw &&
595 sort expect.raw >expect &&
598 git branch -D cruft &&
599 git reflog expire --all --expire=all &&
601 git init --bare expired.git &&
603 --cruft --cruft-expiration="now" \
604 --expire-to="expired.git/objects/pack/pack" &&
606 expired="$(ls expired.git/objects/pack/pack-*.idx)" &&
607 test_path_is_file "${expired%.idx}.mtimes" &&
609 # Since the `--cruft-expiration` is "now", the effective
610 # behavior is to move _all_ unreachable objects out to
611 # the location in `--expire-to`.
612 git show-index <$expired >expired.raw &&
613 cut -d" " -f2 expired.raw | sort >expired.objects &&
614 git rev-list --all --objects --no-object-names \
615 >remaining.objects &&
617 # ...in other words, the combined contents of this
618 # repository and expired.git should be the same as the
619 # set of objects we started with.
620 cat expired.objects remaining.objects | sort >actual &&
621 test_cmp expect actual &&
623 # The "moved" objects (i.e., those in expired.git)
624 # should be the same as the cruft objects which were
625 # expired in the previous step.
626 test_cmp moved.want expired.objects
630 test_expect_success
'--expire-to stores pruned objects (5.minutes.ago)' '
631 git init expire-to-5.minutes.ago &&
633 cd expire-to-5.minutes.ago &&
635 git branch -M main &&
639 # Create two classes of unreachable objects, one which
640 # is older than 5 minutes (stale), and another which is
642 for kind in stale recent
644 git checkout -b $kind main &&
645 test_commit --no-tag $kind || return 1
648 git rev-list --objects --no-object-names main..stale >in &&
649 stale="$(git pack-objects $objdir/pack/pack <in)" &&
650 mtime="$(test-tool chmtime --get =-600 $objdir/pack/pack-$stale.pack)" &&
652 # expect holds the set of objects we expect to find in
653 # this repository after repacking
654 git rev-list --objects --no-object-names recent >expect.raw &&
655 sort expect.raw >expect &&
657 # moved.want holds the set of objects we expect to find
659 git rev-list --objects --no-object-names main..stale >out &&
660 sort out >moved.want &&
663 git branch -D stale recent &&
664 git reflog expire --all --expire=all &&
667 git init --bare expired.git &&
669 --cruft --cruft-expiration=5.minutes.ago \
670 --expire-to="expired.git/objects/pack/pack" &&
672 # Some of the remaining objects in this repository are
673 # unreachable, so use `cat-file --batch-all-objects`
674 # instead of `rev-list` to get their names
675 git cat-file --batch-all-objects --batch-check="%(objectname)" \
676 >remaining.objects &&
677 sort remaining.objects >actual &&
678 test_cmp expect actual &&
683 expired="$(ls objects/pack/pack-*.mtimes)" &&
684 test-tool pack-mtimes $(basename $expired) >out &&
685 cut -d" " -f1 out | sort >../moved.got &&
687 # Ensure that there are as many objects with the
688 # expected mtime as were moved to expired.git.
690 # In other words, ensure that the recorded
691 # mtimes of any moved objects was written
693 grep " $mtime$" out >matching &&
694 test_line_count = $(wc -l <../moved.want) matching
696 test_cmp moved.want moved.got