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

Re: [PATCH 1/2] ref-filter: get rid of duplicate code

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 21, 2018, 17:33 UTC
Message-ID
<xmqqtvuaz1qq.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<01020161b728823f-daec904e-44ff-45f9-9c89-d447b89c7802-000000@eu-west-1.amazonses.com>
Olga Telezhnaya <olyatelezhnaya@gmail.com> writes:
Show 8 quoted lines
> Make one function from 2 duplicate pieces and invoke it twice.
>
> Signed-off-by: Olga Telezhnaia <olyatelezhnaya@gmail.com>
> Mentored-by: Christian Couder <christian.couder@gmail.com>
> Mentored by: Jeff King <peff@peff.net>
> ---
>  ref-filter.c | 45 +++++++++++++++++++++------------------------
>  1 file changed, 21 insertions(+), 24 deletions(-)

Nice code reduction, removing 2 instances of 11-line section and replacing with a 17-line function ;-)

This looks related to your earlier changes for "cat-file --batch" but I do not recall seeing this refactoring. It seems to be done correctly.

Good spotting.
Show 79 quoted lines
> diff --git a/ref-filter.c b/ref-filter.c
> index f9e25aea7a97e..83ffd84affe52 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -1354,15 +1354,31 @@ static const char *get_refname(struct used_atom *atom, struct ref_array_item *re
>  	return show_ref(&atom->u.refname, ref->refname);
>  }
>  
> +static void get_object(struct ref_array_item *ref, const struct object_id *oid,
> +		       int deref, struct object **obj)
> +{
> +	int eaten;
> +	unsigned long size;
> +	void *buf = get_obj(oid, obj, &size, &eaten);
> +	if (!buf)
> +		die(_("missing object %s for %s"),
> +		    oid_to_hex(oid), ref->refname);
> +	if (!*obj)
> +		die(_("parse_object_buffer failed on %s for %s"),
> +		    oid_to_hex(oid), ref->refname);
> +
> +	grab_values(ref->value, deref, *obj, buf, size);
> +	if (!eaten)
> +		free(buf);
> +}
> +
>  /*
>   * Parse the object referred by ref, and grab needed value.
>   */
>  static void populate_value(struct ref_array_item *ref)
>  {
> -	void *buf;
>  	struct object *obj;
> -	int eaten, i;
> -	unsigned long size;
> +	int i;
>  	const struct object_id *tagged;
>  
>  	ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
> @@ -1483,17 +1499,7 @@ static void populate_value(struct ref_array_item *ref)
>  	return;
>  
>   need_obj:
> -	buf = get_obj(&ref->objectname, &obj, &size, &eaten);
> -	if (!buf)
> -		die(_("missing object %s for %s"),
> -		    oid_to_hex(&ref->objectname), ref->refname);
> -	if (!obj)
> -		die(_("parse_object_buffer failed on %s for %s"),
> -		    oid_to_hex(&ref->objectname), ref->refname);
> -
> -	grab_values(ref->value, 0, obj, buf, size);
> -	if (!eaten)
> -		free(buf);
> +	get_object(ref, &ref->objectname, 0, &obj);
>  
>  	/*
>  	 * If there is no atom that wants to know about tagged
> @@ -1514,16 +1520,7 @@ static void populate_value(struct ref_array_item *ref)
>  	 * is not consistent with what deref_tag() does
>  	 * which peels the onion to the core.
>  	 */
> -	buf = get_obj(tagged, &obj, &size, &eaten);
> -	if (!buf)
> -		die(_("missing object %s for %s"),
> -		    oid_to_hex(tagged), ref->refname);
> -	if (!obj)
> -		die(_("parse_object_buffer failed on %s for %s"),
> -		    oid_to_hex(tagged), ref->refname);
> -	grab_values(ref->value, 1, obj, buf, size);
> -	if (!eaten)
> -		free(buf);
> +	get_object(ref, tagged, 1, &obj);
>  }
>  
>  /*
>
> --
> https://github.com/git/git/pull/460
Previous: Olga Telezhnaya
Message 7 of 7 in “ref-filter: get rid of duplicate code”
  1. 1/2 ref-filter: get rid of duplicate codeOlga Telezhnaya, Feb 21, 2018
  2. 2/2 ref-filter: get rid of gotoOlga Telezhnaya, Feb 21, 2018
  3. Junio C HamanoFeb 21, 2018
  4. Оля ТележнаяFeb 22, 2018
  5. 1/2 ref-filter: get rid of duplicate codeOlga Telezhnaya, Feb 21, 2018
  6. 2/2 ref-filter: get rid of gotoOlga Telezhnaya, Feb 21, 2018
  7. Junio C HamanoFeb 21, 2018

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.