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
Jeff King <peff@peff.net>
Date
May 10, 2018, 06:40 UTC
Message-ID
<20180510064014.GA31779@sigill.intra.peff.net>
In-Reply-To
<20180510020010.GA5375@syl.local>
On Wed, May 09, 2018 at 07:00:10PM -0700, Taylor Blau wrote:
Show 25 quoted lines
> >  - they test with context (-C3) for us. It looks like GNU grep omits
> >    context lines with "-o", but we show a bunch of blank lines. This is
> >    I guess a bug (though it seems kind of an odd combination to specify
> >    in the first place)
> 
> I'm torn on what to do here. Currently, with -C3, I get a bunch of lines
> like:
> 
>   <file>-
> 
> Which I think is technically _right_, but I agree that it is certainly
> an odd combination of things to give to 'git grep'. I think that we
> could either:
> 
>   1. Continue outputting blank lines,
>   2. Ignore '-C<n>' with '-o', or
>   3. die() when given this combination.
> 
> I think that (1) is the most "correct" (for some definition), so I'm
> inclined to adopt that approach. I suppose that (2) is closer to what
> GNU grep offers, and that is the point of this series, so perhaps it
> makes sense to pick that instead.
> 
> I'll go with (2) for now, but I would appreciate others' thoughts before
> sending a subsequent re-roll of this series.

We talked about this off-list, so I want to summarize here for the archive.

It turns out that the GNU grep behavior isn't universal. Here's what I get:

  $ grep -o -C3 the README.md
  the
  the
  the
  the
  the
  the
  the
  the
  --
  the
  the
  the
  the
  the
  the
  the
  the
  the
  the
  the
  the
  the
  the

So that's not _quite_ ignoring -C. It's still showing the "--", which indicates that the first chunk are all matches within 6 lines of each other (so their context is melded into a single hunk), but it omits the lines entirely. Which at least seems like it could be _plausibly_ useful.

BSD grep seems to actually show the context lines. Which is more information, but if you want to actually see the context, why are you using "-o" in the first place?

So my general feeling is that disallowing the combination is probably fine, because it's a vaguely crazy thing to ask for. But that GNU grep's behavior is the most likely to actually be useful. The BSD behavior seems more like "this is what we happen to produce" to me.

And it should be pretty easy to reproduce the GNU grep behavior by just not outputting anything in show_line().

-Peff
Previous: Taylor BlauNext: Eric Sunshine
Message 9 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.