Message ID | 20221117113023.65865-6-tenglong.tl@alibaba-inc.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | ls-tree: introduce '--pattern' option | expand |
On Thu, Nov 17 2022, Teng Long wrote: > From: Teng Long <dyroneteng@gmail.com> > > 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 <dyroneteng@gmail.com> > --- > 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) > {
On Thu, Nov 17 2022, Teng Long wrote: (I saw this in the latest "seen" push-out) > From: Teng Long <dyroneteng@gmail.com> > [...] > +__attribute__((unused)) > +static int match_pattern(const char *line) > +{ > + int ret = 0; > + regex_t r; > + regmatch_t m[1]; Here we hardcode the size of "m". (Re-arranged a bit) > + > + ret = regcomp(&r, pattern, 0); > + if (ret) { > + regerror(ret, &r, errbuf, sizeof(errbuf)); > + die("failed regcomp() for pattern '%s' (%s)", pattern, errbuf); Needs _(). > + } > + ret = regexec(&r, line, 1, m, 0); Here we hardcode that "1" again, but we should use ARRAY_SIZE() instead. See an existing example at: git grep -W regexec.*ARRAY (Re-arranged from above) > + char errbuf[64]; This is a super short errbuf, in other cases we hardcode this to 1024, which seems reasonable.
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)) +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) {