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

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

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Sep 6, 2015, 19:52 UTC
Message-ID
<CAPig+cSN1s6qAj7p_+qG9W52X2L-7eBR+-j+918vfycADvUyWw@mail.gmail.com>
In-Reply-To
<1441479135-5285-6-git-send-email-Karthik.188@gmail.com>
On Sat, Sep 5, 2015 at 2:52 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
Show 22 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.
>
> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---
> diff --git a/ref-filter.c b/ref-filter.c
> index a993216..e99c342 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> +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 */
> +               return 1;

If this is invoked as match_atom_name("colors", "color", ...), then it will return true (matched, but no value), which is not correct at all; "colors" is not a match for atom %(color). Maybe you meant?

    if (!body[0] || (body[0] == ':' && !body[1])) {

But, that's getting ugly and complicated, and would be bettered handled by reordering the logic of this function for dealing with the various valid and invalid cases. However...

Show 6 quoted lines
> +       }
> +       if (body[0] != ':')
> +               return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
> +       *val = body + 1; /* "atomname:val" */
> +       return 1;
> +}

It's not clear why this function exists in the first place. It's only purpose seems to be to specially recognize instances of atoms which should have a ":" but lack it, so that the caller can then report an error.

But why is this better than the more generic solution of merely reporting an error for *any* unrecognized atom? How does it help to single out these special cases?

There is a further inconsistency in that you are only specially recognizing %(color) and %(align) lacking ":", but not %(content:lines) lacking "=" in patch 8/14. Why do %(color:...) and %(align:...) get special treatment but %(content:lines=...) does not? Such inconsistency again argues in favor of a generic "unrecognized atom" detection and reporting over special case handling.

Show 30 quoted lines
>  /*
>   * In a format string, find the next occurrence of %(atom).
>   */
> @@ -721,10 +738,12 @@ static void populate_value(struct ref_array_item *ref)
>                         refname = branch_get_push(branch, NULL);
>                         if (!refname)
>                                 continue;
> -               } else if (starts_with(name, "color:")) {
> +               } else if (match_atom_name(name, "color", &valp)) {
>                         char color[COLOR_MAXLEN] = "";
>
> -                       if (color_parse(name + 6, color) < 0)
> +                       if (!valp)
> +                               die(_("expected format: %%(color:<color>)"));
> +                       if (color_parse(valp, color) < 0)
>                                 die(_("unable to parse format"));
>                         v->s = xstrdup(color);
>                         continue;
> diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh
> index 505a360..c4f0378 100755
> --- a/t/t6302-for-each-ref-filter.sh
> +++ b/t/t6302-for-each-ref-filter.sh
> @@ -81,4 +81,8 @@ test_expect_success 'filtering with --contains' '
> +test_expect_success '%(color) must fail' '
> +       test_must_fail git for-each-ref --format="%(color)%(refname)"
> +'
> +
>  test_done
> --
> 2.5.1
Previous: Karthik NayakNext: Karthik Nayak
Message 7 of 30 in “port tag.c to use ref-filter APIs”
  1. 00/14 port tag.c to use ref-filter APIsKarthik Nayak, Sep 5, 2015
  2. 01/14 ref-filter: move `struct atom_value` to ref-filter.cKarthik Nayak, Sep 5, 2015
  3. 02/14 ref-filter: introduce ref_formatting_state and ref_formatting_stackKarthik Nayak, Sep 5, 2015
  4. 03/14 utf8: add function to align a string into given strbufKarthik Nayak, Sep 5, 2015
  5. 04/14 ref-filter: introduce handler function for each atomKarthik Nayak, Sep 5, 2015
  6. 05/14 ref-filter: introduce match_atom_name()Karthik Nayak, Sep 5, 2015
  7. Eric SunshineSep 6, 2015
  8. Karthik NayakSep 7, 2015
  9. 06/14 ref-filter: implement an `align` atomKarthik Nayak, Sep 5, 2015
  10. Eric SunshineSep 6, 2015
  11. Karthik NayakSep 7, 2015
  12. 07/14 ref-filter: add option to filter out tags, branches and remotesKarthik Nayak, Sep 5, 2015
  13. 08/14 ref-filter: add support for %(contents:lines=X)Karthik Nayak, Sep 5, 2015
  14. 09/14 ref-filter: add support to sort by versionKarthik Nayak, Sep 5, 2015
  15. 10/14 ref-filter: add option to match literal patternKarthik Nayak, Sep 5, 2015
  16. 11/14 tag.c: use 'ref-filter' data structuresKarthik Nayak, Sep 5, 2015
  17. 12/14 tag.c: use 'ref-filter' APIsKarthik Nayak, Sep 5, 2015
  18. 13/14 tag.c: implement '--format' optionKarthik Nayak, Sep 5, 2015
  19. 14/14 tag.c: implement '--merged' and '--no-merged' optionsKarthik Nayak, Sep 5, 2015
  20. Matthieu MoySep 6, 2015
  21. Karthik NayakSep 6, 2015
  22. Junio C HamanoSep 7, 2015
  23. Matthieu MoySep 7, 2015
  24. Karthik NayakSep 7, 2015
  25. Matthieu MoySep 7, 2015
  26. Karthik NayakSep 8, 2015
  27. Junio C HamanoSep 8, 2015
  28. Karthik NayakSep 9, 2015
  29. Junio C HamanoSep 9, 2015
  30. Junio C HamanoSep 8, 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.