]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5302-pack-index.sh
Merge branch 'es/add-doc-list-short-form-of-all-in-synopsis'
[thirdparty/git.git] / t / t5302-pack-index.sh
CommitLineData
6e541776
NP
1#!/bin/sh
2#
3# Copyright (c) 2007 Nicolas Pitre
4#
5
6test_description='pack index with 64-bit offsets and object CRC'
b2e5d75d
ÆAB
7
8TEST_PASSES_SANITIZE_LEAK=true
6e541776
NP
9. ./test-lib.sh
10
796d1383 11test_expect_success 'setup' '
1610dda8 12 rawsz=$(test_oid rawsz) &&
13 rm -rf .git &&
14 git init &&
15 git config pack.threads 1 &&
16 i=1 &&
17 while test $i -le 100
18 do
74d2f569 19 iii=$(printf "%03i" $i) &&
1610dda8 20 test-tool genrandom "bar" 200 > wide_delta_$iii &&
21 test-tool genrandom "baz $iii" 50 >> wide_delta_$iii &&
22 test-tool genrandom "foo"$i 100 > deep_delta_$iii &&
23 test-tool genrandom "foo"$(expr $i + 1) 100 >> deep_delta_$iii &&
24 test-tool genrandom "foo"$(expr $i + 2) 100 >> deep_delta_$iii &&
25 echo $iii >file_$iii &&
26 test-tool genrandom "$iii" 8192 >>file_$iii &&
27 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii &&
28 i=$(expr $i + 1) || return 1
29 done &&
30 { echo 101 && test-tool genrandom 100 8192; } >file_101 &&
31 git update-index --add file_101 &&
32 tree=$(git write-tree) &&
33 commit=$(git commit-tree $tree </dev/null) && {
34 echo $tree &&
35 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
36 } >obj-list &&
37 git update-ref HEAD $commit
796d1383 38'
6e541776 39
1610dda8 40test_expect_success 'pack-objects with index version 1' '
41 pack1=$(git pack-objects --index-version=1 test-1 <obj-list) &&
42 git verify-pack -v "test-1-${pack1}.pack"
43'
6e541776 44
1610dda8 45test_expect_success 'pack-objects with index version 2' '
46 pack2=$(git pack-objects --index-version=2 test-2 <obj-list) &&
47 git verify-pack -v "test-2-${pack2}.pack"
48'
6e541776 49
1610dda8 50test_expect_success 'both packs should be identical' '
51 cmp "test-1-${pack1}.pack" "test-2-${pack2}.pack"
52'
6e541776 53
1610dda8 54test_expect_success 'index v1 and index v2 should be different' '
55 ! cmp "test-1-${pack1}.idx" "test-2-${pack2}.idx"
56'
6e541776 57
1610dda8 58test_expect_success 'index-pack with index version 1' '
59 git index-pack --index-version=1 -o 1.idx "test-1-${pack1}.pack"
60'
6e541776 61
1610dda8 62test_expect_success 'index-pack with index version 2' '
63 git index-pack --index-version=2 -o 2.idx "test-1-${pack1}.pack"
64'
6e541776 65
1610dda8 66test_expect_success 'index-pack results should match pack-objects ones' '
67 cmp "test-1-${pack1}.idx" "1.idx" &&
68 cmp "test-2-${pack2}.idx" "2.idx"
69'
6e541776 70
e337a04d
JH
71test_expect_success 'index-pack --verify on index version 1' '
72 git index-pack --verify "test-1-${pack1}.pack"
73'
74
75test_expect_success 'index-pack --verify on index version 2' '
76 git index-pack --verify "test-2-${pack2}.pack"
77'
78
1610dda8 79test_expect_success 'pack-objects --index-version=2, is not accepted' '
80 test_must_fail git pack-objects --index-version=2, test-3 <obj-list
81'
6a301345 82
1610dda8 83test_expect_success 'index v2: force some 64-bit offsets with pack-objects' '
84 pack3=$(git pack-objects --index-version=2,0x40000 test-3 <obj-list)
85'
8ed2fca4 86
8ed2fca4 87if msg=$(git verify-pack -v "test-3-${pack3}.pack" 2>&1) ||
bbf08124 88 ! (echo "$msg" | grep "pack too large .* off_t")
8ed2fca4 89then
18bf8798 90 test_set_prereq OFF64_T
8ed2fca4 91else
fadb5156 92 say "# skipping tests concerning 64-bit offsets"
8ed2fca4
JS
93fi
94
1610dda8 95test_expect_success OFF64_T 'index v2: verify a pack with some 64-bit offsets' '
96 git verify-pack -v "test-3-${pack3}.pack"
97'
6e541776 98
1610dda8 99test_expect_success OFF64_T '64-bit offsets: should be different from previous index v2 results' '
100 ! cmp "test-2-${pack2}.idx" "test-3-${pack3}.idx"
101'
6e541776 102
1610dda8 103test_expect_success OFF64_T 'index v2: force some 64-bit offsets with index-pack' '
104 git index-pack --index-version=2,0x40000 -o 3.idx "test-1-${pack1}.pack"
105'
6e541776 106
1610dda8 107test_expect_success OFF64_T '64-bit offsets: index-pack result should match pack-objects one' '
108 cmp "test-3-${pack3}.idx" "3.idx"
109'
6e541776 110
e337a04d
JH
111test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2 (cheat)' '
112 # This cheats by knowing which lower offset should still be encoded
113 # in 64-bit representation.
114 git index-pack --verify --index-version=2,0x40000 "test-3-${pack3}.pack"
115'
116
3c9fc074 117test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2' '
e337a04d
JH
118 git index-pack --verify "test-3-${pack3}.pack"
119'
120
2b5c208f
NP
121# returns the object number for given object in given pack index
122index_obj_nr()
123{
1610dda8 124 idx_file=$1
125 object_sha1=$2
126 nr=0
127 git show-index < $idx_file |
128 while read offs sha1 extra
129 do
130 nr=$(($nr + 1))
131 test "$sha1" = "$object_sha1" || continue
132 echo "$(($nr - 1))"
133 break
134 done
2b5c208f
NP
135}
136
137# returns the pack offset for given object as found in given pack index
138index_obj_offset()
139{
1610dda8 140 idx_file=$1
141 object_sha1=$2
142 git show-index < $idx_file | grep $object_sha1 |
143 ( read offs extra && echo "$offs" )
2b5c208f
NP
144}
145
1610dda8 146test_expect_success '[index v1] 1) stream pack to repository' '
147 git index-pack --index-version=1 --stdin < "test-1-${pack1}.pack" &&
148 git prune-packed &&
149 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
150 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
151 cmp "test-1-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"
152'
6e541776
NP
153
154test_expect_success \
1610dda8 155 '[index v1] 2) create a stealth corruption in a delta base reference' '
156 # This test assumes file_101 is a delta smaller than 16 bytes.
157 # It should be against file_100 but we substitute its base for file_099
158 sha1_101=$(git hash-object file_101) &&
159 sha1_099=$(git hash-object file_099) &&
160 offs_101=$(index_obj_offset 1.idx $sha1_101) &&
161 nr_099=$(index_obj_nr 1.idx $sha1_099) &&
162 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
163 recordsz=$((rawsz + 4)) &&
164 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
165 if=".git/objects/pack/pack-${pack1}.idx" \
166 skip=$((4 + 256 * 4 + $nr_099 * recordsz)) \
167 bs=1 count=$rawsz conv=notrunc &&
168 git cat-file blob $sha1_101 > file_101_foo1
169'
6e541776 170
41ac414e 171test_expect_success \
1610dda8 172 '[index v1] 3) corrupted delta happily returned wrong data' '
173 test -f file_101_foo1 && ! cmp file_101 file_101_foo1
174'
6e541776 175
41ac414e 176test_expect_success \
1610dda8 177 '[index v1] 4) confirm that the pack is actually corrupted' '
178 test_must_fail git fsck --full $commit
179'
6e541776
NP
180
181test_expect_success \
1610dda8 182 '[index v1] 5) pack-objects happily reuses corrupted data' '
183 pack4=$(git pack-objects test-4 <obj-list) &&
184 test -f "test-4-${pack4}.pack"
185'
6e541776 186
1610dda8 187test_expect_success '[index v1] 6) newly created pack is BAD !' '
188 test_must_fail git verify-pack -v "test-4-${pack4}.pack"
189'
6e541776 190
1610dda8 191test_expect_success '[index v2] 1) stream pack to repository' '
192 rm -f .git/objects/pack/* &&
193 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
194 git prune-packed &&
195 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
196 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
197 cmp "test-2-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"
198'
6e541776
NP
199
200test_expect_success \
1610dda8 201 '[index v2] 2) create a stealth corruption in a delta base reference' '
202 # This test assumes file_101 is a delta smaller than 16 bytes.
203 # It should be against file_100 but we substitute its base for file_099
204 sha1_101=$(git hash-object file_101) &&
205 sha1_099=$(git hash-object file_099) &&
206 offs_101=$(index_obj_offset 1.idx $sha1_101) &&
207 nr_099=$(index_obj_nr 1.idx $sha1_099) &&
208 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
209 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
210 if=".git/objects/pack/pack-${pack1}.idx" \
211 skip=$((8 + 256 * 4 + $nr_099 * rawsz)) \
212 bs=1 count=$rawsz conv=notrunc &&
213 git cat-file blob $sha1_101 > file_101_foo2
214'
6e541776 215
41ac414e 216test_expect_success \
1610dda8 217 '[index v2] 3) corrupted delta happily returned wrong data' '
218 test -f file_101_foo2 && ! cmp file_101 file_101_foo2
219'
6e541776 220
41ac414e 221test_expect_success \
1610dda8 222 '[index v2] 4) confirm that the pack is actually corrupted' '
223 test_must_fail git fsck --full $commit
224'
6e541776 225
41ac414e 226test_expect_success \
1610dda8 227 '[index v2] 5) pack-objects refuses to reuse corrupted data' '
228 test_must_fail git pack-objects test-5 <obj-list &&
229 test_must_fail git pack-objects --no-reuse-object test-6 <obj-list
230'
6e541776 231
85fe23ed 232test_expect_success \
1610dda8 233 '[index v2] 6) verify-pack detects CRC mismatch' '
234 rm -f .git/objects/pack/* &&
235 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
236 git verify-pack ".git/objects/pack/pack-${pack1}.pack" &&
237 obj=$(git hash-object file_001) &&
238 nr=$(index_obj_nr ".git/objects/pack/pack-${pack1}.idx" $obj) &&
239 chmod +w ".git/objects/pack/pack-${pack1}.idx" &&
240 printf xxxx | dd of=".git/objects/pack/pack-${pack1}.idx" conv=notrunc \
241 bs=1 count=4 seek=$((8 + 256 * 4 + $(wc -l <obj-list) * rawsz + $nr * 4)) &&
242 ( while read obj
243 do git cat-file -p $obj >/dev/null || exit 1
244 done <obj-list ) &&
245 test_must_fail git verify-pack ".git/objects/pack/pack-${pack1}.pack"
3de89c9d 246'
85fe23ed 247
a672ea6a 248test_expect_success 'running index-pack in the object store' '
1610dda8 249 rm -f .git/objects/pack/* &&
250 cp test-1-${pack1}.pack .git/objects/pack/pack-${pack1}.pack &&
251 (
252 cd .git/objects/pack &&
253 git index-pack pack-${pack1}.pack
254 ) &&
255 test -f .git/objects/pack/pack-${pack1}.idx
a672ea6a
NP
256'
257
f99b7af6 258test_expect_success 'index-pack --strict warns upon missing tagger in tag' '
1610dda8 259 sha=$(git rev-parse HEAD) &&
260 cat >wrong-tag <<EOF &&
f99b7af6
JS
261object $sha
262type commit
263tag guten tag
264
265This is an invalid tag.
266EOF
267
34959d80 268 tag=$(git hash-object -t tag -w --stdin --literally <wrong-tag) &&
1610dda8 269 pack1=$(echo $tag $sha | git pack-objects tag-test) &&
270 echo remove tag object &&
271 thirtyeight=${tag#??} &&
272 rm -f .git/objects/${tag%$thirtyeight}/$thirtyeight &&
273 git index-pack --strict tag-test-${pack1}.pack 2>err &&
274 grep "^warning:.* expected .tagger. line" err
f99b7af6
JS
275'
276
ffb2c0fe 277test_expect_success 'index-pack --fsck-objects also warns upon missing tagger in tag' '
1610dda8 278 git index-pack --fsck-objects tag-test-${pack1}.pack 2>err &&
279 grep "^warning:.* expected .tagger. line" err
ffb2c0fe
JT
280'
281
cea69151
JK
282test_expect_success 'index-pack -v --stdin produces progress for both phases' '
283 pack=$(git pack-objects --all pack </dev/null) &&
284 GIT_PROGRESS_DELAY=0 git index-pack -v --stdin <pack-$pack.pack 2>err &&
6789275d
JH
285 test_grep "Receiving objects" err &&
286 test_grep "Resolving deltas" err
cea69151
JK
287'
288
0cf5fbc2
MC
289test_expect_success 'too-large packs report the breach' '
290 pack=$(git pack-objects --all pack </dev/null) &&
291 sz="$(test_file_size pack-$pack.pack)" &&
292 test "$sz" -gt 20 &&
293 test_must_fail git index-pack --max-input-size=20 pack-$pack.pack 2>err &&
294 grep "maximum allowed size (20 bytes)" err
295'
296
6e541776 297test_done