blob: 2a97b27b0a68f94ab7204764ced5fd1457da3d9c [file] [log] [blame]
Rene Scharfed3d49c32005-06-02 22:50:17 +02001#!/bin/sh
2#
3# Copyright (C) 2005 Rene Scharfe
4#
5
John Keeping925cecc2013-11-10 15:47:29 +00006test_description='git archive and git get-tar-commit-id test
Rene Scharfed3d49c32005-06-02 22:50:17 +02007
Rene Scharfe5b860402005-06-03 18:21:23 +02008This test covers the topics of file contents, commit date handling and
9commit id embedding:
Rene Scharfed3d49c32005-06-02 22:50:17 +020010
11 The contents of the repository is compared to the extracted tar
12 archive. The repository contains simple text files, symlinks and a
Pavel Roskin3dff5372007-02-03 23:49:16 -050013 binary file (/bin/sh). Only paths shorter than 99 characters are
Rene Scharfe5b860402005-06-03 18:21:23 +020014 used.
Rene Scharfed3d49c32005-06-02 22:50:17 +020015
John Keeping925cecc2013-11-10 15:47:29 +000016 git archive applies the commit date to every file in the archive it
Rene Scharfed3d49c32005-06-02 22:50:17 +020017 creates. The test sets the commit date to a specific value and checks
18 if the tar archive contains that value.
19
John Keeping925cecc2013-11-10 15:47:29 +000020 When giving git archive a commit id (in contrast to a tree id) it
Rene Scharfed3d49c32005-06-02 22:50:17 +020021 embeds this commit id into the tar archive as a comment. The test
Junio C Hamano5be60072007-07-02 22:52:14 -070022 checks the ability of git get-tar-commit-id to figure it out from the
Rene Scharfed3d49c32005-06-02 22:50:17 +020023 tar file.
24
25'
26
27. ./test-lib.sh
28
René Scharfe38c9c9b2007-09-06 18:51:11 +020029SUBSTFORMAT=%H%n
René Scharfe8460b2f2007-09-03 20:07:01 +020030
René Scharfe9bf1ac42013-05-20 11:58:29 +020031test_lazy_prereq TAR_NEEDS_PAX_FALLBACK '
32 (
33 mkdir pax &&
34 cd pax &&
35 "$TAR" xf "$TEST_DIRECTORY"/t5000/pax.tar &&
36 test -f PaxHeaders.1791/file
37 )
38'
39
Jeff King96174142013-12-03 08:21:40 -050040test_lazy_prereq GZIP 'gzip --version'
41
René Scharfe9bf1ac42013-05-20 11:58:29 +020042get_pax_header() {
43 file=$1
44 header=$2=
45
46 while read len rest
47 do
48 if test "$len" = $(echo "$len $rest" | wc -c)
49 then
50 case "$rest" in
51 $header*)
52 echo "${rest#$header}"
53 ;;
54 esac
55 fi
56 done <"$file"
57}
58
René Scharfedeb9c8e2013-05-20 11:58:26 +020059check_tar() {
60 tarfile=$1.tar
61 listfile=$1.lst
62 dir=$1
63 dir_with_prefix=$dir/$2
64
65 test_expect_success ' extract tar archive' '
66 (mkdir $dir && cd $dir && "$TAR" xf -) <$tarfile
67 '
68
René Scharfe9bf1ac42013-05-20 11:58:29 +020069 test_expect_success TAR_NEEDS_PAX_FALLBACK ' interpret pax headers' '
70 (
71 cd $dir &&
72 for header in *.paxheader
73 do
74 data=${header%.paxheader}.data &&
Elia Pintod0b30a32014-06-06 07:55:58 -070075 if test -h $data || test -e $data
René Scharfe9bf1ac42013-05-20 11:58:29 +020076 then
77 path=$(get_pax_header $header path) &&
78 if test -n "$path"
79 then
80 mv "$data" "$path"
81 fi
82 fi
83 done
84 )
85 '
86
René Scharfedeb9c8e2013-05-20 11:58:26 +020087 test_expect_success ' validate filenames' '
88 (cd ${dir_with_prefix}a && find .) | sort >$listfile &&
89 test_cmp a.lst $listfile
90 '
91
92 test_expect_success ' validate file contents' '
93 diff -r a ${dir_with_prefix}a
94 '
95}
96
Rene Scharfed3d49c32005-06-02 22:50:17 +020097test_expect_success \
98 'populate workdir' \
René Scharfe13552412013-05-20 11:58:25 +020099 'mkdir a &&
Rene Scharfed3d49c32005-06-02 22:50:17 +0200100 echo simple textfile >a/a &&
René Scharfe9bf1ac42013-05-20 11:58:29 +0200101 ten=0123456789 && hundred=$ten$ten$ten$ten$ten$ten$ten$ten$ten$ten &&
102 echo long filename >a/four$hundred &&
Rene Scharfed3d49c32005-06-02 22:50:17 +0200103 mkdir a/bin &&
Nguyễn Thái Ngọc Duyc6806682018-03-24 08:44:42 +0100104 test-tool genrandom "frotz" 500000 >a/bin/sh &&
René Scharfe760da962007-09-14 00:13:06 +0200105 printf "A\$Format:%s\$O" "$SUBSTFORMAT" >a/substfile1 &&
106 printf "A not substituted O" >a/substfile2 &&
Johannes Sixt704a3142009-03-04 22:38:24 +0100107 if test_have_prereq SYMLINKS; then
108 ln -s a a/l1
109 else
110 printf %s a > a/l1
111 fi &&
Rene Scharfe4c691722006-03-25 23:21:07 +0100112 (p=long_path_to_a_file && cd a &&
113 for depth in 1 2 3 4 5; do mkdir $p && cd $p; done &&
114 echo text >file_with_long_path) &&
Rene Scharfed3d49c32005-06-02 22:50:17 +0200115 (cd a && find .) | sort >a.lst'
116
117test_expect_success \
René Scharfe008d8962008-06-08 18:42:33 +0200118 'add ignored file' \
119 'echo ignore me >a/ignored &&
René Scharfead946572009-04-18 00:17:49 +0200120 echo ignored export-ignore >.git/info/attributes'
René Scharfe008d8962008-06-08 18:42:33 +0200121
René Scharfe21711ca2014-07-05 21:35:01 +0200122test_expect_success 'add files to repository' '
123 git add a &&
124 GIT_COMMITTER_DATE="2005-05-27 22:00" git commit -m initial
125'
Rene Scharfed3d49c32005-06-02 22:50:17 +0200126
René Scharfec420df72013-05-20 11:58:24 +0200127test_expect_success 'setup export-subst' '
128 echo "substfile?" export-subst >>.git/info/attributes &&
129 git log --max-count=1 "--pretty=format:A${SUBSTFORMAT}O" HEAD \
130 >a/substfile1
131'
132
Rene Scharfed3d49c32005-06-02 22:50:17 +0200133test_expect_success \
Charles Baileyddff8562008-10-25 11:38:14 -0400134 'create bare clone' \
135 'git clone --bare . bare.git &&
René Scharfead946572009-04-18 00:17:49 +0200136 cp .git/info/attributes bare.git/info/attributes'
Charles Baileyddff8562008-10-25 11:38:14 -0400137
138test_expect_success \
René Scharfe008d8962008-06-08 18:42:33 +0200139 'remove ignored file' \
140 'rm a/ignored'
141
142test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -0700143 'git archive' \
144 'git archive HEAD >b.tar'
René Scharfe8ff21b12007-04-09 17:12:53 +0200145
René Scharfedeb9c8e2013-05-20 11:58:26 +0200146check_tar b
147
René Scharfe03d9bc52013-05-20 11:58:27 +0200148test_expect_success 'git archive --prefix=prefix/' '
149 git archive --prefix=prefix/ HEAD >with_prefix.tar
150'
151
152check_tar with_prefix prefix/
153
154test_expect_success 'git-archive --prefix=olde-' '
155 git archive --prefix=olde- HEAD >with_olde-prefix.tar
156'
157
158check_tar with_olde-prefix olde-
159
Nguyễn Thái Ngọc Duy55440492012-05-03 08:51:04 +0700160test_expect_success 'git archive on large files' '
161 test_config core.bigfilethreshold 1 &&
162 git archive HEAD >b3.tar &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200163 test_cmp_bin b.tar b3.tar
Nguyễn Thái Ngọc Duy55440492012-05-03 08:51:04 +0700164'
165
Rene Scharfed3d49c32005-06-02 22:50:17 +0200166test_expect_success \
Charles Baileyddff8562008-10-25 11:38:14 -0400167 'git archive in a bare repo' \
168 '(cd bare.git && git archive HEAD) >b3.tar'
169
170test_expect_success \
171 'git archive vs. the same in a bare repo' \
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200172 'test_cmp_bin b.tar b3.tar'
Charles Baileyddff8562008-10-25 11:38:14 -0400173
Carlos Manuel Duclos Vergaraaec0c1b2009-02-16 18:20:25 +0100174test_expect_success 'git archive with --output' \
175 'git archive --output=b4.tar HEAD &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200176 test_cmp_bin b.tar b4.tar'
Carlos Manuel Duclos Vergaraaec0c1b2009-02-16 18:20:25 +0100177
Jeff King1bc01ef2011-11-19 02:40:04 -0500178test_expect_success 'git archive --remote' \
Thomas Rast48139262009-06-27 20:47:43 +0200179 'git archive --remote=. HEAD >b5.tar &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200180 test_cmp_bin b.tar b5.tar'
Thomas Rast48139262009-06-27 20:47:43 +0200181
Junio C Hamanoeb0224c2016-11-22 13:37:04 -0800182test_expect_success 'git archive --remote with configured remote' '
183 git config remote.foo.url . &&
184 (
185 cd a &&
186 git archive --remote=foo --output=../b5-nick.tar HEAD
187 ) &&
188 test_cmp_bin b.tar b5-nick.tar
189'
190
Charles Baileyddff8562008-10-25 11:38:14 -0400191test_expect_success \
Rene Scharfed3d49c32005-06-02 22:50:17 +0200192 'validate file modification time' \
Jeff King30684df2008-05-13 04:45:32 -0400193 'mkdir extract &&
Junio C Hamanobfce5082008-07-25 12:35:10 -0700194 "$TAR" xf b.tar -C extract a/a &&
Junio C Hamanodeb98452018-04-25 13:29:00 +0900195 test-tool chmtime --get extract/a/a >b.mtime &&
Jeff King30684df2008-05-13 04:45:32 -0400196 echo "1117231200" >expected.mtime &&
Miklos Vajna188c3822009-03-16 21:18:42 +0100197 test_cmp expected.mtime b.mtime'
Rene Scharfed3d49c32005-06-02 22:50:17 +0200198
199test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -0700200 'git get-tar-commit-id' \
201 'git get-tar-commit-id <b.tar >b.commitid &&
Miklos Vajna188c3822009-03-16 21:18:42 +0100202 test_cmp .git/$(git symbolic-ref HEAD) b.commitid'
Rene Scharfed3d49c32005-06-02 22:50:17 +0200203
René Scharfefe12d8e2010-02-08 00:30:20 +0100204test_expect_success 'git archive with --output, override inferred format' '
205 git archive --format=tar --output=d4.zip HEAD &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200206 test_cmp_bin b.tar d4.zip
René Scharfefe12d8e2010-02-08 00:30:20 +0100207'
208
Junio C Hamanoeb0224c2016-11-22 13:37:04 -0800209test_expect_success 'git archive --list outside of a git repo' '
210 nongit git archive --list
211'
212
213test_expect_success 'git archive --remote outside of a git repo' '
214 git archive HEAD >expect.tar &&
215 nongit git archive --remote="$PWD" HEAD >actual.tar &&
216 test_cmp_bin expect.tar actual.tar
217'
René Scharfe265d5282007-04-05 22:55:43 +0200218
Jeff Kingee27ca42011-11-17 18:04:22 -0500219test_expect_success 'clients cannot access unreachable commits' '
220 test_commit unreachable &&
Elia Pintof5efd512014-04-30 09:23:07 -0700221 sha1=$(git rev-parse HEAD) &&
Jeff Kingee27ca42011-11-17 18:04:22 -0500222 git reset --hard HEAD^ &&
223 git archive $sha1 >remote.tar &&
224 test_must_fail git archive --remote=. $sha1 >remote.tar
225'
226
Scott J. Goldman7671b632014-02-28 05:04:19 -0500227test_expect_success 'upload-archive can allow unreachable commits' '
228 test_commit unreachable1 &&
Elia Pintof5efd512014-04-30 09:23:07 -0700229 sha1=$(git rev-parse HEAD) &&
Scott J. Goldman7671b632014-02-28 05:04:19 -0500230 git reset --hard HEAD^ &&
231 git archive $sha1 >remote.tar &&
232 test_config uploadarchive.allowUnreachable true &&
233 git archive --remote=. $sha1 >remote.tar
234'
235
Jeff King767cf452011-06-21 21:26:31 -0400236test_expect_success 'setup tar filters' '
237 git config tar.tar.foo.command "tr ab ba" &&
Jeff King7b977302011-06-21 23:17:35 -0400238 git config tar.bar.command "tr ab ba" &&
Jeff King785a0422013-01-23 01:23:27 -0500239 git config tar.bar.remote true &&
240 git config tar.invalid baz
Jeff King767cf452011-06-21 21:26:31 -0400241'
242
243test_expect_success 'archive --list mentions user filter' '
244 git archive --list >output &&
245 grep "^tar\.foo\$" output &&
246 grep "^bar\$" output
247'
248
Jeff King1bc01ef2011-11-19 02:40:04 -0500249test_expect_success 'archive --list shows only enabled remote filters' '
Jeff King767cf452011-06-21 21:26:31 -0400250 git archive --list --remote=. >output &&
Jeff King7b977302011-06-21 23:17:35 -0400251 ! grep "^tar\.foo\$" output &&
Jeff King767cf452011-06-21 21:26:31 -0400252 grep "^bar\$" output
253'
254
255test_expect_success 'invoke tar filter by format' '
256 git archive --format=tar.foo HEAD >config.tar.foo &&
257 tr ab ba <config.tar.foo >config.tar &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200258 test_cmp_bin b.tar config.tar &&
Jeff King767cf452011-06-21 21:26:31 -0400259 git archive --format=bar HEAD >config.bar &&
260 tr ab ba <config.bar >config.tar &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200261 test_cmp_bin b.tar config.tar
Jeff King767cf452011-06-21 21:26:31 -0400262'
263
264test_expect_success 'invoke tar filter by extension' '
265 git archive -o config-implicit.tar.foo HEAD &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200266 test_cmp_bin config.tar.foo config-implicit.tar.foo &&
Jeff King767cf452011-06-21 21:26:31 -0400267 git archive -o config-implicit.bar HEAD &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200268 test_cmp_bin config.tar.foo config-implicit.bar
Jeff King767cf452011-06-21 21:26:31 -0400269'
270
271test_expect_success 'default output format remains tar' '
272 git archive -o config-implicit.baz HEAD &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200273 test_cmp_bin b.tar config-implicit.baz
Jeff King767cf452011-06-21 21:26:31 -0400274'
275
276test_expect_success 'extension matching requires dot' '
277 git archive -o config-implicittar.foo HEAD &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200278 test_cmp_bin b.tar config-implicittar.foo
Jeff King767cf452011-06-21 21:26:31 -0400279'
280
Jeff King1bc01ef2011-11-19 02:40:04 -0500281test_expect_success 'only enabled filters are available remotely' '
Jeff King7b977302011-06-21 23:17:35 -0400282 test_must_fail git archive --remote=. --format=tar.foo HEAD \
283 >remote.tar.foo &&
284 git archive --remote=. --format=bar >remote.bar HEAD &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200285 test_cmp_bin remote.bar config.bar
Jeff King7b977302011-06-21 23:17:35 -0400286'
287
Jeff King0e804e02011-06-21 21:27:35 -0400288test_expect_success GZIP 'git archive --format=tgz' '
289 git archive --format=tgz HEAD >j.tgz
290'
291
292test_expect_success GZIP 'git archive --format=tar.gz' '
293 git archive --format=tar.gz HEAD >j1.tar.gz &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200294 test_cmp_bin j.tgz j1.tar.gz
Jeff King0e804e02011-06-21 21:27:35 -0400295'
296
297test_expect_success GZIP 'infer tgz from .tgz filename' '
298 git archive --output=j2.tgz HEAD &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200299 test_cmp_bin j.tgz j2.tgz
Jeff King0e804e02011-06-21 21:27:35 -0400300'
301
302test_expect_success GZIP 'infer tgz from .tar.gz filename' '
303 git archive --output=j3.tar.gz HEAD &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200304 test_cmp_bin j.tgz j3.tar.gz
Jeff King0e804e02011-06-21 21:27:35 -0400305'
306
Jeff King96174142013-12-03 08:21:40 -0500307test_expect_success GZIP 'extract tgz file' '
308 gzip -d -c <j.tgz >j.tar &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200309 test_cmp_bin b.tar j.tar
Jeff King0e804e02011-06-21 21:27:35 -0400310'
311
Jeff King1bc01ef2011-11-19 02:40:04 -0500312test_expect_success GZIP 'remote tar.gz is allowed by default' '
Jeff King7b977302011-06-21 23:17:35 -0400313 git archive --remote=. --format=tar.gz HEAD >remote.tar.gz &&
Stepan Kasalb93e6e32014-06-04 17:57:52 +0200314 test_cmp_bin j.tgz remote.tar.gz
Jeff King7b977302011-06-21 23:17:35 -0400315'
316
Jeff King1bc01ef2011-11-19 02:40:04 -0500317test_expect_success GZIP 'remote tar.gz can be disabled' '
Jeff King7b977302011-06-21 23:17:35 -0400318 git config tar.tar.gz.remote false &&
319 test_must_fail git archive --remote=. --format=tar.gz HEAD \
320 >remote.tar.gz
321'
322
Nguyễn Thái Ngọc Duyed22b412014-09-21 10:55:06 +0700323test_expect_success 'archive and :(glob)' '
324 git archive -v HEAD -- ":(glob)**/sh" >/dev/null 2>actual &&
325 cat >expect <<EOF &&
326a/
327a/bin/
328a/bin/sh
329EOF
330 test_cmp expect actual
331'
332
333test_expect_success 'catch non-matching pathspec' '
334 test_must_fail git archive -v HEAD -- "*.abc" >/dev/null
335'
336
Jeff Kinge51217e2016-06-30 05:08:57 -0400337# Pull the size and date of each entry in a tarfile using the system tar.
338#
339# We'll pull out only the year from the date; that avoids any question of
340# timezones impacting the result (as long as we keep our test times away from a
341# year boundary; our reference times are all in August).
342#
343# The output of tar_info is expected to be "<size> <year>", both in decimal. It
344# ignores the return value of tar. We have to do this, because some of our test
345# input is only partial (the real data is 64GB in some cases).
346tar_info () {
347 "$TAR" tvf "$1" |
348 awk '{
349 split($4, date, "-")
350 print $3 " " date[1]
351 }'
352}
353
354# See if our system tar can handle a tar file with huge sizes and dates far in
355# the future, and that we can actually parse its output.
356#
357# The reference file was generated by GNU tar, and the magic time and size are
358# both octal 01000000000001, which overflows normal ustar fields.
359test_lazy_prereq TAR_HUGE '
360 echo "68719476737 4147" >expect &&
361 tar_info "$TEST_DIRECTORY"/t5000/huge-and-future.tar >actual &&
362 test_cmp expect actual
363'
364
Junio C Hamano29493582016-07-14 13:04:43 -0700365test_expect_success LONG_IS_64BIT 'set up repository with huge blob' '
Jeff Kinge51217e2016-06-30 05:08:57 -0400366 obj_d=19 &&
367 obj_f=f9c8273ec45a8938e6999cb59b3ff66739902a &&
368 obj=${obj_d}${obj_f} &&
369 mkdir -p .git/objects/$obj_d &&
370 cp "$TEST_DIRECTORY"/t5000/$obj .git/objects/$obj_d/$obj_f &&
371 rm -f .git/index &&
372 git update-index --add --cacheinfo 100644,$obj,huge &&
373 git commit -m huge
374'
375
376# We expect git to die with SIGPIPE here (otherwise we
377# would generate the whole 64GB).
Junio C Hamano29493582016-07-14 13:04:43 -0700378test_expect_success LONG_IS_64BIT 'generate tar with huge size' '
Jeff Kinge51217e2016-06-30 05:08:57 -0400379 {
380 git archive HEAD
381 echo $? >exit-code
382 } | test_copy_bytes 4096 >huge.tar &&
383 echo 141 >expect &&
384 test_cmp expect exit-code
385'
386
Junio C Hamano29493582016-07-14 13:04:43 -0700387test_expect_success TAR_HUGE,LONG_IS_64BIT 'system tar can read our huge size' '
Jeff Kinge51217e2016-06-30 05:08:57 -0400388 echo 68719476737 >expect &&
389 tar_info huge.tar | cut -d" " -f1 >actual &&
390 test_cmp expect actual
391'
392
Johannes Schindelina07fb052017-04-20 22:52:13 +0200393test_expect_success TIME_IS_64BIT 'set up repository with far-future commit' '
Jeff Kinge51217e2016-06-30 05:08:57 -0400394 rm -f .git/index &&
395 echo content >file &&
396 git add file &&
397 GIT_COMMITTER_DATE="@68719476737 +0000" \
398 git commit -m "tempori parendum"
399'
400
Johannes Schindelina07fb052017-04-20 22:52:13 +0200401test_expect_success TIME_IS_64BIT 'generate tar with future mtime' '
Jeff Kinge51217e2016-06-30 05:08:57 -0400402 git archive HEAD >future.tar
403'
404
Johannes Schindelinefac8ac2017-04-20 22:58:21 +0200405test_expect_success TAR_HUGE,TIME_IS_64BIT,TIME_T_IS_64BIT 'system tar can read our future mtime' '
Jeff Kinge51217e2016-06-30 05:08:57 -0400406 echo 4147 >expect &&
407 tar_info future.tar | cut -d" " -f2 >actual &&
408 test_cmp expect actual
409'
410
Rene Scharfed3d49c32005-06-02 22:50:17 +0200411test_done