]>
Commit | Line | Data |
---|---|---|
b335d3f1 AR |
1 | #!/bin/sh |
2 | ||
3 | test_description='git cat-file' | |
4 | ||
5 | . ./test-lib.sh | |
6 | ||
7 | echo_without_newline () { | |
8 | printf '%s' "$*" | |
9 | } | |
10 | ||
11 | strlen () { | |
12 | echo_without_newline "$1" | wc -c | sed -e 's/^ *//' | |
13 | } | |
14 | ||
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]$//')" | |
20 | fi | |
21 | } | |
22 | ||
23 | run_tests () { | |
24 | type=$1 | |
25 | sha1=$2 | |
26 | size=$3 | |
27 | content=$4 | |
28 | pretty_content=$5 | |
29 | no_ts=$6 | |
30 | ||
a8128ed6 AR |
31 | batch_output="$sha1 $type $size |
32 | $content" | |
33 | ||
b335d3f1 AR |
34 | test_expect_success "$type exists" ' |
35 | git cat-file -e $sha1 | |
36 | ' | |
37 | ||
38 | test_expect_success "Type of $type is correct" ' | |
03c893cb JK |
39 | echo $type >expect && |
40 | git cat-file -t $sha1 >actual && | |
41 | test_cmp expect actual | |
b335d3f1 AR |
42 | ' |
43 | ||
44 | test_expect_success "Size of $type is correct" ' | |
03c893cb JK |
45 | echo $size >expect && |
46 | git cat-file -s $sha1 >actual && | |
47 | test_cmp expect actual | |
b335d3f1 AR |
48 | ' |
49 | ||
3e370f9f KN |
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 | ||
b335d3f1 AR |
62 | test -z "$content" || |
63 | test_expect_success "Content of $type is correct" ' | |
03c893cb JK |
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 | |
b335d3f1 AR |
67 | ' |
68 | ||
69 | test_expect_success "Pretty content of $type is correct" ' | |
03c893cb JK |
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 | |
b335d3f1 | 73 | ' |
05d5667f | 74 | |
a8128ed6 AR |
75 | test -z "$content" || |
76 | test_expect_success "--batch output of $type is correct" ' | |
03c893cb JK |
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 | |
a8128ed6 AR |
80 | ' |
81 | ||
05d5667f | 82 | test_expect_success "--batch-check output of $type is correct" ' |
03c893cb JK |
83 | echo "$sha1 $type $size" >expect && |
84 | echo_without_newline $sha1 | git cat-file --batch-check >actual && | |
85 | test_cmp expect actual | |
05d5667f | 86 | ' |
93d2a607 JK |
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 | ' | |
97be0407 JK |
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 | ' | |
6554dfa9 JK |
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 | ' | |
b335d3f1 AR |
122 | } |
123 | ||
124 | hello_content="Hello World" | |
125 | hello_size=$(strlen "$hello_content") | |
126 | hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin) | |
127 | ||
128 | test_expect_success "setup" ' | |
129 | echo_without_newline "$hello_content" > hello && | |
130 | git update-index --add hello | |
131 | ' | |
132 | ||
133 | run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content" | |
134 | ||
97be0407 JK |
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 | |
141 | ' | |
142 | ||
b335d3f1 | 143 | tree_sha1=$(git write-tree) |
e95f5313 | 144 | tree_size=$(($(test_oid rawsz) + 13)) |
b335d3f1 AR |
145 | tree_pretty_content="100644 blob $hello_sha1 hello" |
146 | ||
147 | run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content" | |
148 | ||
41ccfdd9 | 149 | commit_message="Initial commit" |
b335d3f1 | 150 | commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1) |
e95f5313 | 151 | commit_size=$(($(test_oid hexsz) + 137)) |
b335d3f1 AR |
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 | |
155 | ||
156 | $commit_message" | |
157 | ||
158 | run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1 | |
159 | ||
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 | |
166 | ||
b335d3f1 AR |
167 | $tag_description" |
168 | ||
acf9de4c | 169 | tag_sha1=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w) |
b335d3f1 AR |
170 | tag_size=$(strlen "$tag_content") |
171 | ||
9cfa5126 | 172 | run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1 |
b335d3f1 AR |
173 | |
174 | test_expect_success \ | |
175 | "Reach a blob from a tag pointing to it" \ | |
176 | "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\"" | |
177 | ||
a8128ed6 | 178 | for batch in batch batch-check |
05d5667f | 179 | do |
a8128ed6 AR |
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 | |
05d5667f AR |
193 | ' |
194 | ||
a8128ed6 AR |
195 | test_expect_success "Passing --$batch with <type> fails" ' |
196 | test_must_fail git cat-file blob --$batch $hello_sha1 | |
05d5667f | 197 | ' |
05d5667f | 198 | |
a8128ed6 AR |
199 | test_expect_success "Passing sha1 with --$batch fails" ' |
200 | test_must_fail git cat-file --$batch $hello_sha1 | |
201 | ' | |
202 | done | |
05d5667f | 203 | |
122d5346 DT |
204 | for opt in t s e p |
205 | do | |
206 | test_expect_success "Passing -$opt with --follow-symlinks fails" ' | |
207 | test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1 | |
208 | ' | |
209 | done | |
210 | ||
3c076dbe LW |
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)" | |
215 | ' | |
216 | ||
217 | test_expect_success "--batch-check for a non-existent hash" ' | |
218 | test "0000000000000000000000000000000000000042 missing | |
219 | 0000000000000000000000000000000000000084 missing" = \ | |
220 | "$( ( echo 0000000000000000000000000000000000000042; | |
bdbc17e8 MD |
221 | echo_without_newline 0000000000000000000000000000000000000084; ) | |
222 | git cat-file --batch-check)" | |
3c076dbe LW |
223 | ' |
224 | ||
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; | |
bdbc17e8 MD |
230 | echo_without_newline 0000000000000000000000000000000000000000; ) | |
231 | git cat-file --batch)" | |
05d5667f AR |
232 | ' |
233 | ||
2e3a16b2 | 234 | test_expect_success "--batch-check for an empty line" ' |
05d5667f AR |
235 | test " missing" = "$(echo | git cat-file --batch-check)" |
236 | ' | |
237 | ||
4ef8d1dd | 238 | test_expect_success 'empty --batch-check notices missing object' ' |
8125a58b | 239 | echo "$ZERO_OID missing" >expect && |
240 | echo "$ZERO_OID" | git cat-file --batch-check="" >actual && | |
4ef8d1dd JH |
241 | test_cmp expect actual |
242 | ' | |
243 | ||
a8128ed6 AR |
244 | batch_input="$hello_sha1 |
245 | $commit_sha1 | |
246 | $tag_sha1 | |
247 | deadbeef | |
248 | ||
249 | " | |
250 | ||
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" | |
259 | ||
6c41e21d MB |
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)" | |
262 | ' | |
a8128ed6 | 263 | |
05d5667f AR |
264 | batch_check_input="$hello_sha1 |
265 | $tree_sha1 | |
266 | $commit_sha1 | |
267 | $tag_sha1 | |
268 | deadbeef | |
269 | ||
270 | " | |
271 | ||
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" | |
278 | ||
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)" | |
282 | ' | |
283 | ||
65ea9c3c | 284 | test_expect_success 'setup blobs which are likely to delta' ' |
c680668d | 285 | test-tool genrandom foo 10240 >foo && |
65ea9c3c JK |
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 | ||
295 | test_expect_success 'confirm that neither loose blob is a delta' ' | |
99094a7a | 296 | cat >expect <<-EOF && |
8125a58b | 297 | $ZERO_OID |
298 | $ZERO_OID | |
65ea9c3c JK |
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. | |
309 | test_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 | ||
70e4a577 ÆAB |
318 | test_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 | ' | |
3e370f9f | 329 | |
59b8283d ÆAB |
330 | for arg1 in '' --allow-unknown-type |
331 | do | |
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: unable to unpack $bogus_long_sha1 header | |
360 | fatal: Not a valid object name $bogus_long_sha1 | |
361 | EOF | |
362 | else | |
363 | cat >expect <<-EOF | |
364 | error: unable to unpack $bogus_long_sha1 header | |
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 | |
403 | done | |
404 | ||
dd45a562 ÆAB |
405 | test_expect_success '-e is OK with a broken object without --allow-unknown-type' ' |
406 | git cat-file -e $bogus_short_sha1 | |
407 | ' | |
408 | ||
409 | test_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 | ||
413 | test_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 | ||
418 | test_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 | ||
426 | test_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 | ||
440 | test_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 | ||
445 | test_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 | ||
3e370f9f | 466 | test_expect_success "Type of broken object is correct" ' |
70e4a577 ÆAB |
467 | echo $bogus_short_type >expect && |
468 | git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual && | |
3e370f9f KN |
469 | test_cmp expect actual |
470 | ' | |
471 | ||
472 | test_expect_success "Size of broken object is correct" ' | |
70e4a577 ÆAB |
473 | echo $bogus_short_size >expect && |
474 | git cat-file -s --allow-unknown-type $bogus_short_sha1 >actual && | |
3e370f9f KN |
475 | test_cmp expect actual |
476 | ' | |
3e370f9f KN |
477 | |
478 | test_expect_success "Type of broken object is correct when type is large" ' | |
70e4a577 ÆAB |
479 | echo $bogus_long_type >expect && |
480 | git cat-file -t --allow-unknown-type $bogus_long_sha1 >actual && | |
3e370f9f KN |
481 | test_cmp expect actual |
482 | ' | |
483 | ||
484 | test_expect_success "Size of large broken object is correct when type is large" ' | |
70e4a577 ÆAB |
485 | echo $bogus_long_size >expect && |
486 | git cat-file -s --allow-unknown-type $bogus_long_sha1 >actual && | |
3e370f9f KN |
487 | test_cmp expect actual |
488 | ' | |
489 | ||
7e7d220d ÆAB |
490 | test_expect_success 'cat-file -t and -s on corrupt loose object' ' |
491 | git init --bare corrupt-loose.git && | |
492 | ( | |
493 | cd corrupt-loose.git && | |
494 | ||
495 | # Setup and create the empty blob and its path | |
496 | empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) && | |
497 | git hash-object -w --stdin </dev/null && | |
498 | ||
499 | # Create another blob and its path | |
500 | echo other >other.blob && | |
501 | other_blob=$(git hash-object -w --stdin <other.blob) && | |
502 | other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) && | |
503 | ||
504 | # Before the swap the size is 0 | |
505 | cat >out.expect <<-EOF && | |
506 | 0 | |
507 | EOF | |
508 | git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual && | |
509 | test_must_be_empty err.actual && | |
510 | test_cmp out.expect out.actual && | |
511 | ||
512 | # Swap the two to corrupt the repository | |
513 | mv -f "$other_path" "$empty_path" && | |
514 | test_must_fail git fsck 2>err.fsck && | |
515 | grep "hash mismatch" err.fsck && | |
516 | ||
517 | # confirm that cat-file is reading the new swapped-in | |
518 | # blob... | |
519 | cat >out.expect <<-EOF && | |
520 | blob | |
521 | EOF | |
522 | git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual && | |
523 | test_must_be_empty err.actual && | |
524 | test_cmp out.expect out.actual && | |
525 | ||
526 | # ... since it has a different size now. | |
527 | cat >out.expect <<-EOF && | |
528 | 6 | |
529 | EOF | |
530 | git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual && | |
531 | test_must_be_empty err.actual && | |
532 | test_cmp out.expect out.actual && | |
533 | ||
534 | # So far "cat-file" has been happy to spew the found | |
535 | # content out as-is. Try to make it zlib-invalid. | |
536 | mv -f other.blob "$empty_path" && | |
537 | test_must_fail git fsck 2>err.fsck && | |
538 | grep "^error: inflate: data stream error (" err.fsck | |
539 | ) | |
540 | ' | |
541 | ||
122d5346 DT |
542 | # Tests for git cat-file --follow-symlinks |
543 | test_expect_success 'prep for symlink tests' ' | |
544 | echo_without_newline "$hello_content" >morx && | |
545 | test_ln_s_add morx same-dir-link && | |
546 | test_ln_s_add dir link-to-dir && | |
547 | test_ln_s_add ../fleem out-of-repo-link && | |
548 | test_ln_s_add .. out-of-repo-link-dir && | |
549 | test_ln_s_add same-dir-link link-to-link && | |
550 | test_ln_s_add nope broken-same-dir-link && | |
551 | mkdir dir && | |
552 | test_ln_s_add ../morx dir/parent-dir-link && | |
553 | test_ln_s_add .. dir/link-dir && | |
554 | test_ln_s_add ../../escape dir/out-of-repo-link && | |
555 | test_ln_s_add ../.. dir/out-of-repo-link-dir && | |
556 | test_ln_s_add nope dir/broken-link-in-dir && | |
557 | mkdir dir/subdir && | |
558 | test_ln_s_add ../../morx dir/subdir/grandparent-dir-link && | |
559 | test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link && | |
560 | test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir && | |
561 | test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing && | |
562 | test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link && | |
563 | echo_without_newline "$hello_content" >dir/subdir/ind2 && | |
564 | echo_without_newline "$hello_content" >dir/ind1 && | |
565 | test_ln_s_add dir dirlink && | |
566 | test_ln_s_add dir/subdir subdirlink && | |
567 | test_ln_s_add subdir/ind2 dir/link-to-child && | |
568 | test_ln_s_add dir/link-to-child link-to-down-link && | |
569 | test_ln_s_add dir/.. up-down && | |
570 | test_ln_s_add dir/../ up-down-trailing && | |
571 | test_ln_s_add dir/../morx up-down-file && | |
572 | test_ln_s_add dir/../../morx up-up-down-file && | |
573 | test_ln_s_add subdirlink/../../morx up-two-down-file && | |
574 | test_ln_s_add loop1 loop2 && | |
575 | test_ln_s_add loop2 loop1 && | |
576 | git add morx dir/subdir/ind2 dir/ind1 && | |
577 | git commit -am "test" && | |
578 | echo $hello_sha1 blob $hello_size >found | |
579 | ' | |
580 | ||
581 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' ' | |
582 | echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual && | |
583 | test_cmp found actual && | |
584 | echo HEAD:nope missing >expect && | |
585 | echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual && | |
586 | test_cmp expect actual | |
587 | ' | |
588 | ||
589 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' ' | |
590 | echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual && | |
591 | test_cmp found actual | |
592 | ' | |
593 | ||
594 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' ' | |
595 | echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual && | |
596 | test_cmp found actual | |
597 | ' | |
598 | ||
599 | ||
600 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' ' | |
601 | echo dangling 25 >expect && | |
602 | echo HEAD:broken-same-dir-link >>expect && | |
603 | echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual && | |
604 | test_cmp expect actual | |
605 | ' | |
606 | ||
607 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' ' | |
608 | echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual && | |
609 | test_cmp found actual | |
610 | ' | |
611 | ||
612 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' ' | |
613 | echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual && | |
614 | test_cmp found actual && | |
615 | echo notdir 29 >expect && | |
616 | echo HEAD:dir/parent-dir-link/nope >>expect && | |
617 | echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual && | |
618 | test_cmp expect actual | |
619 | ' | |
620 | ||
621 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' ' | |
622 | echo dangling 22 >expect && | |
623 | echo HEAD:dir/link-dir/nope >>expect && | |
624 | echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual && | |
625 | test_cmp expect actual && | |
626 | echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual && | |
627 | test_cmp found actual && | |
628 | echo dangling 27 >expect && | |
629 | echo HEAD:dir/broken-link-in-dir >>expect && | |
630 | echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual && | |
631 | test_cmp expect actual | |
632 | ' | |
633 | ||
634 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' ' | |
635 | echo notdir 41 >expect && | |
636 | echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect && | |
637 | echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual && | |
638 | test_cmp expect actual && | |
639 | echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual && | |
640 | test_cmp found actual && | |
641 | echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual && | |
642 | test_cmp found actual | |
643 | ' | |
644 | ||
645 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' ' | |
646 | echo dangling 17 >expect && | |
647 | echo HEAD:dirlink/morx >>expect && | |
648 | echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual && | |
649 | test_cmp expect actual && | |
650 | echo $hello_sha1 blob $hello_size >expect && | |
651 | echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual && | |
652 | test_cmp expect actual | |
653 | ' | |
654 | ||
655 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' ' | |
656 | echo dangling 20 >expect && | |
657 | echo HEAD:subdirlink/morx >>expect && | |
658 | echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual && | |
659 | test_cmp expect actual && | |
660 | echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual && | |
661 | test_cmp found actual | |
662 | ' | |
663 | ||
664 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' ' | |
665 | echo notdir 27 >expect && | |
666 | echo HEAD:dir/link-to-child/morx >>expect && | |
667 | echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual && | |
668 | test_cmp expect actual && | |
669 | echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual && | |
670 | test_cmp found actual && | |
671 | echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual && | |
672 | test_cmp found actual | |
673 | ' | |
674 | ||
675 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' ' | |
676 | echo symlink 8 >expect && | |
677 | echo ../fleem >>expect && | |
678 | echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual && | |
679 | test_cmp expect actual && | |
680 | echo symlink 2 >expect && | |
681 | echo .. >>expect && | |
682 | echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual && | |
683 | test_cmp expect actual | |
684 | ' | |
685 | ||
686 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' ' | |
687 | echo symlink 9 >expect && | |
688 | echo ../escape >>expect && | |
689 | echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual && | |
690 | test_cmp expect actual && | |
691 | echo symlink 2 >expect && | |
692 | echo .. >>expect && | |
693 | echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual && | |
694 | test_cmp expect actual | |
695 | ' | |
696 | ||
697 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' ' | |
698 | echo symlink 15 >expect && | |
699 | echo ../great-escape >>expect && | |
700 | echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual && | |
701 | test_cmp expect actual && | |
702 | echo symlink 2 >expect && | |
703 | echo .. >>expect && | |
704 | echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual && | |
705 | test_cmp expect actual && | |
706 | echo symlink 3 >expect && | |
707 | echo ../ >>expect && | |
708 | echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual && | |
709 | test_cmp expect actual | |
710 | ' | |
711 | ||
712 | test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' ' | |
713 | echo HEAD: | git cat-file --batch-check >expect && | |
714 | echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual && | |
715 | test_cmp expect actual && | |
716 | echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual && | |
717 | test_cmp expect actual && | |
718 | echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual && | |
719 | test_cmp found actual && | |
720 | echo symlink 7 >expect && | |
721 | echo ../morx >>expect && | |
722 | echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual && | |
723 | test_cmp expect actual && | |
724 | echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual && | |
725 | test_cmp found actual | |
726 | ' | |
727 | ||
728 | test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' ' | |
729 | echo loop 10 >expect && | |
730 | echo HEAD:loop1 >>expect && | |
731 | echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual && | |
732 | test_cmp expect actual | |
733 | ' | |
734 | ||
735 | test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' ' | |
736 | echo HEAD:morx | git cat-file --batch >expect && | |
737 | echo HEAD:morx | git cat-file --batch --follow-symlinks >actual && | |
738 | test_cmp expect actual | |
739 | ' | |
67f0b6f3 | 740 | |
6a951937 | 741 | test_expect_success 'cat-file --batch-all-objects shows all objects' ' |
3115ee45 | 742 | # make new repos so we know the full set of objects; we will |
6a951937 | 743 | # also make sure that there are some packed and some loose |
aa2f5ef5 JK |
744 | # objects, some referenced and some not, some duplicates, and that |
745 | # there are some available only via alternates. | |
6a951937 JK |
746 | git init all-one && |
747 | ( | |
748 | cd all-one && | |
749 | echo content >file && | |
750 | git add file && | |
751 | git commit -qm base && | |
752 | git rev-parse HEAD HEAD^{tree} HEAD:file && | |
753 | git repack -ad && | |
754 | echo not-cloned | git hash-object -w --stdin | |
755 | ) >expect.unsorted && | |
756 | git clone -s all-one all-two && | |
757 | ( | |
758 | cd all-two && | |
759 | echo local-unref | git hash-object -w --stdin | |
760 | ) >>expect.unsorted && | |
aa2f5ef5 JK |
761 | git -C all-two rev-parse HEAD:file | |
762 | git -C all-two pack-objects .git/objects/pack/pack && | |
6a951937 JK |
763 | sort <expect.unsorted >expect && |
764 | git -C all-two cat-file --batch-all-objects \ | |
3115ee45 | 765 | --batch-check="%(objectname)" >actual && |
6a951937 JK |
766 | test_cmp expect actual |
767 | ' | |
768 | ||
0750bb5b JK |
769 | # The only user-visible difference is that the objects are no longer sorted, |
770 | # and the resulting sort order is undefined. So we can only check that it | |
771 | # produces the same objects as the ordered case, but that at least exercises | |
772 | # the code. | |
773 | test_expect_success 'cat-file --unordered works' ' | |
774 | git -C all-two cat-file --batch-all-objects --unordered \ | |
775 | --batch-check="%(objectname)" >actual.unsorted && | |
776 | sort <actual.unsorted >actual && | |
777 | test_cmp expect actual | |
778 | ' | |
779 | ||
b335d3f1 | 780 | test_done |