Merge branch 'js/xmerge-marker-eol'
[git] / t / t5300-pack-object.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
5
6 test_description='git pack-object
7
8 '
9 . ./test-lib.sh
10
11 TRASH=$(pwd)
12
13 test_expect_success \
14     'setup' \
15     'rm -f .git/index* &&
16      perl -e "print \"a\" x 4096;" > a &&
17      perl -e "print \"b\" x 4096;" > b &&
18      perl -e "print \"c\" x 4096;" > c &&
19      test-genrandom "seed a" 2097152 > a_big &&
20      test-genrandom "seed b" 2097152 > b_big &&
21      git update-index --add a a_big b b_big c &&
22      cat c >d && echo foo >>d && git update-index --add d &&
23      tree=$(git write-tree) &&
24      commit=$(git commit-tree $tree </dev/null) && {
25          echo $tree &&
26          echo $commit &&
27          git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\)       .*/\\1/"
28      } >obj-list && {
29          git diff-tree --root -p $commit &&
30          while read object
31          do
32             t=$(git cat-file -t $object) &&
33             git cat-file $t $object || return 1
34          done <obj-list
35      } >expect'
36
37 test_expect_success \
38     'pack without delta' \
39     'packname_1=$(git pack-objects --window=0 test-1 <obj-list)'
40
41 test_expect_success \
42     'pack-objects with bogus arguments' \
43     'test_must_fail git pack-objects --window=0 test-1 blah blah <obj-list'
44
45 rm -fr .git2
46 mkdir .git2
47
48 test_expect_success \
49     'unpack without delta' \
50     "GIT_OBJECT_DIRECTORY=.git2/objects &&
51      export GIT_OBJECT_DIRECTORY &&
52      git init &&
53      git unpack-objects -n <test-1-${packname_1}.pack &&
54      git unpack-objects <test-1-${packname_1}.pack"
55
56 unset GIT_OBJECT_DIRECTORY
57 cd "$TRASH/.git2"
58
59 test_expect_success \
60     'check unpack without delta' \
61     '(cd ../.git && find objects -type f -print) |
62      while read path
63      do
64          cmp $path ../.git/$path || {
65              echo $path differs.
66              return 1
67          }
68      done'
69 cd "$TRASH"
70
71 test_expect_success \
72     'pack with REF_DELTA' \
73     'pwd &&
74      packname_2=$(git pack-objects test-2 <obj-list)'
75
76 rm -fr .git2
77 mkdir .git2
78
79 test_expect_success \
80     'unpack with REF_DELTA' \
81     'GIT_OBJECT_DIRECTORY=.git2/objects &&
82      export GIT_OBJECT_DIRECTORY &&
83      git init &&
84      git unpack-objects -n <test-2-${packname_2}.pack &&
85      git unpack-objects <test-2-${packname_2}.pack'
86
87 unset GIT_OBJECT_DIRECTORY
88 cd "$TRASH/.git2"
89 test_expect_success \
90     'check unpack with REF_DELTA' \
91     '(cd ../.git && find objects -type f -print) |
92      while read path
93      do
94          cmp $path ../.git/$path || {
95              echo $path differs.
96              return 1
97          }
98      done'
99 cd "$TRASH"
100
101 test_expect_success \
102     'pack with OFS_DELTA' \
103     'pwd &&
104      packname_3=$(git pack-objects --delta-base-offset test-3 <obj-list)'
105
106 rm -fr .git2
107 mkdir .git2
108
109 test_expect_success \
110     'unpack with OFS_DELTA' \
111     'GIT_OBJECT_DIRECTORY=.git2/objects &&
112      export GIT_OBJECT_DIRECTORY &&
113      git init &&
114      git unpack-objects -n <test-3-${packname_3}.pack &&
115      git unpack-objects <test-3-${packname_3}.pack'
116
117 unset GIT_OBJECT_DIRECTORY
118 cd "$TRASH/.git2"
119 test_expect_success \
120     'check unpack with OFS_DELTA' \
121     '(cd ../.git && find objects -type f -print) |
122      while read path
123      do
124          cmp $path ../.git/$path || {
125              echo $path differs.
126              return 1
127          }
128      done'
129 cd "$TRASH"
130
131 test_expect_success 'compare delta flavors' '
132         perl -e '\''
133                 defined($_ = -s $_) or die for @ARGV;
134                 exit 1 if $ARGV[0] <= $ARGV[1];
135         '\'' test-2-$packname_2.pack test-3-$packname_3.pack
136 '
137
138 rm -fr .git2
139 mkdir .git2
140
141 test_expect_success \
142     'use packed objects' \
143     'GIT_OBJECT_DIRECTORY=.git2/objects &&
144      export GIT_OBJECT_DIRECTORY &&
145      git init &&
146      cp test-1-${packname_1}.pack test-1-${packname_1}.idx .git2/objects/pack && {
147          git diff-tree --root -p $commit &&
148          while read object
149          do
150             t=$(git cat-file -t $object) &&
151             git cat-file $t $object || return 1
152          done <obj-list
153     } >current &&
154     cmp expect current'
155
156 test_expect_success \
157     'use packed deltified (REF_DELTA) objects' \
158     'GIT_OBJECT_DIRECTORY=.git2/objects &&
159      export GIT_OBJECT_DIRECTORY &&
160      rm -f .git2/objects/pack/test-* &&
161      cp test-2-${packname_2}.pack test-2-${packname_2}.idx .git2/objects/pack && {
162          git diff-tree --root -p $commit &&
163          while read object
164          do
165             t=$(git cat-file -t $object) &&
166             git cat-file $t $object || return 1
167          done <obj-list
168     } >current &&
169     cmp expect current'
170
171 test_expect_success \
172     'use packed deltified (OFS_DELTA) objects' \
173     'GIT_OBJECT_DIRECTORY=.git2/objects &&
174      export GIT_OBJECT_DIRECTORY &&
175      rm -f .git2/objects/pack/test-* &&
176      cp test-3-${packname_3}.pack test-3-${packname_3}.idx .git2/objects/pack && {
177          git diff-tree --root -p $commit &&
178          while read object
179          do
180             t=$(git cat-file -t $object) &&
181             git cat-file $t $object || return 1
182          done <obj-list
183     } >current &&
184     cmp expect current'
185
186 unset GIT_OBJECT_DIRECTORY
187
188 test_expect_success 'survive missing objects/pack directory' '
189         (
190                 rm -fr missing-pack &&
191                 mkdir missing-pack &&
192                 cd missing-pack &&
193                 git init &&
194                 GOP=.git/objects/pack
195                 rm -fr $GOP &&
196                 git index-pack --stdin --keep=test <../test-3-${packname_3}.pack &&
197                 test -f $GOP/pack-${packname_3}.pack &&
198                 cmp $GOP/pack-${packname_3}.pack ../test-3-${packname_3}.pack &&
199                 test -f $GOP/pack-${packname_3}.idx &&
200                 cmp $GOP/pack-${packname_3}.idx ../test-3-${packname_3}.idx &&
201                 test -f $GOP/pack-${packname_3}.keep
202         )
203 '
204
205 test_expect_success \
206     'verify pack' \
207     'git verify-pack    test-1-${packname_1}.idx \
208                         test-2-${packname_2}.idx \
209                         test-3-${packname_3}.idx'
210
211 test_expect_success \
212     'verify pack -v' \
213     'git verify-pack -v test-1-${packname_1}.idx \
214                         test-2-${packname_2}.idx \
215                         test-3-${packname_3}.idx'
216
217 test_expect_success \
218     'verify-pack catches mismatched .idx and .pack files' \
219     'cat test-1-${packname_1}.idx >test-3.idx &&
220      cat test-2-${packname_2}.pack >test-3.pack &&
221      if git verify-pack test-3.idx
222      then false
223      else :;
224      fi'
225
226 test_expect_success \
227     'verify-pack catches a corrupted pack signature' \
228     'cat test-1-${packname_1}.pack >test-3.pack &&
229      echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=2 &&
230      if git verify-pack test-3.idx
231      then false
232      else :;
233      fi'
234
235 test_expect_success \
236     'verify-pack catches a corrupted pack version' \
237     'cat test-1-${packname_1}.pack >test-3.pack &&
238      echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=7 &&
239      if git verify-pack test-3.idx
240      then false
241      else :;
242      fi'
243
244 test_expect_success \
245     'verify-pack catches a corrupted type/size of the 1st packed object data' \
246     'cat test-1-${packname_1}.pack >test-3.pack &&
247      echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=12 &&
248      if git verify-pack test-3.idx
249      then false
250      else :;
251      fi'
252
253 test_expect_success \
254     'verify-pack catches a corrupted sum of the index file itself' \
255     'l=$(wc -c <test-3.idx) &&
256      l=$(expr $l - 20) &&
257      cat test-1-${packname_1}.pack >test-3.pack &&
258      printf "%20s" "" | dd of=test-3.idx count=20 bs=1 conv=notrunc seek=$l &&
259      if git verify-pack test-3.pack
260      then false
261      else :;
262      fi'
263
264 test_expect_success \
265     'build pack index for an existing pack' \
266     'cat test-1-${packname_1}.pack >test-3.pack &&
267      git index-pack -o tmp.idx test-3.pack &&
268      cmp tmp.idx test-1-${packname_1}.idx &&
269
270      git index-pack test-3.pack &&
271      cmp test-3.idx test-1-${packname_1}.idx &&
272
273      cat test-2-${packname_2}.pack >test-3.pack &&
274      git index-pack -o tmp.idx test-2-${packname_2}.pack &&
275      cmp tmp.idx test-2-${packname_2}.idx &&
276
277      git index-pack test-3.pack &&
278      cmp test-3.idx test-2-${packname_2}.idx &&
279
280      cat test-3-${packname_3}.pack >test-3.pack &&
281      git index-pack -o tmp.idx test-3-${packname_3}.pack &&
282      cmp tmp.idx test-3-${packname_3}.idx &&
283
284      git index-pack test-3.pack &&
285      cmp test-3.idx test-3-${packname_3}.idx &&
286
287      :'
288
289 test_expect_success 'unpacking with --strict' '
290
291         for j in a b c d e f g
292         do
293                 for i in 0 1 2 3 4 5 6 7 8 9
294                 do
295                         o=$(echo $j$i | git hash-object -w --stdin) &&
296                         echo "100644 $o 0 $j$i"
297                 done
298         done >LIST &&
299         rm -f .git/index &&
300         git update-index --index-info <LIST &&
301         LIST=$(git write-tree) &&
302         rm -f .git/index &&
303         head -n 10 LIST | git update-index --index-info &&
304         LI=$(git write-tree) &&
305         rm -f .git/index &&
306         tail -n 10 LIST | git update-index --index-info &&
307         ST=$(git write-tree) &&
308         PACK5=$( git rev-list --objects "$LIST" "$LI" "$ST" | \
309                 git pack-objects test-5 ) &&
310         PACK6=$( (
311                         echo "$LIST"
312                         echo "$LI"
313                         echo "$ST"
314                  ) | git pack-objects test-6 ) &&
315         test_create_repo test-5 &&
316         (
317                 cd test-5 &&
318                 git unpack-objects --strict <../test-5-$PACK5.pack &&
319                 git ls-tree -r $LIST &&
320                 git ls-tree -r $LI &&
321                 git ls-tree -r $ST
322         ) &&
323         test_create_repo test-6 &&
324         (
325                 # tree-only into empty repo -- many unreachables
326                 cd test-6 &&
327                 test_must_fail git unpack-objects --strict <../test-6-$PACK6.pack
328         ) &&
329         (
330                 # already populated -- no unreachables
331                 cd test-5 &&
332                 git unpack-objects --strict <../test-6-$PACK6.pack
333         )
334 '
335
336 test_expect_success 'index-pack with --strict' '
337
338         for j in a b c d e f g
339         do
340                 for i in 0 1 2 3 4 5 6 7 8 9
341                 do
342                         o=$(echo $j$i | git hash-object -w --stdin) &&
343                         echo "100644 $o 0 $j$i"
344                 done
345         done >LIST &&
346         rm -f .git/index &&
347         git update-index --index-info <LIST &&
348         LIST=$(git write-tree) &&
349         rm -f .git/index &&
350         head -n 10 LIST | git update-index --index-info &&
351         LI=$(git write-tree) &&
352         rm -f .git/index &&
353         tail -n 10 LIST | git update-index --index-info &&
354         ST=$(git write-tree) &&
355         PACK5=$( git rev-list --objects "$LIST" "$LI" "$ST" | \
356                 git pack-objects test-5 ) &&
357         PACK6=$( (
358                         echo "$LIST"
359                         echo "$LI"
360                         echo "$ST"
361                  ) | git pack-objects test-6 ) &&
362         test_create_repo test-7 &&
363         (
364                 cd test-7 &&
365                 git index-pack --strict --stdin <../test-5-$PACK5.pack &&
366                 git ls-tree -r $LIST &&
367                 git ls-tree -r $LI &&
368                 git ls-tree -r $ST
369         ) &&
370         test_create_repo test-8 &&
371         (
372                 # tree-only into empty repo -- many unreachables
373                 cd test-8 &&
374                 test_must_fail git index-pack --strict --stdin <../test-6-$PACK6.pack
375         ) &&
376         (
377                 # already populated -- no unreachables
378                 cd test-7 &&
379                 git index-pack --strict --stdin <../test-6-$PACK6.pack
380         )
381 '
382
383 test_expect_success 'honor pack.packSizeLimit' '
384         git config pack.packSizeLimit 3m &&
385         packname_10=$(git pack-objects test-10 <obj-list) &&
386         test 2 = $(ls test-10-*.pack | wc -l)
387 '
388
389 test_expect_success 'verify resulting packs' '
390         git verify-pack test-10-*.pack
391 '
392
393 test_expect_success 'tolerate packsizelimit smaller than biggest object' '
394         git config pack.packSizeLimit 1 &&
395         packname_11=$(git pack-objects test-11 <obj-list) &&
396         test 5 = $(ls test-11-*.pack | wc -l)
397 '
398
399 test_expect_success 'verify resulting packs' '
400         git verify-pack test-11-*.pack
401 '
402
403 #
404 # WARNING!
405 #
406 # The following test is destructive.  Please keep the next
407 # two tests at the end of this file.
408 #
409
410 test_expect_success \
411     'fake a SHA1 hash collision' \
412     'test -f    .git/objects/c8/2de19312b6c3695c0c18f70709a6c535682a67 &&
413      cp -f      .git/objects/9d/235ed07cd19811a6ceb342de82f190e49c9f68 \
414                 .git/objects/c8/2de19312b6c3695c0c18f70709a6c535682a67'
415
416 test_expect_success \
417     'make sure index-pack detects the SHA1 collision' \
418     'test_must_fail git index-pack -o bad.idx test-3.pack 2>msg &&
419      test_i18ngrep "SHA1 COLLISION FOUND" msg'
420
421 test_expect_success \
422     'make sure index-pack detects the SHA1 collision (large blobs)' \
423     'test_must_fail git -c core.bigfilethreshold=1 index-pack -o bad.idx test-3.pack 2>msg &&
424      test_i18ngrep "SHA1 COLLISION FOUND" msg'
425
426 test_done