Merge branch 'ds/midx-repack-to-batch-size'
[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' '
386         test_path_is_file $objdir/pack/multi-pack-index &&
387         GIT_TEST_MULTI_PACK_INDEX=0 git repack -adf &&
388         test_path_is_missing $objdir/pack/multi-pack-index
389 '
390
391 compare_results_with_midx "after repack"
392
393 test_expect_success 'multi-pack-index and pack-bitmap' '
394         git -c repack.writeBitmaps=true repack -ad &&
395         git multi-pack-index write &&
396         git rev-list --test-bitmap HEAD
397 '
398
399 test_expect_success 'multi-pack-index and alternates' '
400         git init --bare alt.git &&
401         echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
402         echo content1 >file1 &&
403         altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
404         git cat-file blob $altblob &&
405         git rev-list --all
406 '
407
408 compare_results_with_midx "with alternate (local midx)"
409
410 test_expect_success 'multi-pack-index in an alternate' '
411         mv .git/objects/pack/* alt.git/objects/pack &&
412         test_commit add_local_objects &&
413         git repack --local &&
414         git multi-pack-index write &&
415         midx_read_expect 1 3 4 $objdir &&
416         git reset --hard HEAD~1 &&
417         rm -f .git/objects/pack/*
418 '
419
420 compare_results_with_midx "with alternate (remote midx)"
421
422 # usage: corrupt_data <file> <pos> [<data>]
423 corrupt_data () {
424         file=$1
425         pos=$2
426         data="${3:-\0}"
427         printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
428 }
429
430 # Force 64-bit offsets by manipulating the idx file.
431 # This makes the IDX file _incorrect_ so be careful to clean up after!
432 test_expect_success 'force some 64-bit offsets with pack-objects' '
433         mkdir objects64 &&
434         mkdir objects64/pack &&
435         for i in $(test_seq 1 11)
436         do
437                 generate_objects 11
438         done &&
439         commit_and_list_objects &&
440         pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
441         idx64=objects64/pack/test-64-$pack64.idx &&
442         chmod u+w $idx64 &&
443         corrupt_data $idx64 $(test_oid idxoff) "\02" &&
444         midx64=$(git multi-pack-index --object-dir=objects64 write) &&
445         midx_read_expect 1 63 5 objects64 " large-offsets"
446 '
447
448 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
449         git multi-pack-index verify --object-dir=objects64
450 '
451
452 NUM_OBJECTS=63
453 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
454 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
455 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
456 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
457 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
458
459 test_expect_success 'verify incorrect 64-bit offset' '
460         corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
461                 "incorrect object offset"
462 '
463
464 test_expect_success 'setup expire tests' '
465         mkdir dup &&
466         (
467                 cd dup &&
468                 git init &&
469                 test-tool genrandom "data" 4096 >large_file.txt &&
470                 git update-index --add large_file.txt &&
471                 for i in $(test_seq 1 20)
472                 do
473                         test_commit $i
474                 done &&
475                 git branch A HEAD &&
476                 git branch B HEAD~8 &&
477                 git branch C HEAD~13 &&
478                 git branch D HEAD~16 &&
479                 git branch E HEAD~18 &&
480                 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
481                 refs/heads/A
482                 ^refs/heads/B
483                 EOF
484                 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
485                 refs/heads/B
486                 ^refs/heads/C
487                 EOF
488                 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
489                 refs/heads/C
490                 ^refs/heads/D
491                 EOF
492                 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
493                 refs/heads/D
494                 ^refs/heads/E
495                 EOF
496                 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
497                 refs/heads/E
498                 EOF
499                 git multi-pack-index write &&
500                 cp -r .git/objects/pack .git/objects/pack-backup
501         )
502 '
503
504 test_expect_success 'expire does not remove any packs' '
505         (
506                 cd dup &&
507                 ls .git/objects/pack >expect &&
508                 git multi-pack-index expire &&
509                 ls .git/objects/pack >actual &&
510                 test_cmp expect actual
511         )
512 '
513
514 test_expect_success 'expire progress off for redirected stderr' '
515         (
516                 cd dup &&
517                 git multi-pack-index expire 2>err &&
518                 test_line_count = 0 err
519         )
520 '
521
522 test_expect_success 'expire force progress on for stderr' '
523         (
524                 cd dup &&
525                 git multi-pack-index --progress expire 2>err &&
526                 test_file_not_empty err
527         )
528 '
529
530 test_expect_success 'expire with the --no-progress option' '
531         (
532                 cd dup &&
533                 git multi-pack-index --no-progress expire 2>err &&
534                 test_line_count = 0 err
535         )
536 '
537
538 test_expect_success 'expire removes unreferenced packs' '
539         (
540                 cd dup &&
541                 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
542                 refs/heads/A
543                 ^refs/heads/C
544                 EOF
545                 git multi-pack-index write &&
546                 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
547                 git multi-pack-index expire &&
548                 ls .git/objects/pack >actual &&
549                 test_cmp expect actual &&
550                 ls .git/objects/pack/ | grep idx >expect-idx &&
551                 test-tool read-midx .git/objects | grep idx >actual-midx &&
552                 test_cmp expect-idx actual-midx &&
553                 git multi-pack-index verify &&
554                 git fsck
555         )
556 '
557
558 test_expect_success 'repack with minimum size does not alter existing packs' '
559         (
560                 cd dup &&
561                 rm -rf .git/objects/pack &&
562                 mv .git/objects/pack-backup .git/objects/pack &&
563                 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
564                 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
565                 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
566                 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
567                 ls .git/objects/pack >expect &&
568                 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
569                 git multi-pack-index repack --batch-size=$MINSIZE &&
570                 ls .git/objects/pack >actual &&
571                 test_cmp expect actual
572         )
573 '
574
575 test_expect_success 'repack respects repack.packKeptObjects=false' '
576         test_when_finished rm -f dup/.git/objects/pack/*keep &&
577         (
578                 cd dup &&
579                 ls .git/objects/pack/*idx >idx-list &&
580                 test_line_count = 5 idx-list &&
581                 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
582                 test_line_count = 5 keep-list &&
583                 for keep in $(cat keep-list)
584                 do
585                         touch $keep || return 1
586                 done &&
587                 git multi-pack-index repack --batch-size=0 &&
588                 ls .git/objects/pack/*idx >idx-list &&
589                 test_line_count = 5 idx-list &&
590                 test-tool read-midx .git/objects | grep idx >midx-list &&
591                 test_line_count = 5 midx-list &&
592                 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
593                 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
594                 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
595                 ls .git/objects/pack/*idx >idx-list &&
596                 test_line_count = 5 idx-list &&
597                 test-tool read-midx .git/objects | grep idx >midx-list &&
598                 test_line_count = 5 midx-list
599         )
600 '
601
602 test_expect_success 'repack creates a new pack' '
603         (
604                 cd dup &&
605                 ls .git/objects/pack/*idx >idx-list &&
606                 test_line_count = 5 idx-list &&
607                 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
608                 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
609                 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
610                 ls .git/objects/pack/*idx >idx-list &&
611                 test_line_count = 6 idx-list &&
612                 test-tool read-midx .git/objects | grep idx >midx-list &&
613                 test_line_count = 6 midx-list
614         )
615 '
616
617 test_expect_success 'expire removes repacked packs' '
618         (
619                 cd dup &&
620                 ls -al .git/objects/pack/*pack &&
621                 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
622                 git multi-pack-index expire &&
623                 ls -S .git/objects/pack/*pack >actual &&
624                 test_cmp expect actual &&
625                 test-tool read-midx .git/objects | grep idx >midx-list &&
626                 test_line_count = 4 midx-list
627         )
628 '
629
630 test_expect_success 'expire works when adding new packs' '
631         (
632                 cd dup &&
633                 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
634                 refs/heads/A
635                 ^refs/heads/B
636                 EOF
637                 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
638                 refs/heads/B
639                 ^refs/heads/C
640                 EOF
641                 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
642                 refs/heads/C
643                 ^refs/heads/D
644                 EOF
645                 git multi-pack-index write &&
646                 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
647                 refs/heads/D
648                 ^refs/heads/E
649                 EOF
650                 git multi-pack-index write &&
651                 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
652                 refs/heads/E
653                 EOF
654                 git multi-pack-index expire &&
655                 ls .git/objects/pack/ | grep idx >expect &&
656                 test-tool read-midx .git/objects | grep idx >actual &&
657                 test_cmp expect actual &&
658                 git multi-pack-index verify
659         )
660 '
661
662 test_expect_success 'expire respects .keep files' '
663         (
664                 cd dup &&
665                 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
666                 refs/heads/A
667                 EOF
668                 git multi-pack-index write &&
669                 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
670                 touch $PACKA.keep &&
671                 git multi-pack-index expire &&
672                 ls -S .git/objects/pack/a-pack* | grep $PACKA >a-pack-files &&
673                 test_line_count = 3 a-pack-files &&
674                 test-tool read-midx .git/objects | grep idx >midx-list &&
675                 test_line_count = 2 midx-list
676         )
677 '
678
679 test_expect_success 'repack --batch-size=0 repacks everything' '
680         cp -r dup dup2 &&
681         (
682                 cd dup &&
683                 rm .git/objects/pack/*.keep &&
684                 ls .git/objects/pack/*idx >idx-list &&
685                 test_line_count = 2 idx-list &&
686                 git multi-pack-index repack --batch-size=0 &&
687                 ls .git/objects/pack/*idx >idx-list &&
688                 test_line_count = 3 idx-list &&
689                 test-tool read-midx .git/objects | grep idx >midx-list &&
690                 test_line_count = 3 midx-list &&
691                 git multi-pack-index expire &&
692                 ls -al .git/objects/pack/*idx >idx-list &&
693                 test_line_count = 1 idx-list &&
694                 git multi-pack-index repack --batch-size=0 &&
695                 ls -al .git/objects/pack/*idx >new-idx-list &&
696                 test_cmp idx-list new-idx-list
697         )
698 '
699
700 test_expect_success 'repack --batch-size=<large> repacks everything' '
701         (
702                 cd dup2 &&
703                 rm .git/objects/pack/*.keep &&
704                 ls .git/objects/pack/*idx >idx-list &&
705                 test_line_count = 2 idx-list &&
706                 git multi-pack-index repack --batch-size=2000000 &&
707                 ls .git/objects/pack/*idx >idx-list &&
708                 test_line_count = 3 idx-list &&
709                 test-tool read-midx .git/objects | grep idx >midx-list &&
710                 test_line_count = 3 midx-list &&
711                 git multi-pack-index expire &&
712                 ls -al .git/objects/pack/*idx >idx-list &&
713                 test_line_count = 1 idx-list
714         )
715 '
716
717 test_done