]> git.ipfire.org Git - thirdparty/git.git/blob - t/t5319-multi-pack-index.sh
help: make sure local html page exists before calling external processes
[thirdparty/git.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 'corrupt MIDX is not reused' '
414 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
415 "incorrect object offset" &&
416 git multi-pack-index write 2>err &&
417 test_i18ngrep checksum.mismatch err &&
418 git multi-pack-index verify
419 '
420
421 test_expect_success 'verify incorrect checksum' '
422 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 1)) &&
423 corrupt_midx_and_verify $pos "\377" $objdir "incorrect checksum"
424 '
425
426 test_expect_success 'repack progress off for redirected stderr' '
427 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
428 test_line_count = 0 err
429 '
430
431 test_expect_success 'repack force progress on for stderr' '
432 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress repack 2>err &&
433 test_file_not_empty err
434 '
435
436 test_expect_success 'repack with the --no-progress option' '
437 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&
438 test_line_count = 0 err
439 '
440
441 test_expect_success 'repack removes multi-pack-index when deleting packs' '
442 test_path_is_file $objdir/pack/multi-pack-index &&
443 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
444 # multi-pack-index after repacking, but set "core.multiPackIndex" to
445 # true so that "git repack" can read the existing MIDX.
446 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
447 test_path_is_missing $objdir/pack/multi-pack-index
448 '
449
450 test_expect_success 'repack preserves multi-pack-index when creating packs' '
451 git init preserve &&
452 test_when_finished "rm -fr preserve" &&
453 (
454 cd preserve &&
455 packdir=.git/objects/pack &&
456 midx=$packdir/multi-pack-index &&
457
458 test_commit 1 &&
459 pack1=$(git pack-objects --all $packdir/pack) &&
460 touch $packdir/pack-$pack1.keep &&
461 test_commit 2 &&
462 pack2=$(git pack-objects --revs $packdir/pack) &&
463 touch $packdir/pack-$pack2.keep &&
464
465 git multi-pack-index write &&
466 cp $midx $midx.bak &&
467
468 cat >pack-input <<-EOF &&
469 HEAD
470 ^HEAD~1
471 EOF
472 test_commit 3 &&
473 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
474 test_commit 4 &&
475 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
476
477 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
478 ls -la $packdir &&
479 test_path_is_file $packdir/pack-$pack1.pack &&
480 test_path_is_file $packdir/pack-$pack2.pack &&
481 test_path_is_missing $packdir/pack-$pack3.pack &&
482 test_path_is_missing $packdir/pack-$pack4.pack &&
483 test_cmp_bin $midx.bak $midx
484 )
485 '
486
487 compare_results_with_midx "after repack"
488
489 test_expect_success 'multi-pack-index and pack-bitmap' '
490 git -c repack.writeBitmaps=true repack -ad &&
491 git multi-pack-index write &&
492 git rev-list --test-bitmap HEAD
493 '
494
495 test_expect_success 'multi-pack-index and alternates' '
496 git init --bare alt.git &&
497 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
498 echo content1 >file1 &&
499 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
500 git cat-file blob $altblob &&
501 git rev-list --all
502 '
503
504 compare_results_with_midx "with alternate (local midx)"
505
506 test_expect_success 'multi-pack-index in an alternate' '
507 mv .git/objects/pack/* alt.git/objects/pack &&
508 test_commit add_local_objects &&
509 git repack --local &&
510 git multi-pack-index write &&
511 midx_read_expect 1 3 4 $objdir &&
512 git reset --hard HEAD~1 &&
513 rm -f .git/objects/pack/*
514 '
515
516 compare_results_with_midx "with alternate (remote midx)"
517
518 # usage: corrupt_data <file> <pos> [<data>]
519 corrupt_data () {
520 file=$1
521 pos=$2
522 data="${3:-\0}"
523 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
524 }
525
526 # Force 64-bit offsets by manipulating the idx file.
527 # This makes the IDX file _incorrect_ so be careful to clean up after!
528 test_expect_success 'force some 64-bit offsets with pack-objects' '
529 mkdir objects64 &&
530 mkdir objects64/pack &&
531 for i in $(test_seq 1 11)
532 do
533 generate_objects 11
534 done &&
535 commit_and_list_objects &&
536 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
537 idx64=objects64/pack/test-64-$pack64.idx &&
538 chmod u+w $idx64 &&
539 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
540 midx64=$(git multi-pack-index --object-dir=objects64 write) &&
541 midx_read_expect 1 63 5 objects64 " large-offsets"
542 '
543
544 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
545 git multi-pack-index verify --object-dir=objects64
546 '
547
548 NUM_OBJECTS=63
549 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
550 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
551 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
552 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
553 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
554
555 test_expect_success 'verify incorrect 64-bit offset' '
556 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
557 "incorrect object offset"
558 '
559
560 test_expect_success 'setup expire tests' '
561 mkdir dup &&
562 (
563 cd dup &&
564 git init &&
565 test-tool genrandom "data" 4096 >large_file.txt &&
566 git update-index --add large_file.txt &&
567 for i in $(test_seq 1 20)
568 do
569 test_commit $i
570 done &&
571 git branch A HEAD &&
572 git branch B HEAD~8 &&
573 git branch C HEAD~13 &&
574 git branch D HEAD~16 &&
575 git branch E HEAD~18 &&
576 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
577 refs/heads/A
578 ^refs/heads/B
579 EOF
580 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
581 refs/heads/B
582 ^refs/heads/C
583 EOF
584 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
585 refs/heads/C
586 ^refs/heads/D
587 EOF
588 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
589 refs/heads/D
590 ^refs/heads/E
591 EOF
592 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
593 refs/heads/E
594 EOF
595 git multi-pack-index write &&
596 cp -r .git/objects/pack .git/objects/pack-backup
597 )
598 '
599
600 test_expect_success 'expire does not remove any packs' '
601 (
602 cd dup &&
603 ls .git/objects/pack >expect &&
604 git multi-pack-index expire &&
605 ls .git/objects/pack >actual &&
606 test_cmp expect actual
607 )
608 '
609
610 test_expect_success 'expire progress off for redirected stderr' '
611 (
612 cd dup &&
613 git multi-pack-index expire 2>err &&
614 test_line_count = 0 err
615 )
616 '
617
618 test_expect_success 'expire force progress on for stderr' '
619 (
620 cd dup &&
621 GIT_PROGRESS_DELAY=0 git multi-pack-index --progress expire 2>err &&
622 test_file_not_empty err
623 )
624 '
625
626 test_expect_success 'expire with the --no-progress option' '
627 (
628 cd dup &&
629 GIT_PROGRESS_DELAY=0 git multi-pack-index --no-progress expire 2>err &&
630 test_line_count = 0 err
631 )
632 '
633
634 test_expect_success 'expire removes unreferenced packs' '
635 (
636 cd dup &&
637 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
638 refs/heads/A
639 ^refs/heads/C
640 EOF
641 git multi-pack-index write &&
642 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
643 git multi-pack-index expire &&
644 ls .git/objects/pack >actual &&
645 test_cmp expect actual &&
646 ls .git/objects/pack/ | grep idx >expect-idx &&
647 test-tool read-midx .git/objects | grep idx >actual-midx &&
648 test_cmp expect-idx actual-midx &&
649 git multi-pack-index verify &&
650 git fsck
651 )
652 '
653
654 test_expect_success 'repack with minimum size does not alter existing packs' '
655 (
656 cd dup &&
657 rm -rf .git/objects/pack &&
658 mv .git/objects/pack-backup .git/objects/pack &&
659 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
660 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
661 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
662 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
663 ls .git/objects/pack >expect &&
664 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
665 git multi-pack-index repack --batch-size=$MINSIZE &&
666 ls .git/objects/pack >actual &&
667 test_cmp expect actual
668 )
669 '
670
671 test_expect_success 'repack respects repack.packKeptObjects=false' '
672 test_when_finished rm -f dup/.git/objects/pack/*keep &&
673 (
674 cd dup &&
675 ls .git/objects/pack/*idx >idx-list &&
676 test_line_count = 5 idx-list &&
677 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
678 test_line_count = 5 keep-list &&
679 for keep in $(cat keep-list)
680 do
681 touch $keep || return 1
682 done &&
683 git multi-pack-index repack --batch-size=0 &&
684 ls .git/objects/pack/*idx >idx-list &&
685 test_line_count = 5 idx-list &&
686 test-tool read-midx .git/objects | grep idx >midx-list &&
687 test_line_count = 5 midx-list &&
688 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
689 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
690 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
691 ls .git/objects/pack/*idx >idx-list &&
692 test_line_count = 5 idx-list &&
693 test-tool read-midx .git/objects | grep idx >midx-list &&
694 test_line_count = 5 midx-list
695 )
696 '
697
698 test_expect_success 'repack creates a new pack' '
699 (
700 cd dup &&
701 ls .git/objects/pack/*idx >idx-list &&
702 test_line_count = 5 idx-list &&
703 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
704 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
705 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
706 ls .git/objects/pack/*idx >idx-list &&
707 test_line_count = 6 idx-list &&
708 test-tool read-midx .git/objects | grep idx >midx-list &&
709 test_line_count = 6 midx-list
710 )
711 '
712
713 test_expect_success 'expire removes repacked packs' '
714 (
715 cd dup &&
716 ls -al .git/objects/pack/*pack &&
717 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
718 git multi-pack-index expire &&
719 ls -S .git/objects/pack/*pack >actual &&
720 test_cmp expect actual &&
721 test-tool read-midx .git/objects | grep idx >midx-list &&
722 test_line_count = 4 midx-list
723 )
724 '
725
726 test_expect_success 'expire works when adding new packs' '
727 (
728 cd dup &&
729 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
730 refs/heads/A
731 ^refs/heads/B
732 EOF
733 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
734 refs/heads/B
735 ^refs/heads/C
736 EOF
737 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
738 refs/heads/C
739 ^refs/heads/D
740 EOF
741 git multi-pack-index write &&
742 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
743 refs/heads/D
744 ^refs/heads/E
745 EOF
746 git multi-pack-index write &&
747 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
748 refs/heads/E
749 EOF
750 git multi-pack-index expire &&
751 ls .git/objects/pack/ | grep idx >expect &&
752 test-tool read-midx .git/objects | grep idx >actual &&
753 test_cmp expect actual &&
754 git multi-pack-index verify
755 )
756 '
757
758 test_expect_success 'expire respects .keep files' '
759 (
760 cd dup &&
761 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
762 refs/heads/A
763 EOF
764 git multi-pack-index write &&
765 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
766 touch $PACKA.keep &&
767 git multi-pack-index expire &&
768 test_path_is_file $PACKA.idx &&
769 test_path_is_file $PACKA.keep &&
770 test_path_is_file $PACKA.pack &&
771 test-tool read-midx .git/objects | grep idx >midx-list &&
772 test_line_count = 2 midx-list
773 )
774 '
775
776 test_expect_success 'repack --batch-size=0 repacks everything' '
777 cp -r dup dup2 &&
778 (
779 cd dup &&
780 rm .git/objects/pack/*.keep &&
781 ls .git/objects/pack/*idx >idx-list &&
782 test_line_count = 2 idx-list &&
783 git multi-pack-index repack --batch-size=0 &&
784 ls .git/objects/pack/*idx >idx-list &&
785 test_line_count = 3 idx-list &&
786 test-tool read-midx .git/objects | grep idx >midx-list &&
787 test_line_count = 3 midx-list &&
788 git multi-pack-index expire &&
789 ls -al .git/objects/pack/*idx >idx-list &&
790 test_line_count = 1 idx-list &&
791 git multi-pack-index repack --batch-size=0 &&
792 ls -al .git/objects/pack/*idx >new-idx-list &&
793 test_cmp idx-list new-idx-list
794 )
795 '
796
797 test_expect_success 'repack --batch-size=<large> repacks everything' '
798 (
799 cd dup2 &&
800 rm .git/objects/pack/*.keep &&
801 ls .git/objects/pack/*idx >idx-list &&
802 test_line_count = 2 idx-list &&
803 git multi-pack-index repack --batch-size=2000000 &&
804 ls .git/objects/pack/*idx >idx-list &&
805 test_line_count = 3 idx-list &&
806 test-tool read-midx .git/objects | grep idx >midx-list &&
807 test_line_count = 3 midx-list &&
808 git multi-pack-index expire &&
809 ls -al .git/objects/pack/*idx >idx-list &&
810 test_line_count = 1 idx-list
811 )
812 '
813
814 test_expect_success 'load reverse index when missing .idx, .pack' '
815 git init repo &&
816 test_when_finished "rm -fr repo" &&
817 (
818 cd repo &&
819
820 git config core.multiPackIndex true &&
821
822 test_commit base &&
823 git repack -ad &&
824 git multi-pack-index write &&
825
826 git rev-parse HEAD >tip &&
827 pack=$(ls .git/objects/pack/pack-*.pack) &&
828 idx=$(ls .git/objects/pack/pack-*.idx) &&
829
830 mv $idx $idx.bak &&
831 git cat-file --batch-check="%(objectsize:disk)" <tip &&
832
833 mv $idx.bak $idx &&
834
835 mv $pack $pack.bak &&
836 git cat-file --batch-check="%(objectsize:disk)" <tip
837 )
838 '
839
840 test_expect_success 'usage shown without sub-command' '
841 test_expect_code 129 git multi-pack-index 2>err &&
842 ! test_i18ngrep "unrecognized subcommand" err
843 '
844
845 test_done