read-cache: touch shared index files when used
[alt-git.git] / t / t1006-cat-file.sh
blobb19f3326946203409fe0e428b9fc73d34134d756
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 tree_sha1=$(git write-tree)
144 tree_size=33
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)
151 commit_size=177
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
156 $commit_message"
158 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
160 tag_header_without_timestamp="object $hello_sha1
161 type blob
162 tag hellotag
163 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
164 tag_description="This is a tag"
165 tag_content="$tag_header_without_timestamp 0000000000 +0000
167 $tag_description"
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
180 for opt in t s e p
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
189 done
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
202 done
204 for opt in t s e p
206 test_expect_success "Passing -$opt with --follow-symlinks fails" '
207 test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
209 done
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
227 $tag_content
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 empty 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
245 $commit_sha1
246 $tag_sha1
247 deadbeef
251 batch_output="$hello_sha1 blob $hello_size
252 $hello_content
253 $commit_sha1 commit $commit_size
254 $commit_content
255 $tag_sha1 tag $tag_size
256 $tag_content
257 deadbeef missing
258 missing"
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
265 $tree_sha1
266 $commit_sha1
267 $tag_sha1
268 deadbeef
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
276 deadbeef missing
277 missing"
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 &&
288 git commit -m foo &&
289 cat >blobs <<-\EOF
290 HEAD:foo
291 HEAD:foo-plus
295 test_expect_success 'confirm that neither loose blob is a delta' '
296 cat >expect <<-EOF &&
297 $_z40
298 $_z40
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' '
310 git repack -ad &&
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
318 bogus_type="bogus"
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 &&
360 mkdir dir &&
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 &&
366 mkdir dir/subdir &&
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 &&
490 echo .. >>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 &&
501 echo .. >>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 &&
512 echo .. >>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 &&
516 echo ../ >>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
550 test_expect_success 'cat-file --batch-all-objects shows all objects' '
551 # make new repos so we know the full set of objects; we will
552 # also make sure that there are some packed and some loose
553 # objects, some referenced and some not, and that there are
554 # some available only via alternates.
555 git init all-one &&
557 cd all-one &&
558 echo content >file &&
559 git add file &&
560 git commit -qm base &&
561 git rev-parse HEAD HEAD^{tree} HEAD:file &&
562 git repack -ad &&
563 echo not-cloned | git hash-object -w --stdin
564 ) >expect.unsorted &&
565 git clone -s all-one all-two &&
567 cd all-two &&
568 echo local-unref | git hash-object -w --stdin
569 ) >>expect.unsorted &&
570 sort <expect.unsorted >expect &&
571 git -C all-two cat-file --batch-all-objects \
572 --batch-check="%(objectname)" >actual &&
573 test_cmp expect actual
576 test_done