@@ -113,53 +113,55 @@ strlen () {
113113
114114run_tests () {
115115 type=$1
116- oid=$2
117- size=$3
118- content=$4
119- pretty_content=$5
116+ object_name=" $2 "
117+ mode=$3
118+ size=$4
119+ content=$5
120+ pretty_content=$6
121+ oid=${7:- " $object_name " }
120122
121123 batch_output=" $oid $type $size
122124$content "
123125
124126 test_expect_success " $type exists" '
125- git cat-file -e $oid
127+ git cat-file -e "$object_name"
126128 '
127129
128130 test_expect_success " Type of $type is correct" '
129131 echo $type >expect &&
130- git cat-file -t $oid >actual &&
132+ git cat-file -t "$object_name" >actual &&
131133 test_cmp expect actual
132134 '
133135
134136 test_expect_success " Size of $type is correct" '
135137 echo $size >expect &&
136- git cat-file -s $oid >actual &&
138+ git cat-file -s "$object_name" >actual &&
137139 test_cmp expect actual
138140 '
139141
140142 test -z " $content " ||
141143 test_expect_success " Content of $type is correct" '
142144 echo_without_newline "$content" >expect &&
143- git cat-file $type $oid >actual &&
145+ git cat-file $type "$object_name" >actual &&
144146 test_cmp expect actual
145147 '
146148
147149 test_expect_success " Pretty content of $type is correct" '
148150 echo_without_newline "$pretty_content" >expect &&
149- git cat-file -p $oid >actual &&
151+ git cat-file -p "$object_name" >actual &&
150152 test_cmp expect actual
151153 '
152154
153155 test -z " $content " ||
154156 test_expect_success " --batch output of $type is correct" '
155157 echo "$batch_output" >expect &&
156- echo $oid | git cat-file --batch >actual &&
158+ echo "$object_name" | git cat-file --batch >actual &&
157159 test_cmp expect actual
158160 '
159161
160162 test_expect_success " --batch-check output of $type is correct" '
161163 echo "$oid $type $size" >expect &&
162- echo_without_newline $oid | git cat-file --batch-check >actual &&
164+ echo_without_newline "$object_name" | git cat-file --batch-check >actual &&
163165 test_cmp expect actual
164166 '
165167
@@ -168,44 +170,59 @@ $content"
168170 test -z " $content " ||
169171 test_expect_success " --batch-command $opt output of $type content is correct" '
170172 echo "$batch_output" >expect &&
171- test_write_lines "contents $oid " | git cat-file --batch-command $opt >actual &&
173+ test_write_lines "contents $object_name " | git cat-file --batch-command $opt >actual &&
172174 test_cmp expect actual
173175 '
174176
175177 test_expect_success " --batch-command $opt output of $type info is correct" '
176178 echo "$oid $type $size" >expect &&
177- test_write_lines "info $oid " |
179+ test_write_lines "info $object_name " |
178180 git cat-file --batch-command $opt >actual &&
179181 test_cmp expect actual
180182 '
181183 done
182184
183185 test_expect_success " custom --batch-check format" '
184186 echo "$type $oid" >expect &&
185- echo $oid | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
187+ echo "$object_name" | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
186188 test_cmp expect actual
187189 '
188190
189191 test_expect_success " custom --batch-command format" '
190192 echo "$type $oid" >expect &&
191- echo "info $oid " | git cat-file --batch-command="%(objecttype) %(objectname)" >actual &&
193+ echo "info $object_name " | git cat-file --batch-command="%(objecttype) %(objectname)" >actual &&
192194 test_cmp expect actual
193195 '
194196
195- test_expect_success ' --batch-check with %(rest)' '
197+ # FIXME: %(rest) is incompatible with object names that include whitespace,
198+ # e.g. HEAD:path/to/a/file with spaces. Use the resolved OID as input to
199+ # test this instead of the raw object name.
200+ if echo " $object_name " | grep " " ; then
201+ test_rest=test_expect_failure
202+ else
203+ test_rest=test_expect_success
204+ fi
205+
206+ $test_rest ' --batch-check with %(rest)' '
196207 echo "$type this is some extra content" >expect &&
197- echo "$oid this is some extra content" |
208+ echo "$object_name this is some extra content" |
198209 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
199210 test_cmp expect actual
200211 '
201212
213+ test_expect_success ' --batch-check with %(objectmode)' '
214+ echo "$mode $oid" >expect &&
215+ echo $object_name | git cat-file --batch-check="%(objectmode) %(objectname)" >actual &&
216+ test_cmp expect actual
217+ '
218+
202219 test -z " $content " ||
203220 test_expect_success " --batch without type ($type )" '
204221 {
205222 echo "$size" &&
206223 echo "$content"
207224 } >expect &&
208- echo $oid | git cat-file --batch="%(objectsize)" >actual &&
225+ echo "$object_name" | git cat-file --batch="%(objectsize)" >actual &&
209226 test_cmp expect actual
210227 '
211228
@@ -215,7 +232,7 @@ $content"
215232 echo "$type" &&
216233 echo "$content"
217234 } >expect &&
218- echo $oid | git cat-file --batch="%(objecttype)" >actual &&
235+ echo "$object_name" | git cat-file --batch="%(objecttype)" >actual &&
219236 test_cmp expect actual
220237 '
221238}
@@ -230,13 +247,14 @@ test_expect_success "setup" '
230247 git config extensions.compatobjectformat $test_compat_hash_algo &&
231248 echo_without_newline "$hello_content" > hello &&
232249 git update-index --add hello &&
250+ echo_without_newline "$hello_content" > "path with spaces" &&
251+ git update-index --add --chmod=+x "path with spaces" &&
233252 git commit -m "add hello file"
234253'
235254
236255run_blob_tests () {
237256 oid=$1
238-
239- run_tests ' blob' $oid $hello_size " $hello_content " " $hello_content "
257+ run_tests ' blob' $oid " " $hello_size " $hello_content " " $hello_content "
240258
241259 test_expect_success ' --batch-command --buffer with flush for blob info' '
242260 echo "$oid blob $hello_size" >expect &&
@@ -269,13 +287,17 @@ test_expect_success '--batch-check without %(rest) considers whole line' '
269287
270288tree_oid=$( git write-tree)
271289tree_compat_oid=$( git rev-parse --output-object-format=$test_compat_hash_algo $tree_oid )
272- tree_size=$(( $(test_oid rawsz) + 13 ))
273- tree_compat_size=$(( $(test_oid -- hash= compat rawsz) + 13 ))
274- tree_pretty_content=" 100644 blob $hello_oid hello${LF} "
275- tree_compat_pretty_content=" 100644 blob $hello_compat_oid hello${LF} "
276-
277- run_tests ' tree' $tree_oid $tree_size " " " $tree_pretty_content "
278- run_tests ' tree' $tree_compat_oid $tree_compat_size " " " $tree_compat_pretty_content "
290+ tree_size=$(( 2 * $(test_oid rawsz) + 13 + 24 ))
291+ tree_compat_size=$(( 2 * $(test_oid -- hash= compat rawsz) + 13 + 24 ))
292+ tree_pretty_content=" 100644 blob $hello_oid hello${LF} 100755 blob $hello_oid path with spaces${LF} "
293+ tree_compat_pretty_content=" 100644 blob $hello_compat_oid hello${LF} 100755 blob $hello_compat_oid path with spaces${LF} "
294+
295+ run_tests ' tree' $tree_oid " " $tree_size " " " $tree_pretty_content "
296+ run_tests ' tree' $tree_compat_oid " " $tree_compat_size " " " $tree_compat_pretty_content "
297+ run_tests ' blob' " $tree_oid :hello" " 100644" $hello_size " " " $hello_content " $hello_oid
298+ run_tests ' blob' " $tree_compat_oid :hello" " 100644" $hello_size " " " $hello_content " $hello_compat_oid
299+ run_tests ' blob' " $tree_oid :path with spaces" " 100755" $hello_size " " " $hello_content " $hello_oid
300+ run_tests ' blob' " $tree_compat_oid :path with spaces" " 100755" $hello_size " " " $hello_content " $hello_compat_oid
279301
280302commit_message=" Initial commit"
281303commit_oid=$( echo_without_newline " $commit_message " | git commit-tree $tree_oid )
@@ -294,8 +316,8 @@ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
294316
295317$commit_message "
296318
297- run_tests ' commit' $commit_oid $commit_size " $commit_content " " $commit_content "
298- run_tests ' commit' $commit_compat_oid $commit_compat_size " $commit_compat_content " " $commit_compat_content "
319+ run_tests ' commit' $commit_oid " " $commit_size " $commit_content " " $commit_content "
320+ run_tests ' commit' $commit_compat_oid " " $commit_compat_size " $commit_compat_content " " $commit_compat_content "
299321
300322tag_header_without_oid=" type blob
301323tag hellotag
@@ -318,8 +340,8 @@ tag_size=$(strlen "$tag_content")
318340tag_compat_oid=$( git rev-parse --output-object-format=$test_compat_hash_algo $tag_oid )
319341tag_compat_size=$( strlen " $tag_compat_content " )
320342
321- run_tests ' tag' $tag_oid $tag_size " $tag_content " " $tag_content "
322- run_tests ' tag' $tag_compat_oid $tag_compat_size " $tag_compat_content " " $tag_compat_content "
343+ run_tests ' tag' $tag_oid " " $tag_size " $tag_content " " $tag_content "
344+ run_tests ' tag' $tag_compat_oid " " $tag_compat_size " $tag_compat_content " " $tag_compat_content "
323345
324346test_expect_success " Reach a blob from a tag pointing to it" '
325347 echo_without_newline "$hello_content" >expect &&
@@ -1198,6 +1220,31 @@ test_expect_success 'cat-file --batch-check respects replace objects' '
11981220 test_cmp expect actual
11991221'
12001222
1223+ test_expect_success ' batch-check with a submodule' '
1224+ # FIXME: this call to mktree is incompatible with compatObjectFormat
1225+ # because the submodule OID cannot be mapped to the compat hash algo.
1226+ test_unconfig extensions.compatobjectformat &&
1227+ printf "160000 commit $(test_oid deadbeef)\tsub\n" >tree-with-sub &&
1228+ tree=$(git mktree <tree-with-sub) &&
1229+ test_config extensions.compatobjectformat $test_compat_hash_algo &&
1230+
1231+ git cat-file --batch-check >actual <<-EOF &&
1232+ $tree:sub
1233+ EOF
1234+ printf "$(test_oid deadbeef) submodule\n" >expect &&
1235+ test_cmp expect actual
1236+ '
1237+
1238+ test_expect_success ' batch-check with a submodule, object exists' '
1239+ printf "160000 commit $commit_oid\tsub\n" >tree-with-sub &&
1240+ tree=$(git mktree <tree-with-sub) &&
1241+ git cat-file --batch-check >actual <<-EOF &&
1242+ $tree:sub
1243+ EOF
1244+ printf "$commit_oid commit $commit_size\n" >expect &&
1245+ test_cmp expect actual
1246+ '
1247+
12011248# Pull the entry for object with oid "$1" out of the output of
12021249# "cat-file --batch", including its object content (which requires
12031250# parsing and reading a set amount of bytes, hence perl).
0 commit comments