From 2e50bf575fbaaf3b0410ae9af0c1ab6af691aa9c Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 27 Oct 2014 12:54:05 -0700 Subject: Revert "grep: fix match highlighting for combined patterns with context lines" This reverts commit d5a7410d22075ecf9f2597d2516bb9914cd55049. It turns out that showing partial matches on the lines that are not matching ones is a feature, not a bug, modelling after ms/mc specifiers in GNU grep's environment variables GREP_COLORS. --- grep.c | 42 +++++++++++++-------------- t/t7810-grep.sh | 90 --------------------------------------------------------- 2 files changed, 20 insertions(+), 112 deletions(-) diff --git a/grep.c b/grep.c index b363a9407b..c668034739 100644 --- a/grep.c +++ b/grep.c @@ -1112,33 +1112,31 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol, output_sep(opt, sign); } if (opt->color) { - if (sign == ':') { - /* paint the hits on matched lines */ - regmatch_t match; - enum grep_context ctx = GREP_CONTEXT_BODY; - int ch = *eol; - int eflags = 0; + regmatch_t match; + enum grep_context ctx = GREP_CONTEXT_BODY; + int ch = *eol; + int eflags = 0; + if (sign == ':') line_color = opt->color_selected; - *eol = '\0'; - while (next_match(opt, bol, eol, ctx, &match, eflags)) { - if (match.rm_so == match.rm_eo) - break; - - output_color(opt, bol, match.rm_so, line_color); - output_color(opt, bol + match.rm_so, - match.rm_eo - match.rm_so, - opt->color_match); - bol += match.rm_eo; - rest -= match.rm_eo; - eflags = REG_NOTBOL; - } - *eol = ch; - } else if (sign == '-') { + else if (sign == '-') line_color = opt->color_context; - } else if (sign == '=') { + else if (sign == '=') line_color = opt->color_function; + *eol = '\0'; + while (next_match(opt, bol, eol, ctx, &match, eflags)) { + if (match.rm_so == match.rm_eo) + break; + + output_color(opt, bol, match.rm_so, line_color); + output_color(opt, bol + match.rm_so, + match.rm_eo - match.rm_so, + opt->color_match); + bol += match.rm_eo; + rest -= match.rm_eo; + eflags = REG_NOTBOL; } + *eol = ch; } output_color(opt, bol, rest, line_color); opt->output(opt, "\n", 1); diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh index 87d1242548..f698001c99 100755 --- a/t/t7810-grep.sh +++ b/t/t7810-grep.sh @@ -1195,94 +1195,4 @@ test_expect_success LIBPCRE 'grep -P "^ "' ' test_cmp expected actual ' -cat >expected <with leading space1 -space: line with leading space2 -space: line with leading space3 -space:line without leading space2 -EOF - -test_expect_success 'grep --color -e A -e B with context' ' - test_config color.grep.context normal && - test_config color.grep.filename normal && - test_config color.grep.function normal && - test_config color.grep.linenumber normal && - test_config color.grep.match red && - test_config color.grep.selected normal && - test_config color.grep.separator normal && - - git grep --color=always -C2 -e "with " -e space2 space | - test_decode_color >actual && - test_cmp expected actual -' - -cat >expected <with leading space2 -space- line with leading space3 -space-line without leading space2 -EOF - -test_expect_success 'grep --color -e A --and -e B with context' ' - test_config color.grep.context normal && - test_config color.grep.filename normal && - test_config color.grep.function normal && - test_config color.grep.linenumber normal && - test_config color.grep.match red && - test_config color.grep.selected normal && - test_config color.grep.separator normal && - - git grep --color=always -C2 -e "with " --and -e space2 space | - test_decode_color >actual && - test_cmp expected actual -' - -cat >expected <with leading space1 -space- line with leading space2 -space: line with leading space3 -space-line without leading space2 -EOF - -test_expect_success 'grep --color -e A --and --not -e B with context' ' - test_config color.grep.context normal && - test_config color.grep.filename normal && - test_config color.grep.function normal && - test_config color.grep.linenumber normal && - test_config color.grep.match red && - test_config color.grep.selected normal && - test_config color.grep.separator normal && - - git grep --color=always -C2 -e "with " --and --not -e space2 space | - test_decode_color >actual && - test_cmp expected actual -' - -cat >expected < -hello.c=int main(int argc, const char **argv) -hello.c-{ -hello.c: printf("Hello world.\n"); -hello.c- return 0; -hello.c- /* char ?? */ -hello.c-} -EOF - -test_expect_success 'grep --color -e A --and -e B -p with context' ' - test_config color.grep.context normal && - test_config color.grep.filename normal && - test_config color.grep.function normal && - test_config color.grep.linenumber normal && - test_config color.grep.match red && - test_config color.grep.selected normal && - test_config color.grep.separator normal && - - git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c | - test_decode_color >actual && - test_cmp expected actual -' - test_done -- cgit v1.2.1