Merge branch 'ps/reftable-stack-tempfile'
[alt-git.git] / t / t5004-archive-corner-cases.sh
blob9f2c6da80e8cd97c12eb5452e940a9022b2e3a84
1 #!/bin/sh
3 test_description='test corner cases of git-archive'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 # the 10knuls.tar file is used to test for an empty git generated tar
9 # without having to invoke tar because an otherwise valid empty GNU tar
10 # will be considered broken by {Open,Net}BSD tar
11 test_expect_success 'create commit with empty tree and fake empty tar' '
12 git commit --allow-empty -m foo &&
13 perl -e "print \"\\0\" x 10240" >10knuls.tar
16 # Make a dir and clean it up afterwards
17 make_dir() {
18 mkdir "$1" &&
19 test_when_finished "rm -rf '$1'"
22 # Check that the dir given in "$1" contains exactly the
23 # set of paths given as arguments.
24 check_dir() {
25 dir=$1; shift
27 echo "$dir" &&
28 for i in "$@"; do
29 echo "$dir/$i"
30 done
31 } | sort >expect &&
32 find "$dir" ! -name pax_global_header -print | sort >actual &&
33 test_cmp expect actual
36 test_lazy_prereq UNZIP_ZIP64_SUPPORT '
37 "$GIT_UNZIP" -v | grep ZIP64_SUPPORT
40 # bsdtar/libarchive versions before 3.1.3 consider a tar file with a
41 # global pax header that is not followed by a file record as corrupt.
42 if "$TAR" tf "$TEST_DIRECTORY"/t5004/empty-with-pax-header.tar >/dev/null 2>&1
43 then
44 test_set_prereq HEADER_ONLY_TAR_OK
47 test_expect_success HEADER_ONLY_TAR_OK 'tar archive of commit with empty tree' '
48 git archive --format=tar HEAD >empty-with-pax-header.tar &&
49 make_dir extract &&
50 "$TAR" xf empty-with-pax-header.tar -C extract &&
51 check_dir extract
54 test_expect_success 'tar archive of empty tree is empty' '
55 git archive --format=tar HEAD: >empty.tar &&
56 test_cmp_bin 10knuls.tar empty.tar
59 test_expect_success 'tar archive of empty tree with prefix' '
60 git archive --format=tar --prefix=foo/ HEAD >prefix.tar &&
61 make_dir extract &&
62 "$TAR" xf prefix.tar -C extract &&
63 check_dir extract foo
66 test_expect_success UNZIP 'zip archive of empty tree is empty' '
67 # Detect the exit code produced when our particular flavor of unzip
68 # sees an empty archive. Infozip will generate a warning and exit with
69 # code 1. But in the name of sanity, we do not expect other unzip
70 # implementations to do the same thing (it would be perfectly
71 # reasonable to exit 0, for example).
73 # This makes our test less rigorous on some platforms (unzip may not
74 # handle the empty repo at all, making our later check of its exit code
75 # a no-op). But we cannot do anything reasonable except skip the test
76 # on such platforms anyway, and this is the moral equivalent.
78 "$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/empty.zip
79 expect_code=$?
80 } &&
82 git archive --format=zip HEAD >empty.zip &&
83 make_dir extract &&
85 cd extract &&
86 test_expect_code $expect_code "$GIT_UNZIP" ../empty.zip
87 ) &&
88 check_dir extract
91 test_expect_success UNZIP 'zip archive of empty tree with prefix' '
92 # We do not have to play exit-code tricks here, because our
93 # result should not be empty; it has a directory in it.
94 git archive --format=zip --prefix=foo/ HEAD >prefix.zip &&
95 make_dir extract &&
97 cd extract &&
98 "$GIT_UNZIP" ../prefix.zip
99 ) &&
100 check_dir extract foo
103 test_expect_success 'archive complains about pathspec on empty tree' '
104 test_must_fail git archive --format=tar HEAD -- foo >/dev/null
107 test_expect_success 'create a commit with an empty subtree' '
108 empty_tree=$(git hash-object -t tree /dev/null) &&
109 root_tree=$(printf "040000 tree $empty_tree\tsub\n" | git mktree)
112 test_expect_success 'archive empty subtree with no pathspec' '
113 git archive --format=tar $root_tree >subtree-all.tar &&
114 test_cmp_bin 10knuls.tar subtree-all.tar
117 test_expect_success 'archive empty subtree by direct pathspec' '
118 git archive --format=tar $root_tree -- sub >subtree-path.tar &&
119 test_cmp_bin 10knuls.tar subtree-path.tar
122 ZIPINFO=zipinfo
124 test_lazy_prereq ZIPINFO '
125 n=$("$ZIPINFO" "$TEST_DIRECTORY"/t5004/empty.zip | sed -n "2s/.* //p")
126 test "x$n" = "x0"
129 test_expect_success ZIPINFO 'zip archive with many entries' '
130 # add a directory with 256 files
131 mkdir 00 &&
132 for a in 0 1 2 3 4 5 6 7 8 9 a b c d e f
134 for b in 0 1 2 3 4 5 6 7 8 9 a b c d e f
136 : >00/$a$b || return 1
137 done
138 done &&
139 git add 00 &&
140 git commit -m "256 files in 1 directory" &&
142 # duplicate it to get 65536 files in 256 directories
143 subtree=$(git write-tree --prefix=00/) &&
144 for c in 0 1 2 3 4 5 6 7 8 9 a b c d e f
146 for d in 0 1 2 3 4 5 6 7 8 9 a b c d e f
148 echo "040000 tree $subtree $c$d" || return 1
149 done
150 done >tree &&
151 tree=$(git mktree <tree) &&
153 # zip them
154 git archive -o many.zip $tree &&
156 # check the number of entries in the ZIP file directory
157 expr 65536 + 256 >expect &&
158 "$ZIPINFO" -h many.zip >zipinfo &&
159 sed -n "2s/.* //p" <zipinfo >actual &&
160 test_cmp expect actual
163 test_expect_success EXPENSIVE,UNZIP,UNZIP_ZIP64_SUPPORT \
164 'zip archive bigger than 4GB' '
165 # build string containing 65536 characters
166 s=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef &&
167 s=$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s &&
168 s=$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s &&
170 # create blob with a length of 65536 + 1 bytes
171 blob=$(echo $s | git hash-object -w --stdin) &&
173 # create tree containing 65500 entries of that blob
174 for i in $(test_seq 1 65500)
176 echo "100644 blob $blob $i" || return 1
177 done >tree &&
178 tree=$(git mktree <tree) &&
180 # zip it, creating an archive a bit bigger than 4GB
181 git archive -0 -o many-big.zip $tree &&
183 "$GIT_UNZIP" -t many-big.zip 9999 65500 &&
184 "$GIT_UNZIP" -t many-big.zip
187 test_expect_success EXPENSIVE,LONG_IS_64BIT,UNZIP,UNZIP_ZIP64_SUPPORT,ZIPINFO \
188 'zip archive with files bigger than 4GB' '
189 # Pack created with:
190 # dd if=/dev/zero of=file bs=1M count=4100 && git hash-object -w file
191 mkdir -p .git/objects/pack &&
193 cd .git/objects/pack &&
194 "$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/big-pack.zip
195 ) &&
196 blob=754a93d6fada4c6873360e6cb4b209132271ab0e &&
197 size=$(expr 4100 "*" 1024 "*" 1024) &&
199 # create a tree containing the file
200 tree=$(echo "100644 blob $blob big-file" | git mktree) &&
202 # zip it, creating an archive with a file bigger than 4GB
203 git archive -o big.zip $tree &&
205 "$GIT_UNZIP" -t big.zip &&
206 "$ZIPINFO" big.zip >big.lst &&
207 grep $size big.lst
210 build_tree() {
211 perl -e '
212 my $hash = $ARGV[0];
213 foreach my $order (2..6) {
214 $first = 10 ** $order;
215 foreach my $i (-13..-9) {
216 my $name = "a" x ($first + $i);
217 print "100644 blob $hash\t$name\n"
220 ' "$1"
223 test_expect_success 'tar archive with long paths' '
224 blob=$(echo foo | git hash-object -w --stdin) &&
225 tree=$(build_tree $blob | git mktree) &&
226 git archive -o long_paths.tar $tree
229 test_done