Merge branch 'maint'
[git/mingw.git] / t / t5302-pack-index.sh
blobfb3a270822c9a4ac2e4f79bfa71c784291859d4f
1 #!/bin/sh
3 # Copyright (c) 2007 Nicolas Pitre
6 test_description='pack index with 64-bit offsets and object CRC'
7 . ./test-lib.sh
9 test_expect_success \
10 'setup' \
11 'rm -rf .git
12 git init &&
13 git config pack.threads 1 &&
14 i=1 &&
15 while test $i -le 100
17 iii=`printf '%03i' $i`
18 test-genrandom "bar" 200 > wide_delta_$iii &&
19 test-genrandom "baz $iii" 50 >> wide_delta_$iii &&
20 test-genrandom "foo"$i 100 > deep_delta_$iii &&
21 test-genrandom "foo"`expr $i + 1` 100 >> deep_delta_$iii &&
22 test-genrandom "foo"`expr $i + 2` 100 >> deep_delta_$iii &&
23 echo $iii >file_$iii &&
24 test-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-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'
37 test_expect_success \
38 '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"'
42 test_expect_success \
43 '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"'
47 test_expect_success \
48 'both packs should be identical' \
49 'cmp "test-1-${pack1}.pack" "test-2-${pack2}.pack"'
51 test_expect_success \
52 'index v1 and index v2 should be different' \
53 '! cmp "test-1-${pack1}.idx" "test-2-${pack2}.idx"'
55 test_expect_success \
56 'index-pack with index version 1' \
57 'git index-pack --index-version=1 -o 1.idx "test-1-${pack1}.pack"'
59 test_expect_success \
60 'index-pack with index version 2' \
61 'git index-pack --index-version=2 -o 2.idx "test-1-${pack1}.pack"'
63 test_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"'
68 test_expect_success \
69 'index v2: force some 64-bit offsets with pack-objects' \
70 'pack3=$(git pack-objects --index-version=2,0x40000 test-3 <obj-list)'
72 if msg=$(git verify-pack -v "test-3-${pack3}.pack" 2>&1) ||
73 ! (echo "$msg" | grep "pack too large .* off_t")
74 then
75 test_set_prereq OFF64_T
76 else
77 say "# skipping tests concerning 64-bit offsets"
80 test_expect_success OFF64_T \
81 'index v2: verify a pack with some 64-bit offsets' \
82 'git verify-pack -v "test-3-${pack3}.pack"'
84 test_expect_success OFF64_T \
85 '64-bit offsets: should be different from previous index v2 results' \
86 '! cmp "test-2-${pack2}.idx" "test-3-${pack3}.idx"'
88 test_expect_success OFF64_T \
89 'index v2: force some 64-bit offsets with index-pack' \
90 'git index-pack --index-version=2,0x40000 -o 3.idx "test-1-${pack1}.pack"'
92 test_expect_success OFF64_T \
93 '64-bit offsets: index-pack result should match pack-objects one' \
94 'cmp "test-3-${pack3}.idx" "3.idx"'
96 # returns the object number for given object in given pack index
97 index_obj_nr()
99 idx_file=$1
100 object_sha1=$2
101 nr=0
102 git show-index < $idx_file |
103 while read offs sha1 extra
105 nr=$(($nr + 1))
106 test "$sha1" = "$object_sha1" || continue
107 echo "$(($nr - 1))"
108 break
109 done
112 # returns the pack offset for given object as found in given pack index
113 index_obj_offset()
115 idx_file=$1
116 object_sha1=$2
117 git show-index < $idx_file | grep $object_sha1 |
118 ( read offs extra && echo "$offs" )
121 test_expect_success \
122 '[index v1] 1) stream pack to repository' \
123 'git index-pack --index-version=1 --stdin < "test-1-${pack1}.pack" &&
124 git prune-packed &&
125 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
126 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
127 cmp "test-1-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"'
129 test_expect_success \
130 '[index v1] 2) create a stealth corruption in a delta base reference' \
131 '# This test assumes file_101 is a delta smaller than 16 bytes.
132 # It should be against file_100 but we substitute its base for file_099
133 sha1_101=`git hash-object file_101` &&
134 sha1_099=`git hash-object file_099` &&
135 offs_101=`index_obj_offset 1.idx $sha1_101` &&
136 nr_099=`index_obj_nr 1.idx $sha1_099` &&
137 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
138 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
139 if=".git/objects/pack/pack-${pack1}.idx" \
140 skip=$((4 + 256 * 4 + $nr_099 * 24)) \
141 bs=1 count=20 conv=notrunc &&
142 git cat-file blob $sha1_101 > file_101_foo1'
144 test_expect_success \
145 '[index v1] 3) corrupted delta happily returned wrong data' \
146 'test -f file_101_foo1 && ! cmp file_101 file_101_foo1'
148 test_expect_success \
149 '[index v1] 4) confirm that the pack is actually corrupted' \
150 'test_must_fail git fsck --full $commit'
152 test_expect_success \
153 '[index v1] 5) pack-objects happily reuses corrupted data' \
154 'pack4=$(git pack-objects test-4 <obj-list) &&
155 test -f "test-4-${pack1}.pack"'
157 test_expect_success \
158 '[index v1] 6) newly created pack is BAD !' \
159 'test_must_fail git verify-pack -v "test-4-${pack1}.pack"'
161 test_expect_success \
162 '[index v2] 1) stream pack to repository' \
163 'rm -f .git/objects/pack/* &&
164 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
165 git prune-packed &&
166 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
167 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
168 cmp "test-2-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"'
170 test_expect_success \
171 '[index v2] 2) create a stealth corruption in a delta base reference' \
172 '# This test assumes file_101 is a delta smaller than 16 bytes.
173 # It should be against file_100 but we substitute its base for file_099
174 sha1_101=`git hash-object file_101` &&
175 sha1_099=`git hash-object file_099` &&
176 offs_101=`index_obj_offset 1.idx $sha1_101` &&
177 nr_099=`index_obj_nr 1.idx $sha1_099` &&
178 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
179 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
180 if=".git/objects/pack/pack-${pack1}.idx" \
181 skip=$((8 + 256 * 4 + $nr_099 * 20)) \
182 bs=1 count=20 conv=notrunc &&
183 git cat-file blob $sha1_101 > file_101_foo2'
185 test_expect_success \
186 '[index v2] 3) corrupted delta happily returned wrong data' \
187 'test -f file_101_foo2 && ! cmp file_101 file_101_foo2'
189 test_expect_success \
190 '[index v2] 4) confirm that the pack is actually corrupted' \
191 'test_must_fail git fsck --full $commit'
193 test_expect_success \
194 '[index v2] 5) pack-objects refuses to reuse corrupted data' \
195 'test_must_fail git pack-objects test-5 <obj-list &&
196 test_must_fail git pack-objects --no-reuse-object test-6 <obj-list'
198 test_expect_success \
199 '[index v2] 6) verify-pack detects CRC mismatch' \
200 'rm -f .git/objects/pack/* &&
201 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
202 git verify-pack ".git/objects/pack/pack-${pack1}.pack" &&
203 obj=`git hash-object file_001` &&
204 nr=`index_obj_nr ".git/objects/pack/pack-${pack1}.idx" $obj` &&
205 chmod +w ".git/objects/pack/pack-${pack1}.idx" &&
206 printf xxxx | dd of=".git/objects/pack/pack-${pack1}.idx" conv=notrunc \
207 bs=1 count=4 seek=$((8 + 256 * 4 + `wc -l <obj-list` * 20 + $nr * 4)) &&
208 ( while read obj
209 do git cat-file -p $obj >/dev/null || exit 1
210 done <obj-list ) &&
211 err=$(test_must_fail git verify-pack \
212 ".git/objects/pack/pack-${pack1}.pack" 2>&1) &&
213 echo "$err" | grep "CRC mismatch"'
215 test_expect_success 'running index-pack in the object store' '
216 rm -f .git/objects/pack/* &&
217 cp test-1-${pack1}.pack .git/objects/pack/pack-${pack1}.pack &&
219 cd .git/objects/pack
220 git index-pack pack-${pack1}.pack
221 ) &&
222 test -f .git/objects/pack/pack-${pack1}.idx
225 test_done