1#!/bin/sh
2
3test_description='git cat-file'
4
5. ./test-lib.sh
6
7echo_without_newline () {
8    printf '%s' "$*"
9}
10
11strlen () {
12    echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
13}
14
15maybe_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]$//')"
20    fi
21}
22
23run_tests () {
24    type=$1
25    sha1=$2
26    size=$3
27    content=$4
28    pretty_content=$5
29    no_ts=$6
30
31    batch_output="$sha1 $type $size
32$content"
33
34    test_expect_success "$type exists" '
35	git cat-file -e $sha1
36    '
37
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
42    '
43
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
48    '
49
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
54    '
55
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
60    '
61
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
67    '
68
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
73    '
74
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
80    '
81
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
86    '
87
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
92    '
93
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
99    '
100
101    test -z "$content" ||
102    test_expect_success "--batch without type ($type)" '
103	{
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
110    '
111
112    test -z "$content" ||
113    test_expect_success "--batch without size ($type)" '
114	{
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
121    '
122}
123
124hello_content="Hello World"
125hello_size=$(strlen "$hello_content")
126hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
127
128test_expect_success "setup" '
129	echo_without_newline "$hello_content" > hello &&
130	git update-index --add hello
131'
132
133run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
134
135test_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
141'
142
143tree_sha1=$(git write-tree)
144tree_size=$(($(test_oid rawsz) + 13))
145tree_pretty_content="100644 blob $hello_sha1	hello"
146
147run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
148
149commit_message="Initial commit"
150commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
151commit_size=$(($(test_oid hexsz) + 137))
152commit_content="tree $tree_sha1
153author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
154committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
155
156$commit_message"
157
158run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
159
160tag_header_without_timestamp="object $hello_sha1
161type blob
162tag hellotag
163tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
164tag_description="This is a tag"
165tag_content="$tag_header_without_timestamp 0000000000 +0000
166
167$tag_description"
168
169tag_sha1=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w)
170tag_size=$(strlen "$tag_content")
171
172run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
173
174test_expect_success \
175    "Reach a blob from a tag pointing to it" \
176    "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
177
178for batch in batch batch-check
179do
180    for opt in t s e p
181    do
182	test_expect_success "Passing -$opt with --$batch fails" '
183	    test_must_fail git cat-file --$batch -$opt $hello_sha1
184	'
185
186	test_expect_success "Passing --$batch with -$opt fails" '
187	    test_must_fail git cat-file -$opt --$batch $hello_sha1
188	'
189    done
190
191    test_expect_success "Passing <type> with --$batch fails" '
192	test_must_fail git cat-file --$batch blob $hello_sha1
193    '
194
195    test_expect_success "Passing --$batch with <type> fails" '
196	test_must_fail git cat-file blob --$batch $hello_sha1
197    '
198
199    test_expect_success "Passing sha1 with --$batch fails" '
200	test_must_fail git cat-file --$batch $hello_sha1
201    '
202done
203
204for opt in t s e p
205do
206    test_expect_success "Passing -$opt with --follow-symlinks fails" '
207	    test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
208	'
209done
210
211test_expect_success "--batch-check for a non-existent named object" '
212    test "foobar42 missing
213foobar84 missing" = \
214    "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
215'
216
217test_expect_success "--batch-check for a non-existent hash" '
218    test "0000000000000000000000000000000000000042 missing
2190000000000000000000000000000000000000084 missing" = \
220    "$( ( echo 0000000000000000000000000000000000000042;
221	 echo_without_newline 0000000000000000000000000000000000000084; ) |
222       git cat-file --batch-check)"
223'
224
225test_expect_success "--batch for an existent and a non-existent hash" '
226    test "$tag_sha1 tag $tag_size
227$tag_content
2280000000000000000000000000000000000000000 missing" = \
229    "$( ( echo $tag_sha1;
230	 echo_without_newline 0000000000000000000000000000000000000000; ) |
231       git cat-file --batch)"
232'
233
234test_expect_success "--batch-check for an empty line" '
235    test " missing" = "$(echo | git cat-file --batch-check)"
236'
237
238test_expect_success 'empty --batch-check notices missing object' '
239	echo "$ZERO_OID missing" >expect &&
240	echo "$ZERO_OID" | git cat-file --batch-check="" >actual &&
241	test_cmp expect actual
242'
243
244batch_input="$hello_sha1
245$commit_sha1
246$tag_sha1
247deadbeef
248
249"
250
251batch_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
257deadbeef missing
258 missing"
259
260test_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)"
262'
263
264batch_check_input="$hello_sha1
265$tree_sha1
266$commit_sha1
267$tag_sha1
268deadbeef
269
270"
271
272batch_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
276deadbeef missing
277 missing"
278
279test_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)"
282'
283
284test_expect_success 'setup blobs which are likely to delta' '
285	test-tool 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
292	EOF
293'
294
295test_expect_success 'confirm that neither loose blob is a delta' '
296	cat >expect <<-EOF &&
297	$ZERO_OID
298	$ZERO_OID
299	EOF
300	git cat-file --batch-check="%(deltabase)" <blobs >actual &&
301	test_cmp expect actual
302'
303
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.
309test_expect_success '%(deltabase) reports packed delta bases' '
310	git repack -ad &&
311	git cat-file --batch-check="%(deltabase)" <blobs >actual &&
312	{
313		grep "$(git rev-parse HEAD:foo)" actual ||
314		grep "$(git rev-parse HEAD:foo-plus)" actual
315	}
316'
317
318test_expect_success 'setup bogus data' '
319	bogus_short_type="bogus" &&
320	bogus_short_content="bogus" &&
321	bogus_short_size=$(strlen "$bogus_short_content") &&
322	bogus_short_sha1=$(echo_without_newline "$bogus_short_content" | git hash-object -t $bogus_short_type --literally -w --stdin) &&
323
324	bogus_long_type="abcdefghijklmnopqrstuvwxyz1234679" &&
325	bogus_long_content="bogus" &&
326	bogus_long_size=$(strlen "$bogus_long_content") &&
327	bogus_long_sha1=$(echo_without_newline "$bogus_long_content" | git hash-object -t $bogus_long_type --literally -w --stdin)
328'
329
330for arg1 in '' --allow-unknown-type
331do
332	for arg2 in -s -t -p
333	do
334		if test "$arg1" = "--allow-unknown-type" && test "$arg2" = "-p"
335		then
336			continue
337		fi
338
339
340		test_expect_success "cat-file $arg1 $arg2 error on bogus short OID" '
341			cat >expect <<-\EOF &&
342			fatal: invalid object type
343			EOF
344
345			if test "$arg1" = "--allow-unknown-type"
346			then
347				git cat-file $arg1 $arg2 $bogus_short_sha1
348			else
349				test_must_fail git cat-file $arg1 $arg2 $bogus_short_sha1 >out 2>actual &&
350				test_must_be_empty out &&
351				test_cmp expect actual
352			fi
353		'
354
355		test_expect_success "cat-file $arg1 $arg2 error on bogus full OID" '
356			if test "$arg2" = "-p"
357			then
358				cat >expect <<-EOF
359				error: header for $bogus_long_sha1 too long, exceeds 32 bytes
360				fatal: Not a valid object name $bogus_long_sha1
361				EOF
362			else
363				cat >expect <<-EOF
364				error: header for $bogus_long_sha1 too long, exceeds 32 bytes
365				fatal: git cat-file: could not get object info
366				EOF
367			fi &&
368
369			if test "$arg1" = "--allow-unknown-type"
370			then
371				git cat-file $arg1 $arg2 $bogus_short_sha1
372			else
373				test_must_fail git cat-file $arg1 $arg2 $bogus_long_sha1 >out 2>actual &&
374				test_must_be_empty out &&
375				test_cmp expect actual
376			fi
377		'
378
379		test_expect_success "cat-file $arg1 $arg2 error on missing short OID" '
380			cat >expect.err <<-EOF &&
381			fatal: Not a valid object name $(test_oid deadbeef_short)
382			EOF
383			test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef_short) >out 2>err.actual &&
384			test_must_be_empty out
385		'
386
387		test_expect_success "cat-file $arg1 $arg2 error on missing full OID" '
388			if test "$arg2" = "-p"
389			then
390				cat >expect.err <<-EOF
391				fatal: Not a valid object name $(test_oid deadbeef)
392				EOF
393			else
394				cat >expect.err <<-\EOF
395				fatal: git cat-file: could not get object info
396				EOF
397			fi &&
398			test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef) >out 2>err.actual &&
399			test_must_be_empty out &&
400			test_cmp expect.err err.actual
401		'
402	done
403done
404
405test_expect_success '-e is OK with a broken object without --allow-unknown-type' '
406	git cat-file -e $bogus_short_sha1
407'
408
409test_expect_success '-e can not be combined with --allow-unknown-type' '
410	test_expect_code 128 git cat-file -e --allow-unknown-type $bogus_short_sha1
411'
412
413test_expect_success '-p cannot print a broken object even with --allow-unknown-type' '
414	test_must_fail git cat-file -p $bogus_short_sha1 &&
415	test_expect_code 128 git cat-file -p --allow-unknown-type $bogus_short_sha1
416'
417
418test_expect_success '<type> <hash> does not work with objects of broken types' '
419	cat >err.expect <<-\EOF &&
420	fatal: invalid object type "bogus"
421	EOF
422	test_must_fail git cat-file $bogus_short_type $bogus_short_sha1 2>err.actual &&
423	test_cmp err.expect err.actual
424'
425
426test_expect_success 'broken types combined with --batch and --batch-check' '
427	echo $bogus_short_sha1 >bogus-oid &&
428
429	cat >err.expect <<-\EOF &&
430	fatal: invalid object type
431	EOF
432
433	test_must_fail git cat-file --batch <bogus-oid 2>err.actual &&
434	test_cmp err.expect err.actual &&
435
436	test_must_fail git cat-file --batch-check <bogus-oid 2>err.actual &&
437	test_cmp err.expect err.actual
438'
439
440test_expect_success 'the --batch and --batch-check options do not combine with --allow-unknown-type' '
441	test_expect_code 128 git cat-file --batch --allow-unknown-type <bogus-oid &&
442	test_expect_code 128 git cat-file --batch-check --allow-unknown-type <bogus-oid
443'
444
445test_expect_success 'the --allow-unknown-type option does not consider replacement refs' '
446	cat >expect <<-EOF &&
447	$bogus_short_type
448	EOF
449	git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
450	test_cmp expect actual &&
451
452	# Create it manually, as "git replace" will die on bogus
453	# types.
454	head=$(git rev-parse --verify HEAD) &&
455	test_when_finished "rm -rf .git/refs/replace" &&
456	mkdir -p .git/refs/replace &&
457	echo $head >.git/refs/replace/$bogus_short_sha1 &&
458
459	cat >expect <<-EOF &&
460	commit
461	EOF
462	git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
463	test_cmp expect actual
464'
465
466test_expect_success "Type of broken object is correct" '
467	echo $bogus_short_type >expect &&
468	git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
469	test_cmp expect actual
470'
471
472test_expect_success "Size of broken object is correct" '
473	echo $bogus_short_size >expect &&
474	git cat-file -s --allow-unknown-type $bogus_short_sha1 >actual &&
475	test_cmp expect actual
476'
477
478test_expect_success 'clean up broken object' '
479	rm .git/objects/$(test_oid_to_path $bogus_short_sha1)
480'
481
482test_expect_success "Type of broken object is correct when type is large" '
483	echo $bogus_long_type >expect &&
484	git cat-file -t --allow-unknown-type $bogus_long_sha1 >actual &&
485	test_cmp expect actual
486'
487
488test_expect_success "Size of large broken object is correct when type is large" '
489	echo $bogus_long_size >expect &&
490	git cat-file -s --allow-unknown-type $bogus_long_sha1 >actual &&
491	test_cmp expect actual
492'
493
494test_expect_success 'clean up broken object' '
495	rm .git/objects/$(test_oid_to_path $bogus_long_sha1)
496'
497
498test_expect_success 'cat-file -t and -s on corrupt loose object' '
499	git init --bare corrupt-loose.git &&
500	(
501		cd corrupt-loose.git &&
502
503		# Setup and create the empty blob and its path
504		empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) &&
505		git hash-object -w --stdin </dev/null &&
506
507		# Create another blob and its path
508		echo other >other.blob &&
509		other_blob=$(git hash-object -w --stdin <other.blob) &&
510		other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) &&
511
512		# Before the swap the size is 0
513		cat >out.expect <<-EOF &&
514		0
515		EOF
516		git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
517		test_must_be_empty err.actual &&
518		test_cmp out.expect out.actual &&
519
520		# Swap the two to corrupt the repository
521		mv -f "$other_path" "$empty_path" &&
522		test_must_fail git fsck 2>err.fsck &&
523		grep "hash-path mismatch" err.fsck &&
524
525		# confirm that cat-file is reading the new swapped-in
526		# blob...
527		cat >out.expect <<-EOF &&
528		blob
529		EOF
530		git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual &&
531		test_must_be_empty err.actual &&
532		test_cmp out.expect out.actual &&
533
534		# ... since it has a different size now.
535		cat >out.expect <<-EOF &&
536		6
537		EOF
538		git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
539		test_must_be_empty err.actual &&
540		test_cmp out.expect out.actual &&
541
542		# So far "cat-file" has been happy to spew the found
543		# content out as-is. Try to make it zlib-invalid.
544		mv -f other.blob "$empty_path" &&
545		test_must_fail git fsck 2>err.fsck &&
546		grep "^error: inflate: data stream error (" err.fsck
547	)
548'
549
550# Tests for git cat-file --follow-symlinks
551test_expect_success 'prep for symlink tests' '
552	echo_without_newline "$hello_content" >morx &&
553	test_ln_s_add morx same-dir-link &&
554	test_ln_s_add dir link-to-dir &&
555	test_ln_s_add ../fleem out-of-repo-link &&
556	test_ln_s_add .. out-of-repo-link-dir &&
557	test_ln_s_add same-dir-link link-to-link &&
558	test_ln_s_add nope broken-same-dir-link &&
559	mkdir dir &&
560	test_ln_s_add ../morx dir/parent-dir-link &&
561	test_ln_s_add .. dir/link-dir &&
562	test_ln_s_add ../../escape dir/out-of-repo-link &&
563	test_ln_s_add ../.. dir/out-of-repo-link-dir &&
564	test_ln_s_add nope dir/broken-link-in-dir &&
565	mkdir dir/subdir &&
566	test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
567	test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
568	test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
569	test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
570	test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
571	echo_without_newline "$hello_content" >dir/subdir/ind2 &&
572	echo_without_newline "$hello_content" >dir/ind1 &&
573	test_ln_s_add dir dirlink &&
574	test_ln_s_add dir/subdir subdirlink &&
575	test_ln_s_add subdir/ind2 dir/link-to-child &&
576	test_ln_s_add dir/link-to-child link-to-down-link &&
577	test_ln_s_add dir/.. up-down &&
578	test_ln_s_add dir/../ up-down-trailing &&
579	test_ln_s_add dir/../morx up-down-file &&
580	test_ln_s_add dir/../../morx up-up-down-file &&
581	test_ln_s_add subdirlink/../../morx up-two-down-file &&
582	test_ln_s_add loop1 loop2 &&
583	test_ln_s_add loop2 loop1 &&
584	git add morx dir/subdir/ind2 dir/ind1 &&
585	git commit -am "test" &&
586	echo $hello_sha1 blob $hello_size >found
587'
588
589test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
590	echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
591	test_cmp found actual &&
592	echo HEAD:nope missing >expect &&
593	echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
594	test_cmp expect actual
595'
596
597test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
598	echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
599	test_cmp found actual
600'
601
602test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
603	echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
604	test_cmp found actual
605'
606
607
608test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
609	echo dangling 25 >expect &&
610	echo HEAD:broken-same-dir-link >>expect &&
611	echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
612	test_cmp expect actual
613'
614
615test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
616	echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
617	test_cmp found actual
618'
619
620test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
621	echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
622	test_cmp found actual &&
623	echo notdir 29 >expect &&
624	echo HEAD:dir/parent-dir-link/nope >>expect &&
625	echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
626	test_cmp expect actual
627'
628
629test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
630	echo dangling 22 >expect &&
631	echo HEAD:dir/link-dir/nope >>expect &&
632	echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
633	test_cmp expect actual &&
634	echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
635	test_cmp found actual &&
636	echo dangling 27 >expect &&
637	echo HEAD:dir/broken-link-in-dir >>expect &&
638	echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
639	test_cmp expect actual
640'
641
642test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
643	echo notdir 41 >expect &&
644	echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
645	echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
646	test_cmp expect actual &&
647	echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
648	test_cmp found actual &&
649	echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
650	test_cmp found actual
651'
652
653test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
654	echo dangling 17 >expect &&
655	echo HEAD:dirlink/morx >>expect &&
656	echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
657	test_cmp expect actual &&
658	echo $hello_sha1 blob $hello_size >expect &&
659	echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
660	test_cmp expect actual
661'
662
663test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
664	echo dangling 20 >expect &&
665	echo HEAD:subdirlink/morx >>expect &&
666	echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
667	test_cmp expect actual &&
668	echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
669	test_cmp found actual
670'
671
672test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
673	echo notdir 27 >expect &&
674	echo HEAD:dir/link-to-child/morx >>expect &&
675	echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
676	test_cmp expect actual &&
677	echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
678	test_cmp found actual &&
679	echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
680	test_cmp found actual
681'
682
683test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
684	echo symlink 8 >expect &&
685	echo ../fleem >>expect &&
686	echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
687	test_cmp expect actual &&
688	echo symlink 2 >expect &&
689	echo .. >>expect &&
690	echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
691	test_cmp expect actual
692'
693
694test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
695	echo symlink 9 >expect &&
696	echo ../escape >>expect &&
697	echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
698	test_cmp expect actual &&
699	echo symlink 2 >expect &&
700	echo .. >>expect &&
701	echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
702	test_cmp expect actual
703'
704
705test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
706	echo symlink 15 >expect &&
707	echo ../great-escape >>expect &&
708	echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
709	test_cmp expect actual &&
710	echo symlink 2 >expect &&
711	echo .. >>expect &&
712	echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
713	test_cmp expect actual &&
714	echo symlink 3 >expect &&
715	echo ../ >>expect &&
716	echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
717	test_cmp expect actual
718'
719
720test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
721	echo HEAD: | git cat-file --batch-check >expect &&
722	echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
723	test_cmp expect actual &&
724	echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
725	test_cmp expect actual &&
726	echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
727	test_cmp found actual &&
728	echo symlink 7 >expect &&
729	echo ../morx >>expect &&
730	echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
731	test_cmp expect actual &&
732	echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
733	test_cmp found actual
734'
735
736test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
737	echo loop 10 >expect &&
738	echo HEAD:loop1 >>expect &&
739	echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
740	test_cmp expect actual
741'
742
743test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
744	echo HEAD:morx | git cat-file --batch >expect &&
745	echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
746	test_cmp expect actual
747'
748
749test_expect_success 'cat-file --batch-all-objects shows all objects' '
750	# make new repos so we know the full set of objects; we will
751	# also make sure that there are some packed and some loose
752	# objects, some referenced and some not, some duplicates, and that
753	# there are some available only via alternates.
754	git init all-one &&
755	(
756		cd all-one &&
757		echo content >file &&
758		git add file &&
759		git commit -qm base &&
760		git rev-parse HEAD HEAD^{tree} HEAD:file &&
761		git repack -ad &&
762		echo not-cloned | git hash-object -w --stdin
763	) >expect.unsorted &&
764	git clone -s all-one all-two &&
765	(
766		cd all-two &&
767		echo local-unref | git hash-object -w --stdin
768	) >>expect.unsorted &&
769	git -C all-two rev-parse HEAD:file |
770		git -C all-two pack-objects .git/objects/pack/pack &&
771	sort <expect.unsorted >expect &&
772	git -C all-two cat-file --batch-all-objects \
773				--batch-check="%(objectname)" >actual &&
774	test_cmp expect actual
775'
776
777# The only user-visible difference is that the objects are no longer sorted,
778# and the resulting sort order is undefined. So we can only check that it
779# produces the same objects as the ordered case, but that at least exercises
780# the code.
781test_expect_success 'cat-file --unordered works' '
782	git -C all-two cat-file --batch-all-objects --unordered \
783				--batch-check="%(objectname)" >actual.unsorted &&
784	sort <actual.unsorted >actual &&
785	test_cmp expect actual
786'
787
788test_expect_success 'set up object list for --batch-all-objects tests' '
789	git -C all-two cat-file --batch-all-objects --batch-check="%(objectname)" >objects
790'
791
792test_expect_success 'cat-file --batch="%(objectname)" with --batch-all-objects will work' '
793	git -C all-two cat-file --batch="%(objectname)" <objects >expect &&
794	git -C all-two cat-file --batch-all-objects --batch="%(objectname)" >actual &&
795	cmp expect actual
796'
797
798test_expect_success 'cat-file --batch="%(rest)" with --batch-all-objects will work' '
799	git -C all-two cat-file --batch="%(rest)" <objects >expect &&
800	git -C all-two cat-file --batch-all-objects --batch="%(rest)" >actual &&
801	cmp expect actual
802'
803
804test_expect_success 'cat-file --batch="batman" with --batch-all-objects will work' '
805	git -C all-two cat-file --batch="batman" <objects >expect &&
806	git -C all-two cat-file --batch-all-objects --batch="batman" >actual &&
807	cmp expect actual
808'
809
810test_expect_success 'set up replacement object' '
811	orig=$(git rev-parse HEAD) &&
812	git cat-file commit $orig >orig &&
813	{
814		cat orig &&
815		echo extra
816	} >fake &&
817	fake=$(git hash-object -t commit -w fake) &&
818	orig_size=$(git cat-file -s $orig) &&
819	fake_size=$(git cat-file -s $fake) &&
820	git replace $orig $fake
821'
822
823test_expect_success 'cat-file --batch respects replace objects' '
824	git cat-file --batch >actual <<-EOF &&
825	$orig
826	EOF
827	{
828		echo "$orig commit $fake_size" &&
829		cat fake &&
830		echo
831	} >expect &&
832	test_cmp expect actual
833'
834
835test_expect_success 'cat-file --batch-check respects replace objects' '
836	git cat-file --batch-check >actual <<-EOF &&
837	$orig
838	EOF
839	echo "$orig commit $fake_size" >expect &&
840	test_cmp expect actual
841'
842
843# Pull the entry for object with oid "$1" out of the output of
844# "cat-file --batch", including its object content (which requires
845# parsing and reading a set amount of bytes, hence perl).
846extract_batch_output () {
847    perl -ne '
848	BEGIN { $oid = shift }
849	if (/^$oid \S+ (\d+)$/) {
850	    print;
851	    read STDIN, my $buf, $1;
852	    print $buf;
853	    print "\n";
854	}
855    ' "$@"
856}
857
858test_expect_success 'cat-file --batch-all-objects --batch ignores replace' '
859	git cat-file --batch-all-objects --batch >actual.raw &&
860	extract_batch_output $orig <actual.raw >actual &&
861	{
862		echo "$orig commit $orig_size" &&
863		cat orig &&
864		echo
865	} >expect &&
866	test_cmp expect actual
867'
868
869test_expect_success 'cat-file --batch-all-objects --batch-check ignores replace' '
870	git cat-file --batch-all-objects --batch-check >actual.raw &&
871	grep ^$orig actual.raw >actual &&
872	echo "$orig commit $orig_size" >expect &&
873	test_cmp expect actual
874'
875
876test_done
877