git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Taylor Blau <me@ttaylorr.com>
Cc: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	git@vger.kernel.org, gitster@pobox.com
Subject: Re: [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'
Date: Thu, 10 May 2018 02:40:14 -0400	[thread overview]
Message-ID: <20180510064014.GA31779@sigill.intra.peff.net> (raw)
In-Reply-To: <20180510020010.GA5375@syl.local>

On Wed, May 09, 2018 at 07:00:10PM -0700, Taylor Blau wrote:

> >  - 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

  reply	other threads:[~2018-05-10  6:40 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-05  4:03 [PATCH 0/2] builtin/grep.c: teach '-o', '--only-matching' Taylor Blau
2018-05-05  4:03 ` [PATCH 1/2] grep.c: extract show_line_header() Taylor Blau
2018-05-05  7:30   ` Eric Sunshine
2018-05-08  0:24     ` Taylor Blau
2018-05-05  4:03 ` [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep' Taylor Blau
2018-05-05  6:49   ` Ævar Arnfjörð Bjarmason
2018-05-08 17:25     ` Jeff King
2018-05-10  2:00       ` Taylor Blau
2018-05-10  6:40         ` Jeff King [this message]
2018-05-05  7:36   ` Eric Sunshine
2018-05-08  0:27     ` Taylor Blau
2018-05-12  3:21 ` [PATCH v2 0/2] builtin/grep.c: teach '-o', '--only-matching' Taylor Blau
2018-05-12  3:21   ` [PATCH v2 1/2] grep.c: extract show_line_header() Taylor Blau
2018-05-12  3:21   ` [PATCH v2 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep' Taylor Blau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180510064014.GA31779@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=me@ttaylorr.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).