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

Re: [PATCH v4 06/10] ref-filter: add option to match literal pattern

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jul 26, 2015, 05:15 UTC
Message-ID
<CAPig+cTAYAxg4aWpaPrHe6Hfz3wCEiypoXZxS_Lir4yNOFzSNA@mail.gmail.com>
In-Reply-To
<1437764685-8633-7-git-send-email-Karthik.188@gmail.com>
On Fri, Jul 24, 2015 at 3:04 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
Show 34 quoted lines
> Since 'ref-filter' only has an option to match path names add an
> option for plain fnmatch pattern-matching.
>
> This is to support the pattern matching options which are used in `git
> tag -l` and `git branch -l` where we can match patterns like `git tag
> -l foo*` which would match all tags which has a "foo*" pattern.
>
> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---
> diff --git a/ref-filter.c b/ref-filter.c
> index 8f2148f..0a64b84 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -951,6 +951,31 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
>
>  /*
>   * Return 1 if the refname matches one of the patterns, otherwise 0.
> + * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
> + * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
> + * matches "refs/heads/mas*", too).
> + */
> +static int match_pattern(const char **patterns, const char *refname)
> +{
> +       for (; *patterns; patterns++) {
> +               /*
> +                * When no '--format' option is given we need to skip the prefix
> +                * for matching refs of tags and branches.
> +                */
> +               if (!starts_with(*patterns, "refs/tags/"))
> +                       skip_prefix(refname, "refs/tags/", &refname);
> +               if (!starts_with(*patterns, "refs/heads/"))
> +                       skip_prefix(refname, "refs/heads/", &refname);
> +               if (!starts_with(*patterns, "refs/remotes/"))
> +                       skip_prefix(refname, "refs/remotes/", &refname);

Note the inefficiency here: You're performing an "expensive" starts_with() on each pattern for each refname even though the patterns will never change. You could instead compute starts_with() for each pattern just once, in a preprocessing step, and remember each result as a boolean, and then use the computed booleans here in place of starts_with(). For a few refnames, this may not make a difference, but for a project with a huge number, it could. Whether such an optimization is worth the complexity (at this time or ever) is something that can be answered by taking measurements.

Also, the repetition in the code is not all that pretty. You could instead place "refs/tags/", "refs/heads/", and "refs/remotes/" in a table and then loop over them rather than repeating the code for each one, though whether that would be an improvement is likely a judgment call (so not something I'd insist upon).

Show 5 quoted lines
> +               if (!wildmatch(*patterns, refname, 0, NULL))
> +                       return 1;
> +       }
> +       return 0;
> +}
Previous: Karthik NayakNext: Karthik Nayak
Message 31 of 36 in “port tag.c to use ref-filter APIs”
  1. 0/10 port tag.c to use ref-filter APIsKarthik Nayak, Jul 24, 2015
  2. 01/10 ref-filter: add option to align atoms to the leftKarthik Nayak, Jul 24, 2015
  3. Junio C HamanoJul 24, 2015
  4. Junio C HamanoJul 24, 2015
  5. Karthik NayakJul 25, 2015
  6. Eric SunshineJul 26, 2015
  7. Karthik NayakJul 26, 2015
  8. Eric SunshineJul 26, 2015
  9. Karthik NayakJul 26, 2015
  10. Matthieu MoyJul 27, 2015
  11. Duy NguyenJul 27, 2015
  12. Jacob KellerJul 27, 2015
  13. Duy NguyenJul 27, 2015
  14. Duy NguyenJul 28, 2015
  15. 02/10 ref-filter: make the 'color' use ref_formatting_stateKarthik Nayak, Jul 24, 2015
  16. Junio C HamanoJul 24, 2015
  17. Karthik NayakJul 25, 2015
  18. Eric SunshineJul 26, 2015
  19. Eric SunshineJul 26, 2015
  20. Karthik NayakJul 26, 2015
  21. 03/10 ref-filter: add option to filter only tagsKarthik Nayak, Jul 24, 2015
  22. 04/10 ref-filter: support printing N lines from tag annotationKarthik Nayak, Jul 24, 2015
  23. Eric SunshineJul 26, 2015
  24. Karthik NayakJul 26, 2015
  25. 05/10 ref-filter: add support to sort by versionKarthik Nayak, Jul 24, 2015
  26. Junio C HamanoJul 25, 2015
  27. Karthik NayakJul 26, 2015
  28. Junio C HamanoJul 27, 2015
  29. Karthik NayakJul 27, 2015
  30. 06/10 ref-filter: add option to match literal patternKarthik Nayak, Jul 24, 2015
  31. Eric SunshineJul 26, 2015
  32. Karthik NayakJul 26, 2015
  33. 07/10 tag.c: use 'ref-filter' data structuresKarthik Nayak, Jul 24, 2015
  34. 08/10 tag.c: use 'ref-filter' APIsKarthik Nayak, Jul 24, 2015
  35. 09/10 tag.c: implement '--format' optionKarthik Nayak, Jul 24, 2015
  36. 10/10 tag.c: implement '--merged' and '--no-merged' optionsKarthik Nayak, Jul 24, 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.