]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5319-multi-pack-index.sh
test framework: further deprecate test_i18ngrep
[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
d0fd9931 96 generate_objects $i || return 1
2c381335
DS
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
d0fd9931 158 generate_objects $i || return 1
2c381335
DS
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
6fb22ca4
TB
171test_expect_success 'write midx with --stdin-packs' '
172 rm -fr $objdir/pack/multi-pack-index &&
173
174 idx="$(find $objdir/pack -name "test-2-*.idx")" &&
175 basename "$idx" >in &&
176
177 git multi-pack-index write --stdin-packs <in &&
178
179 test-tool read-midx $objdir | grep "\.idx$" >packs &&
180
181 test_cmp packs in
182'
183
184compare_results_with_midx "mixed mode (one pack + extra)"
185
ceb96a16
PS
186test_expect_success 'write with no objects and preferred pack' '
187 test_when_finished "rm -rf empty" &&
188 git init empty &&
189 test_must_fail git -C empty multi-pack-index write \
190 --stdin-packs --preferred-pack=does-not-exist </dev/null 2>err &&
191 cat >expect <<-EOF &&
192 warning: unknown preferred pack: ${SQ}does-not-exist${SQ}
193 error: no pack files to index.
194 EOF
195 test_cmp expect err
196'
197
680cba2c
WB
198test_expect_success 'write progress off for redirected stderr' '
199 git multi-pack-index --object-dir=$objdir write 2>err &&
200 test_line_count = 0 err
201'
202
203test_expect_success 'write force progress on for stderr' '
0394f8d0 204 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --progress 2>err &&
680cba2c
WB
205 test_file_not_empty err
206'
207
208test_expect_success 'write with the --no-progress option' '
0394f8d0 209 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --no-progress 2>err &&
680cba2c
WB
210 test_line_count = 0 err
211'
212
2c381335
DS
213test_expect_success 'add more packs' '
214 for j in $(test_seq 11 20)
215 do
216 generate_objects $j &&
217 commit_and_list_objects &&
d0fd9931 218 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list || return 1
2c381335
DS
219 done
220'
221
c4d25228
DS
222compare_results_with_midx "mixed mode (two packs + extra)"
223
2c381335 224test_expect_success 'write midx with twelve packs' '
c4d25228
DS
225 git multi-pack-index --object-dir=$objdir write &&
226 midx_read_expect 12 74 4 $objdir
662148c4
DS
227'
228
c4d25228
DS
229compare_results_with_midx "twelve packs"
230
426c00e4
TB
231test_expect_success 'multi-pack-index *.rev cleanup with --object-dir' '
232 git init repo &&
233 git clone -s repo alternate &&
234
235 test_when_finished "rm -rf repo alternate" &&
236
237 (
238 cd repo &&
239 test_commit base &&
240 git repack -d
241 ) &&
242
243 ours="alternate/.git/objects/pack/multi-pack-index-123.rev" &&
244 theirs="repo/.git/objects/pack/multi-pack-index-abc.rev" &&
245 touch "$ours" "$theirs" &&
246
247 (
248 cd alternate &&
249 git multi-pack-index --object-dir ../repo/.git/objects write
250 ) &&
251
252 # writing a midx in "repo" should not remove the .rev file in the
253 # alternate
254 test_path_is_file repo/.git/objects/pack/multi-pack-index &&
255 test_path_is_file $ours &&
256 test_path_is_missing $theirs
257'
258
d9607542
DS
259test_expect_success 'warn on improper hash version' '
260 git init --object-format=sha1 sha1 &&
261 (
262 cd sha1 &&
263 git config core.multiPackIndex true &&
264 test_commit 1 &&
265 git repack -a &&
266 git multi-pack-index write &&
267 mv .git/objects/pack/multi-pack-index ../mpi-sha1
268 ) &&
269 git init --object-format=sha256 sha256 &&
270 (
271 cd sha256 &&
272 git config core.multiPackIndex true &&
273 test_commit 1 &&
274 git repack -a &&
275 git multi-pack-index write &&
276 mv .git/objects/pack/multi-pack-index ../mpi-sha256
277 ) &&
278 (
279 cd sha1 &&
280 mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
281 git log -1 2>err &&
282 test_i18ngrep "multi-pack-index hash version 2 does not match version 1" err
283 ) &&
284 (
285 cd sha256 &&
286 mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
287 git log -1 2>err &&
288 test_i18ngrep "multi-pack-index hash version 1 does not match version 2" err
289 )
290'
291
9218c6a4
TB
292test_expect_success 'midx picks objects from preferred pack' '
293 test_when_finished rm -rf preferred.git &&
294 git init --bare preferred.git &&
295 (
296 cd preferred.git &&
297
298 a=$(echo "a" | git hash-object -w --stdin) &&
299 b=$(echo "b" | git hash-object -w --stdin) &&
300 c=$(echo "c" | git hash-object -w --stdin) &&
301
302 # Set up two packs, duplicating the object "B" at different
303 # offsets.
304 #
305 # Note that the "BC" pack (the one we choose as preferred) sorts
306 # lexically after the "AB" pack, meaning that omitting the
307 # --preferred-pack argument would cause this test to fail (since
308 # the MIDX code would select the copy of "b" in the "AB" pack).
309 git pack-objects objects/pack/test-AB <<-EOF &&
310 $a
311 $b
312 EOF
313 bc=$(git pack-objects objects/pack/test-BC <<-EOF
314 $b
315 $c
316 EOF
317 ) &&
318
319 git multi-pack-index --object-dir=objects \
320 write --preferred-pack=test-BC-$bc.idx 2>err &&
321 test_must_be_empty err &&
322
323 test-tool read-midx --show-objects objects >out &&
324
325 ofs=$(git show-index <objects/pack/test-BC-$bc.idx | grep $b |
326 cut -d" " -f1) &&
327 printf "%s %s\tobjects/pack/test-BC-%s.pack\n" \
328 "$b" "$ofs" "$bc" >expect &&
329 grep ^$b out >actual &&
330
331 test_cmp expect actual
332 )
333'
d9607542 334
5d3cd09a
TB
335test_expect_success 'preferred packs must be non-empty' '
336 test_when_finished rm -rf preferred.git &&
337 git init preferred.git &&
338 (
339 cd preferred.git &&
340
341 test_commit base &&
342 git repack -ad &&
343
344 empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
345
346 test_must_fail git multi-pack-index write \
347 --preferred-pack=pack-$empty.pack 2>err &&
348 grep "with no objects" err
349 )
350'
351
56ee7ff1
DS
352test_expect_success 'verify multi-pack-index success' '
353 git multi-pack-index verify --object-dir=$objdir
354'
355
680cba2c
WB
356test_expect_success 'verify progress off for redirected stderr' '
357 git multi-pack-index verify --object-dir=$objdir 2>err &&
358 test_line_count = 0 err
359'
360
361test_expect_success 'verify force progress on for stderr' '
362 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
363 test_file_not_empty err
364'
365
366test_expect_success 'verify with the --no-progress option' '
367 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
368 test_line_count = 0 err
369'
370
53ad0407
DS
371# usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
372corrupt_midx_and_verify() {
373 POS=$1 &&
374 DATA="${2:-\0}" &&
375 OBJDIR=$3 &&
376 GREPSTR="$4" &&
66ec0390
DS
377 COMMAND="$5" &&
378 if test -z "$COMMAND"
379 then
380 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
381 fi &&
53ad0407
DS
382 FILE=$OBJDIR/pack/multi-pack-index &&
383 chmod a+w $FILE &&
384 test_when_finished mv midx-backup $FILE &&
385 cp $FILE midx-backup &&
386 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
66ec0390 387 test_must_fail $COMMAND 2>test_err &&
53ad0407
DS
388 grep -v "^+" test_err >err &&
389 test_i18ngrep "$GREPSTR" err
390}
391
392test_expect_success 'verify bad signature' '
393 corrupt_midx_and_verify 0 "\00" $objdir \
394 "multi-pack-index signature"
395'
396
cc6af73c 397NUM_OBJECTS=74
53ad0407
DS
398MIDX_BYTE_VERSION=4
399MIDX_BYTE_OID_VERSION=5
400MIDX_BYTE_CHUNK_COUNT=6
d3f8e211
DS
401MIDX_HEADER_SIZE=12
402MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
403MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
8e72a3c3
DS
404MIDX_NUM_CHUNKS=5
405MIDX_CHUNK_LOOKUP_WIDTH=12
406MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
407 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
408MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
3c5e65ca 409MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
2f23d3f3 410MIDX_OID_FANOUT_WIDTH=4
3c5e65ca 411MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
55c5648d
DS
412MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
413MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
cc6af73c
DS
414MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
415MIDX_OFFSET_WIDTH=8
416MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
417MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
53ad0407
DS
418
419test_expect_success 'verify bad version' '
420 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
421 "multi-pack-index version"
422'
423
424test_expect_success 'verify bad OID version' '
d9607542 425 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
53ad0407
DS
426 "hash version"
427'
428
429test_expect_success 'verify truncated chunk count' '
430 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
6ab3b8b8 431 "final chunk has non-zero id"
53ad0407
DS
432'
433
434test_expect_success 'verify extended chunk count' '
435 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
6ab3b8b8 436 "terminating chunk id appears earlier than expected"
53ad0407
DS
437'
438
d3f8e211
DS
439test_expect_success 'verify missing required chunk' '
440 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
441 "missing required"
442'
443
444test_expect_success 'verify invalid chunk offset' '
445 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
6ab3b8b8 446 "improper chunk offset(s)"
d3f8e211
DS
447'
448
8e72a3c3
DS
449test_expect_success 'verify packnames out of order' '
450 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
451 "pack names out of order"
452'
453
d4bf1d88
DS
454test_expect_success 'verify packnames out of order' '
455 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
456 "failed to load pack"
457'
458
2f23d3f3
DS
459test_expect_success 'verify oid fanout out of order' '
460 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
461 "oid fanout out of order"
462'
463
55c5648d
DS
464test_expect_success 'verify oid lookup out of order' '
465 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
466 "oid lookup out of order"
467'
468
cc6af73c
DS
469test_expect_success 'verify incorrect pack-int-id' '
470 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
471 "bad pack-int-id"
472'
473
474test_expect_success 'verify incorrect offset' '
235d3cdd 475 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
cc6af73c
DS
476 "incorrect object offset"
477'
478
66ec0390 479test_expect_success 'git-fsck incorrect offset' '
235d3cdd 480 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
66ec0390 481 "incorrect object offset" \
dc557087
GC
482 "git -c core.multiPackIndex=true fsck" &&
483 test_unconfig core.multiPackIndex &&
484 test_must_fail git fsck &&
485 git -c core.multiPackIndex=false fsck
66ec0390
DS
486'
487
39bdd303
TB
488test_expect_success 'git fsck shows MIDX output with --progress' '
489 git fsck --progress 2>err &&
490 grep "Verifying OID order in multi-pack-index" err &&
491 grep "Verifying object offsets" err
492'
493
494test_expect_success 'git fsck suppresses MIDX output with --no-progress' '
495 git fsck --no-progress 2>err &&
496 ! grep "Verifying OID order in multi-pack-index" err &&
497 ! grep "Verifying object offsets" err
498'
499
ec1e28ef
TB
500test_expect_success 'corrupt MIDX is not reused' '
501 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
502 "incorrect object offset" &&
503 git multi-pack-index write 2>err &&
504 test_i18ngrep checksum.mismatch err &&
505 git multi-pack-index verify
506'
507
f89ecf79 508test_expect_success 'verify incorrect checksum' '
152923b1
JK
509 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
510 corrupt_midx_and_verify $pos \
511 "\377\377\377\377\377\377\377\377\377\377" \
512 $objdir "incorrect checksum"
f89ecf79
TB
513'
514
680cba2c 515test_expect_success 'repack progress off for redirected stderr' '
efdd2f0d 516 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
680cba2c
WB
517 test_line_count = 0 err
518'
519
520test_expect_success 'repack force progress on for stderr' '
0394f8d0 521 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --progress 2>err &&
680cba2c
WB
522 test_file_not_empty err
523'
524
525test_expect_success 'repack with the --no-progress option' '
0394f8d0 526 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --no-progress 2>err &&
680cba2c
WB
527 test_line_count = 0 err
528'
529
e08f7bb0 530test_expect_success 'repack removes multi-pack-index when deleting packs' '
525e18c0 531 test_path_is_file $objdir/pack/multi-pack-index &&
e08f7bb0
TB
532 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
533 # multi-pack-index after repacking, but set "core.multiPackIndex" to
534 # true so that "git repack" can read the existing MIDX.
535 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
525e18c0
DS
536 test_path_is_missing $objdir/pack/multi-pack-index
537'
538
e08f7bb0
TB
539test_expect_success 'repack preserves multi-pack-index when creating packs' '
540 git init preserve &&
541 test_when_finished "rm -fr preserve" &&
542 (
543 cd preserve &&
544 packdir=.git/objects/pack &&
545 midx=$packdir/multi-pack-index &&
546
547 test_commit 1 &&
548 pack1=$(git pack-objects --all $packdir/pack) &&
549 touch $packdir/pack-$pack1.keep &&
550 test_commit 2 &&
551 pack2=$(git pack-objects --revs $packdir/pack) &&
552 touch $packdir/pack-$pack2.keep &&
553
554 git multi-pack-index write &&
555 cp $midx $midx.bak &&
556
557 cat >pack-input <<-EOF &&
558 HEAD
559 ^HEAD~1
560 EOF
561 test_commit 3 &&
562 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
563 test_commit 4 &&
564 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
565
566 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
567 ls -la $packdir &&
568 test_path_is_file $packdir/pack-$pack1.pack &&
569 test_path_is_file $packdir/pack-$pack2.pack &&
570 test_path_is_missing $packdir/pack-$pack3.pack &&
571 test_path_is_missing $packdir/pack-$pack4.pack &&
572 test_cmp_bin $midx.bak $midx
573 )
574'
575
525e18c0
DS
576compare_results_with_midx "after repack"
577
e9ab2ed7 578test_expect_success 'multi-pack-index and pack-bitmap' '
e255a5e8
TB
579 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
580 git -c repack.writeBitmaps=true repack -ad &&
e9ab2ed7
DS
581 git multi-pack-index write &&
582 git rev-list --test-bitmap HEAD
583'
584
29e2016b
DS
585test_expect_success 'multi-pack-index and alternates' '
586 git init --bare alt.git &&
587 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
588 echo content1 >file1 &&
589 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
590 git cat-file blob $altblob &&
591 git rev-list --all
592'
593
594compare_results_with_midx "with alternate (local midx)"
595
596test_expect_success 'multi-pack-index in an alternate' '
6a22d521
DS
597 mv .git/objects/pack/* alt.git/objects/pack &&
598 test_commit add_local_objects &&
599 git repack --local &&
600 git multi-pack-index write &&
601 midx_read_expect 1 3 4 $objdir &&
602 git reset --hard HEAD~1 &&
603 rm -f .git/objects/pack/*
29e2016b
DS
604'
605
606compare_results_with_midx "with alternate (remote midx)"
607
662148c4
DS
608# usage: corrupt_data <file> <pos> [<data>]
609corrupt_data () {
610 file=$1
611 pos=$2
612 data="${3:-\0}"
613 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
614}
615
616# Force 64-bit offsets by manipulating the idx file.
617# This makes the IDX file _incorrect_ so be careful to clean up after!
618test_expect_success 'force some 64-bit offsets with pack-objects' '
619 mkdir objects64 &&
620 mkdir objects64/pack &&
621 for i in $(test_seq 1 11)
622 do
d0fd9931 623 generate_objects 11 || return 1
662148c4
DS
624 done &&
625 commit_and_list_objects &&
626 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
627 idx64=objects64/pack/test-64-$pack64.idx &&
628 chmod u+w $idx64 &&
3c5e65ca 629 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
f57a7396
TB
630 # objects64 is not a real repository, but can serve as an alternate
631 # anyway so we can write a MIDX into it
632 git init repo &&
633 test_when_finished "rm -fr repo" &&
634 (
635 cd repo &&
636 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
637 midx64=$(git multi-pack-index --object-dir=../objects64 write)
638 ) &&
662148c4 639 midx_read_expect 1 63 5 objects64 " large-offsets"
2c381335
DS
640'
641
56ee7ff1
DS
642test_expect_success 'verify multi-pack-index with 64-bit offsets' '
643 git multi-pack-index verify --object-dir=objects64
644'
645
cc6af73c
DS
646NUM_OBJECTS=63
647MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
648MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
649MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
650MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
651MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
652
653test_expect_success 'verify incorrect 64-bit offset' '
654 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
655 "incorrect object offset"
656'
657
cff97116
DS
658test_expect_success 'setup expire tests' '
659 mkdir dup &&
660 (
661 cd dup &&
662 git init &&
663 test-tool genrandom "data" 4096 >large_file.txt &&
664 git update-index --add large_file.txt &&
665 for i in $(test_seq 1 20)
666 do
d0fd9931 667 test_commit $i || exit 1
cff97116
DS
668 done &&
669 git branch A HEAD &&
670 git branch B HEAD~8 &&
671 git branch C HEAD~13 &&
672 git branch D HEAD~16 &&
673 git branch E HEAD~18 &&
674 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
675 refs/heads/A
676 ^refs/heads/B
677 EOF
678 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
679 refs/heads/B
680 ^refs/heads/C
681 EOF
682 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
683 refs/heads/C
684 ^refs/heads/D
685 EOF
686 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
687 refs/heads/D
688 ^refs/heads/E
689 EOF
690 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
691 refs/heads/E
692 EOF
2af890bb
DS
693 git multi-pack-index write &&
694 cp -r .git/objects/pack .git/objects/pack-backup
cff97116
DS
695 )
696'
697
698test_expect_success 'expire does not remove any packs' '
699 (
700 cd dup &&
701 ls .git/objects/pack >expect &&
702 git multi-pack-index expire &&
703 ls .git/objects/pack >actual &&
704 test_cmp expect actual
705 )
706'
707
680cba2c
WB
708test_expect_success 'expire progress off for redirected stderr' '
709 (
710 cd dup &&
711 git multi-pack-index expire 2>err &&
712 test_line_count = 0 err
713 )
714'
715
716test_expect_success 'expire force progress on for stderr' '
717 (
718 cd dup &&
0394f8d0 719 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --progress 2>err &&
680cba2c
WB
720 test_file_not_empty err
721 )
722'
723
724test_expect_success 'expire with the --no-progress option' '
725 (
726 cd dup &&
0394f8d0 727 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --no-progress 2>err &&
680cba2c
WB
728 test_line_count = 0 err
729 )
730'
731
19575c7c
DS
732test_expect_success 'expire removes unreferenced packs' '
733 (
734 cd dup &&
735 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
736 refs/heads/A
737 ^refs/heads/C
738 EOF
739 git multi-pack-index write &&
740 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
741 git multi-pack-index expire &&
742 ls .git/objects/pack >actual &&
743 test_cmp expect actual &&
744 ls .git/objects/pack/ | grep idx >expect-idx &&
745 test-tool read-midx .git/objects | grep idx >actual-midx &&
746 test_cmp expect-idx actual-midx &&
747 git multi-pack-index verify &&
748 git fsck
749 )
750'
751
2af890bb
DS
752test_expect_success 'repack with minimum size does not alter existing packs' '
753 (
754 cd dup &&
755 rm -rf .git/objects/pack &&
756 mv .git/objects/pack-backup .git/objects/pack &&
e892a568
DS
757 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
758 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
759 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
760 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
2af890bb 761 ls .git/objects/pack >expect &&
3612c233 762 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
2af890bb
DS
763 git multi-pack-index repack --batch-size=$MINSIZE &&
764 ls .git/objects/pack >actual &&
765 test_cmp expect actual
766 )
767'
768
3ce4ca0a
DS
769test_expect_success 'repack respects repack.packKeptObjects=false' '
770 test_when_finished rm -f dup/.git/objects/pack/*keep &&
771 (
772 cd dup &&
773 ls .git/objects/pack/*idx >idx-list &&
774 test_line_count = 5 idx-list &&
775 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
776 test_line_count = 5 keep-list &&
777 for keep in $(cat keep-list)
778 do
779 touch $keep || return 1
780 done &&
781 git multi-pack-index repack --batch-size=0 &&
782 ls .git/objects/pack/*idx >idx-list &&
783 test_line_count = 5 idx-list &&
784 test-tool read-midx .git/objects | grep idx >midx-list &&
785 test_line_count = 5 midx-list &&
786 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
787 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
788 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
789 ls .git/objects/pack/*idx >idx-list &&
790 test_line_count = 5 idx-list &&
791 test-tool read-midx .git/objects | grep idx >midx-list &&
792 test_line_count = 5 midx-list
793 )
794'
795
ce1e4a10
DS
796test_expect_success 'repack creates a new pack' '
797 (
798 cd dup &&
799 ls .git/objects/pack/*idx >idx-list &&
800 test_line_count = 5 idx-list &&
3612c233 801 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
ce1e4a10
DS
802 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
803 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
804 ls .git/objects/pack/*idx >idx-list &&
805 test_line_count = 6 idx-list &&
806 test-tool read-midx .git/objects | grep idx >midx-list &&
807 test_line_count = 6 midx-list
808 )
809'
810
d9f77214
TB
811test_expect_success 'repack (all) ignores cruft pack' '
812 git init repo &&
813 test_when_finished "rm -fr repo" &&
814 (
815 cd repo &&
816
817 test_commit base &&
818 test_commit --no-tag unreachable &&
819
820 git reset --hard base &&
821 git reflog expire --all --expire=all &&
822 git repack --cruft -d &&
823
824 git multi-pack-index write &&
825
826 find $objdir/pack | sort >before &&
827 git multi-pack-index repack --batch-size=0 &&
828 find $objdir/pack | sort >after &&
829
830 test_cmp before after
831 )
832'
833
b62ad568
TB
834test_expect_success 'repack (--batch-size) ignores cruft pack' '
835 git init repo &&
836 test_when_finished "rm -fr repo" &&
837 (
838 cd repo &&
839
840 test_commit_bulk 5 &&
841 test_commit --no-tag unreachable &&
842
843 git reset --hard HEAD^ &&
844 git reflog expire --all --expire=all &&
845 git repack --cruft -d &&
846
847 test_commit four &&
848
849 find $objdir/pack -type f -name "*.pack" | sort >before &&
850 git repack -d &&
851 find $objdir/pack -type f -name "*.pack" | sort >after &&
852
853 pack="$(comm -13 before after)" &&
854 test_file_size "$pack" >sz &&
855 # Set --batch-size to twice the size of the pack created
856 # in the previous step, since this is enough to
857 # accommodate it and the cruft pack.
858 #
859 # This means that the MIDX machinery *could* combine the
860 # new and cruft packs together.
861 #
862 # We ensure that it does not below.
863 batch="$((($(cat sz) * 2)))" &&
864
865 git multi-pack-index write &&
866
867 find $objdir/pack | sort >before &&
868 git multi-pack-index repack --batch-size=$batch &&
869 find $objdir/pack | sort >after &&
870
871 test_cmp before after
872 )
873'
874
ce1e4a10
DS
875test_expect_success 'expire removes repacked packs' '
876 (
877 cd dup &&
878 ls -al .git/objects/pack/*pack &&
879 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
880 git multi-pack-index expire &&
881 ls -S .git/objects/pack/*pack >actual &&
882 test_cmp expect actual &&
883 test-tool read-midx .git/objects | grep idx >midx-list &&
884 test_line_count = 4 midx-list
885 )
886'
887
d2743315
DS
888test_expect_success 'expire works when adding new packs' '
889 (
890 cd dup &&
891 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
892 refs/heads/A
893 ^refs/heads/B
894 EOF
895 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
896 refs/heads/B
897 ^refs/heads/C
898 EOF
899 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
900 refs/heads/C
901 ^refs/heads/D
902 EOF
903 git multi-pack-index write &&
904 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
905 refs/heads/D
906 ^refs/heads/E
907 EOF
908 git multi-pack-index write &&
909 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
910 refs/heads/E
911 EOF
912 git multi-pack-index expire &&
913 ls .git/objects/pack/ | grep idx >expect &&
914 test-tool read-midx .git/objects | grep idx >actual &&
915 test_cmp expect actual &&
916 git multi-pack-index verify
917 )
918'
919
10bfa3f7
DS
920test_expect_success 'expire respects .keep files' '
921 (
922 cd dup &&
923 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
924 refs/heads/A
925 EOF
926 git multi-pack-index write &&
927 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
928 touch $PACKA.keep &&
929 git multi-pack-index expire &&
35a8a354
TB
930 test_path_is_file $PACKA.idx &&
931 test_path_is_file $PACKA.keep &&
932 test_path_is_file $PACKA.pack &&
10bfa3f7
DS
933 test-tool read-midx .git/objects | grep idx >midx-list &&
934 test_line_count = 2 midx-list
935 )
936'
937
757d4579
TB
938test_expect_success 'expiring unreferenced cruft pack retains pack' '
939 git init repo &&
940 test_when_finished "rm -fr repo" &&
941 (
942 cd repo &&
943
944 test_commit base &&
945 test_commit --no-tag unreachable &&
946 unreachable=$(git rev-parse HEAD) &&
947
948 git reset --hard base &&
949 git reflog expire --all --expire=all &&
950 git repack --cruft -d &&
951 mtimes="$(ls $objdir/pack/pack-*.mtimes)" &&
952
953 echo "base..$unreachable" >in &&
954 pack="$(git pack-objects --revs --delta-base-offset \
955 $objdir/pack/pack <in)" &&
956
957 # Preferring the contents of "$pack" will leave the
958 # cruft pack unreferenced (ie., none of the objects
959 # contained in the cruft pack will have their MIDX copy
960 # selected from the cruft pack).
961 git multi-pack-index write --preferred-pack="pack-$pack.pack" &&
962 git multi-pack-index expire &&
963
964 test_path_is_file "$mtimes"
965 )
966'
967
b526d8cb 968test_expect_success 'repack --batch-size=0 repacks everything' '
1eb22c7d 969 cp -r dup dup2 &&
b526d8cb
DS
970 (
971 cd dup &&
972 rm .git/objects/pack/*.keep &&
973 ls .git/objects/pack/*idx >idx-list &&
974 test_line_count = 2 idx-list &&
975 git multi-pack-index repack --batch-size=0 &&
976 ls .git/objects/pack/*idx >idx-list &&
977 test_line_count = 3 idx-list &&
978 test-tool read-midx .git/objects | grep idx >midx-list &&
979 test_line_count = 3 midx-list &&
980 git multi-pack-index expire &&
981 ls -al .git/objects/pack/*idx >idx-list &&
982 test_line_count = 1 idx-list &&
983 git multi-pack-index repack --batch-size=0 &&
984 ls -al .git/objects/pack/*idx >new-idx-list &&
985 test_cmp idx-list new-idx-list
986 )
987'
10bfa3f7 988
1eb22c7d
DS
989test_expect_success 'repack --batch-size=<large> repacks everything' '
990 (
991 cd dup2 &&
992 rm .git/objects/pack/*.keep &&
993 ls .git/objects/pack/*idx >idx-list &&
994 test_line_count = 2 idx-list &&
995 git multi-pack-index repack --batch-size=2000000 &&
996 ls .git/objects/pack/*idx >idx-list &&
997 test_line_count = 3 idx-list &&
998 test-tool read-midx .git/objects | grep idx >midx-list &&
999 test_line_count = 3 midx-list &&
1000 git multi-pack-index expire &&
1001 ls -al .git/objects/pack/*idx >idx-list &&
1002 test_line_count = 1 idx-list
1003 )
1004'
1005
506ec2fb 1006test_expect_success 'load reverse index when missing .idx, .pack' '
c8a45eb6
TB
1007 git init repo &&
1008 test_when_finished "rm -fr repo" &&
1009 (
1010 cd repo &&
1011
1012 git config core.multiPackIndex true &&
1013
1014 test_commit base &&
1015 git repack -ad &&
1016 git multi-pack-index write &&
1017
1018 git rev-parse HEAD >tip &&
506ec2fb 1019 pack=$(ls .git/objects/pack/pack-*.pack) &&
c8a45eb6
TB
1020 idx=$(ls .git/objects/pack/pack-*.idx) &&
1021
1022 mv $idx $idx.bak &&
506ec2fb
TB
1023 git cat-file --batch-check="%(objectsize:disk)" <tip &&
1024
1025 mv $idx.bak $idx &&
1026
1027 mv $pack $pack.bak &&
c8a45eb6
TB
1028 git cat-file --batch-check="%(objectsize:disk)" <tip
1029 )
1030'
1031
88617d11
TB
1032test_expect_success 'usage shown without sub-command' '
1033 test_expect_code 129 git multi-pack-index 2>err &&
1034 ! test_i18ngrep "unrecognized subcommand" err
1035'
1036
73ff4ad0
TB
1037test_expect_success 'complains when run outside of a repository' '
1038 nongit test_must_fail git multi-pack-index write 2>err &&
1039 grep "not a git repository" err
1040'
1041
6eb095d7
PS
1042test_expect_success 'repack with delta islands' '
1043 git init repo &&
1044 test_when_finished "rm -fr repo" &&
1045 (
1046 cd repo &&
1047
1048 test_commit first &&
1049 git repack &&
1050 test_commit second &&
1051 git repack &&
1052
1053 git multi-pack-index write &&
1054 git -c repack.useDeltaIslands=true multi-pack-index repack
1055 )
1056'
1057
a3407730 1058test_done