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