use strpbrk(3) to search for characters from a given set
[git/debian.git] / t / t5319-multi-pack-index.sh
blobcd2f87be6afe241a35e7c3b20fac09d10908f633
1 #!/bin/sh
3 test_description='multi-pack-indexes'
4 . ./test-lib.sh
6 objdir=.git/objects
8 midx_read_expect () {
9 NUM_PACKS=$1
10 NUM_OBJECTS=$2
11 NUM_CHUNKS=$3
12 OBJECT_DIR=$4
13 EXTRA_CHUNKS="$5"
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
31 test_expect_success 'write midx with no packs' '
32 test_when_finished rm -f pack/multi-pack-index &&
33 git multi-pack-index --object-dir=. write &&
34 midx_read_expect 0 0 4 .
37 generate_objects () {
38 i=$1
39 iii=$(printf '%03i' $i)
41 test-tool genrandom "bar" 200 &&
42 test-tool genrandom "baz $iii" 50
43 } >wide_delta_$iii &&
45 test-tool genrandom "foo"$i 100 &&
46 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
47 test-tool genrandom "foo"$(( $i + 2 )) 100
48 } >deep_delta_$iii &&
50 echo $iii &&
51 test-tool genrandom "$iii" 8192
52 } >file_$iii &&
53 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
56 commit_and_list_objects () {
58 echo 101 &&
59 test-tool genrandom 100 8192;
60 } >file_101 &&
61 git update-index --add file_101 &&
62 tree=$(git write-tree) &&
63 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
65 echo $tree &&
66 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
67 } >obj-list &&
68 git reset --hard $commit
71 test_expect_success 'create objects' '
72 test_commit initial &&
73 for i in $(test_seq 1 5)
75 generate_objects $i
76 done &&
77 commit_and_list_objects
80 test_expect_success 'write midx with one v1 pack' '
81 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
82 test_when_finished rm $objdir/pack/test-$pack.pack \
83 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
84 git multi-pack-index --object-dir=$objdir write &&
85 midx_read_expect 1 18 4 $objdir
88 midx_git_two_modes () {
89 git -c core.multiPackIndex=false $1 >expect &&
90 git -c core.multiPackIndex=true $1 >actual &&
91 if [ "$2" = "sorted" ]
92 then
93 sort <expect >expect.sorted &&
94 mv expect.sorted expect &&
95 sort <actual >actual.sorted &&
96 mv actual.sorted actual
97 fi &&
98 test_cmp expect actual
101 compare_results_with_midx () {
102 MSG=$1
103 test_expect_success "check normal git operations: $MSG" '
104 midx_git_two_modes "rev-list --objects --all" &&
105 midx_git_two_modes "log --raw" &&
106 midx_git_two_modes "count-objects --verbose" &&
107 midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
108 midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
112 test_expect_success 'write midx with one v2 pack' '
113 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
114 git multi-pack-index --object-dir=$objdir write &&
115 midx_read_expect 1 18 4 $objdir
118 compare_results_with_midx "one v2 pack"
120 test_expect_success 'corrupt idx not opened' '
121 idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
122 mv $objdir/pack/$idx backup-$idx &&
123 test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
125 # This is the minimum size for a sha-1 based .idx; this lets
126 # us pass perfunctory tests, but anything that actually opens and reads
127 # the idx file will complain.
128 test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
130 git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
131 test_must_be_empty err
134 test_expect_success 'add more objects' '
135 for i in $(test_seq 6 10)
137 generate_objects $i
138 done &&
139 commit_and_list_objects
142 test_expect_success 'write midx with two packs' '
143 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
144 git multi-pack-index --object-dir=$objdir write &&
145 midx_read_expect 2 34 4 $objdir
148 compare_results_with_midx "two packs"
150 test_expect_success 'write progress off for redirected stderr' '
151 git multi-pack-index --object-dir=$objdir write 2>err &&
152 test_line_count = 0 err
155 test_expect_success 'write force progress on for stderr' '
156 git multi-pack-index --object-dir=$objdir --progress write 2>err &&
157 test_file_not_empty err
160 test_expect_success 'write with the --no-progress option' '
161 git multi-pack-index --object-dir=$objdir --no-progress write 2>err &&
162 test_line_count = 0 err
165 test_expect_success 'add more packs' '
166 for j in $(test_seq 11 20)
168 generate_objects $j &&
169 commit_and_list_objects &&
170 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
171 done
174 compare_results_with_midx "mixed mode (two packs + extra)"
176 test_expect_success 'write midx with twelve packs' '
177 git multi-pack-index --object-dir=$objdir write &&
178 midx_read_expect 12 74 4 $objdir
181 compare_results_with_midx "twelve packs"
183 test_expect_success 'verify multi-pack-index success' '
184 git multi-pack-index verify --object-dir=$objdir
187 test_expect_success 'verify progress off for redirected stderr' '
188 git multi-pack-index verify --object-dir=$objdir 2>err &&
189 test_line_count = 0 err
192 test_expect_success 'verify force progress on for stderr' '
193 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
194 test_file_not_empty err
197 test_expect_success 'verify with the --no-progress option' '
198 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
199 test_line_count = 0 err
202 # usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
203 corrupt_midx_and_verify() {
204 POS=$1 &&
205 DATA="${2:-\0}" &&
206 OBJDIR=$3 &&
207 GREPSTR="$4" &&
208 COMMAND="$5" &&
209 if test -z "$COMMAND"
210 then
211 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
212 fi &&
213 FILE=$OBJDIR/pack/multi-pack-index &&
214 chmod a+w $FILE &&
215 test_when_finished mv midx-backup $FILE &&
216 cp $FILE midx-backup &&
217 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
218 test_must_fail $COMMAND 2>test_err &&
219 grep -v "^+" test_err >err &&
220 test_i18ngrep "$GREPSTR" err
223 test_expect_success 'verify bad signature' '
224 corrupt_midx_and_verify 0 "\00" $objdir \
225 "multi-pack-index signature"
228 HASH_LEN=20
229 NUM_OBJECTS=74
230 MIDX_BYTE_VERSION=4
231 MIDX_BYTE_OID_VERSION=5
232 MIDX_BYTE_CHUNK_COUNT=6
233 MIDX_HEADER_SIZE=12
234 MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
235 MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
236 MIDX_NUM_CHUNKS=5
237 MIDX_CHUNK_LOOKUP_WIDTH=12
238 MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
239 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
240 MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
241 MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + 652))
242 MIDX_OID_FANOUT_WIDTH=4
243 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + 1))
244 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
245 MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
246 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
247 MIDX_OFFSET_WIDTH=8
248 MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
249 MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
251 test_expect_success 'verify bad version' '
252 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
253 "multi-pack-index version"
256 test_expect_success 'verify bad OID version' '
257 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\02" $objdir \
258 "hash version"
261 test_expect_success 'verify truncated chunk count' '
262 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
263 "missing required"
266 test_expect_success 'verify extended chunk count' '
267 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
268 "terminating multi-pack-index chunk id appears earlier than expected"
271 test_expect_success 'verify missing required chunk' '
272 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
273 "missing required"
276 test_expect_success 'verify invalid chunk offset' '
277 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
278 "invalid chunk offset (too large)"
281 test_expect_success 'verify packnames out of order' '
282 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
283 "pack names out of order"
286 test_expect_success 'verify packnames out of order' '
287 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
288 "failed to load pack"
291 test_expect_success 'verify oid fanout out of order' '
292 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
293 "oid fanout out of order"
296 test_expect_success 'verify oid lookup out of order' '
297 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
298 "oid lookup out of order"
301 test_expect_success 'verify incorrect pack-int-id' '
302 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
303 "bad pack-int-id"
306 test_expect_success 'verify incorrect offset' '
307 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\07" $objdir \
308 "incorrect object offset"
311 test_expect_success 'git-fsck incorrect offset' '
312 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\07" $objdir \
313 "incorrect object offset" \
314 "git -c core.multipackindex=true fsck"
317 test_expect_success 'repack progress off for redirected stderr' '
318 git multi-pack-index --object-dir=$objdir repack 2>err &&
319 test_line_count = 0 err
322 test_expect_success 'repack force progress on for stderr' '
323 git multi-pack-index --object-dir=$objdir --progress repack 2>err &&
324 test_file_not_empty err
327 test_expect_success 'repack with the --no-progress option' '
328 git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&
329 test_line_count = 0 err
332 test_expect_success 'repack removes multi-pack-index' '
333 test_path_is_file $objdir/pack/multi-pack-index &&
334 GIT_TEST_MULTI_PACK_INDEX=0 git repack -adf &&
335 test_path_is_missing $objdir/pack/multi-pack-index
338 compare_results_with_midx "after repack"
340 test_expect_success 'multi-pack-index and pack-bitmap' '
341 git -c repack.writeBitmaps=true repack -ad &&
342 git multi-pack-index write &&
343 git rev-list --test-bitmap HEAD
346 test_expect_success 'multi-pack-index and alternates' '
347 git init --bare alt.git &&
348 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
349 echo content1 >file1 &&
350 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
351 git cat-file blob $altblob &&
352 git rev-list --all
355 compare_results_with_midx "with alternate (local midx)"
357 test_expect_success 'multi-pack-index in an alternate' '
358 mv .git/objects/pack/* alt.git/objects/pack &&
359 test_commit add_local_objects &&
360 git repack --local &&
361 git multi-pack-index write &&
362 midx_read_expect 1 3 4 $objdir &&
363 git reset --hard HEAD~1 &&
364 rm -f .git/objects/pack/*
367 compare_results_with_midx "with alternate (remote midx)"
369 # usage: corrupt_data <file> <pos> [<data>]
370 corrupt_data () {
371 file=$1
372 pos=$2
373 data="${3:-\0}"
374 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
377 # Force 64-bit offsets by manipulating the idx file.
378 # This makes the IDX file _incorrect_ so be careful to clean up after!
379 test_expect_success 'force some 64-bit offsets with pack-objects' '
380 mkdir objects64 &&
381 mkdir objects64/pack &&
382 for i in $(test_seq 1 11)
384 generate_objects 11
385 done &&
386 commit_and_list_objects &&
387 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
388 idx64=objects64/pack/test-64-$pack64.idx &&
389 chmod u+w $idx64 &&
390 corrupt_data $idx64 2999 "\02" &&
391 midx64=$(git multi-pack-index --object-dir=objects64 write) &&
392 midx_read_expect 1 63 5 objects64 " large-offsets"
395 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
396 git multi-pack-index verify --object-dir=objects64
399 NUM_OBJECTS=63
400 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
401 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
402 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
403 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
404 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
406 test_expect_success 'verify incorrect 64-bit offset' '
407 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
408 "incorrect object offset"
411 test_expect_success 'setup expire tests' '
412 mkdir dup &&
414 cd dup &&
415 git init &&
416 test-tool genrandom "data" 4096 >large_file.txt &&
417 git update-index --add large_file.txt &&
418 for i in $(test_seq 1 20)
420 test_commit $i
421 done &&
422 git branch A HEAD &&
423 git branch B HEAD~8 &&
424 git branch C HEAD~13 &&
425 git branch D HEAD~16 &&
426 git branch E HEAD~18 &&
427 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
428 refs/heads/A
429 ^refs/heads/B
431 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
432 refs/heads/B
433 ^refs/heads/C
435 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
436 refs/heads/C
437 ^refs/heads/D
439 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
440 refs/heads/D
441 ^refs/heads/E
443 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
444 refs/heads/E
446 git multi-pack-index write &&
447 cp -r .git/objects/pack .git/objects/pack-backup
451 test_expect_success 'expire does not remove any packs' '
453 cd dup &&
454 ls .git/objects/pack >expect &&
455 git multi-pack-index expire &&
456 ls .git/objects/pack >actual &&
457 test_cmp expect actual
461 test_expect_success 'expire progress off for redirected stderr' '
463 cd dup &&
464 git multi-pack-index expire 2>err &&
465 test_line_count = 0 err
469 test_expect_success 'expire force progress on for stderr' '
471 cd dup &&
472 git multi-pack-index --progress expire 2>err &&
473 test_file_not_empty err
477 test_expect_success 'expire with the --no-progress option' '
479 cd dup &&
480 git multi-pack-index --no-progress expire 2>err &&
481 test_line_count = 0 err
485 test_expect_success 'expire removes unreferenced packs' '
487 cd dup &&
488 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
489 refs/heads/A
490 ^refs/heads/C
492 git multi-pack-index write &&
493 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
494 git multi-pack-index expire &&
495 ls .git/objects/pack >actual &&
496 test_cmp expect actual &&
497 ls .git/objects/pack/ | grep idx >expect-idx &&
498 test-tool read-midx .git/objects | grep idx >actual-midx &&
499 test_cmp expect-idx actual-midx &&
500 git multi-pack-index verify &&
501 git fsck
505 test_expect_success 'repack with minimum size does not alter existing packs' '
507 cd dup &&
508 rm -rf .git/objects/pack &&
509 mv .git/objects/pack-backup .git/objects/pack &&
510 touch -m -t 201901010000 .git/objects/pack/pack-D* &&
511 touch -m -t 201901010001 .git/objects/pack/pack-C* &&
512 touch -m -t 201901010002 .git/objects/pack/pack-B* &&
513 touch -m -t 201901010003 .git/objects/pack/pack-A* &&
514 ls .git/objects/pack >expect &&
515 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
516 git multi-pack-index repack --batch-size=$MINSIZE &&
517 ls .git/objects/pack >actual &&
518 test_cmp expect actual
522 test_expect_success 'repack creates a new pack' '
524 cd dup &&
525 ls .git/objects/pack/*idx >idx-list &&
526 test_line_count = 5 idx-list &&
527 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
528 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
529 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
530 ls .git/objects/pack/*idx >idx-list &&
531 test_line_count = 6 idx-list &&
532 test-tool read-midx .git/objects | grep idx >midx-list &&
533 test_line_count = 6 midx-list
537 test_expect_success 'expire removes repacked packs' '
539 cd dup &&
540 ls -al .git/objects/pack/*pack &&
541 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
542 git multi-pack-index expire &&
543 ls -S .git/objects/pack/*pack >actual &&
544 test_cmp expect actual &&
545 test-tool read-midx .git/objects | grep idx >midx-list &&
546 test_line_count = 4 midx-list
550 test_expect_success 'expire works when adding new packs' '
552 cd dup &&
553 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
554 refs/heads/A
555 ^refs/heads/B
557 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
558 refs/heads/B
559 ^refs/heads/C
561 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
562 refs/heads/C
563 ^refs/heads/D
565 git multi-pack-index write &&
566 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
567 refs/heads/D
568 ^refs/heads/E
570 git multi-pack-index write &&
571 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
572 refs/heads/E
574 git multi-pack-index expire &&
575 ls .git/objects/pack/ | grep idx >expect &&
576 test-tool read-midx .git/objects | grep idx >actual &&
577 test_cmp expect actual &&
578 git multi-pack-index verify
582 test_expect_success 'expire respects .keep files' '
584 cd dup &&
585 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
586 refs/heads/A
588 git multi-pack-index write &&
589 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
590 touch $PACKA.keep &&
591 git multi-pack-index expire &&
592 ls -S .git/objects/pack/a-pack* | grep $PACKA >a-pack-files &&
593 test_line_count = 3 a-pack-files &&
594 test-tool read-midx .git/objects | grep idx >midx-list &&
595 test_line_count = 2 midx-list
599 test_expect_success 'repack --batch-size=0 repacks everything' '
601 cd dup &&
602 rm .git/objects/pack/*.keep &&
603 ls .git/objects/pack/*idx >idx-list &&
604 test_line_count = 2 idx-list &&
605 git multi-pack-index repack --batch-size=0 &&
606 ls .git/objects/pack/*idx >idx-list &&
607 test_line_count = 3 idx-list &&
608 test-tool read-midx .git/objects | grep idx >midx-list &&
609 test_line_count = 3 midx-list &&
610 git multi-pack-index expire &&
611 ls -al .git/objects/pack/*idx >idx-list &&
612 test_line_count = 1 idx-list &&
613 git multi-pack-index repack --batch-size=0 &&
614 ls -al .git/objects/pack/*idx >new-idx-list &&
615 test_cmp idx-list new-idx-list
619 test_done