Skip to contentSkip to conversation

git-grep: add --color to highlight matches

PATCH In review Nguyễn Thái Ngọc Duy 4 participants 10 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-grep: add --color to highlight matches

---
 Documentation/git-grep.txt |    4 +++
 builtin-grep.c             |    6 +++-
 grep.c                     |   62 +++++++++++++++++++++++++++++++++++++++++---
 grep.h                     |    1 +
 4 files changed, 68 insertions(+), 5 deletions(-)
Documentation/git-grep.txt +4 -0
index a97f055..20ef098 100644
@@ -16,6 +16,7 @@ SYNOPSIS
 	   [-F | --fixed-strings] [-n]
 	   [-l | --files-with-matches] [-L | --files-without-match]
 	   [-c | --count] [--all-match]
+	   [--color]
 	   [-A <post-context>] [-B <pre-context>] [-C <context>]
 	   [-f <file>] [-e] <pattern>
 	   [--and|--or|--not|(|)|-e <pattern>...] [<tree>...]
@@ -85,6 +86,9 @@ OPTIONS
 	Instead of showing every matched line, show the number of
 	lines that match.
 
+--color::
+	Show colored matches.
+
 -[ABC] <context>::
 	Show `context` trailing (`A` -- after), or leading (`B`
 	-- before), or both (`C` -- context) lines, and place a
builtin-grep.c +5 -1
index ef29910..118ec32 100644
@@ -386,7 +386,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
 	 * we grep through the checked-out files. It tends to
 	 * be a lot more optimized
 	 */
-	if (!cached) {
+	if (!cached && !opt->color) {
 		hit = external_grep(opt, paths, cached);
 		if (hit >= 0)
 			return hit;
@@ -708,6 +708,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			opt.relative = 0;
 			continue;
 		}
+		if (!strcmp("--color", arg)) {
+			opt.color = 1;
+			continue;
+		}
 		if (!strcmp("--", arg)) {
 			/* later processing wants to have this at argv[1] */
 			argv--;
grep.c +58 -4

95 lines not on this page · raw message

grep.h +1 -0
index d252dd2..979f7d0 100644
@@ -68,6 +68,7 @@ struct grep_opt {
 	unsigned extended:1;
 	unsigned relative:1;
 	unsigned pathname:1;
+	unsigned color:1;
 	int regflags;
 	unsigned pre_context;
 	unsigned post_context;
Signature · 1 line
1.5.5.GIT
#1 lore raw

Documentation/git-grep.txt OPTIONS · line 91

Quoted 3 lines · Nguyễn Thái Ngọc Duy

+--color::

Documentation/git-grep.txt +2 -0
+	Show colored matches.
+

Why not borrow from GNU grep manpage?

--colour[=WHEN], --color[=WHEN]
Surround the matching string with the marker find in GREP_COLOR environment variable. WHEN may be 'never', 'always', or 'auto'

We probably would also want it configurable (via config variables), as `color.grep' I guess.

I think compatibility with grep (using GREP_COLOR) would be good idea, and easily implemented.

Signature · 3 lines
Jakub Narebski
Poland
ShadeHawk on #git
#2 lore raw
Quoted 6 lines · Jakub Narebski

Documentation/git-grep.txt OPTIONS · line 91

Quoted 3 lines · Nguyen Thai Ngoc Duy

+--color::

Documentation/git-grep.txt +2 -0
+     Show colored matches.
+
Quoted 8 lines

Why not borrow from GNU grep manpage?

--colour[=WHEN], --color[=WHEN]
Surround the matching string with the marker find in GREP_COLOR environment variable. WHEN may be 'never', 'always', or 'auto'

We probably would also want it configurable (via config variables), as `color.grep' I guess.

Yes. But this patch was really to scratch my itch and I felt good after having it colorized so did not bother with configuration system. I will probably make it a config var when I get tired of typing --color too often ;)

Quoted 2 lines

I think compatibility with grep (using GREP_COLOR) would be good idea, and easily implemented.

I had no idea that variable existed.

Quoted 5 lines

--
Jakub Narebski
Poland
ShadeHawk on #git

Signature · 1 line
Duy
#3 lore raw
Quoted 7 lines · Nguyen Thai Ngoc Duy
Quoted 6 lines · Jakub Narebski
Quoted 3 lines · Nguyen Thai Ngoc Duy

+--color::

Documentation/git-grep.txt +2 -0
+     Show colored matches.
+
Quoted 8 lines
Quoted 8 lines

Why not borrow from GNU grep manpage?

--colour[=WHEN], --color[=WHEN]
Surround the matching string with the marker find in GREP_COLOR environment variable. WHEN may be 'never', 'always', or 'auto'

We probably would also want it configurable (via config variables), as `color.grep' I guess.

And color.grep.<slot>, where slot can be 'match', and probably later also 'context' (I don't remember if git-grep implements -C<num> option...).

Quoted 4 lines

Yes. But this patch was really to scratch my itch and I felt good after having it colorized so did not bother with configuration system. I will probably make it a config var when I get tired of typing --color too often ;)

Thanks for sharing, although I guess Junio would prefer more complete patch...

Quoted 4 lines
Quoted 2 lines

I think compatibility with grep (using GREP_COLOR) would be good idea, and easily implemented.

I had no idea that variable existed.

Well, I have though that git always use external grep, so this would be for free. After reading the patch more carefully it seems like it isn't so.

Still, perhaps that would be a good idea, but I guess for later patch.

Signature · 2 lines
Jakub Narebski
Poland
#4 lore raw
Quoted 6 lines · Jakub Narebski
Quoted 5 lines · Nguyen Thai Ngoc Duy
Quoted 4 lines · Jakub Narebski

Nguyen Thai Ngoc Duy <[email protected]> writes:

I think compatibility with grep (using GREP_COLOR) would be good idea, and easily implemented.

Quoted 7 lines
Quoted 1 line

I had no idea that variable existed.

Well, I have though that git always use external grep, so this would be for free. After reading the patch more carefully it seems like it isn't so.

Still, perhaps that would be a good idea, but I guess for later patch.

I can't help but think that colorization for external-grep systems only would still be worthwhile, as the majority of git users are on such systems. It'd still be a separate implementation from adding it to the internal grep functionality anyways, so it's not as if work would be lost by going half the way here.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#5 lore raw

Hi,

Quoted 5 lines · Andreas Ericsson

I can't help but think that colorization for external-grep systems only would still be worthwhile, as the majority of git users are on such systems. It'd still be a separate implementation from adding it to the internal grep functionality anyways, so it's not as if work would be lost by going half the way here.

Huh? I thought that the external grep is only a matter of fallback for performance reasons (if the files are already checked out), or if the user specifies the external grep explicitely.

At least for the platform we bend over most, our beloved anachronistic M$ Windows, I do not recall forcing external grep.

Besides, it would be a kludge at best to work _twice_ to find out the search terms, once in the external grep, and a second time in the coloring code. So I think it should not be done.

Ciao,
Dscho

#6 lore raw
Quoted 3 lines · Johannes Schindelin

Besides, it would be a kludge at best to work _twice_ to find out the search terms, once in the external grep, and a second time in the coloring code. So I think it should not be done.

I think if it's GNU grep, just passing it --color, it will grep and colorize search terms in one turn.

Signature · 1 line
Duy
#7 lore raw

Hi,

Quoted 8 lines · Nguyen Thai Ngoc Duy
Quoted 3 lines · Johannes Schindelin

Besides, it would be a kludge at best to work _twice_ to find out the search terms, once in the external grep, and a second time in the coloring code. So I think it should not be done.

I think if it's GNU grep, just passing it --color, it will grep and colorize search terms in one turn.

And what tells you that the called grep is GNU grep?

Ciao,
Dscho

#8 lore raw
Quoted 6 lines · Johannes Schindelin

Hi,

Quoted 2 lines · Nguyen Thai Ngoc Duy

On Mon, May 26, 2008 at 5:16 PM, Johannes Schindelin <[email protected]> wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Besides, it would be a kludge at best to work _twice_ to find out the search terms, once in the external grep, and a second time in the coloring code. So I think it should not be done.

I think if it's GNU grep, just passing it --color, it will grep and colorize search terms in one turn.

And what tells you that the called grep is GNU grep?

A newly added macro like HAS_GNU_GREP? Granted it won't work all the time. The user who set the macro should know what he is doing. This approach is IMHO fine as long as we don't allow color customization.

Signature · 1 line
Duy
#9 lore raw

Hi,

Quoted 11 lines · Nguyen Thai Ngoc Duy
Quoted 8 lines · Johannes Schindelin
Quoted 6 lines · Nguyen Thai Ngoc Duy
Quoted 3 lines · Johannes Schindelin

Besides, it would be a kludge at best to work _twice_ to find out the search terms, once in the external grep, and a second time in the coloring code. So I think it should not be done.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I think if it's GNU grep, just passing it --color, it will grep and colorize search terms in one turn.

And what tells you that the called grep is GNU grep?

A newly added macro like HAS_GNU_GREP? Granted it won't work all the time. The user who set the macro should know what he is doing. This approach is IMHO fine as long as we don't allow color customization.

IMO this is just a kludge that is not even necessary, because the whole notion of relying on an external grep's colorization is crap.

The OP has a patch that adds colorization to Git's internal grep code. The user specifies "--color" with grep? Fine, then it's no external grep. Full stop.

Just like the original patch implements it. Nice, simple, and easy. What is so wrong with that?

Ciao,
Dscho