зеркало из https://github.com/microsoft/git.git
pretty: colorize pattern matches in commit messages
The "git log" command limits its output to the commits that contain strings matched by a pattern when the "--grep=<pattern>" option is used, but unlike output from "git grep -e <pattern>", the matches are not highlighted, making them harder to spot. Teach the pretty-printer code to highlight matches from the "--grep=<pattern>", "--author=<pattern>" and "--committer=<pattern>" options (to view the last one, you may have to ask for --pretty=fuller). Also, it must be noted that we are effectively greping the content twice (because it would be a hassle to rework the existing matching code to do a /g match and then pass it all down to the coloring code), however it only slows down "git log --author=^H" on this repository by around 1-2% (compared to v2.33.0), so it should be a small enough slow down to justify the addition of the feature. Signed-off-by: Hamza Mahfooz <someguy@effective-light.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Родитель
3f566c4e69
Коммит
6a5c337922
|
@ -104,9 +104,12 @@ color.grep.<slot>::
|
|||
`matchContext`;;
|
||||
matching text in context lines
|
||||
`matchSelected`;;
|
||||
matching text in selected lines
|
||||
matching text in selected lines. Also, used to customize the following
|
||||
linkgit:git-log[1] subcommands: `--grep`, `--author` and `--committer`.
|
||||
`selected`;;
|
||||
non-matching text in selected lines
|
||||
non-matching text in selected lines. Also, used to customize the
|
||||
following linkgit:git-log[1] subcommands: `--grep`, `--author` and
|
||||
`--committer`.
|
||||
`separator`;;
|
||||
separators between fields on a line (`:`, `-`, and `=`)
|
||||
and between hunks (`--`)
|
||||
|
|
101
pretty.c
101
pretty.c
|
@ -431,6 +431,52 @@ const char *show_ident_date(const struct ident_split *ident,
|
|||
return show_date(date, tz, mode);
|
||||
}
|
||||
|
||||
static inline void strbuf_add_with_color(struct strbuf *sb, const char *color,
|
||||
const char *buf, size_t buflen)
|
||||
{
|
||||
strbuf_addstr(sb, color);
|
||||
strbuf_add(sb, buf, buflen);
|
||||
if (*color)
|
||||
strbuf_addstr(sb, GIT_COLOR_RESET);
|
||||
}
|
||||
|
||||
static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
|
||||
const char *line, size_t linelen,
|
||||
int color, enum grep_context ctx,
|
||||
enum grep_header_field field)
|
||||
{
|
||||
const char *buf, *eol, *line_color, *match_color;
|
||||
regmatch_t match;
|
||||
int eflags = 0;
|
||||
|
||||
buf = line;
|
||||
eol = buf + linelen;
|
||||
|
||||
if (!opt || !want_color(color) || opt->invert)
|
||||
goto end;
|
||||
|
||||
line_color = opt->colors[GREP_COLOR_SELECTED];
|
||||
match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
|
||||
|
||||
while (grep_next_match(opt, buf, eol, ctx, &match, field, eflags)) {
|
||||
if (match.rm_so == match.rm_eo)
|
||||
break;
|
||||
|
||||
strbuf_add_with_color(sb, line_color, buf, match.rm_so);
|
||||
strbuf_add_with_color(sb, match_color, buf + match.rm_so,
|
||||
match.rm_eo - match.rm_so);
|
||||
buf += match.rm_eo;
|
||||
eflags = REG_NOTBOL;
|
||||
}
|
||||
|
||||
if (eflags)
|
||||
strbuf_add_with_color(sb, line_color, buf, eol - buf);
|
||||
else {
|
||||
end:
|
||||
strbuf_add(sb, buf, eol - buf);
|
||||
}
|
||||
}
|
||||
|
||||
void pp_user_info(struct pretty_print_context *pp,
|
||||
const char *what, struct strbuf *sb,
|
||||
const char *line, const char *encoding)
|
||||
|
@ -496,9 +542,26 @@ 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);
|
||||
struct strbuf id = STRBUF_INIT;
|
||||
enum grep_header_field field = GREP_HEADER_FIELD_MAX;
|
||||
struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
|
||||
|
||||
if (!strcmp(what, "Author"))
|
||||
field = GREP_HEADER_AUTHOR;
|
||||
else if (!strcmp(what, "Commit"))
|
||||
field = GREP_HEADER_COMMITTER;
|
||||
|
||||
strbuf_addf(sb, "%s: ", what);
|
||||
if (pp->fmt == CMIT_FMT_FULLER)
|
||||
strbuf_addchars(sb, ' ', 4);
|
||||
|
||||
strbuf_addf(&id, "%.*s <%.*s>", (int)namelen, namebuf,
|
||||
(int)maillen, mailbuf);
|
||||
|
||||
append_line_with_color(sb, opt, id.buf, id.len, pp->color,
|
||||
GREP_CONTEXT_HEAD, field);
|
||||
strbuf_addch(sb, '\n');
|
||||
strbuf_release(&id);
|
||||
}
|
||||
|
||||
switch (pp->fmt) {
|
||||
|
@ -1939,8 +2002,9 @@ 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 strbuf *sb, struct grep_opt *opt,
|
||||
int color, int tabwidth, const char *line,
|
||||
int linelen)
|
||||
{
|
||||
const char *tab;
|
||||
|
||||
|
@ -1957,7 +2021,9 @@ static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
|
|||
break;
|
||||
|
||||
/* Output the data .. */
|
||||
strbuf_add(sb, line, tab - line);
|
||||
append_line_with_color(sb, opt, line, tab - line, color,
|
||||
GREP_CONTEXT_BODY,
|
||||
GREP_HEADER_FIELD_MAX);
|
||||
|
||||
/* .. and the de-tabified tab */
|
||||
strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
|
||||
|
@ -1972,7 +2038,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_line_with_color(sb, opt, line, linelen, color, GREP_CONTEXT_BODY,
|
||||
GREP_HEADER_FIELD_MAX);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -1984,11 +2051,16 @@ 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(sb, opt, pp->color, pp->expand_tabs_in_log,
|
||||
line, linelen);
|
||||
else
|
||||
strbuf_add(sb, line, linelen);
|
||||
append_line_with_color(sb, opt, line, linelen, pp->color,
|
||||
GREP_CONTEXT_BODY,
|
||||
GREP_HEADER_FIELD_MAX);
|
||||
}
|
||||
|
||||
static int is_mboxrd_from(const char *line, int len)
|
||||
|
@ -2006,7 +2078,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);
|
||||
|
@ -2027,14 +2101,17 @@ 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,
|
||||
line, linelen);
|
||||
strbuf_add_tabexpand(sb, opt, pp->color,
|
||||
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_line_with_color(sb, opt, line, linelen,
|
||||
pp->color, GREP_CONTEXT_BODY,
|
||||
GREP_HEADER_FIELD_MAX);
|
||||
}
|
||||
strbuf_addch(sb, '\n');
|
||||
}
|
||||
|
|
|
@ -449,6 +449,57 @@ test_expect_success !FAIL_PREREQS 'log with various grep.patternType configurati
|
|||
)
|
||||
'
|
||||
|
||||
test_expect_success 'log --author' '
|
||||
cat >expect <<-\EOF &&
|
||||
Author: <BOLD;RED>A U<RESET> Thor <author@example.com>
|
||||
EOF
|
||||
git log -1 --color=always --author="A U" >log &&
|
||||
grep Author log >actual.raw &&
|
||||
test_decode_color <actual.raw >actual &&
|
||||
test_cmp expect actual
|
||||
'
|
||||
|
||||
test_expect_success 'log --committer' '
|
||||
cat >expect <<-\EOF &&
|
||||
Commit: C O Mitter <committer@<BOLD;RED>example<RESET>.com>
|
||||
EOF
|
||||
git log -1 --color=always --pretty=fuller --committer="example" >log &&
|
||||
grep "Commit:" log >actual.raw &&
|
||||
test_decode_color <actual.raw >actual &&
|
||||
test_cmp expect actual
|
||||
'
|
||||
|
||||
test_expect_success 'log -i --grep with color' '
|
||||
cat >expect <<-\EOF &&
|
||||
<BOLD;RED>Sec<RESET>ond
|
||||
<BOLD;RED>sec<RESET>ond
|
||||
EOF
|
||||
git log --color=always -i --grep=^sec >log &&
|
||||
grep -i sec log >actual.raw &&
|
||||
test_decode_color <actual.raw >actual &&
|
||||
test_cmp expect actual
|
||||
'
|
||||
|
||||
test_expect_success '-c color.grep.selected log --grep' '
|
||||
cat >expect <<-\EOF &&
|
||||
<GREEN>th<RESET><BOLD;RED>ir<RESET><GREEN>d<RESET>
|
||||
EOF
|
||||
git -c color.grep.selected="green" log --color=always --grep=ir >log &&
|
||||
grep ir log >actual.raw &&
|
||||
test_decode_color <actual.raw >actual &&
|
||||
test_cmp expect actual
|
||||
'
|
||||
|
||||
test_expect_success '-c color.grep.matchSelected log --grep' '
|
||||
cat >expect <<-\EOF &&
|
||||
<BLUE>i<RESET>n<BLUE>i<RESET>t<BLUE>i<RESET>al
|
||||
EOF
|
||||
git -c color.grep.matchSelected="blue" log --color=always --grep=i >log &&
|
||||
grep al log >actual.raw &&
|
||||
test_decode_color <actual.raw >actual &&
|
||||
test_cmp expect actual
|
||||
'
|
||||
|
||||
cat > expect <<EOF
|
||||
* Second
|
||||
* sixth
|
||||
|
|
Загрузка…
Ссылка в новой задаче