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