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