Subject: Re: [PATCH v5 2/6] object-name: explicitly handle OBJ_BAD in show_ambiguous_object()

From: Josh Steadmon <[email-masked]>
Date: Thu, 23 Dec 2021 13:51:58 -0800

On 2021.11.25 23:03, Ævar Arnfjörð Bjarmason wrote:
> Amend the "unknown type" handling in the code that displays the
> ambiguous object list to assert() that we're either going to get the
> "real" object types we can pass to type_name(), or a -1 (OBJ_BAD)
> return value from oid_object_info().
> 
> See [1] for the current output, and [1] for the commit that added the
> "unknown type" handling.
> 
> We are never going to get an "unknown type" in the sense of custom
> types crafted with "hash-object --literally", since we're not using
> the OBJECT_INFO_ALLOW_UNKNOWN_TYPE flag.
> 
> If we manage to otherwise unpack such an object without errors we'll
> die() in parse_loose_header_extended() called by sort_ambiguous()
> before we get to show_ambiguous_object(), as is asserted by the test
> added in the preceding commit.
> 
> So saying "unknown type" here was always misleading, we really meant
> to say that we had a failure parsing the object at all, if the problem
> is only that it's type is unknown we won't reach this code.

Are there situations other than repo corruption where this could happen?
Maybe it would be more useful to just die() at this point and give the
user advice on how to investigate / fix the corruption, rather than
trying to disambiguate the objects involved.

> So let's emit a generic "[bad object]" instead. As our tests added in
> the preceding commit show, we'll have emitted various "error" output
> already in those cases.
> 
> We should do better in the truly "unknown type" cases, which we'd need
> to handle if we were passing down the OBJECT_INFO_ALLOW_UNKNOWN_TYPE
> flag. But let's leave that for some future improvement. In a
> subsequent commit I'll improve the output we do show, and not having
> to handle the "unknown type" (as in OBJECT_INFO_ALLOW_UNKNOWN_TYPE)
> simplifies that change.
> 
> 1. 5cc044e0257 (get_short_oid: sort ambiguous objects by type,
>    then SHA-1, 2018-05-10)
> 2. 1ffa26c461 (get_short_sha1: list ambiguous objects on error,
>    2016-09-26)
> 
> Signed-off-by: Ævar Arnfjörð Bjarmason <[email-masked]>
> ---
>  object-name.c                       | 14 ++++++++++++--
>  t/t1512-rev-parse-disambiguation.sh |  2 +-
>  2 files changed, 13 insertions(+), 3 deletions(-)
> 
> diff --git a/object-name.c b/object-name.c
> index fdff4601b2c..9750634ee76 100644
> --- a/object-name.c
> +++ b/object-name.c
> @@ -361,6 +361,16 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
>  		return 0;
>  
>  	type = oid_object_info(ds->repo, oid, NULL);
> +
> +	if (type < 0) {
> +		strbuf_addstr(&desc, "[bad object]");
> +		goto out;
> +	}
> +
> +	assert(type == OBJ_TREE || type == OBJ_COMMIT ||
> +	       type == OBJ_BLOB || type == OBJ_TAG);
> +	strbuf_addstr(&desc, type_name(type));
> +
>  	if (type == OBJ_COMMIT) {
>  		struct commit *commit = lookup_commit(ds->repo, oid);
>  		if (commit) {
> @@ -374,9 +384,9 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
>  			strbuf_addf(&desc, " %s", tag->tag);
>  	}
>  
> -	advise("  %s %s%s",
> +out:
> +	advise("  %s %s",
>  	       repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV),
> -	       type_name(type) ? type_name(type) : "unknown type",
>  	       desc.buf);
>  
>  	strbuf_release(&desc);
> diff --git a/t/t1512-rev-parse-disambiguation.sh b/t/t1512-rev-parse-disambiguation.sh
> index ae1c0cf2b21..f1948980dff 100755
> --- a/t/t1512-rev-parse-disambiguation.sh
> +++ b/t/t1512-rev-parse-disambiguation.sh
> @@ -100,7 +100,7 @@ test_expect_success POSIXPERM 'ambigous zlib corrupt loose blob' '
>  	error: unable to unpack cafe... header
>  	error: inflate: data stream error (incorrect header check)
>  	error: unable to unpack cafe... header
> -	hint:   cafe... unknown type
> +	hint:   cafe... [bad object]
>  	hint:   cafe... blob
>  	fatal: ambiguous argument '\''cafe...'\'': unknown revision or path not in the working tree.
>  	Use '\''--'\'' to separate paths from revisions, like this:
> -- 
> 2.34.1.838.g779e9098efb
>