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]$//')"
31 batch_output="$sha1 $type $size
34 test_expect_success "$type exists" '
38 test_expect_success "Type of $type is correct" '
40 git cat-file -t $sha1 >actual &&
41 test_cmp expect actual
44 test_expect_success "Size of $type is correct" '
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" '
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" '
58 git cat-file -s --allow-unknown-type $sha1 >actual &&
59 test_cmp expect actual
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
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)" '
105 maybe_remove_timestamp "$content" $no_ts
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)" '
116 maybe_remove_timestamp "$content" $no_ts
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 tree_sha1=$(git write-tree)
145 tree_pretty_content="100644 blob $hello_sha1 hello"
147 run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
149 commit_message="Initial commit"
150 commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
152 commit_content="tree $tree_sha1
153 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
154 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
158 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
160 tag_header_without_timestamp="object $hello_sha1
163 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
164 tag_description="This is a tag"
165 tag_content="$tag_header_without_timestamp 0000000000 +0000
169 tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
170 tag_size=$(strlen "$tag_content")
172 run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
174 test_expect_success \
175 "Reach a blob from a tag pointing to it" \
176 "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
178 for batch in batch batch-check
182 test_expect_success "Passing -$opt with --$batch fails" '
183 test_must_fail git cat-file --$batch -$opt $hello_sha1
186 test_expect_success "Passing --$batch with -$opt fails" '
187 test_must_fail git cat-file -$opt --$batch $hello_sha1
191 test_expect_success "Passing <type> with --$batch fails" '
192 test_must_fail git cat-file --$batch blob $hello_sha1
195 test_expect_success "Passing --$batch with <type> fails" '
196 test_must_fail git cat-file blob --$batch $hello_sha1
199 test_expect_success "Passing sha1 with --$batch fails" '
200 test_must_fail git cat-file --$batch $hello_sha1
206 test_expect_success "Passing -$opt with --follow-symlinks fails" '
207 test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
211 test_expect_success "--batch-check for a non-existent named object" '
212 test "foobar42 missing
213 foobar84 missing" = \
214 "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
217 test_expect_success "--batch-check for a non-existent hash" '
218 test "0000000000000000000000000000000000000042 missing
219 0000000000000000000000000000000000000084 missing" = \
220 "$( ( echo 0000000000000000000000000000000000000042;
221 echo_without_newline 0000000000000000000000000000000000000084; ) \
222 | git cat-file --batch-check)"
225 test_expect_success "--batch for an existent and a non-existent hash" '
226 test "$tag_sha1 tag $tag_size
228 0000000000000000000000000000000000000000 missing" = \
229 "$( ( echo $tag_sha1;
230 echo_without_newline 0000000000000000000000000000000000000000; ) \
231 | git cat-file --batch)"
234 test_expect_success "--batch-check for an emtpy line" '
235 test " missing" = "$(echo | git cat-file --batch-check)"
238 test_expect_success 'empty --batch-check notices missing object' '
239 echo "$_z40 missing" >expect &&
240 echo "$_z40" | git cat-file --batch-check="" >actual &&
241 test_cmp expect actual
244 batch_input="$hello_sha1
251 batch_output="$hello_sha1 blob $hello_size
253 $commit_sha1 commit $commit_size
255 $tag_sha1 tag $tag_size
260 test_expect_success '--batch with multiple sha1s gives correct format' '
261 test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
264 batch_check_input="$hello_sha1
272 batch_check_output="$hello_sha1 blob $hello_size
273 $tree_sha1 tree $tree_size
274 $commit_sha1 commit $commit_size
275 $tag_sha1 tag $tag_size
279 test_expect_success "--batch-check with multiple sha1s gives correct format" '
280 test "$batch_check_output" = \
281 "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
284 test_expect_success 'setup blobs which are likely to delta' '
285 test-genrandom foo 10240 >foo &&
286 { cat foo; echo plus; } >foo-plus &&
287 git add foo foo-plus &&
295 test_expect_success 'confirm that neither loose blob is a delta' '
296 cat >expect <<-EOF &&
300 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
301 test_cmp expect actual
304 # To avoid relying too much on the current delta heuristics,
305 # we will check only that one of the two objects is a delta
306 # against the other, but not the order. We can do so by just
307 # asking for the base of both, and checking whether either
308 # sha1 appears in the output.
309 test_expect_success '%(deltabase) reports packed delta bases' '
311 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
313 grep "$(git rev-parse HEAD:foo)" actual ||
314 grep "$(git rev-parse HEAD:foo-plus)" actual
319 bogus_content="bogus"
320 bogus_size=$(strlen "$bogus_content")
321 bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
323 test_expect_success "Type of broken object is correct" '
324 echo $bogus_type >expect &&
325 git cat-file -t --allow-unknown-type $bogus_sha1 >actual &&
326 test_cmp expect actual
329 test_expect_success "Size of broken object is correct" '
330 echo $bogus_size >expect &&
331 git cat-file -s --allow-unknown-type $bogus_sha1 >actual &&
332 test_cmp expect actual
334 bogus_type="abcdefghijklmnopqrstuvwxyz1234679"
335 bogus_content="bogus"
336 bogus_size=$(strlen "$bogus_content")
337 bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
339 test_expect_success "Type of broken object is correct when type is large" '
340 echo $bogus_type >expect &&
341 git cat-file -t --allow-unknown-type $bogus_sha1 >actual &&
342 test_cmp expect actual
345 test_expect_success "Size of large broken object is correct when type is large" '
346 echo $bogus_size >expect &&
347 git cat-file -s --allow-unknown-type $bogus_sha1 >actual &&
348 test_cmp expect actual
351 # Tests for git cat-file --follow-symlinks
352 test_expect_success 'prep for symlink tests' '
353 echo_without_newline "$hello_content" >morx &&
354 test_ln_s_add morx same-dir-link &&
355 test_ln_s_add dir link-to-dir &&
356 test_ln_s_add ../fleem out-of-repo-link &&
357 test_ln_s_add .. out-of-repo-link-dir &&
358 test_ln_s_add same-dir-link link-to-link &&
359 test_ln_s_add nope broken-same-dir-link &&
361 test_ln_s_add ../morx dir/parent-dir-link &&
362 test_ln_s_add .. dir/link-dir &&
363 test_ln_s_add ../../escape dir/out-of-repo-link &&
364 test_ln_s_add ../.. dir/out-of-repo-link-dir &&
365 test_ln_s_add nope dir/broken-link-in-dir &&
367 test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
368 test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
369 test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
370 test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
371 test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
372 echo_without_newline "$hello_content" >dir/subdir/ind2 &&
373 echo_without_newline "$hello_content" >dir/ind1 &&
374 test_ln_s_add dir dirlink &&
375 test_ln_s_add dir/subdir subdirlink &&
376 test_ln_s_add subdir/ind2 dir/link-to-child &&
377 test_ln_s_add dir/link-to-child link-to-down-link &&
378 test_ln_s_add dir/.. up-down &&
379 test_ln_s_add dir/../ up-down-trailing &&
380 test_ln_s_add dir/../morx up-down-file &&
381 test_ln_s_add dir/../../morx up-up-down-file &&
382 test_ln_s_add subdirlink/../../morx up-two-down-file &&
383 test_ln_s_add loop1 loop2 &&
384 test_ln_s_add loop2 loop1 &&
385 git add morx dir/subdir/ind2 dir/ind1 &&
386 git commit -am "test" &&
387 echo $hello_sha1 blob $hello_size >found
390 test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
391 echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
392 test_cmp found actual &&
393 echo HEAD:nope missing >expect &&
394 echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
395 test_cmp expect actual
398 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
399 echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
400 test_cmp found actual
403 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
404 echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
405 test_cmp found actual
409 test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
410 echo dangling 25 >expect &&
411 echo HEAD:broken-same-dir-link >>expect &&
412 echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
413 test_cmp expect actual
416 test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
417 echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
418 test_cmp found actual
421 test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
422 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
423 test_cmp found actual &&
424 echo notdir 29 >expect &&
425 echo HEAD:dir/parent-dir-link/nope >>expect &&
426 echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
427 test_cmp expect actual
430 test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
431 echo dangling 22 >expect &&
432 echo HEAD:dir/link-dir/nope >>expect &&
433 echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
434 test_cmp expect actual &&
435 echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
436 test_cmp found actual &&
437 echo dangling 27 >expect &&
438 echo HEAD:dir/broken-link-in-dir >>expect &&
439 echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
440 test_cmp expect actual
443 test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
444 echo notdir 41 >expect &&
445 echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
446 echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
447 test_cmp expect actual &&
448 echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
449 test_cmp found actual &&
450 echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
451 test_cmp found actual
454 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
455 echo dangling 17 >expect &&
456 echo HEAD:dirlink/morx >>expect &&
457 echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
458 test_cmp expect actual &&
459 echo $hello_sha1 blob $hello_size >expect &&
460 echo HEAD:dirlink/ind1 | 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 dir/subdir links' '
465 echo dangling 20 >expect &&
466 echo HEAD:subdirlink/morx >>expect &&
467 echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
468 test_cmp expect actual &&
469 echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
470 test_cmp found actual
473 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
474 echo notdir 27 >expect &&
475 echo HEAD:dir/link-to-child/morx >>expect &&
476 echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
477 test_cmp expect actual &&
478 echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
479 test_cmp found actual &&
480 echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
481 test_cmp found actual
484 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
485 echo symlink 8 >expect &&
486 echo ../fleem >>expect &&
487 echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
488 test_cmp expect actual &&
489 echo symlink 2 >expect &&
491 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
492 test_cmp expect actual
495 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
496 echo symlink 9 >expect &&
497 echo ../escape >>expect &&
498 echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
499 test_cmp expect actual &&
500 echo symlink 2 >expect &&
502 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
503 test_cmp expect actual
506 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
507 echo symlink 15 >expect &&
508 echo ../great-escape >>expect &&
509 echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
510 test_cmp expect actual &&
511 echo symlink 2 >expect &&
513 echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
514 test_cmp expect actual &&
515 echo symlink 3 >expect &&
517 echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
518 test_cmp expect actual
521 test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
522 echo HEAD: | git cat-file --batch-check >expect &&
523 echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
524 test_cmp expect actual &&
525 echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
526 test_cmp expect actual &&
527 echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
528 test_cmp found actual &&
529 echo symlink 7 >expect &&
530 echo ../morx >>expect &&
531 echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
532 test_cmp expect actual &&
533 echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
534 test_cmp found actual
537 test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
538 echo loop 10 >expect &&
539 echo HEAD:loop1 >>expect &&
540 echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
541 test_cmp expect actual
544 test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
545 echo HEAD:morx | git cat-file --batch >expect &&
546 echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
547 test_cmp expect actual