]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5004-archive-corner-cases.sh
The third batch
[thirdparty/git.git] / t / t5004-archive-corner-cases.sh
CommitLineData
bd54cf17
JK
1#!/bin/sh
2
3test_description='test corner cases of git-archive'
7615cf94
ÆAB
4
5TEST_PASSES_SANITIZE_LEAK=true
bd54cf17
JK
6. ./test-lib.sh
7
b6bdc2a0
CMAB
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
11test_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
bd54cf17
JK
14'
15
16# Make a dir and clean it up afterwards
17make_dir() {
18 mkdir "$1" &&
19 test_when_finished "rm -rf '$1'"
20}
21
22# Check that the dir given in "$1" contains exactly the
23# set of paths given as arguments.
24check_dir() {
25 dir=$1; shift
26 {
27 echo "$dir" &&
28 for i in "$@"; do
29 echo "$dir/$i"
30 done
31 } | sort >expect &&
abdb9b2e 32 find "$dir" ! -name pax_global_header -print | sort >actual &&
bd54cf17
JK
33 test_cmp expect actual
34}
35
867e40ff
RS
36test_lazy_prereq UNZIP_ZIP64_SUPPORT '
37 "$GIT_UNZIP" -v | grep ZIP64_SUPPORT
38'
843fb919 39
56ee9657
RS
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.
42if "$TAR" tf "$TEST_DIRECTORY"/t5004/empty-with-pax-header.tar >/dev/null 2>&1
43then
44 test_set_prereq HEADER_ONLY_TAR_OK
45fi
46
47test_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
52'
53
bd54cf17 54test_expect_success 'tar archive of empty tree is empty' '
24676f02 55 git archive --format=tar HEAD: >empty.tar &&
b93e6e36 56 test_cmp_bin 10knuls.tar empty.tar
bd54cf17
JK
57'
58
59test_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
64'
65
66test_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).
72 #
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.
9ddc5ac9
JK
77 {
78 "$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/empty.zip
79 expect_code=$?
80 } &&
bd54cf17
JK
81
82 git archive --format=zip HEAD >empty.zip &&
83 make_dir extract &&
84 (
85 cd extract &&
86 test_expect_code $expect_code "$GIT_UNZIP" ../empty.zip
87 ) &&
88 check_dir extract
89'
90
91test_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 &&
96 (
97 cd extract &&
98 "$GIT_UNZIP" ../prefix.zip
99 ) &&
100 check_dir extract foo
101'
102
103test_expect_success 'archive complains about pathspec on empty tree' '
104 test_must_fail git archive --format=tar HEAD -- foo >/dev/null
105'
106
107test_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)
110'
111
112test_expect_success 'archive empty subtree with no pathspec' '
113 git archive --format=tar $root_tree >subtree-all.tar &&
b6bdc2a0 114 test_cmp_bin 10knuls.tar subtree-all.tar
bd54cf17
JK
115'
116
117test_expect_success 'archive empty subtree by direct pathspec' '
118 git archive --format=tar $root_tree -- sub >subtree-path.tar &&
b6bdc2a0 119 test_cmp_bin 10knuls.tar subtree-path.tar
bd54cf17
JK
120'
121
19ee2940
RS
122ZIPINFO=zipinfo
123
124test_lazy_prereq ZIPINFO '
125 n=$("$ZIPINFO" "$TEST_DIRECTORY"/t5004/empty.zip | sed -n "2s/.* //p")
126 test "x$n" = "x0"
127'
128
88329ca8 129test_expect_success ZIPINFO 'zip archive with many entries' '
19ee2940
RS
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
133 do
134 for b in 0 1 2 3 4 5 6 7 8 9 a b c d e f
135 do
d0fd9931 136 : >00/$a$b || return 1
19ee2940
RS
137 done
138 done &&
139 git add 00 &&
140 git commit -m "256 files in 1 directory" &&
141
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
145 do
146 for d in 0 1 2 3 4 5 6 7 8 9 a b c d e f
147 do
d0fd9931 148 echo "040000 tree $subtree $c$d" || return 1
19ee2940
RS
149 done
150 done >tree &&
151 tree=$(git mktree <tree) &&
152
153 # zip them
154 git archive -o many.zip $tree &&
155
156 # check the number of entries in the ZIP file directory
157 expr 65536 + 256 >expect &&
60127996
ÆAB
158 "$ZIPINFO" -h many.zip >zipinfo &&
159 sed -n "2s/.* //p" <zipinfo >actual &&
19ee2940
RS
160 test_cmp expect actual
161'
162
867e40ff
RS
163test_expect_success EXPENSIVE,UNZIP,UNZIP_ZIP64_SUPPORT \
164 'zip archive bigger than 4GB' '
758c1f9d
RS
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 &&
169
170 # create blob with a length of 65536 + 1 bytes
171 blob=$(echo $s | git hash-object -w --stdin) &&
172
173 # create tree containing 65500 entries of that blob
174 for i in $(test_seq 1 65500)
175 do
d0fd9931 176 echo "100644 blob $blob $i" || return 1
758c1f9d
RS
177 done >tree &&
178 tree=$(git mktree <tree) &&
179
180 # zip it, creating an archive a bit bigger than 4GB
181 git archive -0 -o many-big.zip $tree &&
182
183 "$GIT_UNZIP" -t many-big.zip 9999 65500 &&
184 "$GIT_UNZIP" -t many-big.zip
185'
186
867e40ff
RS
187test_expect_success EXPENSIVE,LONG_IS_64BIT,UNZIP,UNZIP_ZIP64_SUPPORT,ZIPINFO \
188 'zip archive with files bigger than 4GB' '
758c1f9d
RS
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 &&
192 (
193 cd .git/objects/pack &&
194 "$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/big-pack.zip
195 ) &&
196 blob=754a93d6fada4c6873360e6cb4b209132271ab0e &&
197 size=$(expr 4100 "*" 1024 "*" 1024) &&
198
199 # create a tree containing the file
200 tree=$(echo "100644 blob $blob big-file" | git mktree) &&
201
202 # zip it, creating an archive with a file bigger than 4GB
203 git archive -o big.zip $tree &&
204
205 "$GIT_UNZIP" -t big.zip &&
206 "$ZIPINFO" big.zip >big.lst &&
207 grep $size big.lst
208'
209
4060c199
RS
210build_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"
218 }
219 }
220 ' "$1"
221}
222
82a46af1 223test_expect_success 'tar archive with long paths' '
4060c199
RS
224 blob=$(echo foo | git hash-object -w --stdin) &&
225 tree=$(build_tree $blob | git mktree) &&
71d41ff6 226 git archive -o long_paths.tar $tree
4060c199
RS
227'
228
bd54cf17 229test_done