submodule--helper: advise on fatal alternate error
[git.git] / t / t1006-cat-file.sh
blob43c4be1e5ef559d10ca276ef777a8fe7ba8a3971
1 #!/bin/sh
3 test_description='git cat-file'
5 . ./test-lib.sh
7 echo_without_newline () {
8 printf '%s' "$*"
11 strlen () {
12 echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
15 maybe_remove_timestamp () {
16 if test -z "$2"; then
17 echo_without_newline "$1"
18 else
19 echo_without_newline "$(printf '%s\n' "$1" | sed -e 's/ [0-9][0-9]* [-+][0-9][0-9][0-9][0-9]$//')"
23 run_tests () {
24 type=$1
25 sha1=$2
26 size=$3
27 content=$4
28 pretty_content=$5
29 no_ts=$6
31 batch_output="$sha1 $type $size
32 $content"
34 test_expect_success "$type exists" '
35 git cat-file -e $sha1
38 test_expect_success "Type of $type is correct" '
39 echo $type >expect &&
40 git cat-file -t $sha1 >actual &&
41 test_cmp expect actual
44 test_expect_success "Size of $type is correct" '
45 echo $size >expect &&
46 git cat-file -s $sha1 >actual &&
47 test_cmp expect actual
50 test_expect_success "Type of $type is correct using --allow-unknown-type" '
51 echo $type >expect &&
52 git cat-file -t --allow-unknown-type $sha1 >actual &&
53 test_cmp expect actual
56 test_expect_success "Size of $type is correct using --allow-unknown-type" '
57 echo $size >expect &&
58 git cat-file -s --allow-unknown-type $sha1 >actual &&
59 test_cmp expect actual
62 test -z "$content" ||
63 test_expect_success "Content of $type is correct" '
64 maybe_remove_timestamp "$content" $no_ts >expect &&
65 maybe_remove_timestamp "$(git cat-file $type $sha1)" $no_ts >actual &&
66 test_cmp expect actual
69 test_expect_success "Pretty content of $type is correct" '
70 maybe_remove_timestamp "$pretty_content" $no_ts >expect &&
71 maybe_remove_timestamp "$(git cat-file -p $sha1)" $no_ts >actual &&
72 test_cmp expect actual
75 test -z "$content" ||
76 test_expect_success "--batch output of $type is correct" '
77 maybe_remove_timestamp "$batch_output" $no_ts >expect &&
78 maybe_remove_timestamp "$(echo $sha1 | git cat-file --batch)" $no_ts >actual &&
79 test_cmp expect actual
82 test_expect_success "--batch-check output of $type is correct" '
83 echo "$sha1 $type $size" >expect &&
84 echo_without_newline $sha1 | git cat-file --batch-check >actual &&
85 test_cmp expect actual
88 test_expect_success "custom --batch-check format" '
89 echo "$type $sha1" >expect &&
90 echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
91 test_cmp expect actual
94 test_expect_success '--batch-check with %(rest)' '
95 echo "$type this is some extra content" >expect &&
96 echo "$sha1 this is some extra content" |
97 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
98 test_cmp expect actual
101 test -z "$content" ||
102 test_expect_success "--batch without type ($type)" '
104 echo "$size" &&
105 maybe_remove_timestamp "$content" $no_ts
106 } >expect &&
107 echo $sha1 | git cat-file --batch="%(objectsize)" >actual.full &&
108 maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
109 test_cmp expect actual
112 test -z "$content" ||
113 test_expect_success "--batch without size ($type)" '
115 echo "$type" &&
116 maybe_remove_timestamp "$content" $no_ts
117 } >expect &&
118 echo $sha1 | git cat-file --batch="%(objecttype)" >actual.full &&
119 maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
120 test_cmp expect actual
124 hello_content="Hello World"
125 hello_size=$(strlen "$hello_content")
126 hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
128 test_expect_success "setup" '
129 echo_without_newline "$hello_content" > hello &&
130 git update-index --add hello
133 run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
135 test_expect_success '--batch-check without %(rest) considers whole line' '
136 echo "$hello_sha1 blob $hello_size" >expect &&
137 git update-index --add --cacheinfo 100644 $hello_sha1 "white space" &&
138 test_when_finished "git update-index --remove \"white space\"" &&
139 echo ":white space" | git cat-file --batch-check >actual &&
140 test_cmp expect actual
143 test_oid_init
145 tree_sha1=$(git write-tree)
146 tree_size=$(($(test_oid rawsz) + 13))
147 tree_pretty_content="100644 blob $hello_sha1 hello"
149 run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
151 commit_message="Initial commit"
152 commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
153 commit_size=$(($(test_oid hexsz) + 137))
154 commit_content="tree $tree_sha1
155 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
156 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
158 $commit_message"
160 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
162 tag_header_without_timestamp="object $hello_sha1
163 type blob
164 tag hellotag
165 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
166 tag_description="This is a tag"
167 tag_content="$tag_header_without_timestamp 0000000000 +0000
169 $tag_description"
171 tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
172 tag_size=$(strlen "$tag_content")
174 run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
176 test_expect_success \
177 "Reach a blob from a tag pointing to it" \
178 "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
180 for batch in batch batch-check
182 for opt in t s e p
184 test_expect_success "Passing -$opt with --$batch fails" '
185 test_must_fail git cat-file --$batch -$opt $hello_sha1
188 test_expect_success "Passing --$batch with -$opt fails" '
189 test_must_fail git cat-file -$opt --$batch $hello_sha1
191 done
193 test_expect_success "Passing <type> with --$batch fails" '
194 test_must_fail git cat-file --$batch blob $hello_sha1
197 test_expect_success "Passing --$batch with <type> fails" '
198 test_must_fail git cat-file blob --$batch $hello_sha1
201 test_expect_success "Passing sha1 with --$batch fails" '
202 test_must_fail git cat-file --$batch $hello_sha1
204 done
206 for opt in t s e p
208 test_expect_success "Passing -$opt with --follow-symlinks fails" '
209 test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
211 done
213 test_expect_success "--batch-check for a non-existent named object" '
214 test "foobar42 missing
215 foobar84 missing" = \
216 "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
219 test_expect_success "--batch-check for a non-existent hash" '
220 test "0000000000000000000000000000000000000042 missing
221 0000000000000000000000000000000000000084 missing" = \
222 "$( ( echo 0000000000000000000000000000000000000042;
223 echo_without_newline 0000000000000000000000000000000000000084; ) |
224 git cat-file --batch-check)"
227 test_expect_success "--batch for an existent and a non-existent hash" '
228 test "$tag_sha1 tag $tag_size
229 $tag_content
230 0000000000000000000000000000000000000000 missing" = \
231 "$( ( echo $tag_sha1;
232 echo_without_newline 0000000000000000000000000000000000000000; ) |
233 git cat-file --batch)"
236 test_expect_success "--batch-check for an empty line" '
237 test " missing" = "$(echo | git cat-file --batch-check)"
240 test_expect_success 'empty --batch-check notices missing object' '
241 echo "$ZERO_OID missing" >expect &&
242 echo "$ZERO_OID" | git cat-file --batch-check="" >actual &&
243 test_cmp expect actual
246 batch_input="$hello_sha1
247 $commit_sha1
248 $tag_sha1
249 deadbeef
253 batch_output="$hello_sha1 blob $hello_size
254 $hello_content
255 $commit_sha1 commit $commit_size
256 $commit_content
257 $tag_sha1 tag $tag_size
258 $tag_content
259 deadbeef missing
260 missing"
262 test_expect_success '--batch with multiple sha1s gives correct format' '
263 test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
266 batch_check_input="$hello_sha1
267 $tree_sha1
268 $commit_sha1
269 $tag_sha1
270 deadbeef
274 batch_check_output="$hello_sha1 blob $hello_size
275 $tree_sha1 tree $tree_size
276 $commit_sha1 commit $commit_size
277 $tag_sha1 tag $tag_size
278 deadbeef missing
279 missing"
281 test_expect_success "--batch-check with multiple sha1s gives correct format" '
282 test "$batch_check_output" = \
283 "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
286 test_expect_success 'setup blobs which are likely to delta' '
287 test-tool genrandom foo 10240 >foo &&
288 { cat foo; echo plus; } >foo-plus &&
289 git add foo foo-plus &&
290 git commit -m foo &&
291 cat >blobs <<-\EOF
292 HEAD:foo
293 HEAD:foo-plus
297 test_expect_success 'confirm that neither loose blob is a delta' '
298 cat >expect <<-EOF &&
299 $ZERO_OID
300 $ZERO_OID
302 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
303 test_cmp expect actual
306 # To avoid relying too much on the current delta heuristics,
307 # we will check only that one of the two objects is a delta
308 # against the other, but not the order. We can do so by just
309 # asking for the base of both, and checking whether either
310 # sha1 appears in the output.
311 test_expect_success '%(deltabase) reports packed delta bases' '
312 git repack -ad &&
313 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
315 grep "$(git rev-parse HEAD:foo)" actual ||
316 grep "$(git rev-parse HEAD:foo-plus)" actual
320 bogus_type="bogus"
321 bogus_content="bogus"
322 bogus_size=$(strlen "$bogus_content")
323 bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
325 test_expect_success "Type of broken object is correct" '
326 echo $bogus_type >expect &&
327 git cat-file -t --allow-unknown-type $bogus_sha1 >actual &&
328 test_cmp expect actual
331 test_expect_success "Size of broken object is correct" '
332 echo $bogus_size >expect &&
333 git cat-file -s --allow-unknown-type $bogus_sha1 >actual &&
334 test_cmp expect actual
336 bogus_type="abcdefghijklmnopqrstuvwxyz1234679"
337 bogus_content="bogus"
338 bogus_size=$(strlen "$bogus_content")
339 bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
341 test_expect_success "Type of broken object is correct when type is large" '
342 echo $bogus_type >expect &&
343 git cat-file -t --allow-unknown-type $bogus_sha1 >actual &&
344 test_cmp expect actual
347 test_expect_success "Size of large broken object is correct when type is large" '
348 echo $bogus_size >expect &&
349 git cat-file -s --allow-unknown-type $bogus_sha1 >actual &&
350 test_cmp expect actual
353 # Tests for git cat-file --follow-symlinks
354 test_expect_success 'prep for symlink tests' '
355 echo_without_newline "$hello_content" >morx &&
356 test_ln_s_add morx same-dir-link &&
357 test_ln_s_add dir link-to-dir &&
358 test_ln_s_add ../fleem out-of-repo-link &&
359 test_ln_s_add .. out-of-repo-link-dir &&
360 test_ln_s_add same-dir-link link-to-link &&
361 test_ln_s_add nope broken-same-dir-link &&
362 mkdir dir &&
363 test_ln_s_add ../morx dir/parent-dir-link &&
364 test_ln_s_add .. dir/link-dir &&
365 test_ln_s_add ../../escape dir/out-of-repo-link &&
366 test_ln_s_add ../.. dir/out-of-repo-link-dir &&
367 test_ln_s_add nope dir/broken-link-in-dir &&
368 mkdir dir/subdir &&
369 test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
370 test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
371 test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
372 test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
373 test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
374 echo_without_newline "$hello_content" >dir/subdir/ind2 &&
375 echo_without_newline "$hello_content" >dir/ind1 &&
376 test_ln_s_add dir dirlink &&
377 test_ln_s_add dir/subdir subdirlink &&
378 test_ln_s_add subdir/ind2 dir/link-to-child &&
379 test_ln_s_add dir/link-to-child link-to-down-link &&
380 test_ln_s_add dir/.. up-down &&
381 test_ln_s_add dir/../ up-down-trailing &&
382 test_ln_s_add dir/../morx up-down-file &&
383 test_ln_s_add dir/../../morx up-up-down-file &&
384 test_ln_s_add subdirlink/../../morx up-two-down-file &&
385 test_ln_s_add loop1 loop2 &&
386 test_ln_s_add loop2 loop1 &&
387 git add morx dir/subdir/ind2 dir/ind1 &&
388 git commit -am "test" &&
389 echo $hello_sha1 blob $hello_size >found
392 test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
393 echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
394 test_cmp found actual &&
395 echo HEAD:nope missing >expect &&
396 echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
397 test_cmp expect actual
400 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
401 echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
402 test_cmp found actual
405 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
406 echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
407 test_cmp found actual
411 test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
412 echo dangling 25 >expect &&
413 echo HEAD:broken-same-dir-link >>expect &&
414 echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
415 test_cmp expect actual
418 test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
419 echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
420 test_cmp found actual
423 test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
424 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
425 test_cmp found actual &&
426 echo notdir 29 >expect &&
427 echo HEAD:dir/parent-dir-link/nope >>expect &&
428 echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
429 test_cmp expect actual
432 test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
433 echo dangling 22 >expect &&
434 echo HEAD:dir/link-dir/nope >>expect &&
435 echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
436 test_cmp expect actual &&
437 echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
438 test_cmp found actual &&
439 echo dangling 27 >expect &&
440 echo HEAD:dir/broken-link-in-dir >>expect &&
441 echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
442 test_cmp expect actual
445 test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
446 echo notdir 41 >expect &&
447 echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
448 echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
449 test_cmp expect actual &&
450 echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
451 test_cmp found actual &&
452 echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
453 test_cmp found actual
456 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
457 echo dangling 17 >expect &&
458 echo HEAD:dirlink/morx >>expect &&
459 echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
460 test_cmp expect actual &&
461 echo $hello_sha1 blob $hello_size >expect &&
462 echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
463 test_cmp expect actual
466 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
467 echo dangling 20 >expect &&
468 echo HEAD:subdirlink/morx >>expect &&
469 echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
470 test_cmp expect actual &&
471 echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
472 test_cmp found actual
475 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
476 echo notdir 27 >expect &&
477 echo HEAD:dir/link-to-child/morx >>expect &&
478 echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
479 test_cmp expect actual &&
480 echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
481 test_cmp found actual &&
482 echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
483 test_cmp found actual
486 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
487 echo symlink 8 >expect &&
488 echo ../fleem >>expect &&
489 echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
490 test_cmp expect actual &&
491 echo symlink 2 >expect &&
492 echo .. >>expect &&
493 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
494 test_cmp expect actual
497 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
498 echo symlink 9 >expect &&
499 echo ../escape >>expect &&
500 echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
501 test_cmp expect actual &&
502 echo symlink 2 >expect &&
503 echo .. >>expect &&
504 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
505 test_cmp expect actual
508 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
509 echo symlink 15 >expect &&
510 echo ../great-escape >>expect &&
511 echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
512 test_cmp expect actual &&
513 echo symlink 2 >expect &&
514 echo .. >>expect &&
515 echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
516 test_cmp expect actual &&
517 echo symlink 3 >expect &&
518 echo ../ >>expect &&
519 echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
520 test_cmp expect actual
523 test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
524 echo HEAD: | git cat-file --batch-check >expect &&
525 echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
526 test_cmp expect actual &&
527 echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
528 test_cmp expect actual &&
529 echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
530 test_cmp found actual &&
531 echo symlink 7 >expect &&
532 echo ../morx >>expect &&
533 echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
534 test_cmp expect actual &&
535 echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
536 test_cmp found actual
539 test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
540 echo loop 10 >expect &&
541 echo HEAD:loop1 >>expect &&
542 echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
543 test_cmp expect actual
546 test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
547 echo HEAD:morx | git cat-file --batch >expect &&
548 echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
549 test_cmp expect actual
552 test_expect_success 'cat-file --batch-all-objects shows all objects' '
553 # make new repos so we know the full set of objects; we will
554 # also make sure that there are some packed and some loose
555 # objects, some referenced and some not, some duplicates, and that
556 # there are some available only via alternates.
557 git init all-one &&
559 cd all-one &&
560 echo content >file &&
561 git add file &&
562 git commit -qm base &&
563 git rev-parse HEAD HEAD^{tree} HEAD:file &&
564 git repack -ad &&
565 echo not-cloned | git hash-object -w --stdin
566 ) >expect.unsorted &&
567 git clone -s all-one all-two &&
569 cd all-two &&
570 echo local-unref | git hash-object -w --stdin
571 ) >>expect.unsorted &&
572 git -C all-two rev-parse HEAD:file |
573 git -C all-two pack-objects .git/objects/pack/pack &&
574 sort <expect.unsorted >expect &&
575 git -C all-two cat-file --batch-all-objects \
576 --batch-check="%(objectname)" >actual &&
577 test_cmp expect actual
580 # The only user-visible difference is that the objects are no longer sorted,
581 # and the resulting sort order is undefined. So we can only check that it
582 # produces the same objects as the ordered case, but that at least exercises
583 # the code.
584 test_expect_success 'cat-file --unordered works' '
585 git -C all-two cat-file --batch-all-objects --unordered \
586 --batch-check="%(objectname)" >actual.unsorted &&
587 sort <actual.unsorted >actual &&
588 test_cmp expect actual
591 test_done