Subject: Re: [RFC PATCH 5/6] ls-tree: introduce 'match_pattern()' function

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Thu, 17 Nov 2022 15:02:34 +0100

On Thu, Nov 17 2022, Teng Long wrote:

> From: Teng Long <[email-masked]>
>
> In preparation for actually implementing the '--pattern' option, we
> add a new method called 'match_pattern' that uses regular expressions
> to match 'ls-tree' entities.
>
> Signed-off-by: Teng Long <[email-masked]>
> ---
>  builtin/ls-tree.c | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
>
> diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c
> index 7661170f7ca..03dd3fbcb26 100644
> --- a/builtin/ls-tree.c
> +++ b/builtin/ls-tree.c
> @@ -24,6 +24,7 @@ static struct pathspec pathspec;
>  static int chomp_prefix;
>  static const char *ls_tree_prefix;
>  static const char *format;
> +static const char *pattern;
>  struct show_tree_data {
>  	unsigned mode;
>  	enum object_type type;
> @@ -46,6 +47,32 @@ static enum ls_tree_cmdmode {
>  	MODE_OBJECT_ONLY,
>  } cmdmode;
>  
> +__attribute__((unused))

This isn't portable (we'd need a check in git-compat-util.h, and in any
case just squashing this whole commit into 6/6 where it actually gets
used would be better.

> +static int match_pattern(const char *line)
> +{
> +	int ret = 0;
> +	regex_t r;
> +	regmatch_t m[1];
> +	char errbuf[64];
> +
> +	ret = regcomp(&r, pattern, 0);
> +	if (ret) {
> +		regerror(ret, &r, errbuf, sizeof(errbuf));
> +		die("failed regcomp() for pattern '%s' (%s)", pattern, errbuf);
> +	}
> +	ret = regexec(&r, line, 1, m, 0);
> +	if (ret) {
> +		if (ret == REG_NOMATCH)
> +			goto cleanup;
> +		regerror(ret, &r, errbuf, sizeof(errbuf));
> +		die("failed regexec() for subject '%s' (%s)", line, errbuf);
> +	}
> +
> +cleanup:
> +	regfree(&r);
> +	return ret;
> +}
> +
>  static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
>  			      const enum object_type type, unsigned int padded)
>  {