git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 2/3] tag: don't misreport type of tagged objects in errors

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 30, 2022, 06:07 UTC
Message-ID
<xmqqsfgxjugi.fsf@gitster.g>
In-Reply-To
<patch-v2-2.3-96398731841-20221230T011725Z-avarab@gmail.com>
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
Show 24 quoted lines
> diff --git a/blob.c b/blob.c
> index 8f83523b0cd..d6e3d64213d 100644
> --- a/blob.c
> +++ b/blob.c
> @@ -5,12 +5,19 @@
>  
>  const char *blob_type = "blob";
>  
> -struct blob *lookup_blob(struct repository *r, const struct object_id *oid)
> +struct blob *lookup_blob_type(struct repository *r,
> +			      const struct object_id *oid,
> +			      enum object_type type)
>  {
>  	struct object *obj = lookup_object(r, oid);
>  	if (!obj)
>  		return create_object(r, oid, alloc_blob_node(r));
> -	return object_as_type(obj, OBJ_BLOB, 0);
> +	return object_as_type_hint(obj, OBJ_BLOB, type);
> +}
> +
> +struct blob *lookup_blob(struct repository *r, const struct object_id *oid)
> +{
> +	return lookup_blob_type(r, oid, OBJ_NONE);
>  }

Between lookup_blob() and lookup_blob_type(), the distinction is that the latter calls object_as_type_hint() to ensure that the real type of the given object is of the type, which is given as "hint"?

Very confusing naming. Perhaps because "hint" argument given to "as-type-hint" is playing a role that is FAR stronger than "hint"? It sounds more like enforcement. Perhaps s/hint/check/ or something?

I am not sure exactly why, but lookup_blob_type() does look confusing. A natural question anybody would ask after seeing the function mame and the extra parmater is: "If we want/expect to see a blob, why do we give an extra type?" To answer that question, "_type" at the end of the function name and "type" that is an extra argument should say what that thing is used for. "enum object_type" is enough to say what the extra argument is---we should use the parameter name to convey what it is there for.

Show 9 quoted lines
> +void *object_as_type_hint(struct object *obj, enum object_type type,
> +			  enum object_type hint)
> +{
> +	if (hint != OBJ_NONE && obj->type != OBJ_NONE && obj->type != type) {
> +		error(_("object %s is a %s, not a %s"), oid_to_hex(&obj->oid),
> +		      type_name(type), type_name(obj->type));
> +		obj->type = type;
> +		return NULL;
> +	}

There must be something utterly wrong in the above implementation. As long as "hint" is ANY type other than OBJ_NONE, "check if type and obj->type match and otherwise complain" logic kicks in, and the actual value of "hint" does not contribute anything to the outcome.

IOW, the "hint" parameter is misleading to be "enum object_type", as it is only used as a Boolean "do we bother to check?".

> +	return object_as_type(obj, type, 0);;
Stray extra semicolon at the end.
> +}
Previous: Ævar Arnfjörð BjarmasonNext: Taylor Blau
Message 19 of 22 in “object.c: use has_object() instead of repo_has_object_file()”
  1. Kousik SanagavarapuNov 16, 2022
  2. Jeff KingNov 16, 2022
  3. Jonathan TanNov 16, 2022
  4. 0/2 fixing parse_object() check for type mismatchJeff King, Nov 17, 2022
  5. 1/2 parse_object(): drop extra "has" check before checking object typeJeff King, Nov 17, 2022
  6. 2/2 parse_object(): check on-disk type of suspected blobJeff King, Nov 17, 2022
  7. Ævar Arnfjörð BjarmasonNov 18, 2022
  8. Jeff KingNov 21, 2022
  9. 0/4 tag: don't misreport type of tagged objects in errorsÆvar Arnfjörð Bjarmason, Nov 18, 2022
  10. 1/4 object-file.c: free the "t.tag" in check_tag()Ævar Arnfjörð Bjarmason, Nov 18, 2022
  11. 2/4 object tests: add test for unexpected objects in tagsÆvar Arnfjörð Bjarmason, Nov 18, 2022
  12. 4/4 tag: don't emit potentially incorrect "object is a X, not a Y"Ævar Arnfjörð Bjarmason, Nov 18, 2022
  13. 3/4 tag: don't misreport type of tagged objects in errorsÆvar Arnfjörð Bjarmason, Nov 18, 2022
  14. 0/3 tag: don't misreport type of tagged objects in errorsÆvar Arnfjörð Bjarmason, Dec 30, 2022
  15. 1/3 object tests: add test for unexpected objects in tagsÆvar Arnfjörð Bjarmason, Dec 30, 2022
  16. Junio C HamanoDec 30, 2022
  17. 3/3 tag: don't emit potentially incorrect "object is a X, not a Y"Ævar Arnfjörð Bjarmason, Dec 30, 2022
  18. 2/3 tag: don't misreport type of tagged objects in errorsÆvar Arnfjörð Bjarmason, Dec 30, 2022
  19. Junio C HamanoDec 30, 2022
  20. Taylor BlauNov 18, 2022
  21. Jeff KingNov 21, 2022
  22. Ævar Arnfjörð BjarmasonNov 22, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.