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] pretty: colorize pattern matches in commit messages
Date: Wed, 1 Sep 2021 08:16:16 -0400 [thread overview]
Message-ID: <20210901121616.2109658-1-someguy@effective-light.com> (raw)
Currently, for example when
git log --grep=pattern
is executed, the outputted commits that are matched by the pattern do not
have the relevant substring matches highlighted.
Signed-off-by: Hamza Mahfooz <someguy@effective-light.com>
---
pretty.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 98 insertions(+), 11 deletions(-)
diff --git a/pretty.c b/pretty.c
index 9631529c10..2886916ae6 100644
--- a/pretty.c
+++ b/pretty.c
@@ -431,15 +431,80 @@ const char *show_ident_date(const struct ident_split *ident,
return show_date(date, tz, mode);
}
+static void append_matched_line(struct grep_opt *opt, const char *line,
+ size_t linelen, enum grep_pat_token token,
+ int field, struct strbuf *sb)
+{
+ struct grep_pat *pat;
+ struct strbuf tmp_sb;
+ regmatch_t tmp_match, match;
+ char *buf, *eol, *color;
+ int cflags = 0;
+
+ strbuf_init(&tmp_sb, linelen + 1);
+ strbuf_add(&tmp_sb, line, linelen);
+ buf = tmp_sb.buf;
+ eol = buf + linelen;
+
+ if (!opt || !want_color(opt->color))
+ goto skip;
+
+ color = opt->colors[GREP_COLOR_MATCH_CONTEXT];
+
+ for (;;) {
+ match.rm_so = match.rm_eo = -1;
+
+ for (pat = (token == GREP_PATTERN_HEAD ?
+ opt->header_list : opt->pattern_list);
+ pat; pat = pat->next) {
+ if (pat->token == token &&
+ (field == -1 || pat->field == field) &&
+ !regexec(&pat->regexp, buf, 1, &tmp_match,
+ cflags)) {
+
+ if ((match.rm_so >= 0 && match.rm_eo >= 0) &&
+ (tmp_match.rm_so > match.rm_so ||
+ (tmp_match.rm_so == match.rm_so &&
+ tmp_match.rm_eo < match.rm_eo)))
+ continue;
+
+ match.rm_so = tmp_match.rm_so;
+ match.rm_eo = tmp_match.rm_eo;
+ }
+ }
+
+ if (match.rm_so == match.rm_eo)
+ break;
+
+ strbuf_grow(sb, strlen(color) + strlen(GIT_COLOR_RESET));
+ strbuf_add(sb, buf, match.rm_so);
+ strbuf_add(sb, color, strlen(color));
+ strbuf_add(sb, buf + match.rm_so,
+ match.rm_eo - match.rm_so);
+ strbuf_add(sb, GIT_COLOR_RESET,
+ strlen(GIT_COLOR_RESET));
+ buf += match.rm_eo;
+ cflags = REG_NOTBOL;
+ }
+
+skip:
+ strbuf_add(sb, buf, eol - buf);
+
+ strbuf_release(&tmp_sb);
+}
+
void pp_user_info(struct pretty_print_context *pp,
const char *what, struct strbuf *sb,
const char *line, const char *encoding)
{
+ struct strbuf id;
struct ident_split ident;
char *line_end;
const char *mailbuf, *namebuf;
size_t namelen, maillen;
int max_length = 78; /* per rfc2822 */
+ int field = -1;
+ struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
if (pp->fmt == CMIT_FMT_ONELINE)
return;
@@ -496,9 +561,22 @@ void pp_user_info(struct pretty_print_context *pp,
strbuf_addch(sb, '\n');
strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf);
} else {
- strbuf_addf(sb, "%s: %.*s%.*s <%.*s>\n", what,
- (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, " ",
- (int)namelen, namebuf, (int)maillen, mailbuf);
+ strbuf_init(&id, namelen + maillen + 4);
+
+ if (!strcmp(what, "Author"))
+ field = GREP_HEADER_AUTHOR;
+ else if (!strcmp(what, "Commit"))
+ field = GREP_HEADER_COMMITTER;
+
+ strbuf_addf(sb, "%s: %.*s", what,
+ (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, " ");
+ strbuf_addf(&id, "%.*s <%.*s>", (int)namelen, namebuf,
+ (int)maillen, mailbuf);
+
+ append_matched_line(opt, id.buf, id.len,
+ GREP_PATTERN_HEAD, field, sb);
+ strbuf_addch(sb, '\n');
+ strbuf_release(&id);
}
switch (pp->fmt) {
@@ -1855,6 +1933,7 @@ static void pp_header(struct pretty_print_context *pp,
}
}
+
void pp_title_line(struct pretty_print_context *pp,
const char **msg_p,
struct strbuf *sb,
@@ -1935,8 +2014,8 @@ static int pp_utf8_width(const char *start, const char *end)
return width;
}
-static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
- const char *line, int linelen)
+static void strbuf_add_tabexpand(struct grep_opt *opt, struct strbuf *sb,
+ int tabwidth, const char *line, int linelen)
{
const char *tab;
@@ -1953,7 +2032,8 @@ static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
break;
/* Output the data .. */
- strbuf_add(sb, line, tab - line);
+ append_matched_line(opt, line, tab - line, GREP_PATTERN_BODY,
+ -1, sb);
/* .. and the de-tabified tab */
strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
@@ -1968,7 +2048,8 @@ static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
* worrying about width - there's nothing more to
* align.
*/
- strbuf_add(sb, line, linelen);
+ append_matched_line(opt, line, linelen,
+ GREP_PATTERN_BODY, -1, sb);
}
/*
@@ -1980,11 +2061,14 @@ static void pp_handle_indent(struct pretty_print_context *pp,
struct strbuf *sb, int indent,
const char *line, int linelen)
{
+ struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
+
strbuf_addchars(sb, ' ', indent);
if (pp->expand_tabs_in_log)
- strbuf_add_tabexpand(sb, pp->expand_tabs_in_log, line, linelen);
+ strbuf_add_tabexpand(opt, sb, pp->expand_tabs_in_log, line, linelen);
else
- strbuf_add(sb, line, linelen);
+ append_matched_line(opt, line, linelen, GREP_PATTERN_BODY, -1,
+ sb);
}
static int is_mboxrd_from(const char *line, int len)
@@ -2002,7 +2086,9 @@ void pp_remainder(struct pretty_print_context *pp,
struct strbuf *sb,
int indent)
{
+ struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
int first = 1;
+
for (;;) {
const char *line = *msg_p;
int linelen = get_one_line(line);
@@ -2023,14 +2109,15 @@ void pp_remainder(struct pretty_print_context *pp,
if (indent)
pp_handle_indent(pp, sb, indent, line, linelen);
else if (pp->expand_tabs_in_log)
- strbuf_add_tabexpand(sb, pp->expand_tabs_in_log,
+ strbuf_add_tabexpand(opt, sb, pp->expand_tabs_in_log,
line, linelen);
else {
if (pp->fmt == CMIT_FMT_MBOXRD &&
is_mboxrd_from(line, linelen))
strbuf_addch(sb, '>');
- strbuf_add(sb, line, linelen);
+ append_matched_line(opt, line, linelen,
+ GREP_PATTERN_BODY, -1, sb);
}
strbuf_addch(sb, '\n');
}
--
2.33.0
next reply other threads:[~2021-09-01 12:17 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-01 12:16 Hamza Mahfooz [this message]
2021-09-01 17:17 ` [PATCH] pretty: colorize pattern matches in commit messages Felipe Contreras
2021-09-01 23:26 ` Junio C Hamano
-- strict thread matches above, loose matches on Subject: below --
2021-09-05 12:05 Hamza Mahfooz
2021-09-05 18:01 ` 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
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=20210901121616.2109658-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.
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).