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