refs: write packed_refs file using stdio
[git/debian.git] / t / t1006-cat-file.sh
bloba72e700ae434b07b08b48a68d4c0ebaa8173002b
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 -z "$content" ||
51 test_expect_success "Content of $type is correct" '
52 maybe_remove_timestamp "$content" $no_ts >expect &&
53 maybe_remove_timestamp "$(git cat-file $type $sha1)" $no_ts >actual &&
54 test_cmp expect actual
57 test_expect_success "Pretty content of $type is correct" '
58 maybe_remove_timestamp "$pretty_content" $no_ts >expect &&
59 maybe_remove_timestamp "$(git cat-file -p $sha1)" $no_ts >actual &&
60 test_cmp expect actual
63 test -z "$content" ||
64 test_expect_success "--batch output of $type is correct" '
65 maybe_remove_timestamp "$batch_output" $no_ts >expect &&
66 maybe_remove_timestamp "$(echo $sha1 | git cat-file --batch)" $no_ts >actual &&
67 test_cmp expect actual
70 test_expect_success "--batch-check output of $type is correct" '
71 echo "$sha1 $type $size" >expect &&
72 echo_without_newline $sha1 | git cat-file --batch-check >actual &&
73 test_cmp expect actual
76 test_expect_success "custom --batch-check format" '
77 echo "$type $sha1" >expect &&
78 echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
79 test_cmp expect actual
82 test_expect_success '--batch-check with %(rest)' '
83 echo "$type this is some extra content" >expect &&
84 echo "$sha1 this is some extra content" |
85 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
86 test_cmp expect actual
89 test -z "$content" ||
90 test_expect_success "--batch without type ($type)" '
92 echo "$size" &&
93 maybe_remove_timestamp "$content" $no_ts
94 } >expect &&
95 echo $sha1 | git cat-file --batch="%(objectsize)" >actual.full &&
96 maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
97 test_cmp expect actual
100 test -z "$content" ||
101 test_expect_success "--batch without size ($type)" '
103 echo "$type" &&
104 maybe_remove_timestamp "$content" $no_ts
105 } >expect &&
106 echo $sha1 | git cat-file --batch="%(objecttype)" >actual.full &&
107 maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
108 test_cmp expect actual
112 hello_content="Hello World"
113 hello_size=$(strlen "$hello_content")
114 hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
116 test_expect_success "setup" '
117 echo_without_newline "$hello_content" > hello &&
118 git update-index --add hello
121 run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
123 test_expect_success '--batch-check without %(rest) considers whole line' '
124 echo "$hello_sha1 blob $hello_size" >expect &&
125 git update-index --add --cacheinfo 100644 $hello_sha1 "white space" &&
126 test_when_finished "git update-index --remove \"white space\"" &&
127 echo ":white space" | git cat-file --batch-check >actual &&
128 test_cmp expect actual
131 tree_sha1=$(git write-tree)
132 tree_size=33
133 tree_pretty_content="100644 blob $hello_sha1 hello"
135 run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
137 commit_message="Initial commit"
138 commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
139 commit_size=177
140 commit_content="tree $tree_sha1
141 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
142 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
144 $commit_message"
146 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
148 tag_header_without_timestamp="object $hello_sha1
149 type blob
150 tag hellotag
151 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
152 tag_description="This is a tag"
153 tag_content="$tag_header_without_timestamp 0000000000 +0000
155 $tag_description"
157 tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
158 tag_size=$(strlen "$tag_content")
160 run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
162 test_expect_success \
163 "Reach a blob from a tag pointing to it" \
164 "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
166 for batch in batch batch-check
168 for opt in t s e p
170 test_expect_success "Passing -$opt with --$batch fails" '
171 test_must_fail git cat-file --$batch -$opt $hello_sha1
174 test_expect_success "Passing --$batch with -$opt fails" '
175 test_must_fail git cat-file -$opt --$batch $hello_sha1
177 done
179 test_expect_success "Passing <type> with --$batch fails" '
180 test_must_fail git cat-file --$batch blob $hello_sha1
183 test_expect_success "Passing --$batch with <type> fails" '
184 test_must_fail git cat-file blob --$batch $hello_sha1
187 test_expect_success "Passing sha1 with --$batch fails" '
188 test_must_fail git cat-file --$batch $hello_sha1
190 done
192 test_expect_success "--batch-check for a non-existent named object" '
193 test "foobar42 missing
194 foobar84 missing" = \
195 "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
198 test_expect_success "--batch-check for a non-existent hash" '
199 test "0000000000000000000000000000000000000042 missing
200 0000000000000000000000000000000000000084 missing" = \
201 "$( ( echo 0000000000000000000000000000000000000042;
202 echo_without_newline 0000000000000000000000000000000000000084; ) \
203 | git cat-file --batch-check)"
206 test_expect_success "--batch for an existent and a non-existent hash" '
207 test "$tag_sha1 tag $tag_size
208 $tag_content
209 0000000000000000000000000000000000000000 missing" = \
210 "$( ( echo $tag_sha1;
211 echo_without_newline 0000000000000000000000000000000000000000; ) \
212 | git cat-file --batch)"
215 test_expect_success "--batch-check for an emtpy line" '
216 test " missing" = "$(echo | git cat-file --batch-check)"
219 test_expect_success 'empty --batch-check notices missing object' '
220 echo "$_z40 missing" >expect &&
221 echo "$_z40" | git cat-file --batch-check="" >actual &&
222 test_cmp expect actual
225 batch_input="$hello_sha1
226 $commit_sha1
227 $tag_sha1
228 deadbeef
232 batch_output="$hello_sha1 blob $hello_size
233 $hello_content
234 $commit_sha1 commit $commit_size
235 $commit_content
236 $tag_sha1 tag $tag_size
237 $tag_content
238 deadbeef missing
239 missing"
241 test_expect_success '--batch with multiple sha1s gives correct format' '
242 test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
245 batch_check_input="$hello_sha1
246 $tree_sha1
247 $commit_sha1
248 $tag_sha1
249 deadbeef
253 batch_check_output="$hello_sha1 blob $hello_size
254 $tree_sha1 tree $tree_size
255 $commit_sha1 commit $commit_size
256 $tag_sha1 tag $tag_size
257 deadbeef missing
258 missing"
260 test_expect_success "--batch-check with multiple sha1s gives correct format" '
261 test "$batch_check_output" = \
262 "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
265 test_expect_success 'setup blobs which are likely to delta' '
266 test-genrandom foo 10240 >foo &&
267 { cat foo; echo plus; } >foo-plus &&
268 git add foo foo-plus &&
269 git commit -m foo &&
270 cat >blobs <<-\EOF
271 HEAD:foo
272 HEAD:foo-plus
276 test_expect_success 'confirm that neither loose blob is a delta' '
277 cat >expect <<-EOF
278 $_z40
279 $_z40
281 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
282 test_cmp expect actual
285 # To avoid relying too much on the current delta heuristics,
286 # we will check only that one of the two objects is a delta
287 # against the other, but not the order. We can do so by just
288 # asking for the base of both, and checking whether either
289 # sha1 appears in the output.
290 test_expect_success '%(deltabase) reports packed delta bases' '
291 git repack -ad &&
292 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
294 grep "$(git rev-parse HEAD:foo)" actual ||
295 grep "$(git rev-parse HEAD:foo-plus)" actual
299 test_done