From: Hamza Mahfooz <someguy@effective-light.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
Hamza Mahfooz <someguy@effective-light.com>
Subject: [PATCH v5 1/2] grep: refactor next_match() and match_one_pattern() for external use
Date: Thu, 16 Sep 2021 10:09:42 -0400 [thread overview]
Message-ID: <20210916140943.2442184-1-someguy@effective-light.com> (raw)
These changes are made in preparation of, the colorization support for the
"git log" subcommands that, rely on regex functionality (i.e. "--author",
"--committer" and "--grep"). These changes are necessary primarily because
the format of "bol" differs in the context that I require to use
match_one_pattern() in and because next_match() doesn't handle the case of
"ctx == GREP_CONTEXT_HEAD" at all. So, teach each function how to handle
the new cases.
Signed-off-by: Hamza Mahfooz <someguy@effective-light.com>
---
v5: separate grep changes from pretty changes.
---
grep.c | 53 ++++++++++++++++++++++++++++++++++++-----------------
grep.h | 3 +++
2 files changed, 39 insertions(+), 17 deletions(-)
diff --git a/grep.c b/grep.c
index 424a39591b..e77c8643d2 100644
--- a/grep.c
+++ b/grep.c
@@ -951,31 +951,38 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
enum grep_context ctx,
regmatch_t *pmatch, int eflags)
{
+ const char *field;
+ size_t len;
int hit = 0;
int saved_ch = 0;
const char *start = bol;
+ const char *end = eol;
if ((p->token != GREP_PATTERN) &&
- ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
+ ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)) &&
+ ((p->token == GREP_PATTERN_BODY) != (ctx == GREP_CONTEXT_BODY)))
return 0;
if (p->token == GREP_PATTERN_HEAD) {
- const char *field;
- size_t len;
- assert(p->field < ARRAY_SIZE(header_field));
- field = header_field[p->field].field;
- len = header_field[p->field].len;
- if (strncmp(bol, field, len))
- return 0;
- bol += len;
switch (p->field) {
case GREP_HEADER_AUTHOR:
case GREP_HEADER_COMMITTER:
saved_ch = strip_timestamp(bol, &eol);
+ if (eol == end)
+ goto again;
break;
default:
break;
}
+
+ assert(p->field < ARRAY_SIZE(header_field));
+ field = header_field[p->field].field;
+ len = header_field[p->field].len;
+
+ if (strncmp(bol, field, len))
+ goto restore;
+
+ bol += len;
}
again:
@@ -1021,12 +1028,17 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
goto again;
}
}
- if (p->token == GREP_PATTERN_HEAD && saved_ch)
- *eol = saved_ch;
+
if (hit) {
pmatch[0].rm_so += bol - start;
pmatch[0].rm_eo += bol - start;
}
+
+restore:
+ if (p->token == GREP_PATTERN_HEAD && saved_ch)
+ *eol = saved_ch;
+
+
return hit;
}
@@ -1159,21 +1171,27 @@ static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
return 1;
}
-static int next_match(struct grep_opt *opt, char *bol, char *eol,
- enum grep_context ctx, regmatch_t *pmatch, int eflags)
+int grep_next_match(struct grep_opt *opt, char *bol, char *eol,
+ enum grep_context ctx, regmatch_t *pmatch,
+ enum grep_header_field field, int eflags)
{
struct grep_pat *p;
int hit = 0;
pmatch->rm_so = pmatch->rm_eo = -1;
if (bol < eol) {
- for (p = opt->pattern_list; p; p = p->next) {
+ for (p = ((ctx == GREP_CONTEXT_HEAD)
+ ? opt->header_list : opt->pattern_list);
+ p; p = p->next) {
switch (p->token) {
case GREP_PATTERN: /* atom */
case GREP_PATTERN_HEAD:
case GREP_PATTERN_BODY:
- hit |= match_next_pattern(p, bol, eol, ctx,
- pmatch, eflags);
+ if ((field == GREP_HEADER_FIELD_MAX) ||
+ (p->field == field))
+ hit |= match_next_pattern(p, bol, eol,
+ ctx, pmatch,
+ eflags);
break;
default:
break;
@@ -1262,7 +1280,8 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
line_color = opt->colors[GREP_COLOR_FUNCTION];
}
*eol = '\0';
- while (next_match(opt, bol, eol, ctx, &match, eflags)) {
+ while (grep_next_match(opt, bol, eol, ctx, &match,
+ GREP_HEADER_FIELD_MAX, eflags)) {
if (match.rm_so == match.rm_eo)
break;
diff --git a/grep.h b/grep.h
index 72f82b1e30..d2943e29ea 100644
--- a/grep.h
+++ b/grep.h
@@ -177,6 +177,9 @@ void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const
void compile_grep_patterns(struct grep_opt *opt);
void free_grep_patterns(struct grep_opt *opt);
int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
+int grep_next_match(struct grep_opt *opt, char *bol, char *eol,
+ enum grep_context ctx, regmatch_t *pmatch,
+ enum grep_header_field field, int eflags);
struct grep_source {
char *name;
--
2.33.0
next reply other threads:[~2021-09-16 14:10 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-16 14:09 Hamza Mahfooz [this message]
2021-09-16 14:09 ` [PATCH v5 2/2] pretty: colorize pattern matches in commit messages Hamza Mahfooz
2021-09-17 7:10 ` Eric Sunshine
2021-09-17 21:14 ` Hamza Mahfooz
2021-09-17 22:00 ` Jeff King
2021-09-17 22:15 ` Eric Sunshine
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210916140943.2442184-1-someguy@effective-light.com \
--to=someguy@effective-light.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).