Skip to content

Commit 84f8508

Browse files
committed
Merge branch 'vd/cat-file-objectmode-update' into seen
* vd/cat-file-objectmode-update: cat-file.c: add batch handling for submodules cat-file: add %(objectmode) atom t1006: update 'run_tests' to test generic object specifiers
2 parents b909057 + ea506bf commit 84f8508

File tree

3 files changed

+103
-35
lines changed

3 files changed

+103
-35
lines changed

Documentation/git-cat-file.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ newline. The available atoms are:
307307
`objecttype`::
308308
The type of the object (the same as `cat-file -t` reports).
309309

310+
`objectmode`::
311+
If the specified object has mode information (such as a tree or
312+
index entry), the mode expressed as an octal integer. Otherwise,
313+
empty string.
314+
310315
`objectsize`::
311316
The size, in bytes, of the object (the same as `cat-file -s`
312317
reports).
@@ -368,6 +373,14 @@ If a name is specified that might refer to more than one object (an ambiguous sh
368373
<object> SP ambiguous LF
369374
------------
370375

376+
If a name is specified that refers to a submodule entry in a tree and the
377+
target object does not exist in the repository, then `cat-file` will ignore
378+
any custom format and print (with the object ID of the submodule):
379+
380+
------------
381+
<oid> SP submodule LF
382+
------------
383+
371384
If `--follow-symlinks` is used, and a symlink in the repository points
372385
outside the repository, then `cat-file` will ignore any custom format
373386
and print:

builtin/cat-file.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ struct expand_data {
275275
struct object_id oid;
276276
enum object_type type;
277277
unsigned long size;
278+
unsigned short mode;
278279
off_t disk_size;
279280
const char *rest;
280281
struct object_id delta_base_oid;
@@ -306,6 +307,7 @@ struct expand_data {
306307
*/
307308
unsigned skip_object_info : 1;
308309
};
310+
#define EXPAND_DATA_INIT { .mode = S_IFINVALID }
309311

310312
static int is_atom(const char *atom, const char *s, int slen)
311313
{
@@ -345,6 +347,9 @@ static int expand_atom(struct strbuf *sb, const char *atom, int len,
345347
else
346348
strbuf_addstr(sb,
347349
oid_to_hex(&data->delta_base_oid));
350+
} else if (is_atom("objectmode", atom, len)) {
351+
if (!data->mark_query && !(S_IFINVALID == data->mode))
352+
strbuf_addf(sb, "%06o", data->mode);
348353
} else
349354
return 0;
350355
return 1;
@@ -489,7 +494,10 @@ static void batch_object_write(const char *obj_name,
489494
&data->oid, &data->info,
490495
OBJECT_INFO_LOOKUP_REPLACE);
491496
if (ret < 0) {
492-
report_object_status(opt, obj_name, &data->oid, "missing");
497+
if (data->mode == S_IFGITLINK)
498+
report_object_status(opt, oid_to_hex(&data->oid), &data->oid, "submodule");
499+
else
500+
report_object_status(opt, obj_name, &data->oid, "missing");
493501
return;
494502
}
495503

@@ -611,6 +619,7 @@ static void batch_one_object(const char *obj_name,
611619
goto out;
612620
}
613621

622+
data->mode = ctx.mode;
614623
batch_object_write(obj_name, scratch, opt, data, NULL, 0);
615624

616625
out:
@@ -864,7 +873,7 @@ static int batch_objects(struct batch_options *opt)
864873
{
865874
struct strbuf input = STRBUF_INIT;
866875
struct strbuf output = STRBUF_INIT;
867-
struct expand_data data;
876+
struct expand_data data = EXPAND_DATA_INIT;
868877
int save_warning;
869878
int retval = 0;
870879

@@ -873,7 +882,6 @@ static int batch_objects(struct batch_options *opt)
873882
* object_info to be handed to odb_read_object_info_extended for each
874883
* object.
875884
*/
876-
memset(&data, 0, sizeof(data));
877885
data.mark_query = 1;
878886
expand_format(&output,
879887
opt->format ? opt->format : DEFAULT_FORMAT,

t/t1006-cat-file.sh

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -113,53 +113,55 @@ strlen () {
113113

114114
run_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

236255
run_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

270288
tree_oid=$(git write-tree)
271289
tree_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

280302
commit_message="Initial commit"
281303
commit_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

300322
tag_header_without_oid="type blob
301323
tag hellotag
@@ -318,8 +340,8 @@ tag_size=$(strlen "$tag_content")
318340
tag_compat_oid=$(git rev-parse --output-object-format=$test_compat_hash_algo $tag_oid)
319341
tag_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

324346
test_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

Comments
 (0)