git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] grep: fix match highlighting for combined patterns with context lines

From
René Scharfe <l.s.r@web.de>
Date
Oct 26, 2014, 18:15 UTC
Message-ID
<544D3A3C.4080906@web.de>
In-Reply-To
<1413870963-66431-1-git-send-email-zoltan.klinger@gmail.com>
Am 21.10.2014 um 07:56 schrieb Zoltan Klinger:
Show 23 quoted lines
> When git grep is run with combined patterns such as '-e p1 --and -e p2'
> and surrounding context lines are requested, the output contains
> incorrectly highlighted matches.
>
> Consider the following output (highlighted matches are surrounded by '*'
> characters):
>      $ cat testfile
>      foo a
>      foo b
>      foo bar
>      baz bar foo
>      bar x
>      bar y
>      $ git grep -n -C2 -e foo --and -e bar testfile
>      testfile-1-*foo* a
>      testfile-2-*foo* b
>      testfile:3:*foo* *bar*
>      testfile:4:baz *bar* *foo*
>      testfile-5-*bar* x
>      testfile-6-*bar* y
>
> Lines 1, 2, 5 and 6 do not match the combined patterns, they only
> contain incorrectly highlighted 'false positives'.

The old code highlights all search terms, anywhere. I wouldn't call the ones in the context lines false positives. The user might be interested in those occurrences as well (I know I am ;).

GNU grep allows coloring to be configured in much greater detail with its GREP_COLORS variable. I didn't think that level of tuning is desirable until now. What your patch does is equivalent to change the default of "ms=01;31:mc=01;31" (color matching string in selected lines and context lines) to "ms=01;31:mc=" (color matching string in selected lines).

The difference is only visible with -v or git grep's --not and --and.

So, if you really don't want matching string in context lines to be colored, perhaps it's time to add a color.grep.contextmatch for matching text in context lines?

René
Previous: Junio C HamanoNext: René Scharfe
Message 6 of 12 in “grep: fix match highlighting for combined patterns with context lines”
  1. grep: fix match highlighting for combined patterns with context linesZoltan Klinger, Oct 21, 2014
  2. Junio C HamanoOct 21, 2014
  3. Junio C HamanoOct 21, 2014
  4. Zoltan KlingerOct 22, 2014
  5. Junio C HamanoOct 22, 2014
  6. René ScharfeOct 26, 2014
  7. [RFC] grep: add color.grep.matchcontext and color.grep.matchselectedRené Scharfe, Oct 27, 2014
  8. Junio C HamanoOct 27, 2014
  9. Junio C HamanoOct 27, 2014
  10. Zoltan KlingerOct 27, 2014
  11. Junio C HamanoOct 28, 2014
  12. René ScharfeOct 28, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.