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
'packed obs in alt ODB are repacked even when local repo is packless' '
110 mkdir alt_objects/pack &&
111 mv .git/objects/pack/* alt_objects/pack &&
113 test_no_missing_in_packs
116 test_expect_success
'packed obs in alt ODB are repacked when local repo has packs' '
117 rm -f .git/objects/pack/* &&
118 echo new_content >>file1 &&
121 git commit -m more_content &&
124 test_no_missing_in_packs
127 test_expect_success
'packed obs in alternate ODB kept pack are repacked' '
128 # swap the .keep so the commit object is in the pack with .keep
129 for p in alt_objects/pack/*.pack
131 base_name=$(basename $p .pack) &&
132 if test_path_is_file alt_objects/pack/$base_name.keep
134 rm alt_objects/pack/$base_name.keep
136 touch alt_objects/pack/$base_name.keep
140 test_no_missing_in_packs
143 test_expect_success
'packed unreachable obs in alternate ODB are not loosened' '
144 rm -f alt_objects/pack/*.keep &&
145 mv .git/objects/pack/* alt_objects/pack/ &&
146 coid=$(git rev-parse HEAD^{commit}) &&
147 git reset --hard HEAD^ &&
149 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
150 # The pack-objects call on the next line is equivalent to
151 # git repack -A -d without the call to prune-packed
152 git pack-objects --honor-pack-keep --non-empty --all --reflog \
153 --unpack-unreachable </dev/null pack &&
154 rm -f .git/objects/pack/* &&
155 mv pack-* .git/objects/pack/ &&
156 git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
157 ! grep "^$coid " packlist &&
158 echo >.git/objects/info/alternates &&
159 test_must_fail git show $coid
162 test_expect_success
'local packed unreachable obs that exist in alternate ODB are not loosened' '
163 echo $(pwd)/alt_objects >.git/objects/info/alternates &&
164 echo "$coid" | git pack-objects --non-empty --all --reflog pack &&
165 rm -f .git/objects/pack/* &&
166 mv pack-* .git/objects/pack/ &&
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 &&
175 echo >.git/objects/info/alternates &&
176 test_must_fail git show $coid
179 test_expect_success
'objects made unreachable by grafts only are kept' '
181 git commit --allow-empty -m "commit 4" &&
182 H0=$(git rev-parse HEAD) &&
183 H1=$(git rev-parse HEAD^) &&
184 H2=$(git rev-parse HEAD^^) &&
185 echo "$H0 $H2" >.git/info/grafts &&
186 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
191 test_expect_success
'repack --keep-pack' '
192 test_create_repo keep-pack &&
195 P1=$(commit_and_pack 1) &&
196 P2=$(commit_and_pack 2) &&
197 P3=$(commit_and_pack 3) &&
198 P4=$(commit_and_pack 4) &&
199 ls .git/objects/pack/*.pack >old-counts &&
200 test_line_count = 4 old-counts &&
201 git repack -a -d --keep-pack $P1 --keep-pack $P4 &&
202 ls .git/objects/pack/*.pack >new-counts &&
203 grep -q $P1 new-counts &&
204 grep -q $P4 new-counts &&
205 test_line_count = 3 new-counts &&
210 test_expect_success
'bitmaps are created by default in bare repos' '
211 git clone --bare .git bare.git &&
212 rm -f bare.git/objects/pack/*.bitmap &&
213 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
214 git -C bare.git repack -ad &&
215 bitmap=$(ls bare.git/objects/pack/*.bitmap) &&
216 test_path_is_file "$bitmap"
219 test_expect_success
'incremental repack does not complain' '
220 git -C bare.git repack -q 2>repack.err &&
221 test_must_be_empty repack.err
224 test_expect_success
'bitmaps can be disabled on bare repos' '
225 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
226 git -c repack.writeBitmaps=false -C bare.git repack -ad &&
227 bitmap=$(ls bare.git/objects/pack/*.bitmap || :) &&
231 test_expect_success
'no bitmaps created if .keep files present' '
232 pack=$(ls bare.git/objects/pack/*.pack) &&
233 test_path_is_file "$pack" &&
234 keep=${pack%.pack}.keep &&
235 test_when_finished "rm -f \"\$keep\"" &&
237 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
238 git -C bare.git repack -ad 2>stderr &&
239 test_must_be_empty stderr &&
240 find bare.git/objects/pack/ -type f -name "*.bitmap" >actual &&
241 test_must_be_empty actual
244 test_expect_success
'auto-bitmaps do not complain if unavailable' '
245 test_config -C bare.git pack.packSizeLimit 1M &&
246 blob=$(test-tool genrandom big $((1024*1024)) |
247 git -C bare.git hash-object -w --stdin) &&
248 git -C bare.git update-ref refs/tags/big $blob &&
249 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
250 git -C bare.git repack -ad 2>stderr &&
251 test_must_be_empty stderr &&
252 find bare.git/objects/pack -type f -name "*.bitmap" >actual &&
253 test_must_be_empty actual
257 midx
=$objdir/pack
/multi-pack-index
259 test_expect_success
'setup for --write-midx tests' '
263 git config core.multiPackIndex true &&
269 test_expect_success
'--write-midx unchanged' '
272 GIT_TEST_MULTI_PACK_INDEX=0 git repack &&
273 test_path_is_missing $midx &&
274 test_path_is_missing $midx-*.bitmap &&
276 GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
278 test_path_is_file $midx &&
279 test_path_is_missing $midx-*.bitmap &&
280 test_midx_consistent $objdir
284 test_expect_success
'--write-midx with a new pack' '
289 GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
291 test_path_is_file $midx &&
292 test_path_is_missing $midx-*.bitmap &&
293 test_midx_consistent $objdir
297 test_expect_success
'--write-midx with -b' '
300 GIT_TEST_MULTI_PACK_INDEX=0 git repack -mb &&
302 test_path_is_file $midx &&
303 test_path_is_file $midx-*.bitmap &&
304 test_midx_consistent $objdir
308 test_expect_success
'--write-midx with -d' '
311 test_commit repack &&
313 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Ad --write-midx &&
315 test_path_is_file $midx &&
316 test_path_is_missing $midx-*.bitmap &&
317 test_midx_consistent $objdir
321 test_expect_success
'cleans up MIDX when appropriate' '
325 test_commit repack-2 &&
326 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
328 checksum=$(midx_checksum $objdir) &&
329 test_path_is_file $midx &&
330 test_path_is_file $midx-$checksum.bitmap &&
332 test_commit repack-3 &&
333 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
335 test_path_is_file $midx &&
336 test_path_is_missing $midx-$checksum.bitmap &&
337 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
339 test_commit repack-4 &&
340 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb &&
342 find $objdir/pack -type f -name "multi-pack-index*" >files &&
343 test_must_be_empty files
347 test_expect_success
'--write-midx with preferred bitmap tips' '
348 git init midx-preferred-tips &&
349 test_when_finished "rm -fr midx-preferred-tips" &&
351 cd midx-preferred-tips &&
353 test_commit_bulk --message="%s" 103 &&
355 git log --format="%H" >commits.raw &&
356 sort <commits.raw >commits &&
358 git log --format="create refs/tags/%s/%s %H" HEAD >refs &&
359 git update-ref --stdin <refs &&
361 git repack --write-midx --write-bitmap-index &&
362 test_path_is_file $midx &&
363 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
365 test-tool bitmap list-commits | sort >bitmaps &&
366 comm -13 bitmaps commits >before &&
367 test_line_count = 1 before &&
369 rm -fr $midx-$(midx_checksum $objdir).bitmap &&
372 # instead of constructing the snapshot ourselves (c.f., the test
373 # "write a bitmap with --refs-snapshot (preferred tips)" in
374 # t5326), mark the missing commit as preferred by adding it to
375 # the pack.preferBitmapTips configuration.
376 git for-each-ref --format="%(refname:rstrip=1)" \
377 --points-at="$(cat before)" >missing &&
378 git config pack.preferBitmapTips "$(cat missing)" &&
379 git repack --write-midx --write-bitmap-index &&
381 test-tool bitmap list-commits | sort >bitmaps &&
382 comm -13 bitmaps commits >after &&
384 ! test_cmp before after
388 # The first argument is expected to be a filename
389 # and that file should contain the name of a .idx
390 # file. Send the list of objects in that .idx file
392 get_sorted_objects_from_pack
() {
393 git show-index
<$
(cat "$1") >raw
&&
397 test_expect_success
'--write-midx -b packs non-kept objects' '
399 test_when_finished "rm -fr repo" &&
403 # Create a kept pack-file
406 find $objdir/pack -name "*.idx" >before &&
407 test_line_count = 1 before &&
408 before_name=$(cat before) &&
409 >${before_name%.idx}.keep &&
411 # Create a non-kept pack-file
415 # Create loose objects
419 git repack --write-midx -a -b -d &&
421 # There should be two pack-files now, the
422 # old, kept pack and the new, non-kept pack.
423 find $objdir/pack -name "*.idx" | sort >after &&
424 test_line_count = 2 after &&
425 find $objdir/pack -name "*.keep" >kept &&
426 kept_name=$(cat kept) &&
427 echo ${kept_name%.keep}.idx >kept-idx &&
428 test_cmp before kept-idx &&
430 # Get object list from the kept pack.
431 get_sorted_objects_from_pack before >old.objects &&
433 # Get object list from the one non-kept pack-file
434 comm -13 before after >new-pack &&
435 test_line_count = 1 new-pack &&
436 get_sorted_objects_from_pack new-pack >new.objects &&
438 # None of the objects in the new pack should
439 # exist within the kept pack.
440 comm -12 old.objects new.objects >shared.objects &&
441 test_must_be_empty shared.objects
445 test_expect_success
'--write-midx removes stale pack-based bitmaps' '
448 test_when_finished "rm -fr repo" &&
452 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Ab &&
454 pack_bitmap=$(ls $objdir/pack/pack-*.bitmap) &&
455 test_path_is_file "$pack_bitmap" &&
458 GIT_TEST_MULTI_PACK_INDEX=0 git repack -bm &&
460 test_path_is_file $midx &&
461 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
462 test_path_is_missing $pack_bitmap
466 test_expect_success
'--write-midx with --pack-kept-objects' '
468 test_when_finished "rm -fr repo" &&
475 one="$(echo "one" | git pack-objects --revs $objdir/pack/pack)" &&
476 two="$(echo "one..two" | git pack-objects --revs $objdir/pack/pack)" &&
478 keep="$objdir/pack/pack-$one.keep" &&
481 git repack --write-midx --write-bitmap-index --geometric=2 -d \
482 --pack-kept-objects &&
484 test_path_is_file $keep &&
485 test_path_is_file $midx &&
486 test_path_is_file $midx-$(midx_checksum $objdir).bitmap
490 test_expect_success TTY
'--quiet disables progress' '
491 test_terminal env GIT_PROGRESS_DELAY=0 \
492 git -C midx repack -ad --quiet --write-midx 2>stderr &&
493 test_must_be_empty stderr
496 test_expect_success
'clean up .tmp-* packs on error' '
497 test_must_fail ok=sigpipe git \
498 -c repack.cruftwindow=bogus \
499 repack -ad --cruft &&
500 find $objdir/pack -name '.tmp-
*' >tmpfiles &&
501 test_must_be_empty tmpfiles
504 test_expect_success
'repack -ad cleans up old .tmp-* packs' '
505 git rev-parse HEAD >input &&
506 git pack-objects $objdir/pack/.tmp-1234 <input &&
508 find $objdir/pack -name '.tmp-
*' >tmpfiles &&
509 test_must_be_empty tmpfiles
512 test_expect_success
'setup for update-server-info' '
513 git init update-server-info &&
514 test_commit -C update-server-info message
517 test_server_info_present
() {
518 test_path_is_file update-server-info
/.git
/objects
/info
/packs
&&
519 test_path_is_file update-server-info
/.git
/info
/refs
522 test_server_info_missing
() {
523 test_path_is_missing update-server-info
/.git
/objects
/info
/packs
&&
524 test_path_is_missing update-server-info
/.git
/info
/refs
527 test_server_info_cleanup
() {
528 rm -f update-server-info
/.git
/objects
/info
/packs update-server-info
/.git
/info
/refs
&&
529 test_server_info_missing
532 test_expect_success
'updates server info by default' '
533 test_server_info_cleanup &&
534 git -C update-server-info repack &&
535 test_server_info_present
538 test_expect_success
'-n skips updating server info' '
539 test_server_info_cleanup &&
540 git -C update-server-info repack -n &&
541 test_server_info_missing
544 test_expect_success
'repack.updateServerInfo=true updates server info' '
545 test_server_info_cleanup &&
546 git -C update-server-info -c repack.updateServerInfo=true repack &&
547 test_server_info_present
550 test_expect_success
'repack.updateServerInfo=false skips updating server info' '
551 test_server_info_cleanup &&
552 git -C update-server-info -c repack.updateServerInfo=false repack &&
553 test_server_info_missing
556 test_expect_success
'-n overrides repack.updateServerInfo=true' '
557 test_server_info_cleanup &&
558 git -C update-server-info -c repack.updateServerInfo=true repack -n &&
559 test_server_info_missing
562 test_expect_success
'--expire-to stores pruned objects (now)' '
563 git init expire-to-now &&
567 git branch -M main &&
571 git checkout -b cruft &&
572 test_commit --no-tag cruft &&
574 git rev-list --objects --no-object-names main..cruft >moved.raw &&
575 sort moved.raw >moved.want &&
577 git rev-list --all --objects --no-object-names >expect.raw &&
578 sort expect.raw >expect &&
581 git branch -D cruft &&
582 git reflog expire --all --expire=all &&
584 git init --bare expired.git &&
586 --cruft --cruft-expiration="now" \
587 --expire-to="expired.git/objects/pack/pack" &&
589 expired="$(ls expired.git/objects/pack/pack-*.idx)" &&
590 test_path_is_file "${expired%.idx}.mtimes" &&
592 # Since the `--cruft-expiration` is "now", the effective
593 # behavior is to move _all_ unreachable objects out to
594 # the location in `--expire-to`.
595 git show-index <$expired >expired.raw &&
596 cut -d" " -f2 expired.raw | sort >expired.objects &&
597 git rev-list --all --objects --no-object-names \
598 >remaining.objects &&
600 # ...in other words, the combined contents of this
601 # repository and expired.git should be the same as the
602 # set of objects we started with.
603 cat expired.objects remaining.objects | sort >actual &&
604 test_cmp expect actual &&
606 # The "moved" objects (i.e., those in expired.git)
607 # should be the same as the cruft objects which were
608 # expired in the previous step.
609 test_cmp moved.want expired.objects
613 test_expect_success
'--expire-to stores pruned objects (5.minutes.ago)' '
614 git init expire-to-5.minutes.ago &&
616 cd expire-to-5.minutes.ago &&
618 git branch -M main &&
622 # Create two classes of unreachable objects, one which
623 # is older than 5 minutes (stale), and another which is
625 for kind in stale recent
627 git checkout -b $kind main &&
628 test_commit --no-tag $kind || return 1
631 git rev-list --objects --no-object-names main..stale >in &&
632 stale="$(git pack-objects $objdir/pack/pack <in)" &&
633 mtime="$(test-tool chmtime --get =-600 $objdir/pack/pack-$stale.pack)" &&
635 # expect holds the set of objects we expect to find in
636 # this repository after repacking
637 git rev-list --objects --no-object-names recent >expect.raw &&
638 sort expect.raw >expect &&
640 # moved.want holds the set of objects we expect to find
642 git rev-list --objects --no-object-names main..stale >out &&
643 sort out >moved.want &&
646 git branch -D stale recent &&
647 git reflog expire --all --expire=all &&
650 git init --bare expired.git &&
652 --cruft --cruft-expiration=5.minutes.ago \
653 --expire-to="expired.git/objects/pack/pack" &&
655 # Some of the remaining objects in this repository are
656 # unreachable, so use `cat-file --batch-all-objects`
657 # instead of `rev-list` to get their names
658 git cat-file --batch-all-objects --batch-check="%(objectname)" \
659 >remaining.objects &&
660 sort remaining.objects >actual &&
661 test_cmp expect actual &&
666 expired="$(ls objects/pack/pack-*.mtimes)" &&
667 test-tool pack-mtimes $(basename $expired) >out &&
668 cut -d" " -f1 out | sort >../moved.got &&
670 # Ensure that there are as many objects with the
671 # expected mtime as were moved to expired.git.
673 # In other words, ensure that the recorded
674 # mtimes of any moved objects was written
676 grep " $mtime$" out >matching &&
677 test_line_count = $(wc -l <../moved.want) matching
679 test_cmp moved.want moved.got