diff mbox series

[3/3] cat-file: avoid verifying submodules' OIDs

Message ID 951f73397c15f76da75bbd74a02f36da0116623f.1710183362.git.gitgitgadget@gmail.com (mailing list archive)
State New
Headers show
Series cat-file: add %(objectmode) avoid verifying submodules' OIDs | expand

Commit Message

Johannes Schindelin March 11, 2024, 6:56 p.m. UTC
From: Johannes Schindelin <johannes.schindelin@gmx.de>

Submodules are strange creatures. They have OIDs, but the corresponding
objects are not expected to be present in the current directory.

Let's teach `cat-file` about this: This command should not even attempt
to look up those objects, let alone declare them "missing".

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 Documentation/git-cat-file.txt |  5 +++++
 builtin/cat-file.c             | 32 ++++++++++++++++++++++++++------
 t/t1006-cat-file.sh            | 10 ++++++++++
 3 files changed, 41 insertions(+), 6 deletions(-)

Comments

Jeff King March 12, 2024, 8:58 a.m. UTC | #1
On Mon, Mar 11, 2024 at 06:56:02PM +0000, Johannes Schindelin via GitGitGadget wrote:

> diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt
> index de29e6d79d9..69b50d2042f 100644
> --- a/Documentation/git-cat-file.txt
> +++ b/Documentation/git-cat-file.txt
> @@ -412,6 +412,11 @@ Note also that multiple copies of an object may be present in the object
>  database; in this case, it is undefined which copy's size or delta base
>  will be reported.
>  
> +Submodules are handled specially in `git cat-file`, as the objects
> +corresponding to the recorded OIDs are not expected to be present in the
> +current repository. For that reason, submodules are reported as having
> +type `submodule` and mode 1600000 and all other fields are zeroed out.

I think there's an extra 0 in the mode here?

It may also be worth being more explicit about when Git knows something
is a submodule. Naively, reading the above I might think that:

  git ls-tree --format='%(objectname)' HEAD | git cat-file --batch-check

would do something special with submodules. But it can't, as there's no
context carried in just the objectname. This is obvious if you are
familiar with how Git works, but I'm not sure it would be for all end
users. So we could say something along the lines of:

  When `cat-file` is given a name within a tree that points to a
  submodule (e.g., `HEAD:my-submodule`), ...

-Peff
Junio C Hamano March 12, 2024, 6:35 p.m. UTC | #2
"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
writes:

> +Submodules are handled specially in `git cat-file`, as the objects
> +corresponding to the recorded OIDs are not expected to be present in the
> +current repository. For that reason, submodules are reported as having
> +type `submodule` and mode 1600000 and all other fields are zeroed out.

While the above may not be technically wrong per-se, I am not sure
if that is the more important part of what we want to tell our
users.  For example, "git ls-tree HEAD -- sha1collisiondetection"
reports "160000 commit ...object.name.... sha1collisiondetection".
Is it correct to say ...

    Submodules are handled specially in `git ls-tree`, as the
    objects corresponding to the recorded OIDs are not expected to
    be present in the current repository.

...?  I do not think so.

For the same reason, as an explanation for the reason why "git
cat-file -t :sha1collisiondetection" just reports "submodule", the
new text does not sit well.

I actually have to wonder if the new behaviour proposed by this
patch is a solution that is in search of a problem, or trying to
solve an unstated problem in a wrong way.

    O=$(git rev-parse --verify :sha1collisiondetection)
    git cat-file -t "$O"
    
should fail because the object whose name is $O is not available.
Why should then this succeed and give a different result?

    git cat-file -t :sha1collisiondetection

The "cat-file" command is about objects.  While object's type may
sometimes be inferrable (by being contained in a tree), if the user
asks us to determine the type of the object, we should actually hit
the object store, whether the commit object in question happens to
be on our history or somebody else's history that our gitlink points
at.

So, I am not yet convinced that I should take this patch.  Previous
two steps looked good, though.

Thanks.

> index 73bd78c0b63..c59ad682d1f 100644
> --- a/builtin/cat-file.c
> +++ b/builtin/cat-file.c
> @@ -128,7 +128,9 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
>  	switch (opt) {
>  	case 't':
>  		oi.type_name = &sb;
> -		if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
> +		if (obj_context.mode == S_IFGITLINK)
> +			strbuf_addstr(&sb, "submodule");
> +		else if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
>  			die("git cat-file: could not get object info");
Jeff King March 12, 2024, 10:17 p.m. UTC | #3
On Tue, Mar 12, 2024 at 11:35:16AM -0700, Junio C Hamano wrote:

> I actually have to wonder if the new behaviour proposed by this
> patch is a solution that is in search of a problem, or trying to
> solve an unstated problem in a wrong way.
> 
>     O=$(git rev-parse --verify :sha1collisiondetection)
>     git cat-file -t "$O"
>     
> should fail because the object whose name is $O is not available.
> Why should then this succeed and give a different result?
> 
>     git cat-file -t :sha1collisiondetection
> 
> The "cat-file" command is about objects.  While object's type may
> sometimes be inferrable (by being contained in a tree), if the user
> asks us to determine the type of the object, we should actually hit
> the object store, whether the commit object in question happens to
> be on our history or somebody else's history that our gitlink points
> at.
> 
> So, I am not yet convinced that I should take this patch.  Previous
> two steps looked good, though.

I'm not sure about "-t" in particular, but for batch output, I think if
we stop at patch 2 it would be impossible to tell the difference between
a submodule entry and a corrupt repo (or bad request). E.g., if I do
this:

  (echo HEAD:Makefile; echo HEAD:sha1collisiondetection) |
  git cat-file --batch-check='%(objectname) %(objectmode)'

after only patch 2, I'd get:

  4e255c81f22386389c7460d8f5e59426673b5a5a 100644
  HEAD:sha1collisiondetection missing

We can't tell if HEAD didn't resolve, or it doesn't have that path, or
if it's a regular blob entry and the repository is corrupt. Whereas
after patch 3, we get:

  4e255c81f22386389c7460d8f5e59426673b5a5a 100644
  855827c583bc30645ba427885caa40c5b81764d2 160000

and the mode tells us that we resolved it to a submodule.

The current behavior is not too surprising for cat-file, since it's
whole purpose is to give you information about the objects themselves,
and we don't have one here. But with this %(objectmode) format, we're
really moving into a realm of "resolve this name for me and show me the
context". We don't care about the details of the object at all!

I think you could make an argument that the problem is shoe-horning new,
slightly-mismatched functionality into cat-file. But there are lots of
practical reasons to want to do so, as we discussed elsewhere. Since
gitlinks are the only place where we'd expect an object to be missing,
"simulating" them here isn't too bad. But I suspect there's a more
general solution where cat-file learns to print dummy values for any
missing object, letting the caller see what we _could_ find out. And
then the submodule case just falls out naturally. I doubt we could make
it the default for historical compatibility; we'd need a new option.

This is all speculative on my part, of course. Probably Dscho or
Victoria can explain their use case better. :)

-Peff
Junio C Hamano March 13, 2024, 3:22 p.m. UTC | #4
Jeff King <peff@peff.net> writes:

> I think you could make an argument that the problem is
> shoe-horning new, slightly-mismatched functionality into
> cat-file. But there are lots of practical reasons to want to do
> so, as we discussed elsewhere.  Since gitlinks are the only place
> where we'd expect an object to be missing, "simulating" them here
> isn't too bad.

100% agreed.  This is something we should be asking about "HEAD:"
tree object, not about "HEAD:sha1collisiondetection" object, if we
are to ask cat-file.  After all "cat-file p HEAD:" tells us that the
thing is a submodule already.  But unfortunately the "--batch" thing
is limited to "give me an object and what you want to know about the
object, and I'll tell you what I know about it" exchange, so it is a
very bad match when you cannot really give it an object (which you
do not have, like the target of the gitlink).  So...

> But I suspect there's a more
> general solution where cat-file learns to print dummy values for any
> missing object, letting the caller see what we _could_ find out. And
> then the submodule case just falls out naturally. I doubt we could make
> it the default for historical compatibility; we'd need a new option.

... "--batch" obviously needs to be extended, and %(objectmode) may
be one direction to do so, but it would also work to allow us to ask
about "HEAD:" and what it has at paths, which match a pathspec
"sha1collisiondetection", an equivalent to give "cat-file --batch" a
command to drive "ls-tree".

> This is all speculative on my part, of course. Probably Dscho or
> Victoria can explain their use case better. :)

Likewise.
diff mbox series

Patch

diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt
index de29e6d79d9..69b50d2042f 100644
--- a/Documentation/git-cat-file.txt
+++ b/Documentation/git-cat-file.txt
@@ -412,6 +412,11 @@  Note also that multiple copies of an object may be present in the object
 database; in this case, it is undefined which copy's size or delta base
 will be reported.
 
+Submodules are handled specially in `git cat-file`, as the objects
+corresponding to the recorded OIDs are not expected to be present in the
+current repository. For that reason, submodules are reported as having
+type `submodule` and mode 1600000 and all other fields are zeroed out.
+
 GIT
 ---
 Part of the linkgit:git[1] suite
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 73bd78c0b63..c59ad682d1f 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -128,7 +128,9 @@  static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
 	switch (opt) {
 	case 't':
 		oi.type_name = &sb;
-		if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
+		if (obj_context.mode == S_IFGITLINK)
+			strbuf_addstr(&sb, "submodule");
+		else if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
 			die("git cat-file: could not get object info");
 		if (sb.len) {
 			printf("%s\n", sb.buf);
@@ -319,17 +321,26 @@  static void expand_atom(struct strbuf *sb, const char *atom, int len,
 		if (!data->mark_query)
 			strbuf_addstr(sb, oid_to_hex(&data->oid));
 	} else if (is_atom("objecttype", atom, len)) {
-		if (data->mark_query)
+		if (data->mode == S_IFGITLINK) {
+			if (!data->mark_query)
+				strbuf_addstr(sb, "submodule");
+		} else if (data->mark_query)
 			data->info.typep = &data->type;
 		else
 			strbuf_addstr(sb, type_name(data->type));
 	} else if (is_atom("objectsize", atom, len)) {
-		if (data->mark_query)
+		if (data->mode == S_IFGITLINK) {
+			if (!data->mark_query)
+				strbuf_addstr(sb, "0");
+		} else if (data->mark_query)
 			data->info.sizep = &data->size;
 		else
 			strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
 	} else if (is_atom("objectsize:disk", atom, len)) {
-		if (data->mark_query)
+		if (data->mode == S_IFGITLINK) {
+			if (!data->mark_query)
+				strbuf_addstr(sb, "0");
+		} else if (data->mark_query)
 			data->info.disk_sizep = &data->disk_size;
 		else
 			strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
@@ -448,7 +459,8 @@  static void print_default_format(struct strbuf *scratch, struct expand_data *dat
 				 struct batch_options *opt)
 {
 	strbuf_addf(scratch, "%s %s %"PRIuMAX"%c", oid_to_hex(&data->oid),
-		    type_name(data->type),
+		    data->mode == S_IFGITLINK ?
+		    "submodule" : type_name(data->type),
 		    (uintmax_t)data->size, opt->output_delim);
 }
 
@@ -470,7 +482,15 @@  static void batch_object_write(const char *obj_name,
 		if (use_mailmap)
 			data->info.typep = &data->type;
 
-		if (pack)
+		if (data->mode == S_IFGITLINK) {
+			data->type = OBJ_BAD; /* `type_name()` does not know submodules */
+			data->size = 0;
+			data->disk_size = 0;
+			data->rest = NULL;
+			oidcpy(&data->delta_base_oid, null_oid());
+			memset(&data->info, 0, sizeof(data->info));
+			ret = 0; /* no info to look up */
+		} else if (pack)
 			ret = packed_object_info(the_repository, pack, offset,
 						 &data->info);
 		else
diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh
index 6f25cc20ec6..3368b663ef3 100755
--- a/t/t1006-cat-file.sh
+++ b/t/t1006-cat-file.sh
@@ -1178,6 +1178,16 @@  test_expect_success 'cat-file --batch-check respects replace objects' '
 	test_cmp expect actual
 '
 
+test_expect_success 'batch-command with a submodule' '
+	printf "160000 commit %0.*d\tsub\n" $(test_oid hexsz) 17 >tree-with-sub &&
+	tree=$(git mktree <tree-with-sub) &&
+	git cat-file --batch-check >actual <<-EOF &&
+	$tree:sub
+	EOF
+	printf "%0.*d submodule 0\n" $(test_oid hexsz) 17 >expect &&
+	test_cmp expect actual
+'
+
 # Pull the entry for object with oid "$1" out of the output of
 # "cat-file --batch", including its object content (which requires
 # parsing and reading a set amount of bytes, hence perl).