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

Re: [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
May 5, 2018, 06:49 UTC
Message-ID
<87fu36y4u0.fsf@evledraar.gmail.com>
In-Reply-To
<c8b527c5de3b0e5422d2c1afb91d454d1e46fff4.1525492696.git.me@ttaylorr.com>
On Sat, May 05 2018, Taylor Blau wrote:
> +--o::
> +--only-matching::
> +	Show only the matching part of the lines.
> +
Makes sense to steal GNU grep's description here:
    Print only the matched (non-empty) parts of a matching line, with
    each such part on a separate output line.
> +			if (!opt->only_matching)
> +				output_color(opt, bol, match.rm_so, line_color);

This should also have braces, see "When there are multiple arms to a conditional" in Documentation/CodingGuidelines.

Show 12 quoted lines
>  '
>
> +cat >expected <<EOF
> +file:1:5:mmap
> +file:2:5:mmap
> +file:3:5:mmap
> +file:3:14:mmap
> +file:4:5:mmap
> +file:4:14:mmap
> +file:5:5:mmap
> +file:5:14:mmap
> +EOF

This should be set up as part of the test itself, see e.g. my c8b2cec09e ("branch: add test for -m renaming multiple config sections", 2017-06-18) for how to do that.

Show 25 quoted lines
> +test_expect_success 'grep --only-matching' '
> +	git grep --only-matching --line-number --column mmap file >actual &&
> +	test_cmp expected actual
> +'
> +
> +cat >expected <<EOF
> +file
> +1:5:mmap
> +2:5:mmap
> +3:5:mmap
> +3:14:mmap
> +4:5:mmap
> +4:14:mmap
> +5:5:mmap
> +5:14:mmap
> +EOF
> +
> +test_expect_success 'grep --only-matching --heading' '
> +	git grep --only-matching --heading --line-number --column mmap file >actual &&
> +	test_cmp expected actual
> +'
> +
>  cat >expected <<EOF
>  <BOLD;GREEN>hello.c<RESET>
>  4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)

We should test this a lot more, I think a good way to do that would be to extend this series by first importing GNU grep's -o tests, see http://git.savannah.gnu.org/cgit/grep.git/tree/tests/foad1 they are license-compatible. Then change the grep_test() function to call git grep instead.

It should also be tested with the various grep.patternType options to make sure it works with basic, extended, perl, fixed etc.

Previous: Taylor BlauNext: Jeff King
Message 6 of 14 in “builtin/grep.c: teach '-o', '--only-matching'”
  1. 0/2 builtin/grep.c: teach '-o', '--only-matching'Taylor Blau, May 5, 2018
  2. 1/2 grep.c: extract show_line_header()Taylor Blau, May 5, 2018
  3. Eric SunshineMay 5, 2018
  4. Taylor BlauMay 8, 2018
  5. 2/2 builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'Taylor Blau, May 5, 2018
  6. Ævar Arnfjörð BjarmasonMay 5, 2018
  7. Jeff KingMay 8, 2018
  8. Taylor BlauMay 10, 2018
  9. Jeff KingMay 10, 2018
  10. Eric SunshineMay 5, 2018
  11. Taylor BlauMay 8, 2018
  12. 0/2 builtin/grep.c: teach '-o', '--only-matching'Taylor Blau, May 12, 2018
  13. 1/2 grep.c: extract show_line_header()Taylor Blau, May 12, 2018
  14. 2/2 builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'Taylor Blau, May 12, 2018

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.