3 test_description='git cat-file'
7 echo_without_newline () {
12 echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
15 maybe_remove_timestamp () {
17 echo_without_newline "$1"
19 echo_without_newline "$(printf '%s\n' "$1" | sed -e 's/ [0-9][0-9]* [-+][0-9][0-9][0-9][0-9]$//')"
29 test_expect_success "$name" '
30 echo "$expected" >expect &&
31 echo $sha1 | git cat-file --batch-check="$atoms" >actual &&
32 test_cmp expect actual
44 batch_output="$sha1 $type $size
47 test_expect_success "$type exists" '
51 test_expect_success "Type of $type is correct" '
53 git cat-file -t $sha1 >actual &&
54 test_cmp expect actual
57 test_expect_success "Size of $type is correct" '
59 git cat-file -s $sha1 >actual &&
60 test_cmp expect actual
63 test_expect_success "Type of $type is correct using --allow-unknown-type" '
65 git cat-file -t --allow-unknown-type $sha1 >actual &&
66 test_cmp expect actual
69 test_expect_success "Size of $type is correct using --allow-unknown-type" '
71 git cat-file -s --allow-unknown-type $sha1 >actual &&
72 test_cmp expect actual
76 test_expect_success "Content of $type is correct" '
77 maybe_remove_timestamp "$content" $no_ts >expect &&
78 maybe_remove_timestamp "$(git cat-file $type $sha1)" $no_ts >actual &&
79 test_cmp expect actual
82 test_expect_success "Pretty content of $type is correct" '
83 maybe_remove_timestamp "$pretty_content" $no_ts >expect &&
84 maybe_remove_timestamp "$(git cat-file -p $sha1)" $no_ts >actual &&
85 test_cmp expect actual
89 test_expect_success "--batch output of $type is correct" '
90 maybe_remove_timestamp "$batch_output" $no_ts >expect &&
91 maybe_remove_timestamp "$(echo $sha1 | git cat-file --batch)" $no_ts >actual &&
92 test_cmp expect actual
95 test_expect_success "--batch-check output of $type is correct" '
96 echo "$sha1 $type $size" >expect &&
97 echo_without_newline $sha1 | git cat-file --batch-check >actual &&
98 test_cmp expect actual
101 test_expect_success "custom --batch-check format" '
102 echo "$type $sha1" >expect &&
103 echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
104 test_cmp expect actual
107 test_expect_success '--batch-check with %(rest)' '
108 echo "$type this is some extra content" >expect &&
109 echo "$sha1 this is some extra content" |
110 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
111 test_cmp expect actual
114 test -z "$content" ||
115 test_expect_success "--batch without type ($type)" '
118 maybe_remove_timestamp "$content" $no_ts
120 echo $sha1 | git cat-file --batch="%(objectsize)" >actual.full &&
121 maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
122 test_cmp expect actual
125 test -z "$content" ||
126 test_expect_success "--batch without size ($type)" '
129 maybe_remove_timestamp "$content" $no_ts
131 echo $sha1 | git cat-file --batch="%(objecttype)" >actual.full &&
132 maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
133 test_cmp expect actual
136 for atom in refname parent body trailers upstream push symref flag
138 test_atom "Check %($atom) gives empty output" "$sha1" "%($atom)" ""
141 test_atom "Check %(HEAD) gives only one space as output" "$sha1" '%(HEAD)' ' '
144 hello_content="Hello World"
145 hello_size=$(strlen "$hello_content")
146 hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
148 test_expect_success "setup" '
149 echo_without_newline "$hello_content" > hello &&
150 git update-index --add hello
153 run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
155 test_expect_success '--batch-check without %(rest) considers whole line' '
156 echo "$hello_sha1 blob $hello_size" >expect &&
157 git update-index --add --cacheinfo 100644 $hello_sha1 "white space" &&
158 test_when_finished "git update-index --remove \"white space\"" &&
159 echo ":white space" | git cat-file --batch-check >actual &&
160 test_cmp expect actual
163 shortname=`echo $hello_sha1 | sed 's/^.\{0\}\(.\{7\}\).*/\1/'`
164 test_atom 'Check format option %(objectname:short) works' "$hello_sha1" '%(objectname:short)' "$shortname"
166 test_atom 'Check format option %(align) is not broken' \
167 "$hello_sha1" "%(align:8)%(objecttype)%(end)%(objectname)" "blob $hello_sha1"
169 tree_sha1=$(git write-tree)
171 tree_pretty_content="100644 blob $hello_sha1 hello"
173 run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
175 commit_message="Initial commit"
176 commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
178 commit_content="tree $tree_sha1
179 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
180 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
184 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
186 test_atom "Check format option %(if) is not broken" "$commit_sha1" \
187 "%(if)%(author)%(then)%(objectname)%(end)" "$commit_sha1"
188 test_atom "Check %(tree) works for commit" "$commit_sha1" "%(tree)" "$tree_sha1"
189 test_atom "Check %(numparent) works for commit" "$commit_sha1" "%(numparent)" "0"
190 test_atom "Check %(authorname) works for commit" "$commit_sha1" "%(authorname)" "$GIT_AUTHOR_NAME"
191 test_atom "Check %(authoremail) works for commit" "$commit_sha1" "%(authoremail)" "<$GIT_AUTHOR_EMAIL>"
192 test_atom "Check %(committername) works for commit" "$commit_sha1" "%(committername)" "$GIT_COMMITTER_NAME"
193 test_atom "Check %(committeremail) works for commit" "$commit_sha1" "%(committeremail)" "<$GIT_COMMITTER_EMAIL>"
194 test_atom "Check %(subject) works for commit" "$commit_sha1" "%(subject)" "$commit_message"
195 test_atom "Check %(contents) works for commit" "$commit_sha1" "%(contents)" "$commit_message"
197 tag_header_without_timestamp="object $hello_sha1
200 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
201 tag_description="This is a tag"
202 tag_content="$tag_header_without_timestamp 0000000000 +0000
206 tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
207 tag_size=$(strlen "$tag_content")
209 run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
211 test_atom "Check %(object) works for tag" "$tag_sha1" "%(object)" "$hello_sha1"
212 test_atom "Check %(type) works for tag" "$tag_sha1" "%(type)" "blob"
213 test_atom "Check %(tag) works for tag" "$tag_sha1" "%(tag)" "hellotag"
214 test_atom "Check %(taggername) works for tag" "$tag_sha1" "%(taggername)" "$GIT_COMMITTER_NAME"
215 test_atom "Check %(taggeremail) works for tag" "$tag_sha1" "%(taggeremail)" "<$GIT_COMMITTER_EMAIL>"
216 test_atom "Check %(subject) works for tag" "$tag_sha1" "%(subject)" "$tag_description"
217 test_atom "Check %(contents) works for tag" "$tag_sha1" "%(contents)" "$tag_description"
219 test_atom "Check %(color) gives no additional output" "$sha1" \
220 "%(objectname) %(color:green) %(objecttype)" "$sha1 $type"
222 test_expect_success \
223 "Reach a blob from a tag pointing to it" \
224 "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
226 for batch in batch batch-check
230 test_expect_success "Passing -$opt with --$batch fails" '
231 test_must_fail git cat-file --$batch -$opt $hello_sha1
234 test_expect_success "Passing --$batch with -$opt fails" '
235 test_must_fail git cat-file -$opt --$batch $hello_sha1
239 test_expect_success "Passing <type> with --$batch fails" '
240 test_must_fail git cat-file --$batch blob $hello_sha1
243 test_expect_success "Passing --$batch with <type> fails" '
244 test_must_fail git cat-file blob --$batch $hello_sha1
247 test_expect_success "Passing sha1 with --$batch fails" '
248 test_must_fail git cat-file --$batch $hello_sha1
254 test_expect_success "Passing -$opt with --follow-symlinks fails" '
255 test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
259 test_expect_success "--batch-check for a non-existent named object" '
260 test "foobar42 missing
261 foobar84 missing" = \
262 "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
265 test_expect_success "--batch-check for a non-existent hash" '
266 test "0000000000000000000000000000000000000042 missing
267 0000000000000000000000000000000000000084 missing" = \
268 "$( ( echo 0000000000000000000000000000000000000042;
269 echo_without_newline 0000000000000000000000000000000000000084; ) \
270 | git cat-file --batch-check)"
273 test_expect_success "--batch for an existent and a non-existent hash" '
274 test "$tag_sha1 tag $tag_size
276 0000000000000000000000000000000000000000 missing" = \
277 "$( ( echo $tag_sha1;
278 echo_without_newline 0000000000000000000000000000000000000000; ) \
279 | git cat-file --batch)"
282 test_expect_success "--batch-check for an empty line" '
283 test " missing" = "$(echo | git cat-file --batch-check)"
286 test_expect_success 'empty --batch-check notices missing object' '
287 echo "$_z40 missing" >expect &&
288 echo "$_z40" | git cat-file --batch-check="" >actual &&
289 test_cmp expect actual
292 batch_input="$hello_sha1
299 batch_output="$hello_sha1 blob $hello_size
301 $commit_sha1 commit $commit_size
303 $tag_sha1 tag $tag_size
308 test_expect_success '--batch with multiple sha1s gives correct format' '
309 test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
312 batch_check_input="$hello_sha1
320 batch_check_output="$hello_sha1 blob $hello_size
321 $tree_sha1 tree $tree_size
322 $commit_sha1 commit $commit_size
323 $tag_sha1 tag $tag_size
327 test_expect_success "--batch-check with multiple sha1s gives correct format" '
328 test "$batch_check_output" = \
329 "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
332 test_expect_success 'setup blobs which are likely to delta' '
333 test-genrandom foo 10240 >foo &&
334 { cat foo; echo plus; } >foo-plus &&
335 git add foo foo-plus &&
343 test_expect_success 'confirm that neither loose blob is a delta' '
344 cat >expect <<-EOF &&
348 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
349 test_cmp expect actual
352 # To avoid relying too much on the current delta heuristics,
353 # we will check only that one of the two objects is a delta
354 # against the other, but not the order. We can do so by just
355 # asking for the base of both, and checking whether either
356 # sha1 appears in the output.
357 test_expect_success '%(deltabase) reports packed delta bases' '
359 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
361 grep "$(git rev-parse HEAD:foo)" actual ||
362 grep "$(git rev-parse HEAD:foo-plus)" actual
367 bogus_content="bogus"
368 bogus_size=$(strlen "$bogus_content")
369 bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
371 test_expect_success "Type of broken object is correct" '
372 echo $bogus_type >expect &&
373 git cat-file -t --allow-unknown-type $bogus_sha1 >actual &&
374 test_cmp expect actual
377 test_expect_success "Size of broken object is correct" '
378 echo $bogus_size >expect &&
379 git cat-file -s --allow-unknown-type $bogus_sha1 >actual &&
380 test_cmp expect actual
382 bogus_type="abcdefghijklmnopqrstuvwxyz1234679"
383 bogus_content="bogus"
384 bogus_size=$(strlen "$bogus_content")
385 bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
387 test_expect_success "Type of broken object is correct when type is large" '
388 echo $bogus_type >expect &&
389 git cat-file -t --allow-unknown-type $bogus_sha1 >actual &&
390 test_cmp expect actual
393 test_expect_success "Size of large broken object is correct when type is large" '
394 echo $bogus_size >expect &&
395 git cat-file -s --allow-unknown-type $bogus_sha1 >actual &&
396 test_cmp expect actual
399 # Tests for git cat-file --follow-symlinks
400 test_expect_success 'prep for symlink tests' '
401 echo_without_newline "$hello_content" >morx &&
402 test_ln_s_add morx same-dir-link &&
403 test_ln_s_add dir link-to-dir &&
404 test_ln_s_add ../fleem out-of-repo-link &&
405 test_ln_s_add .. out-of-repo-link-dir &&
406 test_ln_s_add same-dir-link link-to-link &&
407 test_ln_s_add nope broken-same-dir-link &&
409 test_ln_s_add ../morx dir/parent-dir-link &&
410 test_ln_s_add .. dir/link-dir &&
411 test_ln_s_add ../../escape dir/out-of-repo-link &&
412 test_ln_s_add ../.. dir/out-of-repo-link-dir &&
413 test_ln_s_add nope dir/broken-link-in-dir &&
415 test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
416 test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
417 test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
418 test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
419 test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
420 echo_without_newline "$hello_content" >dir/subdir/ind2 &&
421 echo_without_newline "$hello_content" >dir/ind1 &&
422 test_ln_s_add dir dirlink &&
423 test_ln_s_add dir/subdir subdirlink &&
424 test_ln_s_add subdir/ind2 dir/link-to-child &&
425 test_ln_s_add dir/link-to-child link-to-down-link &&
426 test_ln_s_add dir/.. up-down &&
427 test_ln_s_add dir/../ up-down-trailing &&
428 test_ln_s_add dir/../morx up-down-file &&
429 test_ln_s_add dir/../../morx up-up-down-file &&
430 test_ln_s_add subdirlink/../../morx up-two-down-file &&
431 test_ln_s_add loop1 loop2 &&
432 test_ln_s_add loop2 loop1 &&
433 git add morx dir/subdir/ind2 dir/ind1 &&
434 git commit -am "test" &&
435 echo $hello_sha1 blob $hello_size >found
438 test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
439 echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
440 test_cmp found actual &&
441 echo HEAD:nope missing >expect &&
442 echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
443 test_cmp expect actual
446 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
447 echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
448 test_cmp found actual
451 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
452 echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
453 test_cmp found actual
457 test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
458 echo dangling 25 >expect &&
459 echo HEAD:broken-same-dir-link >>expect &&
460 echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
461 test_cmp expect actual
464 test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
465 echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
466 test_cmp found actual
469 test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
470 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
471 test_cmp found actual &&
472 echo notdir 29 >expect &&
473 echo HEAD:dir/parent-dir-link/nope >>expect &&
474 echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
475 test_cmp expect actual
478 test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
479 echo dangling 22 >expect &&
480 echo HEAD:dir/link-dir/nope >>expect &&
481 echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
482 test_cmp expect actual &&
483 echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
484 test_cmp found actual &&
485 echo dangling 27 >expect &&
486 echo HEAD:dir/broken-link-in-dir >>expect &&
487 echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
488 test_cmp expect actual
491 test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
492 echo notdir 41 >expect &&
493 echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
494 echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
495 test_cmp expect actual &&
496 echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
497 test_cmp found actual &&
498 echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
499 test_cmp found actual
502 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
503 echo dangling 17 >expect &&
504 echo HEAD:dirlink/morx >>expect &&
505 echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
506 test_cmp expect actual &&
507 echo $hello_sha1 blob $hello_size >expect &&
508 echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
509 test_cmp expect actual
512 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
513 echo dangling 20 >expect &&
514 echo HEAD:subdirlink/morx >>expect &&
515 echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
516 test_cmp expect actual &&
517 echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
518 test_cmp found actual
521 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
522 echo notdir 27 >expect &&
523 echo HEAD:dir/link-to-child/morx >>expect &&
524 echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
525 test_cmp expect actual &&
526 echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
527 test_cmp found actual &&
528 echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
529 test_cmp found actual
532 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
533 echo symlink 8 >expect &&
534 echo ../fleem >>expect &&
535 echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
536 test_cmp expect actual &&
537 echo symlink 2 >expect &&
539 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
540 test_cmp expect actual
543 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
544 echo symlink 9 >expect &&
545 echo ../escape >>expect &&
546 echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
547 test_cmp expect actual &&
548 echo symlink 2 >expect &&
550 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
551 test_cmp expect actual
554 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
555 echo symlink 15 >expect &&
556 echo ../great-escape >>expect &&
557 echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
558 test_cmp expect actual &&
559 echo symlink 2 >expect &&
561 echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
562 test_cmp expect actual &&
563 echo symlink 3 >expect &&
565 echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
566 test_cmp expect actual
569 test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
570 echo HEAD: | git cat-file --batch-check >expect &&
571 echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
572 test_cmp expect actual &&
573 echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
574 test_cmp expect actual &&
575 echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
576 test_cmp found actual &&
577 echo symlink 7 >expect &&
578 echo ../morx >>expect &&
579 echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
580 test_cmp expect actual &&
581 echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
582 test_cmp found actual
585 test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
586 echo loop 10 >expect &&
587 echo HEAD:loop1 >>expect &&
588 echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
589 test_cmp expect actual
592 test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
593 echo HEAD:morx | git cat-file --batch >expect &&
594 echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
595 test_cmp expect actual
598 test_expect_success 'cat-file --batch-all-objects shows all objects' '
599 # make new repos so we know the full set of objects; we will
600 # also make sure that there are some packed and some loose
601 # objects, some referenced and some not, and that there are
602 # some available only via alternates.
606 echo content >file &&
608 git commit -qm base &&
609 git rev-parse HEAD HEAD^{tree} HEAD:file &&
611 echo not-cloned | git hash-object -w --stdin
612 ) >expect.unsorted &&
613 git clone -s all-one all-two &&
616 echo local-unref | git hash-object -w --stdin
617 ) >>expect.unsorted &&
618 sort <expect.unsorted >expect &&
619 git -C all-two cat-file --batch-all-objects \
620 --batch-check="%(objectname)" >actual &&
621 test_cmp expect actual