Skip to content

Commit b0b910e

Browse files
vdyegitster
authored andcommitted
cat-file.c: add batch handling for submodules
When an object specification is passed to 'cat-file --batch[-check]' referring to a submodule (e.g. 'HEAD:path/to/my/submodule'), the current behavior of the command is to print the "missing" error message. However, it is often valuable for callers to distinguish between paths that are actually missing and "the submodule tree entry exists, but the object does not exist in the repository". To disambiguate without needing to invoke a separate Git process (e.g. 'ls-tree'), print the message "<oid> submodule" for such objects instead of "<object> missing". In addition to the change from "missing" to "submodule", the new message differs from the old in that it always prints the resolved tree entry's OID, rather than the input object specification. Note that this implementation maintains a distinction between submodules where the commit OID is not present in the repo, and submodules where the commit OID *is* present; the former will now print "<object> submodule", but the latter will still print the full object content. Signed-off-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aba1438 commit b0b910e

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

Documentation/git-cat-file.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ If a name is specified that might refer to more than one object (an ambiguous sh
373373
<object> SP ambiguous LF
374374
------------
375375

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+
376384
If `--follow-symlinks` is used, and a symlink in the repository points
377385
outside the repository, then `cat-file` will ignore any custom format
378386
and print:

builtin/cat-file.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,10 @@ static void batch_object_write(const char *obj_name,
496496
&data->oid, &data->info,
497497
OBJECT_INFO_LOOKUP_REPLACE);
498498
if (ret < 0) {
499-
report_object_status(opt, obj_name, &data->oid, "missing");
499+
if (data->mode == S_IFGITLINK)
500+
report_object_status(opt, oid_to_hex(&data->oid), &data->oid, "submodule");
501+
else
502+
report_object_status(opt, obj_name, &data->oid, "missing");
500503
return;
501504
}
502505

t/t1006-cat-file.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,31 @@ test_expect_success 'cat-file --batch-check respects replace objects' '
12201220
test_cmp expect actual
12211221
'
12221222

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+
12231248
# Pull the entry for object with oid "$1" out of the output of
12241249
# "cat-file --batch", including its object content (which requires
12251250
# parsing and reading a set amount of bytes, hence perl).

0 commit comments

Comments
 (0)