archive: don't add empty directories to archives
[git] / t / t5004-archive-corner-cases.sh
1 #!/bin/sh
2
3 test_description='test corner cases of git-archive'
4 . ./test-lib.sh
5
6 test_expect_success 'create commit with empty tree' '
7         git commit --allow-empty -m foo
8 '
9
10 # Make a dir and clean it up afterwards
11 make_dir() {
12         mkdir "$1" &&
13         test_when_finished "rm -rf '$1'"
14 }
15
16 # Check that the dir given in "$1" contains exactly the
17 # set of paths given as arguments.
18 check_dir() {
19         dir=$1; shift
20         {
21                 echo "$dir" &&
22                 for i in "$@"; do
23                         echo "$dir/$i"
24                 done
25         } | sort >expect &&
26         find "$dir" ! -name pax_global_header -print | sort >actual &&
27         test_cmp expect actual
28 }
29
30 test_lazy_prereq UNZIP_ZIP64_SUPPORT '
31         "$GIT_UNZIP" -v | grep ZIP64_SUPPORT
32 '
33
34 # bsdtar/libarchive versions before 3.1.3 consider a tar file with a
35 # global pax header that is not followed by a file record as corrupt.
36 if "$TAR" tf "$TEST_DIRECTORY"/t5004/empty-with-pax-header.tar >/dev/null 2>&1
37 then
38         test_set_prereq HEADER_ONLY_TAR_OK
39 fi
40
41 test_expect_success HEADER_ONLY_TAR_OK 'tar archive of commit with empty tree' '
42         git archive --format=tar HEAD >empty-with-pax-header.tar &&
43         make_dir extract &&
44         "$TAR" xf empty-with-pax-header.tar -C extract &&
45         check_dir extract
46 '
47
48 test_expect_success 'tar archive of empty tree is empty' '
49         git archive --format=tar HEAD: >empty.tar &&
50         perl -e "print \"\\0\" x 10240" >10knuls.tar &&
51         test_cmp_bin 10knuls.tar empty.tar
52 '
53
54 test_expect_success 'tar archive of empty tree with prefix' '
55         git archive --format=tar --prefix=foo/ HEAD >prefix.tar &&
56         make_dir extract &&
57         "$TAR" xf prefix.tar -C extract &&
58         check_dir extract foo
59 '
60
61 test_expect_success UNZIP 'zip archive of empty tree is empty' '
62         # Detect the exit code produced when our particular flavor of unzip
63         # sees an empty archive. Infozip will generate a warning and exit with
64         # code 1. But in the name of sanity, we do not expect other unzip
65         # implementations to do the same thing (it would be perfectly
66         # reasonable to exit 0, for example).
67         #
68         # This makes our test less rigorous on some platforms (unzip may not
69         # handle the empty repo at all, making our later check of its exit code
70         # a no-op). But we cannot do anything reasonable except skip the test
71         # on such platforms anyway, and this is the moral equivalent.
72         {
73                 "$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/empty.zip
74                 expect_code=$?
75         } &&
76
77         git archive --format=zip HEAD >empty.zip &&
78         make_dir extract &&
79         (
80                 cd extract &&
81                 test_expect_code $expect_code "$GIT_UNZIP" ../empty.zip
82         ) &&
83         check_dir extract
84 '
85
86 test_expect_success UNZIP 'zip archive of empty tree with prefix' '
87         # We do not have to play exit-code tricks here, because our
88         # result should not be empty; it has a directory in it.
89         git archive --format=zip --prefix=foo/ HEAD >prefix.zip &&
90         make_dir extract &&
91         (
92                 cd extract &&
93                 "$GIT_UNZIP" ../prefix.zip
94         ) &&
95         check_dir extract foo
96 '
97
98 test_expect_success 'archive complains about pathspec on empty tree' '
99         test_must_fail git archive --format=tar HEAD -- foo >/dev/null
100 '
101
102 test_expect_success 'create a commit with an empty subtree' '
103         empty_tree=$(git hash-object -t tree /dev/null) &&
104         root_tree=$(printf "040000 tree $empty_tree\tsub\n" | git mktree)
105 '
106
107 test_expect_success 'archive empty subtree with no pathspec' '
108         git archive --format=tar $root_tree >subtree-all.tar &&
109         make_dir extract &&
110         "$TAR" xf subtree-all.tar -C extract &&
111         check_dir extract
112 '
113
114 test_expect_success 'archive empty subtree by direct pathspec' '
115         git archive --format=tar $root_tree -- sub >subtree-path.tar &&
116         make_dir extract &&
117         "$TAR" xf subtree-path.tar -C extract &&
118         check_dir extract
119 '
120
121 ZIPINFO=zipinfo
122
123 test_lazy_prereq ZIPINFO '
124         n=$("$ZIPINFO" "$TEST_DIRECTORY"/t5004/empty.zip | sed -n "2s/.* //p")
125         test "x$n" = "x0"
126 '
127
128 test_expect_success ZIPINFO 'zip archive with many entries' '
129         # add a directory with 256 files
130         mkdir 00 &&
131         for a in 0 1 2 3 4 5 6 7 8 9 a b c d e f
132         do
133                 for b in 0 1 2 3 4 5 6 7 8 9 a b c d e f
134                 do
135                         : >00/$a$b
136                 done
137         done &&
138         git add 00 &&
139         git commit -m "256 files in 1 directory" &&
140
141         # duplicate it to get 65536 files in 256 directories
142         subtree=$(git write-tree --prefix=00/) &&
143         for c in 0 1 2 3 4 5 6 7 8 9 a b c d e f
144         do
145                 for d in 0 1 2 3 4 5 6 7 8 9 a b c d e f
146                 do
147                         echo "040000 tree $subtree      $c$d"
148                 done
149         done >tree &&
150         tree=$(git mktree <tree) &&
151
152         # zip them
153         git archive -o many.zip $tree &&
154
155         # check the number of entries in the ZIP file directory
156         expr 65536 + 256 >expect &&
157         "$ZIPINFO" many.zip | head -2 | sed -n "2s/.* //p" >actual &&
158         test_cmp expect actual
159 '
160
161 test_expect_success EXPENSIVE,UNZIP,UNZIP_ZIP64_SUPPORT \
162         'zip archive bigger than 4GB' '
163         # build string containing 65536 characters
164         s=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef &&
165         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 &&
166         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 &&
167
168         # create blob with a length of 65536 + 1 bytes
169         blob=$(echo $s | git hash-object -w --stdin) &&
170
171         # create tree containing 65500 entries of that blob
172         for i in $(test_seq 1 65500)
173         do
174                 echo "100644 blob $blob $i"
175         done >tree &&
176         tree=$(git mktree <tree) &&
177
178         # zip it, creating an archive a bit bigger than 4GB
179         git archive -0 -o many-big.zip $tree &&
180
181         "$GIT_UNZIP" -t many-big.zip 9999 65500 &&
182         "$GIT_UNZIP" -t many-big.zip
183 '
184
185 test_expect_success EXPENSIVE,LONG_IS_64BIT,UNZIP,UNZIP_ZIP64_SUPPORT,ZIPINFO \
186         'zip archive with files bigger than 4GB' '
187         # Pack created with:
188         #   dd if=/dev/zero of=file bs=1M count=4100 && git hash-object -w file
189         mkdir -p .git/objects/pack &&
190         (
191                 cd .git/objects/pack &&
192                 "$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/big-pack.zip
193         ) &&
194         blob=754a93d6fada4c6873360e6cb4b209132271ab0e &&
195         size=$(expr 4100 "*" 1024 "*" 1024) &&
196
197         # create a tree containing the file
198         tree=$(echo "100644 blob $blob  big-file" | git mktree) &&
199
200         # zip it, creating an archive with a file bigger than 4GB
201         git archive -o big.zip $tree &&
202
203         "$GIT_UNZIP" -t big.zip &&
204         "$ZIPINFO" big.zip >big.lst &&
205         grep $size big.lst
206 '
207
208 test_done