]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5319-multi-pack-index.sh
commit-graph: rewrite to use checksum_valid()
[thirdparty/git.git] / t / t5319-multi-pack-index.sh
CommitLineData
a3407730
DS
1#!/bin/sh
2
3test_description='multi-pack-indexes'
4. ./test-lib.sh
5
52fe41ff 6GIT_TEST_MULTI_PACK_INDEX=0
c4d25228
DS
7objdir=.git/objects
8
d9607542
DS
9HASH_LEN=$(test_oid rawsz)
10
4d80560c 11midx_read_expect () {
396f2570 12 NUM_PACKS=$1
d7cacf29 13 NUM_OBJECTS=$2
662148c4
DS
14 NUM_CHUNKS=$3
15 OBJECT_DIR=$4
16 EXTRA_CHUNKS="$5"
3227565c
DS
17 {
18 cat <<-EOF &&
d9607542 19 header: 4d494458 1 $HASH_LEN $NUM_CHUNKS $NUM_PACKS
662148c4 20 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
d7cacf29 21 num_objects: $NUM_OBJECTS
3227565c
DS
22 packs:
23 EOF
24 if test $NUM_PACKS -ge 1
25 then
662148c4 26 ls $OBJECT_DIR/pack/ | grep idx | sort
3227565c 27 fi &&
662148c4 28 printf "object-dir: $OBJECT_DIR\n"
3227565c 29 } >expect &&
662148c4 30 test-tool read-midx $OBJECT_DIR >actual &&
4d80560c
DS
31 test_cmp expect actual
32}
33
3c5e65ca 34test_expect_success 'setup' '
3c5e65ca 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
796d61cd
DR
47test_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
d9607542 52test_expect_success SHA1 'warn if a midx contains no oid' '
796d61cd
DR
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
a3407730
DS
56'
57
2c381335
DS
58generate_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
77commit_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
92test_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
101test_expect_success 'write midx with one v1 pack' '
c4d25228
DS
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
2c381335
DS
107'
108
c4d25228 109midx_git_two_modes () {
b4a14394
JK
110 git -c core.multiPackIndex=false $1 >expect &&
111 git -c core.multiPackIndex=true $1 >actual &&
e9ab2ed7
DS
112 if [ "$2" = "sorted" ]
113 then
b4a14394
JK
114 sort <expect >expect.sorted &&
115 mv expect.sorted expect &&
116 sort <actual >actual.sorted &&
117 mv actual.sorted actual
e9ab2ed7 118 fi &&
c4d25228
DS
119 test_cmp expect actual
120}
121
122compare_results_with_midx () {
123 MSG=$1
124 test_expect_success "check normal git operations: $MSG" '
125 midx_git_two_modes "rev-list --objects --all" &&
e9ab2ed7
DS
126 midx_git_two_modes "log --raw" &&
127 midx_git_two_modes "count-objects --verbose" &&
5670ad98
JK
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
c4d25228
DS
130 '
131}
132
2c381335 133test_expect_success 'write midx with one v2 pack' '
c4d25228
DS
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
2c381335
DS
137'
138
c4d25228
DS
139compare_results_with_midx "one v2 pack"
140
c8a45eb6 141test_expect_success 'corrupt idx reports errors' '
fc789156
JK
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 &&
c8a45eb6 152 grep "index unavailable" err
fc789156
JK
153'
154
2c381335
DS
155test_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
163test_expect_success 'write midx with two packs' '
c4d25228
DS
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
2c381335
DS
167'
168
c4d25228
DS
169compare_results_with_midx "two packs"
170
680cba2c
WB
171test_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
176test_expect_success 'write force progress on for stderr' '
efdd2f0d 177 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress write 2>err &&
680cba2c
WB
178 test_file_not_empty err
179'
180
181test_expect_success 'write with the --no-progress option' '
efdd2f0d 182 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress write 2>err &&
680cba2c
WB
183 test_line_count = 0 err
184'
185
2c381335
DS
186test_expect_success 'add more packs' '
187 for j in $(test_seq 11 20)
188 do
189 generate_objects $j &&
190 commit_and_list_objects &&
c4d25228 191 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
2c381335
DS
192 done
193'
194
c4d25228
DS
195compare_results_with_midx "mixed mode (two packs + extra)"
196
2c381335 197test_expect_success 'write midx with twelve packs' '
c4d25228
DS
198 git multi-pack-index --object-dir=$objdir write &&
199 midx_read_expect 12 74 4 $objdir
662148c4
DS
200'
201
c4d25228
DS
202compare_results_with_midx "twelve packs"
203
d9607542
DS
204test_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
9218c6a4
TB
237test_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'
d9607542 279
56ee7ff1
DS
280test_expect_success 'verify multi-pack-index success' '
281 git multi-pack-index verify --object-dir=$objdir
282'
283
680cba2c
WB
284test_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
289test_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
294test_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
53ad0407
DS
299# usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
300corrupt_midx_and_verify() {
301 POS=$1 &&
302 DATA="${2:-\0}" &&
303 OBJDIR=$3 &&
304 GREPSTR="$4" &&
66ec0390
DS
305 COMMAND="$5" &&
306 if test -z "$COMMAND"
307 then
308 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
309 fi &&
53ad0407
DS
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 &&
66ec0390 315 test_must_fail $COMMAND 2>test_err &&
53ad0407
DS
316 grep -v "^+" test_err >err &&
317 test_i18ngrep "$GREPSTR" err
318}
319
320test_expect_success 'verify bad signature' '
321 corrupt_midx_and_verify 0 "\00" $objdir \
322 "multi-pack-index signature"
323'
324
cc6af73c 325NUM_OBJECTS=74
53ad0407
DS
326MIDX_BYTE_VERSION=4
327MIDX_BYTE_OID_VERSION=5
328MIDX_BYTE_CHUNK_COUNT=6
d3f8e211
DS
329MIDX_HEADER_SIZE=12
330MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
331MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
8e72a3c3
DS
332MIDX_NUM_CHUNKS=5
333MIDX_CHUNK_LOOKUP_WIDTH=12
334MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
335 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
336MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
3c5e65ca 337MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
2f23d3f3 338MIDX_OID_FANOUT_WIDTH=4
3c5e65ca 339MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
55c5648d
DS
340MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
341MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
cc6af73c
DS
342MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
343MIDX_OFFSET_WIDTH=8
344MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
345MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
53ad0407
DS
346
347test_expect_success 'verify bad version' '
348 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
349 "multi-pack-index version"
350'
351
352test_expect_success 'verify bad OID version' '
d9607542 353 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
53ad0407
DS
354 "hash version"
355'
356
357test_expect_success 'verify truncated chunk count' '
358 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
6ab3b8b8 359 "final chunk has non-zero id"
53ad0407
DS
360'
361
362test_expect_success 'verify extended chunk count' '
363 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
6ab3b8b8 364 "terminating chunk id appears earlier than expected"
53ad0407
DS
365'
366
d3f8e211
DS
367test_expect_success 'verify missing required chunk' '
368 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
369 "missing required"
370'
371
372test_expect_success 'verify invalid chunk offset' '
373 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
6ab3b8b8 374 "improper chunk offset(s)"
d3f8e211
DS
375'
376
8e72a3c3
DS
377test_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
d4bf1d88
DS
382test_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
2f23d3f3
DS
387test_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
55c5648d
DS
392test_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
cc6af73c
DS
397test_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
402test_expect_success 'verify incorrect offset' '
235d3cdd 403 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
cc6af73c
DS
404 "incorrect object offset"
405'
406
66ec0390 407test_expect_success 'git-fsck incorrect offset' '
235d3cdd 408 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
66ec0390
DS
409 "incorrect object offset" \
410 "git -c core.multipackindex=true fsck"
411'
412
680cba2c 413test_expect_success 'repack progress off for redirected stderr' '
efdd2f0d 414 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
680cba2c
WB
415 test_line_count = 0 err
416'
417
418test_expect_success 'repack force progress on for stderr' '
efdd2f0d 419 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress repack 2>err &&
680cba2c
WB
420 test_file_not_empty err
421'
422
423test_expect_success 'repack with the --no-progress option' '
efdd2f0d 424 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&
680cba2c
WB
425 test_line_count = 0 err
426'
427
e08f7bb0 428test_expect_success 'repack removes multi-pack-index when deleting packs' '
525e18c0 429 test_path_is_file $objdir/pack/multi-pack-index &&
e08f7bb0
TB
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 &&
525e18c0
DS
434 test_path_is_missing $objdir/pack/multi-pack-index
435'
436
e08f7bb0
TB
437test_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
525e18c0
DS
474compare_results_with_midx "after repack"
475
e9ab2ed7
DS
476test_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
29e2016b
DS
482test_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
491compare_results_with_midx "with alternate (local midx)"
492
493test_expect_success 'multi-pack-index in an alternate' '
6a22d521
DS
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/*
29e2016b
DS
501'
502
503compare_results_with_midx "with alternate (remote midx)"
504
662148c4
DS
505# usage: corrupt_data <file> <pos> [<data>]
506corrupt_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!
515test_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 &&
3c5e65ca 526 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
662148c4
DS
527 midx64=$(git multi-pack-index --object-dir=objects64 write) &&
528 midx_read_expect 1 63 5 objects64 " large-offsets"
2c381335
DS
529'
530
56ee7ff1
DS
531test_expect_success 'verify multi-pack-index with 64-bit offsets' '
532 git multi-pack-index verify --object-dir=objects64
533'
534
cc6af73c
DS
535NUM_OBJECTS=63
536MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
537MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
538MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
539MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
540MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
541
542test_expect_success 'verify incorrect 64-bit offset' '
543 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
544 "incorrect object offset"
545'
546
cff97116
DS
547test_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
2af890bb
DS
582 git multi-pack-index write &&
583 cp -r .git/objects/pack .git/objects/pack-backup
cff97116
DS
584 )
585'
586
587test_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
680cba2c
WB
597test_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
605test_expect_success 'expire force progress on for stderr' '
606 (
607 cd dup &&
efdd2f0d 608 GIT_PROGRESS_DELAY=0 git multi-pack-index --progress expire 2>err &&
680cba2c
WB
609 test_file_not_empty err
610 )
611'
612
613test_expect_success 'expire with the --no-progress option' '
614 (
615 cd dup &&
efdd2f0d 616 GIT_PROGRESS_DELAY=0 git multi-pack-index --no-progress expire 2>err &&
680cba2c
WB
617 test_line_count = 0 err
618 )
619'
620
19575c7c
DS
621test_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
2af890bb
DS
641test_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 &&
e892a568
DS
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* &&
2af890bb 650 ls .git/objects/pack >expect &&
3612c233 651 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
2af890bb
DS
652 git multi-pack-index repack --batch-size=$MINSIZE &&
653 ls .git/objects/pack >actual &&
654 test_cmp expect actual
655 )
656'
657
3ce4ca0a
DS
658test_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
ce1e4a10
DS
685test_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 &&
3612c233 690 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
ce1e4a10
DS
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
700test_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
d2743315
DS
713test_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
10bfa3f7
DS
745test_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 &&
35a8a354
TB
755 test_path_is_file $PACKA.idx &&
756 test_path_is_file $PACKA.keep &&
757 test_path_is_file $PACKA.pack &&
10bfa3f7
DS
758 test-tool read-midx .git/objects | grep idx >midx-list &&
759 test_line_count = 2 midx-list
760 )
761'
762
b526d8cb 763test_expect_success 'repack --batch-size=0 repacks everything' '
1eb22c7d 764 cp -r dup dup2 &&
b526d8cb
DS
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'
10bfa3f7 783
1eb22c7d
DS
784test_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
506ec2fb 801test_expect_success 'load reverse index when missing .idx, .pack' '
c8a45eb6
TB
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 &&
506ec2fb 814 pack=$(ls .git/objects/pack/pack-*.pack) &&
c8a45eb6
TB
815 idx=$(ls .git/objects/pack/pack-*.idx) &&
816
817 mv $idx $idx.bak &&
506ec2fb
TB
818 git cat-file --batch-check="%(objectsize:disk)" <tip &&
819
820 mv $idx.bak $idx &&
821
822 mv $pack $pack.bak &&
c8a45eb6
TB
823 git cat-file --batch-check="%(objectsize:disk)" <tip
824 )
825'
826
a3407730 827test_done