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