aboutsummaryrefslogtreecommitdiff
path: root/builtin-grep.c
diff options
context:
space:
mode:
authorRené Scharfe <rene.scharfe@lsrfire.ath.cx>2009-03-07 13:34:46 +0100
committerJunio C Hamano <gitster@pobox.com>2009-03-07 11:34:59 -0800
commita94982ef39e1bb9a6f782b5b6ced22e97d6859b5 (patch)
treeab9a91565f8f37ce24eb8b89faa1e685ef1f1933 /builtin-grep.c
parent7e8f59d577e5615ceff06da0d9dde36a63608d53 (diff)
downloadgit-a94982ef39e1bb9a6f782b5b6ced22e97d6859b5.tar.gz
git-a94982ef39e1bb9a6f782b5b6ced22e97d6859b5.tar.xz
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>
Diffstat (limited to 'builtin-grep.c')
-rw-r--r--builtin-grep.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/builtin-grep.c b/builtin-grep.c
index e2c0f0161..9e7e766a4 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -30,6 +30,10 @@ static int grep_config(const char *var, const char *value, void *cb)
opt->color = git_config_colorbool(var, value, -1);
return 0;
}
+ if (!strcmp(var, "grep.color.external") ||
+ !strcmp(var, "color.grep.external")) {
+ return git_config_string(&(opt->color_external), var, value);
+ }
if (!strcmp(var, "grep.color.match") ||
!strcmp(var, "color.grep.match")) {
if (!value)
@@ -287,6 +291,21 @@ static int flush_grep(struct grep_opt *opt,
return status;
}
+static void grep_add_color(struct strbuf *sb, const char *escape_seq)
+{
+ size_t orig_len = sb->len;
+
+ while (*escape_seq) {
+ if (*escape_seq == 'm')
+ strbuf_addch(sb, ';');
+ else if (*escape_seq != '\033' && *escape_seq != '[')
+ strbuf_addch(sb, *escape_seq);
+ escape_seq++;
+ }
+ if (sb->len > orig_len && sb->buf[sb->len - 1] == ';')
+ strbuf_setlen(sb, sb->len - 1);
+}
+
static int external_grep(struct grep_opt *opt, const char **paths, int cached)
{
int i, nr, argc, hit, len, status;
@@ -357,6 +376,23 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
push_arg("-e");
push_arg(p->pattern);
}
+ if (opt->color) {
+ struct strbuf sb = STRBUF_INIT;
+
+ grep_add_color(&sb, opt->color_match);
+ setenv("GREP_COLOR", sb.buf, 1);
+
+ strbuf_reset(&sb);
+ strbuf_addstr(&sb, "mt=");
+ grep_add_color(&sb, opt->color_match);
+ strbuf_addstr(&sb, ":sl=:cx=:fn=:ln=:bn=:se=");
+ setenv("GREP_COLORS", sb.buf, 1);
+
+ strbuf_release(&sb);
+
+ if (opt->color_external && strlen(opt->color_external) > 0)
+ push_arg(opt->color_external);
+ }
hit = 0;
argc = nr;