Git fork
1#!/bin/sh
2#
3# Copyright (C) 2005 Rene Scharfe
4#
5
6test_description='git archive and git get-tar-commit-id test
7
8This test covers the topics of file contents, commit date handling and
9commit id embedding:
10
11 The contents of the repository is compared to the extracted tar
12 archive. The repository contains simple text files, symlinks and a
13 binary file (/bin/sh). Only paths shorter than 99 characters are
14 used.
15
16 git archive applies the commit date to every file in the archive it
17 creates. The test sets the commit date to a specific value and checks
18 if the tar archive contains that value.
19
20 When giving git archive a commit id (in contrast to a tree id) it
21 embeds this commit id into the tar archive as a comment. The test
22 checks the ability of git get-tar-commit-id to figure it out from the
23 tar file.
24
25'
26
27TEST_CREATE_REPO_NO_TEMPLATE=1
28. ./test-lib.sh
29
30SUBSTFORMAT=%H%n
31
32test_lazy_prereq TAR_NEEDS_PAX_FALLBACK '
33 (
34 mkdir pax &&
35 cd pax &&
36 "$TAR" xf "$TEST_DIRECTORY"/t5000/pax.tar &&
37 test -f PaxHeaders.1791/file
38 )
39'
40
41test_lazy_prereq GZIP 'gzip --version'
42
43get_pax_header() {
44 file=$1
45 header=$2=
46
47 while read len rest
48 do
49 if test "$len" = $(echo "$len $rest" | wc -c)
50 then
51 case "$rest" in
52 $header*)
53 echo "${rest#$header}"
54 ;;
55 esac
56 fi
57 done <"$file"
58}
59
60check_tar() {
61 tarfile=$1.tar
62 listfile=$1.lst
63 dir=$1
64 dir_with_prefix=$dir/$2
65
66 test_expect_success ' extract tar archive' '
67 (mkdir $dir && cd $dir && "$TAR" xf -) <$tarfile
68 '
69
70 test_expect_success TAR_NEEDS_PAX_FALLBACK ' interpret pax headers' '
71 (
72 cd $dir &&
73 for header in *.paxheader
74 do
75 data=${header%.paxheader}.data &&
76 if test -h $data || test -e $data
77 then
78 path=$(get_pax_header $header path) &&
79 if test -n "$path"
80 then
81 mv "$data" "$path" || exit 1
82 fi
83 fi
84 done
85 )
86 '
87
88 test_expect_success ' validate filenames' '
89 (cd ${dir_with_prefix}a && find .) | sort >$listfile &&
90 test_cmp a.lst $listfile
91 '
92
93 test_expect_success ' validate file contents' '
94 diff -r a ${dir_with_prefix}a
95 '
96}
97
98check_added() {
99 dir=$1
100 path_in_fs=$2
101 path_in_archive=$3
102
103 test_expect_success " validate extra file $path_in_archive" '
104 diff -r $path_in_fs $dir/$path_in_archive
105 '
106}
107
108check_mtime() {
109 dir=$1
110 path_in_archive=$2
111 mtime=$3
112
113 test_expect_success " validate mtime of $path_in_archive" '
114 test-tool chmtime --get $dir/$path_in_archive >actual.mtime &&
115 echo $mtime >expect.mtime &&
116 test_cmp expect.mtime actual.mtime
117 '
118}
119
120test_expect_success 'setup' '
121 test_oid_cache <<-EOF
122 obj sha1:19f9c8273ec45a8938e6999cb59b3ff66739902a
123 obj sha256:3c666f798798601571f5cec0adb57ce4aba8546875e7693177e0535f34d2c49b
124 EOF
125'
126
127test_expect_success '--list notices extra parameters' '
128 test_must_fail git archive --list blah &&
129 test_must_fail git archive --remote=. --list blah
130'
131
132test_expect_success 'end-of-options is correctly eaten' '
133 git archive --list --end-of-options &&
134 git archive --remote=. --list --end-of-options
135'
136
137test_expect_success 'populate workdir' '
138 mkdir a &&
139 echo "a files_named_a" >.gitattributes &&
140 git add .gitattributes &&
141 echo simple textfile >a/a &&
142 ten=0123456789 &&
143 hundred="$ten$ten$ten$ten$ten$ten$ten$ten$ten$ten" &&
144 echo long filename >"a/four$hundred" &&
145 mkdir a/bin &&
146 test-tool genrandom "frotz" 500000 >a/bin/sh &&
147 printf "A\$Format:%s\$O" "$SUBSTFORMAT" >a/substfile1 &&
148 printf "A not substituted O" >a/substfile2 &&
149 if test_have_prereq SYMLINKS
150 then
151 ln -s a a/l1
152 else
153 printf %s a >a/l1
154 fi &&
155 (
156 p=long_path_to_a_file &&
157 cd a &&
158 for depth in 1 2 3 4 5
159 do
160 mkdir $p &&
161 cd $p || exit 1
162 done &&
163 echo text >file_with_long_path
164 ) &&
165 (cd a && find .) | sort >a.lst
166'
167
168test_expect_success \
169 'add ignored file' \
170 'echo ignore me >a/ignored &&
171 mkdir .git/info &&
172 echo ignored export-ignore >.git/info/attributes'
173
174test_expect_success 'add files to repository' '
175 git add a &&
176 GIT_COMMITTER_DATE="2005-05-27 22:00" git commit -m initial
177'
178
179test_expect_success 'setup export-subst' '
180 echo "substfile?" export-subst >>.git/info/attributes &&
181 git log --max-count=1 "--pretty=format:A${SUBSTFORMAT}O" HEAD \
182 >a/substfile1
183'
184
185test_expect_success 'create bare clone' '
186 git clone --template= --bare . bare.git &&
187 mkdir bare.git/info &&
188 cp .git/info/attributes bare.git/info/attributes
189'
190
191test_expect_success 'remove ignored file' '
192 rm a/ignored
193'
194
195test_expect_success 'git archive' '
196 git archive HEAD >b.tar
197'
198
199check_tar b
200check_mtime b a/a 1117231200
201
202test_expect_success 'git archive --mtime' '
203 git archive --mtime=2002-02-02T02:02:02-0200 HEAD >with_mtime.tar
204'
205
206check_tar with_mtime
207check_mtime with_mtime a/a 1012622522
208
209test_expect_success 'git archive --prefix=prefix/' '
210 git archive --prefix=prefix/ HEAD >with_prefix.tar
211'
212
213check_tar with_prefix prefix/
214
215test_expect_success 'git-archive --prefix=olde-' '
216 git archive --prefix=olde- HEAD >with_olde-prefix.tar
217'
218
219check_tar with_olde-prefix olde-
220
221test_expect_success 'git archive --add-file' '
222 echo untracked >untracked &&
223 git archive --add-file=untracked HEAD >with_untracked.tar
224'
225
226check_tar with_untracked
227check_added with_untracked untracked untracked
228
229test_expect_success 'git archive --add-file twice' '
230 echo untracked >untracked &&
231 git archive --prefix=one/ --add-file=untracked \
232 --prefix=two/ --add-file=untracked \
233 --prefix= HEAD >with_untracked2.tar
234'
235
236check_tar with_untracked2
237check_added with_untracked2 untracked one/untracked
238check_added with_untracked2 untracked two/untracked
239
240test_expect_success 'git archive on large files' '
241 test_config core.bigfilethreshold 1 &&
242 git archive HEAD >b3.tar &&
243 test_cmp_bin b.tar b3.tar
244'
245
246test_expect_success 'git archive in a bare repo' '
247 git --git-dir bare.git archive HEAD >b3.tar
248'
249
250test_expect_success 'git archive vs. the same in a bare repo' '
251 test_cmp_bin b.tar b3.tar
252'
253
254test_expect_success 'git archive with --output' '
255 git archive --output=b4.tar HEAD &&
256 test_cmp_bin b.tar b4.tar
257'
258
259test_expect_success 'git archive --remote' '
260 git archive --remote=. HEAD >b5.tar &&
261 test_cmp_bin b.tar b5.tar
262'
263
264test_expect_success 'git archive --remote with configured remote' '
265 git config remote.foo.url . &&
266 (
267 cd a &&
268 git archive --remote=foo --output=../b5-nick.tar HEAD
269 ) &&
270 test_cmp_bin b.tar b5-nick.tar
271'
272
273test_expect_success 'git get-tar-commit-id' '
274 git get-tar-commit-id <b.tar >actual &&
275 git rev-parse HEAD >expect &&
276 test_cmp expect actual
277'
278
279test_expect_success 'git archive with --output, override inferred format' '
280 git archive --format=tar --output=d4.zip HEAD &&
281 test_cmp_bin b.tar d4.zip
282'
283
284test_expect_success GZIP 'git archive with --output and --remote creates .tgz' '
285 git archive --output=d5.tgz --remote=. HEAD &&
286 gzip -d -c <d5.tgz >d5.tar &&
287 test_cmp_bin b.tar d5.tar
288'
289
290test_expect_success 'git archive --list outside of a git repo' '
291 nongit git archive --list
292'
293
294test_expect_success 'git archive --remote outside of a git repo' '
295 git archive HEAD >expect.tar &&
296 nongit git archive --remote="$PWD" HEAD >actual.tar &&
297 test_cmp_bin expect.tar actual.tar
298'
299
300test_expect_success 'clients cannot access unreachable commits' '
301 test_commit unreachable &&
302 sha1=$(git rev-parse HEAD) &&
303 git reset --hard HEAD^ &&
304 git archive $sha1 >remote.tar &&
305 test_must_fail git archive --remote=. $sha1 >remote.tar
306'
307
308test_expect_success 'upload-archive can allow unreachable commits' '
309 test_commit unreachable1 &&
310 sha1=$(git rev-parse HEAD) &&
311 git reset --hard HEAD^ &&
312 git archive $sha1 >remote.tar &&
313 test_config uploadarchive.allowUnreachable true &&
314 git archive --remote=. $sha1 >remote.tar
315'
316
317test_expect_success 'setup tar filters' '
318 git config tar.tar.foo.command "tr ab ba" &&
319 git config tar.bar.command "tr ab ba" &&
320 git config tar.bar.remote true &&
321 git config tar.invalid baz
322'
323
324test_expect_success 'archive --list mentions user filter' '
325 git archive --list >output &&
326 grep "^tar\.foo\$" output &&
327 grep "^bar\$" output
328'
329
330test_expect_success 'archive --list shows only enabled remote filters' '
331 git archive --list --remote=. >output &&
332 ! grep "^tar\.foo\$" output &&
333 grep "^bar\$" output
334'
335
336test_expect_success 'invoke tar filter by format' '
337 git archive --format=tar.foo HEAD >config.tar.foo &&
338 tr ab ba <config.tar.foo >config.tar &&
339 test_cmp_bin b.tar config.tar &&
340 git archive --format=bar HEAD >config.bar &&
341 tr ab ba <config.bar >config.tar &&
342 test_cmp_bin b.tar config.tar
343'
344
345test_expect_success 'invoke tar filter by extension' '
346 git archive -o config-implicit.tar.foo HEAD &&
347 test_cmp_bin config.tar.foo config-implicit.tar.foo &&
348 git archive -o config-implicit.bar HEAD &&
349 test_cmp_bin config.tar.foo config-implicit.bar
350'
351
352test_expect_success 'default output format remains tar' '
353 git archive -o config-implicit.baz HEAD &&
354 test_cmp_bin b.tar config-implicit.baz
355'
356
357test_expect_success 'extension matching requires dot' '
358 git archive -o config-implicittar.foo HEAD &&
359 test_cmp_bin b.tar config-implicittar.foo
360'
361
362test_expect_success 'only enabled filters are available remotely' '
363 test_must_fail git archive --remote=. --format=tar.foo HEAD \
364 >remote.tar.foo &&
365 git archive --remote=. --format=bar >remote.bar HEAD &&
366 test_cmp_bin remote.bar config.bar
367'
368
369test_expect_success 'invalid filter is reported only once' '
370 test_must_fail git -c tar.invalid.command= archive --format=invalid \
371 HEAD >out 2>err &&
372 test_must_be_empty out &&
373 test_line_count = 1 err
374'
375
376test_expect_success 'git archive --format=tgz' '
377 git archive --format=tgz HEAD >j.tgz
378'
379
380test_expect_success 'git archive --format=tar.gz' '
381 git archive --format=tar.gz HEAD >j1.tar.gz &&
382 test_cmp_bin j.tgz j1.tar.gz
383'
384
385test_expect_success 'infer tgz from .tgz filename' '
386 git archive --output=j2.tgz HEAD &&
387 test_cmp_bin j.tgz j2.tgz
388'
389
390test_expect_success 'infer tgz from .tar.gz filename' '
391 git archive --output=j3.tar.gz HEAD &&
392 test_cmp_bin j.tgz j3.tar.gz
393'
394
395test_expect_success GZIP 'extract tgz file' '
396 gzip -d -c <j.tgz >j.tar &&
397 test_cmp_bin b.tar j.tar
398'
399
400test_expect_success 'remote tar.gz is allowed by default' '
401 git archive --remote=. --format=tar.gz HEAD >remote.tar.gz &&
402 test_cmp_bin j.tgz remote.tar.gz
403'
404
405test_expect_success 'remote tar.gz can be disabled' '
406 git config tar.tar.gz.remote false &&
407 test_must_fail git archive --remote=. --format=tar.gz HEAD \
408 >remote.tar.gz
409'
410
411test_expect_success GZIP 'git archive --format=tgz (external gzip)' '
412 test_config tar.tgz.command "gzip -cn" &&
413 git archive --format=tgz HEAD >external_gzip.tgz
414'
415
416test_expect_success GZIP 'git archive --format=tar.gz (external gzip)' '
417 test_config tar.tar.gz.command "gzip -cn" &&
418 git archive --format=tar.gz HEAD >external_gzip.tar.gz &&
419 test_cmp_bin external_gzip.tgz external_gzip.tar.gz
420'
421
422test_expect_success GZIP 'extract tgz file (external gzip)' '
423 gzip -d -c <external_gzip.tgz >external_gzip.tar &&
424 test_cmp_bin b.tar external_gzip.tar
425'
426
427test_expect_success 'archive and :(glob)' '
428 git archive -v HEAD -- ":(glob)**/sh" >/dev/null 2>actual &&
429 cat >expect <<-\EOF &&
430 a/
431 a/bin/
432 a/bin/sh
433 EOF
434 test_cmp expect actual
435'
436
437test_expect_success 'catch non-matching pathspec' '
438 test_must_fail git archive -v HEAD -- "*.abc" >/dev/null
439'
440
441test_expect_success 'reject paths outside the current directory' '
442 test_must_fail git -C a/bin archive HEAD .. >/dev/null 2>err &&
443 grep "outside the current directory" err
444'
445
446test_expect_success 'allow pathspecs that resolve to the current directory' '
447 git -C a/bin archive -v HEAD ../bin >/dev/null 2>actual &&
448 cat >expect <<-\EOF &&
449 sh
450 EOF
451 test_cmp expect actual
452'
453
454test_expect_success 'attr pathspec in bare repo' '
455 test_expect_code 0 git --git-dir=bare.git archive -v HEAD \
456 ":(attr:files_named_a)" >/dev/null 2>actual &&
457 cat >expect <<-\EOF &&
458 a/
459 a/a
460 EOF
461 test_cmp expect actual
462'
463
464# Pull the size and date of each entry in a tarfile using the system tar.
465#
466# We'll pull out only the year from the date; that avoids any question of
467# timezones impacting the result (as long as we keep our test times away from a
468# year boundary; our reference times are all in August).
469#
470# The output of tar_info is expected to be "<size> <year>", both in decimal. It
471# ignores the return value of tar. We have to do this, because some of our test
472# input is only partial (the real data is 64GB in some cases).
473tar_info () {
474 "$TAR" tvf "$1" |
475 awk '{
476 split($4, date, "-")
477 print $3 " " date[1]
478 }'
479}
480
481# See if our system tar can handle a tar file with huge sizes and dates far in
482# the future, and that we can actually parse its output.
483#
484# The reference file was generated by GNU tar, and the magic time and size are
485# both octal 01000000000001, which overflows normal ustar fields.
486test_lazy_prereq TAR_HUGE '
487 echo "68719476737 4147" >expect &&
488 tar_info "$TEST_DIRECTORY"/t5000/huge-and-future.tar >actual &&
489 test_cmp expect actual
490'
491
492test_expect_success LONG_IS_64BIT 'set up repository with huge blob' '
493 obj=$(test_oid obj) &&
494 path=$(test_oid_to_path $obj) &&
495 mkdir -p .git/objects/$(dirname $path) &&
496 cp "$TEST_DIRECTORY"/t5000/huge-object .git/objects/$path &&
497 rm -f .git/index &&
498 git update-index --add --cacheinfo 100644,$obj,huge &&
499 git commit -m huge
500'
501
502# We expect git to die with SIGPIPE here (otherwise we
503# would generate the whole 64GB).
504test_expect_success LONG_IS_64BIT 'generate tar with huge size' '
505 {
506 git archive HEAD
507 echo $? >exit-code
508 } | test_copy_bytes 4096 >huge.tar &&
509 echo 141 >expect &&
510 test_cmp expect exit-code
511'
512
513test_expect_success TAR_HUGE,LONG_IS_64BIT 'system tar can read our huge size' '
514 echo 68719476737 >expect &&
515 tar_info huge.tar | cut -d" " -f1 >actual &&
516 test_cmp expect actual
517'
518
519test_expect_success TIME_IS_64BIT 'set up repository with far-future (2^34 - 1) commit' '
520 rm -f .git/index &&
521 echo foo >file &&
522 git add file &&
523 GIT_COMMITTER_DATE="@17179869183 +0000" \
524 git commit -m "tempori parendum"
525'
526
527test_expect_success TIME_IS_64BIT 'generate tar with far-future mtime' '
528 git archive HEAD >future.tar
529'
530
531test_expect_success TAR_HUGE,TIME_IS_64BIT,TIME_T_IS_64BIT 'system tar can read our future mtime' '
532 echo 2514 >expect &&
533 tar_info future.tar | cut -d" " -f2 >actual &&
534 test_cmp expect actual
535'
536
537test_expect_success TIME_IS_64BIT 'set up repository with far-far-future (2^36 + 1) commit' '
538 rm -f .git/index &&
539 echo content >file &&
540 git add file &&
541 GIT_TEST_COMMIT_GRAPH=0 GIT_COMMITTER_DATE="@68719476737 +0000" \
542 git commit -m "tempori parendum"
543'
544
545test_expect_success TIME_IS_64BIT 'generate tar with far-far-future mtime' '
546 git archive HEAD >future.tar
547'
548
549test_expect_success TAR_HUGE,TIME_IS_64BIT,TIME_T_IS_64BIT 'system tar can read our future mtime' '
550 echo 4147 >expect &&
551 tar_info future.tar | cut -d" " -f2 >actual &&
552 test_cmp expect actual
553'
554
555test_done