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

Re: [PATCH v17 05/14] ref-filter: introduce match_atom_name()

From
Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Date
Sep 10, 2015, 16:49 UTC
Message-ID
<vpqbndap6d1.fsf@scolette.imag.fr>
In-Reply-To
<1441900110-4015-6-git-send-email-Karthik.188@gmail.com>
Karthik Nayak <karthik.188@gmail.com> writes:
Show 32 quoted lines
> Introduce match_atom_name() which helps in checking if a particular
> atom is the atom we're looking for and if it has a value attached to
> it or not.
>
> Use it instead of starts_with() for checking the value of %(color:...)
> atom. Write a test for the same.
>
> Mentored-by: Christian Couder <christian.couder@gmail.com>
> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
> Thanks-to: Junio C Hamano <gitster@pobox.com>
> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---
>  ref-filter.c                   | 23 +++++++++++++++++++++--
>  t/t6302-for-each-ref-filter.sh |  4 ++++
>  2 files changed, 25 insertions(+), 2 deletions(-)
>
> diff --git a/ref-filter.c b/ref-filter.c
> index a993216..70d36fe 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -189,6 +189,22 @@ static void pop_stack_element(struct ref_formatting_stack **stack)
>  	*stack = prev;
>  }
>  
> +static int match_atom_name(const char *name, const char *atom_name, const char **val)
> +{
> +	const char *body;
> +
> +	if (!skip_prefix(name, atom_name, &body))
> +		return 0; /* doesn't even begin with "atom_name" */
> +	if (!body[0] || !body[1]) {
> +		*val = NULL; /* %(atom_name) and no customization */

The logic is still hard to follow. If I read correctly, this function accepts "%(colorX)" the same ways as "%(color)" here. I first thought it was a bug, but it doesn't seem to be since %(colorX) would have been rejected earlier.

It would be a bug if colorX was actually a valid atom name though: you would be returning 1 for match_atom_name(name, "color") when name=="colorX". So, I would say this "we can accept one extra character because some earlier code rejected it before" is too hard to follow for reviwers and too fragile.

OTOH, you are now accepting %(atom:) as a synonym to %(atom), and it's not clear whether this is a deliberate decition.

Show 7 quoted lines
> +		return 1;
> +	}
> +	if (body[0] != ':')
> +		return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
> +	*val = body + 1; /* "atom_name:val" */
> +	return 1;
> +}
Reversing the logic like this would be better IMHO:
static int match_atom_name(const char *name, const char *atom_name, const char **val)
{
        const char *body;
	if (!skip_prefix(name, atom_name, &body)) {
		/* doesn't even begin with "atom_name" */
		return 0;
	}
	if (!body[0]) {
		/* "atom_name" and no customization */
                *val = NULL;
                return 1;
	}
	if (body[0] != ':')
		/* "atom_namefoo" is not "atom_name" or "atom_name:..." */
		return 0;
	if (!body[1]) {
		/* "atom_name:" */
		*val = NULL;
		return 1;
	}
	/* "atom_name:... */
	*val = body + 1;
	return 1;
}

=> each case appears very clearly, and we check body[0] != ':' before testing !body[1], so %(colorX) is rejected before noticing the '\0' after the 'X'.

"atom_name:" appears explicitly. If we want to reject it, we know which code to change.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Previous: Karthik NayakNext: Junio C Hamano
Message 7 of 56 in “port tag.c to use ref-filter APIs”
  1. 00/14 port tag.c to use ref-filter APIsKarthik Nayak, Sep 10, 2015
  2. 01/14 ref-filter: move `struct atom_value` to ref-filter.cKarthik Nayak, Sep 10, 2015
  3. 02/14 ref-filter: introduce ref_formatting_state and ref_formatting_stackKarthik Nayak, Sep 10, 2015
  4. 03/14 utf8: add function to align a string into given strbufKarthik Nayak, Sep 10, 2015
  5. 04/14 ref-filter: introduce handler function for each atomKarthik Nayak, Sep 10, 2015
  6. 05/14 ref-filter: introduce match_atom_name()Karthik Nayak, Sep 10, 2015
  7. Matthieu MoySep 10, 2015
  8. Junio C HamanoSep 10, 2015
  9. Matthieu MoySep 10, 2015
  10. Junio C HamanoSep 10, 2015
  11. Matthieu MoySep 10, 2015
  12. Junio C HamanoSep 10, 2015
  13. Karthik NayakSep 10, 2015
  14. Junio C HamanoSep 10, 2015
  15. Karthik NayakSep 10, 2015
  16. Junio C HamanoSep 10, 2015
  17. Karthik NayakSep 10, 2015
  18. 05/14 ref-filter: introduce match_atom_name()Karthik Nayak, Sep 11, 2015
  19. 06/14 ref-filter: implement an `align` atomKarthik Nayak, Sep 11, 2015
  20. 06/14 ref-filter: implement an `align` atomKarthik Nayak, Sep 10, 2015
  21. Matthieu MoySep 10, 2015
  22. Junio C HamanoSep 10, 2015
  23. 06/14 ref-filter: implement an `align` atomKarthik Nayak, Sep 11, 2015
  24. 07/14 ref-filter: add option to filter out tags, branches and remotesKarthik Nayak, Sep 10, 2015
  25. 08/14 ref-filter: add support for %(contents:lines=X)Karthik Nayak, Sep 10, 2015
  26. Junio C HamanoSep 10, 2015
  27. Karthik NayakSep 10, 2015
  28. 08/14 ref-filter: add support for %(contents:lines=X)Karthik Nayak, Sep 11, 2015
  29. 09/14 ref-filter: add support to sort by versionKarthik Nayak, Sep 10, 2015
  30. 10/14 ref-filter: add option to match literal patternKarthik Nayak, Sep 10, 2015
  31. 11/14 tag.c: use 'ref-filter' data structuresKarthik Nayak, Sep 10, 2015
  32. 12/14 tag.c: use 'ref-filter' APIsKarthik Nayak, Sep 10, 2015
  33. 12/14 tag.c: use 'ref-filter' APIsKarthik Nayak, Sep 11, 2015
  34. 13/14 tag.c: implement '--format' optionKarthik Nayak, Sep 10, 2015
  35. Junio C HamanoSep 10, 2015
  36. Karthik NayakSep 10, 2015
  37. 13/14 tag.c: implement '--format' optionKarthik Nayak, Sep 11, 2015
  38. 14/14 tag.c: implement '--merged' and '--no-merged' optionsKarthik Nayak, Sep 10, 2015
  39. John KeepingSep 17, 2015
  40. Junio C HamanoSep 17, 2015
  41. Matthieu MoySep 18, 2015
  42. John KeepingSep 18, 2015
  43. Matthieu MoySep 18, 2015
  44. Karthik NayakSep 18, 2015
  45. Matthieu MoySep 18, 2015
  46. Karthik NayakSep 18, 2015
  47. Matthieu MoySep 18, 2015
  48. Karthik NayakSep 18, 2015
  49. Matthieu MoySep 18, 2015
  50. Matthieu MoySep 10, 2015
  51. Karthik NayakSep 11, 2015
  52. Eric SunshineSep 11, 2015
  53. Junio C HamanoSep 11, 2015
  54. Karthik NayakSep 11, 2015
  55. Matthieu MoySep 12, 2015
  56. Karthik NayakSep 13, 2015

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.