git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 0/2] builtin/grep.c: teach '-o', '--only-matching'
@ 2018-05-05  4:03 Taylor Blau
  2018-05-05  4:03 ` [PATCH 1/2] grep.c: extract show_line_header() Taylor Blau
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Taylor Blau @ 2018-05-05  4:03 UTC (permalink / raw)
  To: git; +Cc: avarab, peff, gitster

Hi,

Attached is a series to teach 'git-grep(1)' how to respond to
'--only-matching' (a-la GNU grep(1)'s --only-matching, including an '-o'
synonym) to only print the matching component(s) of a line. It is based
on v4 of tb/grep-colno, which was sent in [1].

This was suggested to me by Ævar in [2].

This change was fairly straightforward, as Ævar suggests in [3], with
the only complication being that we must print a line header multiple
times when there are >1 matches per-line. This requirement pushes the
implementation towards the extraction of a show_line_header() function,
and some minor changes in show_line() to reflect.

Thank you in advance for your review.


Thanks,
Taylor

[1]: https://public-inbox.org/git/cover.1525488108.git.me@ttaylorr.com
[2]: https://public-inbox.org/git/874lk2e4he.fsf@evledraar.gmail.com
[3]: https://public-inbox.org/git/87in9ucsbb.fsf@evledraar.gmail.com

Taylor Blau (2):
  grep.c: extract show_line_header()
  builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'

 Documentation/git-grep.txt |  6 +++-
 builtin/grep.c             |  1 +
 grep.c                     | 67 +++++++++++++++++++++++++-------------
 grep.h                     |  1 +
 t/t7810-grep.sh            | 33 +++++++++++++++++++
 5 files changed, 85 insertions(+), 23 deletions(-)

--
2.17.0

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH 1/2] grep.c: extract show_line_header()
  2018-05-05  4:03 [PATCH 0/2] builtin/grep.c: teach '-o', '--only-matching' Taylor Blau
@ 2018-05-05  4:03 ` Taylor Blau
  2018-05-05  7:30   ` Eric Sunshine
  2018-05-05  4:03 ` [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep' Taylor Blau
  2018-05-12  3:21 ` [PATCH v2 0/2] builtin/grep.c: teach '-o', '--only-matching' Taylor Blau
  2 siblings, 1 reply; 15+ messages in thread
From: Taylor Blau @ 2018-05-05  4:03 UTC (permalink / raw)
  To: git; +Cc: avarab, peff, gitster

Teach 'git-grep(1)' how to print a line header multiple times from
show_line() in preparation for it learning '--only-matching'.

show_line_header() comprises of the code in show_line() executed in
order to produce a line header. It is a one-for-one extraction of the
existing implementation.

For now, show_line_header() provides no benefit over the change before
this patch. The following patch will call conditionally call
show_line_header() multiple times per invocation to show_line(), which
is the desired benefit of this change.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 grep.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/grep.c b/grep.c
index 37bb39a4a8..89dd719e4d 100644
--- a/grep.c
+++ b/grep.c
@@ -1369,26 +1369,9 @@ static int next_match(struct grep_opt *opt, char *bol, char *eol,
 	return hit;
 }
 
-static void show_line(struct grep_opt *opt, char *bol, char *eol,
-		      const char *name, unsigned lno, unsigned cno, char sign)
+static void show_line_header(struct grep_opt *opt, const char *name,
+                             unsigned lno, unsigned cno, char sign)
 {
-	int rest = eol - bol;
-	const char *match_color, *line_color = NULL;
-
-	if (opt->file_break && opt->last_shown == 0) {
-		if (opt->show_hunk_mark)
-			opt->output(opt, "\n", 1);
-	} else if (opt->pre_context || opt->post_context || opt->funcbody) {
-		if (opt->last_shown == 0) {
-			if (opt->show_hunk_mark) {
-				output_color(opt, "--", 2, opt->color_sep);
-				opt->output(opt, "\n", 1);
-			}
-		} else if (lno > opt->last_shown + 1) {
-			output_color(opt, "--", 2, opt->color_sep);
-			opt->output(opt, "\n", 1);
-		}
-	}
 	if (opt->heading && opt->last_shown == 0) {
 		output_color(opt, name, strlen(name), opt->color_filename);
 		opt->output(opt, "\n", 1);
@@ -1416,6 +1399,29 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 		output_color(opt, buf, strlen(buf), opt->color_columnno);
 		output_sep(opt, sign);
 	}
+}
+
+static void show_line(struct grep_opt *opt, char *bol, char *eol,
+		      const char *name, unsigned lno, unsigned cno, char sign)
+{
+	int rest = eol - bol;
+	const char *match_color, *line_color = NULL;
+
+	if (opt->file_break && opt->last_shown == 0) {
+		if (opt->show_hunk_mark)
+			opt->output(opt, "\n", 1);
+	} else if (opt->pre_context || opt->post_context || opt->funcbody) {
+		if (opt->last_shown == 0) {
+			if (opt->show_hunk_mark) {
+				output_color(opt, "--", 2, opt->color_sep);
+				opt->output(opt, "\n", 1);
+			}
+		} else if (lno > opt->last_shown + 1) {
+			output_color(opt, "--", 2, opt->color_sep);
+			opt->output(opt, "\n", 1);
+		}
+	}
+	show_line_header(opt, name, lno, cno, sign);
 	if (opt->color) {
 		regmatch_t match;
 		enum grep_context ctx = GREP_CONTEXT_BODY;
-- 
2.17.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'
  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  4:03 ` Taylor Blau
  2018-05-05  6:49   ` Ævar Arnfjörð Bjarmason
  2018-05-05  7:36   ` Eric Sunshine
  2018-05-12  3:21 ` [PATCH v2 0/2] builtin/grep.c: teach '-o', '--only-matching' Taylor Blau
  2 siblings, 2 replies; 15+ messages in thread
From: Taylor Blau @ 2018-05-05  4:03 UTC (permalink / raw)
  To: git; +Cc: avarab, peff, gitster

Teach GNU grep(1)'s '-o' ('--only-matching') to 'git-grep'. This option
prints only the matching components of each line. It writes multiple
lines if more than one match exists on a given line.

For example:

  $ git grep -on --column --heading git -- README.md | head -3
  README.md
  15:56:git
  18:20:git

By using show_line_header(), 'git grep --only-matching' correctly
respects the '--header' option:

  $ git grep -on --column --heading git -- README.md | head -4
  README.md
  15:56:git
  18:20:git
  19:16:git

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 Documentation/git-grep.txt |  6 +++++-
 builtin/grep.c             |  1 +
 grep.c                     | 23 ++++++++++++++++++++---
 grep.h                     |  1 +
 t/t7810-grep.sh            | 33 +++++++++++++++++++++++++++++++++
 5 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index d451cd8883..9754923041 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -20,7 +20,7 @@ SYNOPSIS
 	   [-c | --count] [--all-match] [-q | --quiet]
 	   [--max-depth <depth>]
 	   [--color[=<when>] | --no-color]
-	   [--break] [--heading] [-p | --show-function]
+	   [--break] [--heading] [-o | --only-matching] [-p | --show-function]
 	   [-A <post-context>] [-B <pre-context>] [-C <context>]
 	   [-W | --function-context]
 	   [--threads <num>]
@@ -221,6 +221,10 @@ providing this option will cause it to die.
 	Show the filename above the matches in that file instead of
 	at the start of each shown line.
 
+--o::
+--only-matching::
+	Show only the matching part of the lines.
+
 -p::
 --show-function::
 	Show the preceding line that contains the function name of
diff --git a/builtin/grep.c b/builtin/grep.c
index 5c83f17759..5028bf96cf 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -851,6 +851,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			N_("print empty line between matches from different files")),
 		OPT_BOOL(0, "heading", &opt.heading,
 			N_("show filename only once above matches from same file")),
+		OPT_BOOL('o', "only-matching", &opt.only_matching, N_("show only matches")),
 		OPT_GROUP(""),
 		OPT_CALLBACK('C', "context", &opt, N_("n"),
 			N_("show <n> context lines before and after matches"),
diff --git a/grep.c b/grep.c
index 89dd719e4d..da3f8e6266 100644
--- a/grep.c
+++ b/grep.c
@@ -1422,11 +1422,13 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 		}
 	}
 	show_line_header(opt, name, lno, cno, sign);
-	if (opt->color) {
+	if (opt->color || opt->only_matching) {
 		regmatch_t match;
 		enum grep_context ctx = GREP_CONTEXT_BODY;
 		int ch = *eol;
 		int eflags = 0;
+		int first = 1;
+		int offset = 1;
 
 		if (sign == ':')
 			match_color = opt->color_match_selected;
@@ -1443,16 +1445,31 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 			if (match.rm_so == match.rm_eo)
 				break;
 
-			output_color(opt, bol, match.rm_so, line_color);
+			if (!opt->only_matching)
+				output_color(opt, bol, match.rm_so, line_color);
+			else if (!first) {
+				/*
+				 * We are given --only-matching, and this is not
+				 * the first match on a line. Reprint the
+				 * newline and header before showing another
+				 * match.
+				 */
+				opt->output(opt, "\n", 1);
+				show_line_header(opt, name, lno,
+					offset+match.rm_so, sign);
+			}
 			output_color(opt, bol + match.rm_so,
 				     match.rm_eo - match.rm_so, match_color);
+			offset += match.rm_eo;
 			bol += match.rm_eo;
 			rest -= match.rm_eo;
 			eflags = REG_NOTBOL;
+			first = 0;
 		}
 		*eol = ch;
 	}
-	output_color(opt, bol, rest, line_color);
+	if (!opt->only_matching)
+		output_color(opt, bol, rest, line_color);
 	opt->output(opt, "\n", 1);
 }
 
diff --git a/grep.h b/grep.h
index 08a0b391c5..24c1460100 100644
--- a/grep.h
+++ b/grep.h
@@ -126,6 +126,7 @@ struct grep_opt {
 	const char *prefix;
 	int prefix_length;
 	regex_t regexp;
+	int only_matching;
 	int linenum;
 	int columnnum;
 	int invert;
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index a03c3416e7..ef7f4ce725 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -1420,6 +1420,39 @@ test_expect_success 'grep --heading' '
 	test_cmp expected actual
 '
 
+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
+
+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)
-- 
2.17.0

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'
  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-05  7:36   ` Eric Sunshine
  1 sibling, 1 reply; 15+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2018-05-05  6:49 UTC (permalink / raw)
  To: Taylor Blau; +Cc: git, peff, gitster


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.


>  '
>
> +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.

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 1/2] grep.c: extract show_line_header()
  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
  0 siblings, 1 reply; 15+ messages in thread
From: Eric Sunshine @ 2018-05-05  7:30 UTC (permalink / raw)
  To: Taylor Blau
  Cc: Git List, Ævar Arnfjörð Bjarmason, Jeff King,
	Junio C Hamano

On Sat, May 5, 2018 at 12:03 AM, Taylor Blau <me@ttaylorr.com> wrote:
> Teach 'git-grep(1)' how to print a line header multiple times from
> show_line() in preparation for it learning '--only-matching'.
>
> show_line_header() comprises of the code in show_line() executed in

s/of//

> order to produce a line header. It is a one-for-one extraction of the
> existing implementation.
>
> For now, show_line_header() provides no benefit over the change before
> this patch. The following patch will call conditionally call

s/call conditionally call/conditionally call/

> show_line_header() multiple times per invocation to show_line(), which
> is the desired benefit of this change.
>
> Signed-off-by: Taylor Blau <me@ttaylorr.com>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'
  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-05  7:36   ` Eric Sunshine
  2018-05-08  0:27     ` Taylor Blau
  1 sibling, 1 reply; 15+ messages in thread
From: Eric Sunshine @ 2018-05-05  7:36 UTC (permalink / raw)
  To: Taylor Blau
  Cc: Git List, Ævar Arnfjörð Bjarmason, Jeff King,
	Junio C Hamano

On Sat, May 5, 2018 at 12:03 AM, Taylor Blau <me@ttaylorr.com> wrote:
> Teach GNU grep(1)'s '-o' ('--only-matching') to 'git-grep'. This option
> prints only the matching components of each line. It writes multiple
> lines if more than one match exists on a given line.
>
> For example:
>
>   $ git grep -on --column --heading git -- README.md | head -3
>   README.md
>   15:56:git
>   18:20:git
>
> By using show_line_header(), 'git grep --only-matching' correctly
> respects the '--header' option:

What is the '--header' option? I don't see it used in any example.

>   $ git grep -on --column --heading git -- README.md | head -4
>   README.md
>   15:56:git
>   18:20:git
>   19:16:git

How does this example differ from the earlier example (other than
showing 4 lines of output rather than 3)?

> Signed-off-by: Taylor Blau <me@ttaylorr.com>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 1/2] grep.c: extract show_line_header()
  2018-05-05  7:30   ` Eric Sunshine
@ 2018-05-08  0:24     ` Taylor Blau
  0 siblings, 0 replies; 15+ messages in thread
From: Taylor Blau @ 2018-05-08  0:24 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Ævar Arnfjörð Bjarmason, Jeff King,
	Junio C Hamano

On Sat, May 05, 2018 at 03:30:51AM -0400, Eric Sunshine wrote:
> On Sat, May 5, 2018 at 12:03 AM, Taylor Blau <me@ttaylorr.com> wrote:
> > Teach 'git-grep(1)' how to print a line header multiple times from
> > show_line() in preparation for it learning '--only-matching'.
> >
> > show_line_header() comprises of the code in show_line() executed in
>
> s/of//

Ack -- thanks for pointing that out :-).

> > order to produce a line header. It is a one-for-one extraction of the
> > existing implementation.
> >
> > For now, show_line_header() provides no benefit over the change before
> > this patch. The following patch will call conditionally call
>
> s/call conditionally call/conditionally call/

Double ack. Thanks again :-).


Thanks,
Taylor

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'
  2018-05-05  7:36   ` Eric Sunshine
@ 2018-05-08  0:27     ` Taylor Blau
  0 siblings, 0 replies; 15+ messages in thread
From: Taylor Blau @ 2018-05-08  0:27 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Ævar Arnfjörð Bjarmason, Jeff King,
	Junio C Hamano

On Sat, May 05, 2018 at 03:36:12AM -0400, Eric Sunshine wrote:
> On Sat, May 5, 2018 at 12:03 AM, Taylor Blau <me@ttaylorr.com> wrote:
> > Teach GNU grep(1)'s '-o' ('--only-matching') to 'git-grep'. This option
> > prints only the matching components of each line. It writes multiple
> > lines if more than one match exists on a given line.
> >
> > For example:
> >
> >   $ git grep -on --column --heading git -- README.md | head -3
> >   README.md
> >   15:56:git
> >   18:20:git
> >
> > By using show_line_header(), 'git grep --only-matching' correctly
> > respects the '--header' option:
>
> What is the '--header' option? I don't see it used in any example.

I think '--header' is a typo for '--heading', which is used in the
following example.

> >   $ git grep -on --column --heading git -- README.md | head -4
> >   README.md
> >   15:56:git
> >   18:20:git
> >   19:16:git
>
> How does this example differ from the earlier example (other than
> showing 4 lines of output rather than 3)?

Ack. I clipped from my terminal what I meant to be the seocnd
example, and pasted it in for both examples. They are meant to be as
follows:

  1. 'git grep' without heading, showing the full line prefix, and
  2. 'git grep' with heading, showing the file heading with '--heading'.

The later has '| head -n4' on the end to include 3+1 lines (3 matches, 1
heading) whereas the former has '| head -n3' to include 3 lines (3
matches, no heading).

I have updated my patch locally to reflect this.


Thanks,
Taylor

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'
  2018-05-05  6:49   ` Ævar Arnfjörð Bjarmason
@ 2018-05-08 17:25     ` Jeff King
  2018-05-10  2:00       ` Taylor Blau
  0 siblings, 1 reply; 15+ messages in thread
From: Jeff King @ 2018-05-08 17:25 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: Taylor Blau, git, gitster

On Sat, May 05, 2018 at 08:49:43AM +0200, Ævar Arnfjörð Bjarmason wrote:

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

I'm trying to figure out what GNU grep's tests are actually checking
that we don't have. I see:

 - they check that "-i" returns the actual found string in its original
   case. This seems like a subset of finding a non-trivial regex. I.e.,
   "foo.*" should find "foobar". We probably should have a test like
   that.

 - they test multiple hits on the same line, which seems like an
   important and easy-to-screw-up case; we do that, too.

 - they test everything other thing with and without "-i" because those
   are apparently separate code paths in GNU grep, but I don't think
   that applies to us.

 - they test each case with "-b", but we don't have that (we do test
   with "--column", which is good)

 - they test with "-n", which we do here (we don't test without, but
   that seems like an unlikely bug, knowing how it is implemented)

 - they test with -H, but that is already the default for git-grep

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

So I think it probably makes sense to just pick through the list I just
wrote and write our own tests than to try to import GNU grep's specific
tests (and there's a ton of other unrelated tests in that file that may
or may not even run with git-grep).

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

Hmm, this code is all external to the actual matching. So unless one of
those is totally screwing up the regmatch_t return, I'm not sure that's
accomplishing much (and if one of them is, it's totally broken for
coloring, "-c", and maybe other features).

We've usually taken a pretty white-box approach to our testing, covering
things that seem likely to go wrong given the general problem space and
our implementation. But maybe I'm missing something in particular that
you think might be tricky.

-Peff

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'
  2018-05-08 17:25     ` Jeff King
@ 2018-05-10  2:00       ` Taylor Blau
  2018-05-10  6:40         ` Jeff King
  0 siblings, 1 reply; 15+ messages in thread
From: Taylor Blau @ 2018-05-10  2:00 UTC (permalink / raw)
  To: Jeff King
  Cc: Ævar Arnfjörð Bjarmason, Taylor Blau, git, gitster

On Tue, May 08, 2018 at 01:25:17PM -0400, Jeff King wrote:
> On Sat, May 05, 2018 at 08:49:43AM +0200, Ævar Arnfjörð Bjarmason wrote:
>
> > > +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.
>
> I'm trying to figure out what GNU grep's tests are actually checking
> that we don't have. I see:
>
>  - they check that "-i" returns the actual found string in its original
>    case. This seems like a subset of finding a non-trivial regex. I.e.,
>    "foo.*" should find "foobar". We probably should have a test like
>    that.
>
>  - they test multiple hits on the same line, which seems like an
>    important and easy-to-screw-up case; we do that, too.

Agree.

>  - they test everything other thing with and without "-i" because those
>    are apparently separate code paths in GNU grep, but I don't think
>    that applies to us.
>
>  - they test each case with "-b", but we don't have that (we do test
>    with "--column", which is good)

Right, I think that we can ignore these groups.

>  - they test with "-n", which we do here (we don't test without, but
>    that seems like an unlikely bug, knowing how it is implemented)

Fair, let's leave that as is.

>  - they test with -H, but that is already the default for git-grep

Good, we can ignore this one.

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

> So I think it probably makes sense to just pick through the list I just
> wrote and write our own tests than to try to import GNU grep's specific
> tests (and there's a ton of other unrelated tests in that file that may
> or may not even run with git-grep).

I agree. Since some of these cases are already covered, and some don't
have analogues, I think that it is most sensible to go through the above
and add _those_, instead of porting the whole test suite over from GNU.

> > It should also be tested with the various grep.patternType options to
> > make sure it works with basic, extended, perl, fixed etc.
>
> Hmm, this code is all external to the actual matching. So unless one of
> those is totally screwing up the regmatch_t return, I'm not sure that's
> accomplishing much (and if one of them is, it's totally broken for
> coloring, "-c", and maybe other features).
>
> We've usually taken a pretty white-box approach to our testing, covering
> things that seem likely to go wrong given the general problem space and
> our implementation. But maybe I'm missing something in particular that
> you think might be tricky.
>
> -Peff

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'
  2018-05-10  2:00       ` Taylor Blau
@ 2018-05-10  6:40         ` Jeff King
  0 siblings, 0 replies; 15+ messages in thread
From: Jeff King @ 2018-05-10  6:40 UTC (permalink / raw)
  To: Taylor Blau; +Cc: Ævar Arnfjörð Bjarmason, git, gitster

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v2 0/2] builtin/grep.c: teach '-o', '--only-matching'
  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  4:03 ` [PATCH 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep' Taylor Blau
@ 2018-05-12  3:21 ` 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
  2 siblings, 2 replies; 15+ messages in thread
From: Taylor Blau @ 2018-05-12  3:21 UTC (permalink / raw)
  To: git; +Cc: sunshine, avarab, peff, gitster

Hi,

Attached is the second re-roll of my series to add GNU grep's
'--only-matching' to git-grep.

The main thing that has changed since last time is our handling of
-{A,B,C}<N>. Previously, as Peff points out in [1], we handle this in a
buggy way different than GNU.

I agree that although 'git grep -C<N> -o ...' is an unusual invocation,
it is useful to (1) maintain as much consistency as reasonably makes
sense, and (2) to at least not be buggy.

I have also responded to Eric's suggestions in [2], and [3].

Thanks as always for your kind review :-).


Thanks,
Taylor

[1]: https://public-inbox.org/git/20180510064014.GA31779@sigill.intra.peff.net
[2]: https://public-inbox.org/git/CAPig+cSRJWW4-7vj6wK8aOfNB20bqUCSOOySjdPci1r5Vb83Uw@mail.gmail.com
[3]: https://public-inbox.org/git/CAPig+cRbBZ+QTqGiW_wQ9E-gROA-Wtevp1vcRqmJ5YQJ8tYEVQ@mail.gmail.com

Taylor Blau (2):
  grep.c: extract show_line_header()
  builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'

 Documentation/git-grep.txt |  6 ++-
 builtin/grep.c             |  1 +
 grep.c                     | 78 +++++++++++++++++++++++++++-----------
 grep.h                     |  1 +
 t/t7810-grep.sh            | 69 +++++++++++++++++++++++++++++++++
 5 files changed, 132 insertions(+), 23 deletions(-)

--
2.17.0

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v2 1/2] grep.c: extract show_line_header()
  2018-05-12  3:21 ` [PATCH v2 0/2] builtin/grep.c: teach '-o', '--only-matching' Taylor Blau
@ 2018-05-12  3:21   ` Taylor Blau
  2018-05-12  3:21   ` [PATCH v2 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep' Taylor Blau
  1 sibling, 0 replies; 15+ messages in thread
From: Taylor Blau @ 2018-05-12  3:21 UTC (permalink / raw)
  To: git; +Cc: sunshine, avarab, peff, gitster

Teach 'git-grep(1)' how to print a line header multiple times from
show_line() in preparation for it learning '--only-matching'.

show_line_header() comprises the code in show_line() executed in order
to produce a line header. It is a one-for-one extraction of the existing
implementation.

For now, show_line_header() provides no benefit over the change before
this patch. The following patch will conditionally call
show_line_header() multiple times per invocation to show_line(), which
is the desired benefit of this change.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 grep.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/grep.c b/grep.c
index 5ba1b05526..36bf7cf08d 100644
--- a/grep.c
+++ b/grep.c
@@ -1369,26 +1369,9 @@ static int next_match(struct grep_opt *opt, char *bol, char *eol,
 	return hit;
 }
 
-static void show_line(struct grep_opt *opt, char *bol, char *eol,
-		      const char *name, unsigned lno, unsigned cno, char sign)
+static void show_line_header(struct grep_opt *opt, const char *name,
+                             unsigned lno, unsigned cno, char sign)
 {
-	int rest = eol - bol;
-	const char *match_color, *line_color = NULL;
-
-	if (opt->file_break && opt->last_shown == 0) {
-		if (opt->show_hunk_mark)
-			opt->output(opt, "\n", 1);
-	} else if (opt->pre_context || opt->post_context || opt->funcbody) {
-		if (opt->last_shown == 0) {
-			if (opt->show_hunk_mark) {
-				output_color(opt, "--", 2, opt->color_sep);
-				opt->output(opt, "\n", 1);
-			}
-		} else if (lno > opt->last_shown + 1) {
-			output_color(opt, "--", 2, opt->color_sep);
-			opt->output(opt, "\n", 1);
-		}
-	}
 	if (opt->heading && opt->last_shown == 0) {
 		output_color(opt, name, strlen(name), opt->color_filename);
 		opt->output(opt, "\n", 1);
@@ -1417,6 +1400,29 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 		output_color(opt, buf, strlen(buf), opt->color_columnno);
 		output_sep(opt, sign);
 	}
+}
+
+static void show_line(struct grep_opt *opt, char *bol, char *eol,
+		      const char *name, unsigned lno, unsigned cno, char sign)
+{
+	int rest = eol - bol;
+	const char *match_color, *line_color = NULL;
+
+	if (opt->file_break && opt->last_shown == 0) {
+		if (opt->show_hunk_mark)
+			opt->output(opt, "\n", 1);
+	} else if (opt->pre_context || opt->post_context || opt->funcbody) {
+		if (opt->last_shown == 0) {
+			if (opt->show_hunk_mark) {
+				output_color(opt, "--", 2, opt->color_sep);
+				opt->output(opt, "\n", 1);
+			}
+		} else if (lno > opt->last_shown + 1) {
+			output_color(opt, "--", 2, opt->color_sep);
+			opt->output(opt, "\n", 1);
+		}
+	}
+	show_line_header(opt, name, lno, cno, sign);
 	if (opt->color) {
 		regmatch_t match;
 		enum grep_context ctx = GREP_CONTEXT_BODY;
-- 
2.17.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 2/2] builtin/grep.c: teach '-o', '--only-matching' to 'git-grep'
  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   ` Taylor Blau
  1 sibling, 0 replies; 15+ messages in thread
From: Taylor Blau @ 2018-05-12  3:21 UTC (permalink / raw)
  To: git; +Cc: sunshine, avarab, peff, gitster

Teach GNU grep(1)'s '-o' ('--only-matching') to 'git-grep'. This option
prints only the matching components of each line. It writes multiple
lines if more than one match exists on a given line.

For example:

  $ /git grep -on --column git -- README.md | head -3
  README.md:15:56:git
  README.md:18:20:git
  README.md:19:16:git

By using show_line_header(), 'git grep --only-matching' correctly
respects the '--heading' option:

  $ git grep -on --column --heading git -- README.md | head -4
  README.md
  15:56:git
  18:20:git
  19:16:git

We mirror GNU grep's behavior when given -A, -B, or -C with
--only-matching, by displaying only the matching portion(s) of a line,
ignoring contextual line(s), but displaying '--' (context separator)
line(s).

Notably: when show_line() is called on a line that contains _multiple_
matches, we keep track of a relative offset from the beginning of the
line and increment 'cno' in subsequent calls to show_line_header() when
the expression is not extended. In the extended case, we do not do this,
because the column of the first match is undefined, thus relative
offsets are meaningless.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 Documentation/git-grep.txt |  6 +++-
 builtin/grep.c             |  1 +
 grep.c                     | 34 +++++++++++++++++--
 grep.h                     |  1 +
 t/t7810-grep.sh            | 69 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 107 insertions(+), 4 deletions(-)

diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index c48a578cb1..5c09abec4a 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -20,7 +20,7 @@ SYNOPSIS
 	   [-c | --count] [--all-match] [-q | --quiet]
 	   [--max-depth <depth>]
 	   [--color[=<when>] | --no-color]
-	   [--break] [--heading] [-p | --show-function]
+	   [--break] [--heading] [-o | --only-matching] [-p | --show-function]
 	   [-A <post-context>] [-B <pre-context>] [-C <context>]
 	   [-W | --function-context]
 	   [--threads <num>]
@@ -223,6 +223,10 @@ providing this option will cause it to die.
 	Show the filename above the matches in that file instead of
 	at the start of each shown line.
 
+--o::
+--only-matching::
+	Prints only the matching part of the lines.
+
 -p::
 --show-function::
 	Show the preceding line that contains the function name of
diff --git a/builtin/grep.c b/builtin/grep.c
index f9f516dfc4..0507ac335a 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -851,6 +851,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			N_("print empty line between matches from different files")),
 		OPT_BOOL(0, "heading", &opt.heading,
 			N_("show filename only once above matches from same file")),
+		OPT_BOOL('o', "only-matching", &opt.only_matching, N_("show only matches")),
 		OPT_GROUP(""),
 		OPT_CALLBACK('C', "context", &opt, N_("n"),
 			N_("show <n> context lines before and after matches"),
diff --git a/grep.c b/grep.c
index 36bf7cf08d..9297fde643 100644
--- a/grep.c
+++ b/grep.c
@@ -1422,12 +1422,24 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 			opt->output(opt, "\n", 1);
 		}
 	}
+	if (opt->only_matching && sign != ':') {
+		/*
+		 * If we're given '--only-matching' and the line is a contextual
+		 * one (i.e., we're given '-A', '-B', or '-C'), mark the line as
+		 * shown (to advance opt->last_shown), but do not show it (since
+		 * we are given '--only-matching').
+		 */
+		opt->last_shown = lno;
+		return;
+	}
 	show_line_header(opt, name, lno, cno, sign);
-	if (opt->color) {
+	if (opt->color || opt->only_matching) {
 		regmatch_t match;
 		enum grep_context ctx = GREP_CONTEXT_BODY;
 		int ch = *eol;
 		int eflags = 0;
+		int first = 1;
+		int offset = 1;
 
 		if (sign == ':')
 			match_color = opt->color_match_selected;
@@ -1444,16 +1456,32 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 			if (match.rm_so == match.rm_eo)
 				break;
 
-			output_color(opt, bol, match.rm_so, line_color);
+			if (!opt->only_matching) {
+				output_color(opt, bol, match.rm_so, line_color);
+			} else if (!first) {
+				/*
+				 * We are given --only-matching, and this is not
+				 * the first match on a line. Reprint the
+				 * newline and header before showing another
+				 * match.
+				 */
+				opt->output(opt, "\n", 1);
+				show_line_header(opt, name, lno,
+					opt->extended ? 0 : offset+match.rm_so,
+					sign);
+			}
 			output_color(opt, bol + match.rm_so,
 				     match.rm_eo - match.rm_so, match_color);
+			offset += match.rm_eo;
 			bol += match.rm_eo;
 			rest -= match.rm_eo;
 			eflags = REG_NOTBOL;
+			first = 0;
 		}
 		*eol = ch;
 	}
-	output_color(opt, bol, rest, line_color);
+	if (!opt->only_matching)
+		output_color(opt, bol, rest, line_color);
 	opt->output(opt, "\n", 1);
 }
 
diff --git a/grep.h b/grep.h
index 08a0b391c5..24c1460100 100644
--- a/grep.h
+++ b/grep.h
@@ -126,6 +126,7 @@ struct grep_opt {
 	const char *prefix;
 	int prefix_length;
 	regex_t regexp;
+	int only_matching;
 	int linenum;
 	int columnnum;
 	int invert;
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 491b2e044a..6251ae678a 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -1432,6 +1432,75 @@ test_expect_success 'grep --heading' '
 	test_cmp expected actual
 '
 
+test_expect_success 'grep --only-matching' '
+	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
+	git grep --only-matching --line-number --column mmap file >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'grep --only-matching --column (unsupported)' '
+	cat >expected <<-\EOF &&
+	file:mmap
+	file:mmap
+	file:mmap
+	file:mmap
+	file:mmap
+	file:mmap
+	file:mmap
+	file:mmap
+	EOF
+	git grep --only-matching --column --not --not -e mmap -- file >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'grep --only-matching -C' '
+	cat >expected <<-\EOF &&
+	hello.ps1:function
+	hello.ps1:function
+	--
+	hello.ps1:function
+	EOF
+	git grep --only-matching -C1 function hello.ps1 >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'grep --only-matching --heading' '
+	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
+	git grep --only-matching --heading --line-number --column mmap file >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'grep --only-matching -i' '
+	cat >expected <<-\EOF &&
+	hello_world:1:1:Hello
+	hello_world:2:1:HeLLo
+	hello_world:3:1:Hello
+	hello_world:4:1:HeLLo
+	EOF
+	git grep --only-matching --line-number --column \
+		-i hello hello_world >actual &&
+	test_cmp expected actual
+'
+
 cat >expected <<EOF
 <BOLD;GREEN>hello.c<RESET>
 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
-- 
2.17.0

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 1/2] grep.c: extract show_line_header()
  2018-07-02 20:08 ` [PATCH v2 0/2] " Taylor Blau
@ 2018-07-02 20:08   ` Taylor Blau
  0 siblings, 0 replies; 15+ messages in thread
From: Taylor Blau @ 2018-07-02 20:08 UTC (permalink / raw)
  To: git; +Cc: peff, gitster, avarab

The grep code invokes show_line() to display the contents of a matched
or context line in its output. Part of this execution is to print a line
header that includes information such as the kind, the line- and
column-number and etc. of that match.

To prepare for the addition of an option to print only the matching
component(s) of a non-context line, we must prepare for the possibility
that a single line may contain multiple matching parts, and thus will
need multiple headers printed for a single line.

Extracting show_line_header allows us to do just that. In the subsequent
commit, it will be used within the colorization loop to print out only
the matching parts of a line, optionally with LFs delimiting
sub-matches.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 grep.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/grep.c b/grep.c
index 992673fe7e..4ff8a73043 100644
--- a/grep.c
+++ b/grep.c
@@ -1410,26 +1410,9 @@ static int next_match(struct grep_opt *opt, char *bol, char *eol,
 	return hit;
 }
 
-static void show_line(struct grep_opt *opt, char *bol, char *eol,
-		      const char *name, unsigned lno, ssize_t cno, char sign)
+static void show_line_header(struct grep_opt *opt, const char *name,
+			     unsigned lno, ssize_t cno, char sign)
 {
-	int rest = eol - bol;
-	const char *match_color, *line_color = NULL;
-
-	if (opt->file_break && opt->last_shown == 0) {
-		if (opt->show_hunk_mark)
-			opt->output(opt, "\n", 1);
-	} else if (opt->pre_context || opt->post_context || opt->funcbody) {
-		if (opt->last_shown == 0) {
-			if (opt->show_hunk_mark) {
-				output_color(opt, "--", 2, opt->color_sep);
-				opt->output(opt, "\n", 1);
-			}
-		} else if (lno > opt->last_shown + 1) {
-			output_color(opt, "--", 2, opt->color_sep);
-			opt->output(opt, "\n", 1);
-		}
-	}
 	if (opt->heading && opt->last_shown == 0) {
 		output_color(opt, name, strlen(name), opt->color_filename);
 		opt->output(opt, "\n", 1);
@@ -1457,6 +1440,29 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 		output_color(opt, buf, strlen(buf), opt->color_columnno);
 		output_sep(opt, sign);
 	}
+}
+
+static void show_line(struct grep_opt *opt, char *bol, char *eol,
+		      const char *name, unsigned lno, ssize_t cno, char sign)
+{
+	int rest = eol - bol;
+	const char *match_color, *line_color = NULL;
+
+	if (opt->file_break && opt->last_shown == 0) {
+		if (opt->show_hunk_mark)
+			opt->output(opt, "\n", 1);
+	} else if (opt->pre_context || opt->post_context || opt->funcbody) {
+		if (opt->last_shown == 0) {
+			if (opt->show_hunk_mark) {
+				output_color(opt, "--", 2, opt->color_sep);
+				opt->output(opt, "\n", 1);
+			}
+		} else if (lno > opt->last_shown + 1) {
+			output_color(opt, "--", 2, opt->color_sep);
+			opt->output(opt, "\n", 1);
+		}
+	}
+	show_line_header(opt, name, lno, cno, sign);
 	if (opt->color) {
 		regmatch_t match;
 		enum grep_context ctx = GREP_CONTEXT_BODY;
-- 
2.18.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2018-07-02 20:09 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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
  -- strict thread matches above, loose matches on Subject: below --
2018-06-25 21:25 [PATCH 0/2] grep.c: teach --only-matching to 'git-grep(1)' Taylor Blau
2018-07-02 20:08 ` [PATCH v2 0/2] " Taylor Blau
2018-07-02 20:08   ` [PATCH v2 1/2] grep.c: extract show_line_header() Taylor Blau

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).