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
'packed obs in alt ODB are repacked even when local repo is packless' '
94 mkdir alt_objects/pack &&
95 mv .git/objects/pack/* alt_objects/pack &&
97 test_no_missing_in_packs
100 test_expect_success
'packed obs in alt ODB are repacked when local repo has packs' '
101 rm -f .git/objects/pack/* &&
102 echo new_content >>file1 &&
105 git commit -m more_content &&
108 test_no_missing_in_packs
111 test_expect_success
'packed obs in alternate ODB kept pack are repacked' '
112 # swap the .keep so the commit object is in the pack with .keep
113 for p in alt_objects/pack/*.pack
115 base_name=$(basename $p .pack) &&
116 if test_path_is_file alt_objects/pack/$base_name.keep
118 rm alt_objects/pack/$base_name.keep
120 touch alt_objects/pack/$base_name.keep
124 test_no_missing_in_packs
127 test_expect_success
'packed unreachable obs in alternate ODB are not loosened' '
128 rm -f alt_objects/pack/*.keep &&
129 mv .git/objects/pack/* alt_objects/pack/ &&
130 coid=$(git rev-parse HEAD^{commit}) &&
131 git reset --hard HEAD^ &&
133 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
134 # The pack-objects call on the next line is equivalent to
135 # git repack -A -d without the call to prune-packed
136 git pack-objects --honor-pack-keep --non-empty --all --reflog \
137 --unpack-unreachable </dev/null pack &&
138 rm -f .git/objects/pack/* &&
139 mv pack-* .git/objects/pack/ &&
140 git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
141 ! grep "^$coid " packlist &&
142 echo >.git/objects/info/alternates &&
143 test_must_fail git show $coid
146 test_expect_success
'local packed unreachable obs that exist in alternate ODB are not loosened' '
147 echo $(pwd)/alt_objects >.git/objects/info/alternates &&
148 echo "$coid" | git pack-objects --non-empty --all --reflog pack &&
149 rm -f .git/objects/pack/* &&
150 mv pack-* .git/objects/pack/ &&
151 # The pack-objects call on the next line is equivalent to
152 # git repack -A -d without the call to prune-packed
153 git pack-objects --honor-pack-keep --non-empty --all --reflog \
154 --unpack-unreachable </dev/null pack &&
155 rm -f .git/objects/pack/* &&
156 mv pack-* .git/objects/pack/ &&
157 git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
159 echo >.git/objects/info/alternates &&
160 test_must_fail git show $coid
163 test_expect_success
'objects made unreachable by grafts only are kept' '
165 git commit --allow-empty -m "commit 4" &&
166 H0=$(git rev-parse HEAD) &&
167 H1=$(git rev-parse HEAD^) &&
168 H2=$(git rev-parse HEAD^^) &&
169 echo "$H0 $H2" >.git/info/grafts &&
170 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
175 test_expect_success
'repack --keep-pack' '
176 test_create_repo keep-pack &&
179 P1=$(commit_and_pack 1) &&
180 P2=$(commit_and_pack 2) &&
181 P3=$(commit_and_pack 3) &&
182 P4=$(commit_and_pack 4) &&
183 ls .git/objects/pack/*.pack >old-counts &&
184 test_line_count = 4 old-counts &&
185 git repack -a -d --keep-pack $P1 --keep-pack $P4 &&
186 ls .git/objects/pack/*.pack >new-counts &&
187 grep -q $P1 new-counts &&
188 grep -q $P4 new-counts &&
189 test_line_count = 3 new-counts &&
194 test_expect_success
'bitmaps are created by default in bare repos' '
195 git clone --bare .git bare.git &&
196 rm -f bare.git/objects/pack/*.bitmap &&
197 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
198 git -C bare.git repack -ad &&
199 bitmap=$(ls bare.git/objects/pack/*.bitmap) &&
200 test_path_is_file "$bitmap"
203 test_expect_success
'incremental repack does not complain' '
204 git -C bare.git repack -q 2>repack.err &&
205 test_must_be_empty repack.err
208 test_expect_success
'bitmaps can be disabled on bare repos' '
209 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
210 git -c repack.writeBitmaps=false -C bare.git repack -ad &&
211 bitmap=$(ls bare.git/objects/pack/*.bitmap || :) &&
215 test_expect_success
'no bitmaps created if .keep files present' '
216 pack=$(ls bare.git/objects/pack/*.pack) &&
217 test_path_is_file "$pack" &&
218 keep=${pack%.pack}.keep &&
219 test_when_finished "rm -f \"\$keep\"" &&
221 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
222 git -C bare.git repack -ad 2>stderr &&
223 test_must_be_empty stderr &&
224 find bare.git/objects/pack/ -type f -name "*.bitmap" >actual &&
225 test_must_be_empty actual
228 test_expect_success
'auto-bitmaps do not complain if unavailable' '
229 test_config -C bare.git pack.packSizeLimit 1M &&
230 blob=$(test-tool genrandom big $((1024*1024)) |
231 git -C bare.git hash-object -w --stdin) &&
232 git -C bare.git update-ref refs/tags/big $blob &&
233 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
234 git -C bare.git repack -ad 2>stderr &&
235 test_must_be_empty stderr &&
236 find bare.git/objects/pack -type f -name "*.bitmap" >actual &&
237 test_must_be_empty actual
241 midx
=$objdir/pack
/multi-pack-index
243 test_expect_success
'setup for --write-midx tests' '
247 git config core.multiPackIndex true &&
253 test_expect_success
'--write-midx unchanged' '
256 GIT_TEST_MULTI_PACK_INDEX=0 git repack &&
257 test_path_is_missing $midx &&
258 test_path_is_missing $midx-*.bitmap &&
260 GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
262 test_path_is_file $midx &&
263 test_path_is_missing $midx-*.bitmap &&
264 test_midx_consistent $objdir
268 test_expect_success
'--write-midx with a new pack' '
273 GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
275 test_path_is_file $midx &&
276 test_path_is_missing $midx-*.bitmap &&
277 test_midx_consistent $objdir
281 test_expect_success
'--write-midx with -b' '
284 GIT_TEST_MULTI_PACK_INDEX=0 git repack -mb &&
286 test_path_is_file $midx &&
287 test_path_is_file $midx-*.bitmap &&
288 test_midx_consistent $objdir
292 test_expect_success
'--write-midx with -d' '
295 test_commit repack &&
297 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Ad --write-midx &&
299 test_path_is_file $midx &&
300 test_path_is_missing $midx-*.bitmap &&
301 test_midx_consistent $objdir
305 test_expect_success
'cleans up MIDX when appropriate' '
309 test_commit repack-2 &&
310 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
312 checksum=$(midx_checksum $objdir) &&
313 test_path_is_file $midx &&
314 test_path_is_file $midx-$checksum.bitmap &&
316 test_commit repack-3 &&
317 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
319 test_path_is_file $midx &&
320 test_path_is_missing $midx-$checksum.bitmap &&
321 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
323 test_commit repack-4 &&
324 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb &&
326 find $objdir/pack -type f -name "multi-pack-index*" >files &&
327 test_must_be_empty files
331 test_expect_success
'--write-midx with preferred bitmap tips' '
332 git init midx-preferred-tips &&
333 test_when_finished "rm -fr midx-preferred-tips" &&
335 cd midx-preferred-tips &&
337 test_commit_bulk --message="%s" 103 &&
339 git log --format="%H" >commits.raw &&
340 sort <commits.raw >commits &&
342 git log --format="create refs/tags/%s/%s %H" HEAD >refs &&
343 git update-ref --stdin <refs &&
345 git repack --write-midx --write-bitmap-index &&
346 test_path_is_file $midx &&
347 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
349 test-tool bitmap list-commits | sort >bitmaps &&
350 comm -13 bitmaps commits >before &&
351 test_line_count = 1 before &&
353 rm -fr $midx-$(midx_checksum $objdir).bitmap &&
356 # instead of constructing the snapshot ourselves (c.f., the test
357 # "write a bitmap with --refs-snapshot (preferred tips)" in
358 # t5326), mark the missing commit as preferred by adding it to
359 # the pack.preferBitmapTips configuration.
360 git for-each-ref --format="%(refname:rstrip=1)" \
361 --points-at="$(cat before)" >missing &&
362 git config pack.preferBitmapTips "$(cat missing)" &&
363 git repack --write-midx --write-bitmap-index &&
365 test-tool bitmap list-commits | sort >bitmaps &&
366 comm -13 bitmaps commits >after &&
368 ! test_cmp before after
372 # The first argument is expected to be a filename
373 # and that file should contain the name of a .idx
374 # file. Send the list of objects in that .idx file
376 get_sorted_objects_from_pack
() {
377 git show-index
<$
(cat "$1") >raw
&&
381 test_expect_success
'--write-midx -b packs non-kept objects' '
383 test_when_finished "rm -fr repo" &&
387 # Create a kept pack-file
390 find $objdir/pack -name "*.idx" >before &&
391 test_line_count = 1 before &&
392 before_name=$(cat before) &&
393 >${before_name%.idx}.keep &&
395 # Create a non-kept pack-file
399 # Create loose objects
403 git repack --write-midx -a -b -d &&
405 # There should be two pack-files now, the
406 # old, kept pack and the new, non-kept pack.
407 find $objdir/pack -name "*.idx" | sort >after &&
408 test_line_count = 2 after &&
409 find $objdir/pack -name "*.keep" >kept &&
410 kept_name=$(cat kept) &&
411 echo ${kept_name%.keep}.idx >kept-idx &&
412 test_cmp before kept-idx &&
414 # Get object list from the kept pack.
415 get_sorted_objects_from_pack before >old.objects &&
417 # Get object list from the one non-kept pack-file
418 comm -13 before after >new-pack &&
419 test_line_count = 1 new-pack &&
420 get_sorted_objects_from_pack new-pack >new.objects &&
422 # None of the objects in the new pack should
423 # exist within the kept pack.
424 comm -12 old.objects new.objects >shared.objects &&
425 test_must_be_empty shared.objects
429 test_expect_success
'--write-midx removes stale pack-based bitmaps' '
432 test_when_finished "rm -fr repo" &&
436 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Ab &&
438 pack_bitmap=$(ls $objdir/pack/pack-*.bitmap) &&
439 test_path_is_file "$pack_bitmap" &&
442 GIT_TEST_MULTI_PACK_INDEX=0 git repack -bm &&
444 test_path_is_file $midx &&
445 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
446 test_path_is_missing $pack_bitmap
450 test_expect_success
'--write-midx with --pack-kept-objects' '
452 test_when_finished "rm -fr repo" &&
459 one="$(echo "one" | git pack-objects --revs $objdir/pack/pack)" &&
460 two="$(echo "one..two" | git pack-objects --revs $objdir/pack/pack)" &&
462 keep="$objdir/pack/pack-$one.keep" &&
465 git repack --write-midx --write-bitmap-index --geometric=2 -d \
466 --pack-kept-objects &&
468 test_path_is_file $keep &&
469 test_path_is_file $midx &&
470 test_path_is_file $midx-$(midx_checksum $objdir).bitmap
474 test_expect_success TTY
'--quiet disables progress' '
475 test_terminal env GIT_PROGRESS_DELAY=0 \
476 git -C midx repack -ad --quiet --write-midx 2>stderr &&
477 test_must_be_empty stderr
480 test_expect_success
'clean up .tmp-* packs on error' '
481 test_must_fail ok=sigpipe git \
482 -c repack.cruftwindow=bogus \
483 repack -ad --cruft &&
484 find $objdir/pack -name '.tmp-
*' >tmpfiles &&
485 test_must_be_empty tmpfiles
488 test_expect_success
'repack -ad cleans up old .tmp-* packs' '
489 git rev-parse HEAD >input &&
490 git pack-objects $objdir/pack/.tmp-1234 <input &&
492 find $objdir/pack -name '.tmp-
*' >tmpfiles &&
493 test_must_be_empty tmpfiles
496 test_expect_success
'setup for update-server-info' '
497 git init update-server-info &&
498 test_commit -C update-server-info message
501 test_server_info_present
() {
502 test_path_is_file update-server-info
/.git
/objects
/info
/packs
&&
503 test_path_is_file update-server-info
/.git
/info
/refs
506 test_server_info_missing
() {
507 test_path_is_missing update-server-info
/.git
/objects
/info
/packs
&&
508 test_path_is_missing update-server-info
/.git
/info
/refs
511 test_server_info_cleanup
() {
512 rm -f update-server-info
/.git
/objects
/info
/packs update-server-info
/.git
/info
/refs
&&
513 test_server_info_missing
516 test_expect_success
'updates server info by default' '
517 test_server_info_cleanup &&
518 git -C update-server-info repack &&
519 test_server_info_present
522 test_expect_success
'-n skips updating server info' '
523 test_server_info_cleanup &&
524 git -C update-server-info repack -n &&
525 test_server_info_missing
528 test_expect_success
'repack.updateServerInfo=true updates server info' '
529 test_server_info_cleanup &&
530 git -C update-server-info -c repack.updateServerInfo=true repack &&
531 test_server_info_present
534 test_expect_success
'repack.updateServerInfo=false skips updating server info' '
535 test_server_info_cleanup &&
536 git -C update-server-info -c repack.updateServerInfo=false repack &&
537 test_server_info_missing
540 test_expect_success
'-n overrides repack.updateServerInfo=true' '
541 test_server_info_cleanup &&
542 git -C update-server-info -c repack.updateServerInfo=true repack -n &&
543 test_server_info_missing
546 test_expect_success
'--expire-to stores pruned objects (now)' '
547 git init expire-to-now &&
551 git branch -M main &&
555 git checkout -b cruft &&
556 test_commit --no-tag cruft &&
558 git rev-list --objects --no-object-names main..cruft >moved.raw &&
559 sort moved.raw >moved.want &&
561 git rev-list --all --objects --no-object-names >expect.raw &&
562 sort expect.raw >expect &&
565 git branch -D cruft &&
566 git reflog expire --all --expire=all &&
568 git init --bare expired.git &&
570 --cruft --cruft-expiration="now" \
571 --expire-to="expired.git/objects/pack/pack" &&
573 expired="$(ls expired.git/objects/pack/pack-*.idx)" &&
574 test_path_is_file "${expired%.idx}.mtimes" &&
576 # Since the `--cruft-expiration` is "now", the effective
577 # behavior is to move _all_ unreachable objects out to
578 # the location in `--expire-to`.
579 git show-index <$expired >expired.raw &&
580 cut -d" " -f2 expired.raw | sort >expired.objects &&
581 git rev-list --all --objects --no-object-names \
582 >remaining.objects &&
584 # ...in other words, the combined contents of this
585 # repository and expired.git should be the same as the
586 # set of objects we started with.
587 cat expired.objects remaining.objects | sort >actual &&
588 test_cmp expect actual &&
590 # The "moved" objects (i.e., those in expired.git)
591 # should be the same as the cruft objects which were
592 # expired in the previous step.
593 test_cmp moved.want expired.objects
597 test_expect_success
'--expire-to stores pruned objects (5.minutes.ago)' '
598 git init expire-to-5.minutes.ago &&
600 cd expire-to-5.minutes.ago &&
602 git branch -M main &&
606 # Create two classes of unreachable objects, one which
607 # is older than 5 minutes (stale), and another which is
609 for kind in stale recent
611 git checkout -b $kind main &&
612 test_commit --no-tag $kind || return 1
615 git rev-list --objects --no-object-names main..stale >in &&
616 stale="$(git pack-objects $objdir/pack/pack <in)" &&
617 mtime="$(test-tool chmtime --get =-600 $objdir/pack/pack-$stale.pack)" &&
619 # expect holds the set of objects we expect to find in
620 # this repository after repacking
621 git rev-list --objects --no-object-names recent >expect.raw &&
622 sort expect.raw >expect &&
624 # moved.want holds the set of objects we expect to find
626 git rev-list --objects --no-object-names main..stale >out &&
627 sort out >moved.want &&
630 git branch -D stale recent &&
631 git reflog expire --all --expire=all &&
634 git init --bare expired.git &&
636 --cruft --cruft-expiration=5.minutes.ago \
637 --expire-to="expired.git/objects/pack/pack" &&
639 # Some of the remaining objects in this repository are
640 # unreachable, so use `cat-file --batch-all-objects`
641 # instead of `rev-list` to get their names
642 git cat-file --batch-all-objects --batch-check="%(objectname)" \
643 >remaining.objects &&
644 sort remaining.objects >actual &&
645 test_cmp expect actual &&
650 expired="$(ls objects/pack/pack-*.mtimes)" &&
651 test-tool pack-mtimes $(basename $expired) >out &&
652 cut -d" " -f1 out | sort >../moved.got &&
654 # Ensure that there are as many objects with the
655 # expected mtime as were moved to expired.git.
657 # In other words, ensure that the recorded
658 # mtimes of any moved objects was written
660 grep " $mtime$" out >matching &&
661 test_line_count = $(wc -l <../moved.want) matching
663 test_cmp moved.want moved.got