list-objects: pass full pathname to callbacks
[git] / t / t1006-cat-file.sh
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
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 -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
55     '
56
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
61     '
62
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
68     '
69
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
74     '
75
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
80     '
81
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
87     '
88
89     test -z "$content" ||
90     test_expect_success "--batch without type ($type)" '
91         {
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
98     '
99
100     test -z "$content" ||
101     test_expect_success "--batch without size ($type)" '
102         {
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
109     '
110 }
111
112 hello_content="Hello World"
113 hello_size=$(strlen "$hello_content")
114 hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
115
116 test_expect_success "setup" '
117         echo_without_newline "$hello_content" > hello &&
118         git update-index --add hello
119 '
120
121 run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
122
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
129 '
130
131 tree_sha1=$(git write-tree)
132 tree_size=33
133 tree_pretty_content="100644 blob $hello_sha1    hello"
134
135 run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
136
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
143
144 $commit_message"
145
146 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
147
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
154
155 $tag_description"
156
157 tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
158 tag_size=$(strlen "$tag_content")
159
160 run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
161
162 test_expect_success \
163     "Reach a blob from a tag pointing to it" \
164     "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
165
166 for batch in batch batch-check
167 do
168     for opt in t s e p
169     do
170         test_expect_success "Passing -$opt with --$batch fails" '
171             test_must_fail git cat-file --$batch -$opt $hello_sha1
172         '
173
174         test_expect_success "Passing --$batch with -$opt fails" '
175             test_must_fail git cat-file -$opt --$batch $hello_sha1
176         '
177     done
178
179     test_expect_success "Passing <type> with --$batch fails" '
180         test_must_fail git cat-file --$batch blob $hello_sha1
181     '
182
183     test_expect_success "Passing --$batch with <type> fails" '
184         test_must_fail git cat-file blob --$batch $hello_sha1
185     '
186
187     test_expect_success "Passing sha1 with --$batch fails" '
188         test_must_fail git cat-file --$batch $hello_sha1
189     '
190 done
191
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)"
196 '
197
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)"
204 '
205
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)"
213 '
214
215 test_expect_success "--batch-check for an emtpy line" '
216     test " missing" = "$(echo | git cat-file --batch-check)"
217 '
218
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
223 '
224
225 batch_input="$hello_sha1
226 $commit_sha1
227 $tag_sha1
228 deadbeef
229
230 "
231
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"
240
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)"
243 '
244
245 batch_check_input="$hello_sha1
246 $tree_sha1
247 $commit_sha1
248 $tag_sha1
249 deadbeef
250
251 "
252
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"
259
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)"
263 '
264
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
273         EOF
274 '
275
276 test_expect_success 'confirm that neither loose blob is a delta' '
277         cat >expect <<-EOF &&
278         $_z40
279         $_z40
280         EOF
281         git cat-file --batch-check="%(deltabase)" <blobs >actual &&
282         test_cmp expect actual
283 '
284
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 &&
293         {
294                 grep "$(git rev-parse HEAD:foo)" actual ||
295                 grep "$(git rev-parse HEAD:foo-plus)" actual
296         }
297 '
298
299 test_done