t/perf: avoid unnecessary test_export() recursion
[git] / t / t5319-multi-pack-index.sh
1 #!/bin/sh
2
3 test_description='multi-pack-indexes'
4 . ./test-lib.sh
5
6 objdir=.git/objects
7
8 HASH_LEN=$(test_oid rawsz)
9
10 midx_read_expect () {
11         NUM_PACKS=$1
12         NUM_OBJECTS=$2
13         NUM_CHUNKS=$3
14         OBJECT_DIR=$4
15         EXTRA_CHUNKS="$5"
16         {
17                 cat <<-EOF &&
18                 header: 4d494458 1 $HASH_LEN $NUM_CHUNKS $NUM_PACKS
19                 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
20                 num_objects: $NUM_OBJECTS
21                 packs:
22                 EOF
23                 if test $NUM_PACKS -ge 1
24                 then
25                         ls $OBJECT_DIR/pack/ | grep idx | sort
26                 fi &&
27                 printf "object-dir: $OBJECT_DIR\n"
28         } >expect &&
29         test-tool read-midx $OBJECT_DIR >actual &&
30         test_cmp expect actual
31 }
32
33 test_expect_success 'setup' '
34         test_oid_cache <<-EOF
35         idxoff sha1:2999
36         idxoff sha256:3739
37
38         packnameoff sha1:652
39         packnameoff sha256:940
40
41         fanoutoff sha1:1
42         fanoutoff sha256:3
43         EOF
44 '
45
46 test_expect_success "don't write midx with no packs" '
47         test_must_fail git multi-pack-index --object-dir=. write &&
48         test_path_is_missing pack/multi-pack-index
49 '
50
51 test_expect_success SHA1 'warn if a midx contains no oid' '
52         cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
53         test_must_fail git multi-pack-index verify &&
54         rm $objdir/pack/multi-pack-index
55 '
56
57 generate_objects () {
58         i=$1
59         iii=$(printf '%03i' $i)
60         {
61                 test-tool genrandom "bar" 200 &&
62                 test-tool genrandom "baz $iii" 50
63         } >wide_delta_$iii &&
64         {
65                 test-tool genrandom "foo"$i 100 &&
66                 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
67                 test-tool genrandom "foo"$(( $i + 2 )) 100
68         } >deep_delta_$iii &&
69         {
70                 echo $iii &&
71                 test-tool genrandom "$iii" 8192
72         } >file_$iii &&
73         git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
74 }
75
76 commit_and_list_objects () {
77         {
78                 echo 101 &&
79                 test-tool genrandom 100 8192;
80         } >file_101 &&
81         git update-index --add file_101 &&
82         tree=$(git write-tree) &&
83         commit=$(git commit-tree $tree -p HEAD</dev/null) &&
84         {
85                 echo $tree &&
86                 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\)        .*/\\1/"
87         } >obj-list &&
88         git reset --hard $commit
89 }
90
91 test_expect_success 'create objects' '
92         test_commit initial &&
93         for i in $(test_seq 1 5)
94         do
95                 generate_objects $i
96         done &&
97         commit_and_list_objects
98 '
99
100 test_expect_success 'write midx with one v1 pack' '
101         pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
102         test_when_finished rm $objdir/pack/test-$pack.pack \
103                 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
104         git multi-pack-index --object-dir=$objdir write &&
105         midx_read_expect 1 18 4 $objdir
106 '
107
108 midx_git_two_modes () {
109         git -c core.multiPackIndex=false $1 >expect &&
110         git -c core.multiPackIndex=true $1 >actual &&
111         if [ "$2" = "sorted" ]
112         then
113                 sort <expect >expect.sorted &&
114                 mv expect.sorted expect &&
115                 sort <actual >actual.sorted &&
116                 mv actual.sorted actual
117         fi &&
118         test_cmp expect actual
119 }
120
121 compare_results_with_midx () {
122         MSG=$1
123         test_expect_success "check normal git operations: $MSG" '
124                 midx_git_two_modes "rev-list --objects --all" &&
125                 midx_git_two_modes "log --raw" &&
126                 midx_git_two_modes "count-objects --verbose" &&
127                 midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
128                 midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
129         '
130 }
131
132 test_expect_success 'write midx with one v2 pack' '
133         git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
134         git multi-pack-index --object-dir=$objdir write &&
135         midx_read_expect 1 18 4 $objdir
136 '
137
138 compare_results_with_midx "one v2 pack"
139
140 test_expect_success 'corrupt idx not opened' '
141         idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
142         mv $objdir/pack/$idx backup-$idx &&
143         test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
144
145         # This is the minimum size for a sha-1 based .idx; this lets
146         # us pass perfunctory tests, but anything that actually opens and reads
147         # the idx file will complain.
148         test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
149
150         git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
151         test_must_be_empty err
152 '
153
154 test_expect_success 'add more objects' '
155         for i in $(test_seq 6 10)
156         do
157                 generate_objects $i
158         done &&
159         commit_and_list_objects
160 '
161
162 test_expect_success 'write midx with two packs' '
163         git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
164         git multi-pack-index --object-dir=$objdir write &&
165         midx_read_expect 2 34 4 $objdir
166 '
167
168 compare_results_with_midx "two packs"
169
170 test_expect_success 'write progress off for redirected stderr' '
171         git multi-pack-index --object-dir=$objdir write 2>err &&
172         test_line_count = 0 err
173 '
174
175 test_expect_success 'write force progress on for stderr' '
176         git multi-pack-index --object-dir=$objdir --progress write 2>err &&
177         test_file_not_empty err
178 '
179
180 test_expect_success 'write with the --no-progress option' '
181         git multi-pack-index --object-dir=$objdir --no-progress write 2>err &&
182         test_line_count = 0 err
183 '
184
185 test_expect_success 'add more packs' '
186         for j in $(test_seq 11 20)
187         do
188                 generate_objects $j &&
189                 commit_and_list_objects &&
190                 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
191         done
192 '
193
194 compare_results_with_midx "mixed mode (two packs + extra)"
195
196 test_expect_success 'write midx with twelve packs' '
197         git multi-pack-index --object-dir=$objdir write &&
198         midx_read_expect 12 74 4 $objdir
199 '
200
201 compare_results_with_midx "twelve packs"
202
203 test_expect_success 'warn on improper hash version' '
204         git init --object-format=sha1 sha1 &&
205         (
206                 cd sha1 &&
207                 git config core.multiPackIndex true &&
208                 test_commit 1 &&
209                 git repack -a &&
210                 git multi-pack-index write &&
211                 mv .git/objects/pack/multi-pack-index ../mpi-sha1
212         ) &&
213         git init --object-format=sha256 sha256 &&
214         (
215                 cd sha256 &&
216                 git config core.multiPackIndex true &&
217                 test_commit 1 &&
218                 git repack -a &&
219                 git multi-pack-index write &&
220                 mv .git/objects/pack/multi-pack-index ../mpi-sha256
221         ) &&
222         (
223                 cd sha1 &&
224                 mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
225                 git log -1 2>err &&
226                 test_i18ngrep "multi-pack-index hash version 2 does not match version 1" err
227         ) &&
228         (
229                 cd sha256 &&
230                 mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
231                 git log -1 2>err &&
232                 test_i18ngrep "multi-pack-index hash version 1 does not match version 2" err
233         )
234 '
235
236
237 test_expect_success 'verify multi-pack-index success' '
238         git multi-pack-index verify --object-dir=$objdir
239 '
240
241 test_expect_success 'verify progress off for redirected stderr' '
242         git multi-pack-index verify --object-dir=$objdir 2>err &&
243         test_line_count = 0 err
244 '
245
246 test_expect_success 'verify force progress on for stderr' '
247         git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
248         test_file_not_empty err
249 '
250
251 test_expect_success 'verify with the --no-progress option' '
252         git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
253         test_line_count = 0 err
254 '
255
256 # usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
257 corrupt_midx_and_verify() {
258         POS=$1 &&
259         DATA="${2:-\0}" &&
260         OBJDIR=$3 &&
261         GREPSTR="$4" &&
262         COMMAND="$5" &&
263         if test -z "$COMMAND"
264         then
265                 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
266         fi &&
267         FILE=$OBJDIR/pack/multi-pack-index &&
268         chmod a+w $FILE &&
269         test_when_finished mv midx-backup $FILE &&
270         cp $FILE midx-backup &&
271         printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
272         test_must_fail $COMMAND 2>test_err &&
273         grep -v "^+" test_err >err &&
274         test_i18ngrep "$GREPSTR" err
275 }
276
277 test_expect_success 'verify bad signature' '
278         corrupt_midx_and_verify 0 "\00" $objdir \
279                 "multi-pack-index signature"
280 '
281
282 NUM_OBJECTS=74
283 MIDX_BYTE_VERSION=4
284 MIDX_BYTE_OID_VERSION=5
285 MIDX_BYTE_CHUNK_COUNT=6
286 MIDX_HEADER_SIZE=12
287 MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
288 MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
289 MIDX_NUM_CHUNKS=5
290 MIDX_CHUNK_LOOKUP_WIDTH=12
291 MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
292                          $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
293 MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
294 MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
295 MIDX_OID_FANOUT_WIDTH=4
296 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
297 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
298 MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
299 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
300 MIDX_OFFSET_WIDTH=8
301 MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
302 MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
303
304 test_expect_success 'verify bad version' '
305         corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
306                 "multi-pack-index version"
307 '
308
309 test_expect_success 'verify bad OID version' '
310         corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
311                 "hash version"
312 '
313
314 test_expect_success 'verify truncated chunk count' '
315         corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
316                 "missing required"
317 '
318
319 test_expect_success 'verify extended chunk count' '
320         corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
321                 "terminating multi-pack-index chunk id appears earlier than expected"
322 '
323
324 test_expect_success 'verify missing required chunk' '
325         corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
326                 "missing required"
327 '
328
329 test_expect_success 'verify invalid chunk offset' '
330         corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
331                 "invalid chunk offset (too large)"
332 '
333
334 test_expect_success 'verify packnames out of order' '
335         corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
336                 "pack names out of order"
337 '
338
339 test_expect_success 'verify packnames out of order' '
340         corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
341                 "failed to load pack"
342 '
343
344 test_expect_success 'verify oid fanout out of order' '
345         corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
346                 "oid fanout out of order"
347 '
348
349 test_expect_success 'verify oid lookup out of order' '
350         corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
351                 "oid lookup out of order"
352 '
353
354 test_expect_success 'verify incorrect pack-int-id' '
355         corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
356                 "bad pack-int-id"
357 '
358
359 test_expect_success 'verify incorrect offset' '
360         corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
361                 "incorrect object offset"
362 '
363
364 test_expect_success 'git-fsck incorrect offset' '
365         corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
366                 "incorrect object offset" \
367                 "git -c core.multipackindex=true fsck"
368 '
369
370 test_expect_success 'repack progress off for redirected stderr' '
371         git multi-pack-index --object-dir=$objdir repack 2>err &&
372         test_line_count = 0 err
373 '
374
375 test_expect_success 'repack force progress on for stderr' '
376         git multi-pack-index --object-dir=$objdir --progress repack 2>err &&
377         test_file_not_empty err
378 '
379
380 test_expect_success 'repack with the --no-progress option' '
381         git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&
382         test_line_count = 0 err
383 '
384
385 test_expect_success 'repack removes multi-pack-index when deleting packs' '
386         test_path_is_file $objdir/pack/multi-pack-index &&
387         # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
388         # multi-pack-index after repacking, but set "core.multiPackIndex" to
389         # true so that "git repack" can read the existing MIDX.
390         GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
391         test_path_is_missing $objdir/pack/multi-pack-index
392 '
393
394 test_expect_success 'repack preserves multi-pack-index when creating packs' '
395         git init preserve &&
396         test_when_finished "rm -fr preserve" &&
397         (
398                 cd preserve &&
399                 packdir=.git/objects/pack &&
400                 midx=$packdir/multi-pack-index &&
401
402                 test_commit 1 &&
403                 pack1=$(git pack-objects --all $packdir/pack) &&
404                 touch $packdir/pack-$pack1.keep &&
405                 test_commit 2 &&
406                 pack2=$(git pack-objects --revs $packdir/pack) &&
407                 touch $packdir/pack-$pack2.keep &&
408
409                 git multi-pack-index write &&
410                 cp $midx $midx.bak &&
411
412                 cat >pack-input <<-EOF &&
413                 HEAD
414                 ^HEAD~1
415                 EOF
416                 test_commit 3 &&
417                 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
418                 test_commit 4 &&
419                 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
420
421                 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
422                 ls -la $packdir &&
423                 test_path_is_file $packdir/pack-$pack1.pack &&
424                 test_path_is_file $packdir/pack-$pack2.pack &&
425                 test_path_is_missing $packdir/pack-$pack3.pack &&
426                 test_path_is_missing $packdir/pack-$pack4.pack &&
427                 test_cmp_bin $midx.bak $midx
428         )
429 '
430
431 compare_results_with_midx "after repack"
432
433 test_expect_success 'multi-pack-index and pack-bitmap' '
434         git -c repack.writeBitmaps=true repack -ad &&
435         git multi-pack-index write &&
436         git rev-list --test-bitmap HEAD
437 '
438
439 test_expect_success 'multi-pack-index and alternates' '
440         git init --bare alt.git &&
441         echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
442         echo content1 >file1 &&
443         altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
444         git cat-file blob $altblob &&
445         git rev-list --all
446 '
447
448 compare_results_with_midx "with alternate (local midx)"
449
450 test_expect_success 'multi-pack-index in an alternate' '
451         mv .git/objects/pack/* alt.git/objects/pack &&
452         test_commit add_local_objects &&
453         git repack --local &&
454         git multi-pack-index write &&
455         midx_read_expect 1 3 4 $objdir &&
456         git reset --hard HEAD~1 &&
457         rm -f .git/objects/pack/*
458 '
459
460 compare_results_with_midx "with alternate (remote midx)"
461
462 # usage: corrupt_data <file> <pos> [<data>]
463 corrupt_data () {
464         file=$1
465         pos=$2
466         data="${3:-\0}"
467         printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
468 }
469
470 # Force 64-bit offsets by manipulating the idx file.
471 # This makes the IDX file _incorrect_ so be careful to clean up after!
472 test_expect_success 'force some 64-bit offsets with pack-objects' '
473         mkdir objects64 &&
474         mkdir objects64/pack &&
475         for i in $(test_seq 1 11)
476         do
477                 generate_objects 11
478         done &&
479         commit_and_list_objects &&
480         pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
481         idx64=objects64/pack/test-64-$pack64.idx &&
482         chmod u+w $idx64 &&
483         corrupt_data $idx64 $(test_oid idxoff) "\02" &&
484         midx64=$(git multi-pack-index --object-dir=objects64 write) &&
485         midx_read_expect 1 63 5 objects64 " large-offsets"
486 '
487
488 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
489         git multi-pack-index verify --object-dir=objects64
490 '
491
492 NUM_OBJECTS=63
493 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
494 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
495 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
496 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
497 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
498
499 test_expect_success 'verify incorrect 64-bit offset' '
500         corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
501                 "incorrect object offset"
502 '
503
504 test_expect_success 'setup expire tests' '
505         mkdir dup &&
506         (
507                 cd dup &&
508                 git init &&
509                 test-tool genrandom "data" 4096 >large_file.txt &&
510                 git update-index --add large_file.txt &&
511                 for i in $(test_seq 1 20)
512                 do
513                         test_commit $i
514                 done &&
515                 git branch A HEAD &&
516                 git branch B HEAD~8 &&
517                 git branch C HEAD~13 &&
518                 git branch D HEAD~16 &&
519                 git branch E HEAD~18 &&
520                 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
521                 refs/heads/A
522                 ^refs/heads/B
523                 EOF
524                 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
525                 refs/heads/B
526                 ^refs/heads/C
527                 EOF
528                 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
529                 refs/heads/C
530                 ^refs/heads/D
531                 EOF
532                 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
533                 refs/heads/D
534                 ^refs/heads/E
535                 EOF
536                 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
537                 refs/heads/E
538                 EOF
539                 git multi-pack-index write &&
540                 cp -r .git/objects/pack .git/objects/pack-backup
541         )
542 '
543
544 test_expect_success 'expire does not remove any packs' '
545         (
546                 cd dup &&
547                 ls .git/objects/pack >expect &&
548                 git multi-pack-index expire &&
549                 ls .git/objects/pack >actual &&
550                 test_cmp expect actual
551         )
552 '
553
554 test_expect_success 'expire progress off for redirected stderr' '
555         (
556                 cd dup &&
557                 git multi-pack-index expire 2>err &&
558                 test_line_count = 0 err
559         )
560 '
561
562 test_expect_success 'expire force progress on for stderr' '
563         (
564                 cd dup &&
565                 git multi-pack-index --progress expire 2>err &&
566                 test_file_not_empty err
567         )
568 '
569
570 test_expect_success 'expire with the --no-progress option' '
571         (
572                 cd dup &&
573                 git multi-pack-index --no-progress expire 2>err &&
574                 test_line_count = 0 err
575         )
576 '
577
578 test_expect_success 'expire removes unreferenced packs' '
579         (
580                 cd dup &&
581                 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
582                 refs/heads/A
583                 ^refs/heads/C
584                 EOF
585                 git multi-pack-index write &&
586                 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
587                 git multi-pack-index expire &&
588                 ls .git/objects/pack >actual &&
589                 test_cmp expect actual &&
590                 ls .git/objects/pack/ | grep idx >expect-idx &&
591                 test-tool read-midx .git/objects | grep idx >actual-midx &&
592                 test_cmp expect-idx actual-midx &&
593                 git multi-pack-index verify &&
594                 git fsck
595         )
596 '
597
598 test_expect_success 'repack with minimum size does not alter existing packs' '
599         (
600                 cd dup &&
601                 rm -rf .git/objects/pack &&
602                 mv .git/objects/pack-backup .git/objects/pack &&
603                 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
604                 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
605                 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
606                 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
607                 ls .git/objects/pack >expect &&
608                 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
609                 git multi-pack-index repack --batch-size=$MINSIZE &&
610                 ls .git/objects/pack >actual &&
611                 test_cmp expect actual
612         )
613 '
614
615 test_expect_success 'repack respects repack.packKeptObjects=false' '
616         test_when_finished rm -f dup/.git/objects/pack/*keep &&
617         (
618                 cd dup &&
619                 ls .git/objects/pack/*idx >idx-list &&
620                 test_line_count = 5 idx-list &&
621                 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
622                 test_line_count = 5 keep-list &&
623                 for keep in $(cat keep-list)
624                 do
625                         touch $keep || return 1
626                 done &&
627                 git multi-pack-index repack --batch-size=0 &&
628                 ls .git/objects/pack/*idx >idx-list &&
629                 test_line_count = 5 idx-list &&
630                 test-tool read-midx .git/objects | grep idx >midx-list &&
631                 test_line_count = 5 midx-list &&
632                 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
633                 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
634                 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
635                 ls .git/objects/pack/*idx >idx-list &&
636                 test_line_count = 5 idx-list &&
637                 test-tool read-midx .git/objects | grep idx >midx-list &&
638                 test_line_count = 5 midx-list
639         )
640 '
641
642 test_expect_success 'repack creates a new pack' '
643         (
644                 cd dup &&
645                 ls .git/objects/pack/*idx >idx-list &&
646                 test_line_count = 5 idx-list &&
647                 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
648                 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
649                 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
650                 ls .git/objects/pack/*idx >idx-list &&
651                 test_line_count = 6 idx-list &&
652                 test-tool read-midx .git/objects | grep idx >midx-list &&
653                 test_line_count = 6 midx-list
654         )
655 '
656
657 test_expect_success 'expire removes repacked packs' '
658         (
659                 cd dup &&
660                 ls -al .git/objects/pack/*pack &&
661                 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
662                 git multi-pack-index expire &&
663                 ls -S .git/objects/pack/*pack >actual &&
664                 test_cmp expect actual &&
665                 test-tool read-midx .git/objects | grep idx >midx-list &&
666                 test_line_count = 4 midx-list
667         )
668 '
669
670 test_expect_success 'expire works when adding new packs' '
671         (
672                 cd dup &&
673                 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
674                 refs/heads/A
675                 ^refs/heads/B
676                 EOF
677                 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
678                 refs/heads/B
679                 ^refs/heads/C
680                 EOF
681                 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
682                 refs/heads/C
683                 ^refs/heads/D
684                 EOF
685                 git multi-pack-index write &&
686                 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
687                 refs/heads/D
688                 ^refs/heads/E
689                 EOF
690                 git multi-pack-index write &&
691                 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
692                 refs/heads/E
693                 EOF
694                 git multi-pack-index expire &&
695                 ls .git/objects/pack/ | grep idx >expect &&
696                 test-tool read-midx .git/objects | grep idx >actual &&
697                 test_cmp expect actual &&
698                 git multi-pack-index verify
699         )
700 '
701
702 test_expect_success 'expire respects .keep files' '
703         (
704                 cd dup &&
705                 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
706                 refs/heads/A
707                 EOF
708                 git multi-pack-index write &&
709                 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
710                 touch $PACKA.keep &&
711                 git multi-pack-index expire &&
712                 ls -S .git/objects/pack/a-pack* | grep $PACKA >a-pack-files &&
713                 test_line_count = 3 a-pack-files &&
714                 test-tool read-midx .git/objects | grep idx >midx-list &&
715                 test_line_count = 2 midx-list
716         )
717 '
718
719 test_expect_success 'repack --batch-size=0 repacks everything' '
720         cp -r dup dup2 &&
721         (
722                 cd dup &&
723                 rm .git/objects/pack/*.keep &&
724                 ls .git/objects/pack/*idx >idx-list &&
725                 test_line_count = 2 idx-list &&
726                 git multi-pack-index repack --batch-size=0 &&
727                 ls .git/objects/pack/*idx >idx-list &&
728                 test_line_count = 3 idx-list &&
729                 test-tool read-midx .git/objects | grep idx >midx-list &&
730                 test_line_count = 3 midx-list &&
731                 git multi-pack-index expire &&
732                 ls -al .git/objects/pack/*idx >idx-list &&
733                 test_line_count = 1 idx-list &&
734                 git multi-pack-index repack --batch-size=0 &&
735                 ls -al .git/objects/pack/*idx >new-idx-list &&
736                 test_cmp idx-list new-idx-list
737         )
738 '
739
740 test_expect_success 'repack --batch-size=<large> repacks everything' '
741         (
742                 cd dup2 &&
743                 rm .git/objects/pack/*.keep &&
744                 ls .git/objects/pack/*idx >idx-list &&
745                 test_line_count = 2 idx-list &&
746                 git multi-pack-index repack --batch-size=2000000 &&
747                 ls .git/objects/pack/*idx >idx-list &&
748                 test_line_count = 3 idx-list &&
749                 test-tool read-midx .git/objects | grep idx >midx-list &&
750                 test_line_count = 3 midx-list &&
751                 git multi-pack-index expire &&
752                 ls -al .git/objects/pack/*idx >idx-list &&
753                 test_line_count = 1 idx-list
754         )
755 '
756
757 test_done