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