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