@matheusbsbs / matheus-bagliano / commits / a94982ef39
Fork 0

grep: add support for coloring with external greps

Add the config variable color.grep.external, which can be used to switch on coloring of external greps. To enable auto coloring with GNU grep, one needs to set color.grep.external to --color=always to defeat the pager started by git grep. The value of the config variable will be passed to the external grep only if it would colorize internal grep's output, so automatic terminal detected works. The default is to not pass any option, because the external grep command could be a program without color support. Also set the environment variables GREP_COLOR and GREP_COLORS to pass the configured color for matches to the external grep. This works with GNU grep; other variables could be added as needed. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Mar 7, 2009 at 13:34 UTC a94982ef39e1bb9a6f782b5b6ced22e97d6859b5
3 files changed +48 -1
Documentation/config.txt
+11 -1
@@ -553,9 +553,19 @@ color.grep::
553 `never`), never. When set to `true` or `auto`, use color only
554 when the output is written to the terminal. Defaults to `false`.
555
556 +color.grep.external::
557 + The string value of this variable is passed to an external 'grep'
558 + command as a command line option if match highlighting is turned
559 + on. If set to an empty string, no option is passed at all,
560 + turning off coloring for external 'grep' calls; this is the default.
561 + For GNU grep, set it to `--color=always` to highlight matches even
562 + when a pager is used.
563 +
564 color.grep.match::
565 Use customized color for matches. The value of this variable
558 - may be specified as in color.branch.<slot>.
566 + may be specified as in color.branch.<slot>. It is passed using
567 + the environment variables 'GREP_COLOR' and 'GREP_COLORS' when
568 + calling an external 'grep'.
569
570 color.interactive::
571 When set to `always`, always use colors for interactive prompts
builtin-grep.c
+36
@@ -30,6 +30,10 @@ static int grep_config(const char *var, const char *value, void *cb)
30 opt->color = git_config_colorbool(var, value, -1);
31 return 0;
32 }
33 + if (!strcmp(var, "grep.color.external") ||
34 + !strcmp(var, "color.grep.external")) {
35 + return git_config_string(&(opt->color_external), var, value);
36 + }
37 if (!strcmp(var, "grep.color.match") ||
38 !strcmp(var, "color.grep.match")) {
39 if (!value)
@@ -287,6 +291,21 @@ static int flush_grep(struct grep_opt *opt,
291 return status;
292 }
293
294 +static void grep_add_color(struct strbuf *sb, const char *escape_seq)
295 +{
296 + size_t orig_len = sb->len;
297 +
298 + while (*escape_seq) {
299 + if (*escape_seq == 'm')
300 + strbuf_addch(sb, ';');
301 + else if (*escape_seq != '\033' && *escape_seq != '[')
302 + strbuf_addch(sb, *escape_seq);
303 + escape_seq++;
304 + }
305 + if (sb->len > orig_len && sb->buf[sb->len - 1] == ';')
306 + strbuf_setlen(sb, sb->len - 1);
307 +}
308 +
309 static int external_grep(struct grep_opt *opt, const char **paths, int cached)
310 {
311 int i, nr, argc, hit, len, status;
@@ -357,6 +376,23 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
376 push_arg("-e");
377 push_arg(p->pattern);
378 }
379 + if (opt->color) {
380 + struct strbuf sb = STRBUF_INIT;
381 +
382 + grep_add_color(&sb, opt->color_match);
383 + setenv("GREP_COLOR", sb.buf, 1);
384 +
385 + strbuf_reset(&sb);
386 + strbuf_addstr(&sb, "mt=");
387 + grep_add_color(&sb, opt->color_match);
388 + strbuf_addstr(&sb, ":sl=:cx=:fn=:ln=:bn=:se=");
389 + setenv("GREP_COLORS", sb.buf, 1);
390 +
391 + strbuf_release(&sb);
392 +
393 + if (opt->color_external && strlen(opt->color_external) > 0)
394 + push_arg(opt->color_external);
395 + }
396
397 hit = 0;
398 argc = nr;
grep.h
+1
@@ -80,6 +80,7 @@ struct grep_opt {
80 unsigned null_following_name:1;
81 int color;
82 char color_match[COLOR_MAXLEN];
83 + const char *color_external;
84 int regflags;
85 unsigned pre_context;
86 unsigned post_context;