Merge branch 'ds/trace2-topo-walk'
[git] / t / t5302-pack-index.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Nicolas Pitre
4 #
5
6 test_description='pack index with 64-bit offsets and object CRC'
7 . ./test-lib.sh
8
9 test_expect_success 'setup' '
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
17                 iii=$(printf "%03i" $i)
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
36 '
37
38 test_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 '
42
43 test_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 '
47
48 test_expect_success 'both packs should be identical' '
49         cmp "test-1-${pack1}.pack" "test-2-${pack2}.pack"
50 '
51
52 test_expect_success 'index v1 and index v2 should be different' '
53         ! cmp "test-1-${pack1}.idx" "test-2-${pack2}.idx"
54 '
55
56 test_expect_success 'index-pack with index version 1' '
57         git index-pack --index-version=1 -o 1.idx "test-1-${pack1}.pack"
58 '
59
60 test_expect_success 'index-pack with index version 2' '
61         git index-pack --index-version=2 -o 2.idx "test-1-${pack1}.pack"
62 '
63
64 test_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 '
68
69 test_expect_success 'index-pack --verify on index version 1' '
70         git index-pack --verify "test-1-${pack1}.pack"
71 '
72
73 test_expect_success 'index-pack --verify on index version 2' '
74         git index-pack --verify "test-2-${pack2}.pack"
75 '
76
77 test_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 '
80
81 test_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 '
84
85 if msg=$(git verify-pack -v "test-3-${pack3}.pack" 2>&1) ||
86         ! (echo "$msg" | grep "pack too large .* off_t")
87 then
88         test_set_prereq OFF64_T
89 else
90         say "# skipping tests concerning 64-bit offsets"
91 fi
92
93 test_expect_success OFF64_T 'index v2: verify a pack with some 64-bit offsets' '
94         git verify-pack -v "test-3-${pack3}.pack"
95 '
96
97 test_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 '
100
101 test_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 '
104
105 test_expect_success OFF64_T '64-bit offsets: index-pack result should match pack-objects one' '
106         cmp "test-3-${pack3}.idx" "3.idx"
107 '
108
109 test_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
115 test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2' '
116         git index-pack --verify "test-3-${pack3}.pack"
117 '
118
119 # returns the object number for given object in given pack index
120 index_obj_nr()
121 {
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
133 }
134
135 # returns the pack offset for given object as found in given pack index
136 index_obj_offset()
137 {
138         idx_file=$1
139         object_sha1=$2
140         git show-index < $idx_file | grep $object_sha1 |
141         ( read offs extra && echo "$offs" )
142 }
143
144 test_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 '
151
152 test_expect_success \
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 '
168
169 test_expect_success \
170         '[index v1] 3) corrupted delta happily returned wrong data' '
171         test -f file_101_foo1 && ! cmp file_101 file_101_foo1
172 '
173
174 test_expect_success \
175         '[index v1] 4) confirm that the pack is actually corrupted' '
176         test_must_fail git fsck --full $commit
177 '
178
179 test_expect_success \
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 '
184
185 test_expect_success '[index v1] 6) newly created pack is BAD !' '
186         test_must_fail git verify-pack -v "test-4-${pack4}.pack"
187 '
188
189 test_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 '
197
198 test_expect_success \
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 '
213
214 test_expect_success \
215         '[index v2] 3) corrupted delta happily returned wrong data' '
216         test -f file_101_foo2 && ! cmp file_101 file_101_foo2
217 '
218
219 test_expect_success \
220         '[index v2] 4) confirm that the pack is actually corrupted' '
221         test_must_fail git fsck --full $commit
222 '
223
224 test_expect_success \
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 '
229
230 test_expect_success \
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"
244 '
245
246 test_expect_success 'running index-pack in the object store' '
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
254 '
255
256 test_expect_success 'index-pack --strict warns upon missing tagger in tag' '
257         sha=$(git rev-parse HEAD) &&
258         cat >wrong-tag <<EOF &&
259 object $sha
260 type commit
261 tag guten tag
262
263 This is an invalid tag.
264 EOF
265
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
273 '
274
275 test_expect_success 'index-pack --fsck-objects also warns upon missing tagger in tag' '
276         git index-pack --fsck-objects tag-test-${pack1}.pack 2>err &&
277         grep "^warning:.* expected .tagger. line" err
278 '
279
280 test_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
287 test_done