From: Hamza Mahfooz <someguy@effective-light.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>,
Eric Sunshine <sunshine@sunshineco.com>,
Hamza Mahfooz <someguy@effective-light.com>
Subject: [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use
Date: Tue, 21 Sep 2021 17:13:23 -0400 [thread overview]
Message-ID: <20210921211324.1426938-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
match_one_pattern() expects header lines to be prefixed, however, in
pretty, the prefixes are stripped from the lines because the name-email
pairs needs to go through additional parsing, before they can be printed
and because next_match() doesn't handle the case of
"ctx == GREP_CONTEXT_HEAD" at all. So, teach next_match() how to handle the
new case, move header_field[] so it can be used by pretty to reappend
relevant prefixes and teach match_one_pattern() how to handle subsequent
header line match attempts.
Signed-off-by: Hamza Mahfooz <someguy@effective-light.com>
---
v5: separate grep changes from pretty changes.
v6: rescope some variables.
v7: export header_field[] and allow for subsequent matches on header lines
in match_one_pattern().
---
grep.c | 53 ++++++++++++++++++++++++++++-------------------------
grep.h | 13 +++++++++++++
2 files changed, 41 insertions(+), 25 deletions(-)
diff --git a/grep.c b/grep.c
index 14fe8a0fd2..f4126011c5 100644
--- a/grep.c
+++ b/grep.c
@@ -935,15 +935,6 @@ static void strip_timestamp(const char *bol, const char **eol_p)
}
}
-static struct {
- const char *field;
- size_t len;
-} header_field[] = {
- { "author ", 7 },
- { "committer ", 10 },
- { "reflog ", 7 },
-};
-
static int match_one_pattern(struct grep_pat *p,
const char *bol, const char *eol,
enum grep_context ctx,
@@ -953,18 +944,23 @@ static int match_one_pattern(struct grep_pat *p,
const char *start = bol;
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;
+ if (!(eflags & REG_NOTBOL)) {
+ const char *field;
+ size_t len;
+
+ assert(p->field < ARRAY_SIZE(grep_header_fields));
+ field = grep_header_fields[p->field].field;
+ len = grep_header_fields[p->field].len;
+ if (strncmp(bol, field, len))
+ return 0;
+ bol += len;
+ }
+
switch (p->field) {
case GREP_HEADER_AUTHOR:
case GREP_HEADER_COMMITTER:
@@ -1158,22 +1154,28 @@ static int match_next_pattern(struct grep_pat *p,
return 1;
}
-static int next_match(struct grep_opt *opt,
- const char *bol, const char *eol,
- enum grep_context ctx, regmatch_t *pmatch, int eflags)
+int grep_next_match(struct grep_opt *opt,
+ const char *bol, const 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;
@@ -1261,7 +1263,8 @@ static void show_line(struct grep_opt *opt,
else if (sign == '=')
line_color = opt->colors[GREP_COLOR_FUNCTION];
}
- 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 3cb8a83ae8..4847c37280 100644
--- a/grep.h
+++ b/grep.h
@@ -23,6 +23,15 @@ typedef int pcre2_general_context;
#include "thread-utils.h"
#include "userdiff.h"
+static const struct {
+ const char *field;
+ size_t len;
+} grep_header_fields[] = {
+ { "author ", 7 },
+ { "committer ", 10 },
+ { "reflog ", 7 },
+};
+
struct repository;
enum grep_pat_token {
@@ -190,6 +199,10 @@ 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, const char *buf, unsigned long size);
+int grep_next_match(struct grep_opt *opt,
+ const char *bol, const 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-21 21:13 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-21 21:13 Hamza Mahfooz [this message]
2021-09-21 21:13 ` [PATCH v7 2/2] pretty: colorize pattern matches in commit messages Hamza Mahfooz
2021-09-23 17:25 ` [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use Junio C Hamano
2021-09-24 12:04 ` Hamza Mahfooz
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210921211324.1426938-1-someguy@effective-light.com \
--to=someguy@effective-light.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
--cc=sunshine@sunshineco.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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).