grep: color patterns in output
Coloring matches makes them easier to spot in the output. Add two options and two parameters: color.grep (to turn coloring on or off), color.grep.match (to set the color of matches), --color and --no-color (to turn coloring on or off, respectively). The output of external greps is not changed. This patch is based on earlier ones by Nguyễn Thái Ngọc Duy and Thiago Alves. 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:32 UTC
7e8f59d577e5615ceff06da0d9dde36a63608d53
5 files changed
+130
-12
Documentation/config.txt
+9
@@ -548,6 +548,15 @@ color.diff.<slot>::
548
whitespace errors). The values of these variables may be specified as
549
in color.branch.<slot>.
550
551
+color.grep::
552
+ When set to `always`, always highlight matches. When `false` (or
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.match::
557
+ Use customized color for matches. The value of this variable
558
+ may be specified as in color.branch.<slot>.
559
+
560
color.interactive::
561
When set to `always`, always use colors for interactive prompts
562
and displays (such as those used by "git-add --interactive").
Documentation/git-grep.txt
+8
@@ -17,6 +17,7 @@ SYNOPSIS
17
[-l | --files-with-matches] [-L | --files-without-match]
18
[-z | --null]
19
[-c | --count] [--all-match]
20
+ [--color | --no-color]
21
[-A <post-context>] [-B <pre-context>] [-C <context>]
22
[-f <file>] [-e] <pattern>
23
[--and|--or|--not|(|)|-e <pattern>...] [<tree>...]
@@ -105,6 +106,13 @@ OPTIONS
106
Instead of showing every matched line, show the number of
107
lines that match.
108
109
+--color::
110
+ Show colored matches.
111
+
112
+--no-color::
113
+ Turn off match highlighting, even when the configuration file
114
+ gives the default to color output.
115
+
116
-[ABC] <context>::
117
Show `context` trailing (`A` -- after), or leading (`B`
118
-- before), or both (`C` -- context) lines, and place a
builtin-grep.c
+32
@@ -22,6 +22,24 @@
22
23
static int builtin_grep;
24
25
+static int grep_config(const char *var, const char *value, void *cb)
26
+{
27
+ struct grep_opt *opt = cb;
28
+
29
+ if (!strcmp(var, "grep.color") || !strcmp(var, "color.grep")) {
30
+ opt->color = git_config_colorbool(var, value, -1);
31
+ return 0;
32
+ }
33
+ if (!strcmp(var, "grep.color.match") ||
34
+ !strcmp(var, "color.grep.match")) {
35
+ if (!value)
36
+ return config_error_nonbool(var);
37
+ color_parse(value, var, opt->color_match);
38
+ return 0;
39
+ }
40
+ return git_color_default_config(var, value, cb);
41
+}
42
+
43
/*
44
* git grep pathspecs are somewhat different from diff-tree pathspecs;
45
* pathname wildcards are allowed.
@@ -536,6 +554,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
554
opt.pattern_tail = &opt.pattern_list;
555
opt.regflags = REG_NEWLINE;
556
557
+ strcpy(opt.color_match, GIT_COLOR_RED GIT_COLOR_BOLD);
558
+ opt.color = -1;
559
+ git_config(grep_config, &opt);
560
+ if (opt.color == -1)
561
+ opt.color = git_use_color_default;
562
+
563
/*
564
* If there is no -- then the paths must exist in the working
565
* tree. If there is no explicit pattern specified with -e or
@@ -732,6 +756,14 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
756
opt.relative = 0;
757
continue;
758
}
759
+ if (!strcmp("--color", arg)) {
760
+ opt.color = 1;
761
+ continue;
762
+ }
763
+ if (!strcmp("--no-color", arg)) {
764
+ opt.color = 0;
765
+ continue;
766
+ }
767
if (!strcmp("--", arg)) {
768
/* later processing wants to have this at argv[1] */
769
argv--;
grep.c
+78
-12
@@ -253,18 +253,6 @@ static int word_char(char ch)
253
return isalnum(ch) || ch == '_';
254
}
255
256
-static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
257
- const char *name, unsigned lno, char sign)
258
-{
259
- if (opt->null_following_name)
260
- sign = '\0';
261
- if (opt->pathname)
262
- printf("%s%c", name, sign);
263
- if (opt->linenum)
264
- printf("%d%c", lno, sign);
265
- printf("%.*s\n", (int)(eol-bol), bol);
266
-}
267
-
256
static void show_name(struct grep_opt *opt, const char *name)
257
{
258
printf("%s%c", name, opt->null_following_name ? '\0' : '\n');
@@ -437,6 +425,84 @@ static int match_line(struct grep_opt *opt, char *bol, char *eol,
425
return 0;
426
}
427
428
+static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
429
+ enum grep_context ctx,
430
+ regmatch_t *pmatch, int eflags)
431
+{
432
+ regmatch_t match;
433
+
434
+ if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
435
+ return 0;
436
+ if (match.rm_so < 0 || match.rm_eo < 0)
437
+ return 0;
438
+ if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
439
+ if (match.rm_so > pmatch->rm_so)
440
+ return 1;
441
+ if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
442
+ return 1;
443
+ }
444
+ pmatch->rm_so = match.rm_so;
445
+ pmatch->rm_eo = match.rm_eo;
446
+ return 1;
447
+}
448
+
449
+static int next_match(struct grep_opt *opt, char *bol, char *eol,
450
+ enum grep_context ctx, regmatch_t *pmatch, int eflags)
451
+{
452
+ struct grep_pat *p;
453
+ int hit = 0;
454
+
455
+ pmatch->rm_so = pmatch->rm_eo = -1;
456
+ if (bol < eol) {
457
+ for (p = opt->pattern_list; p; p = p->next) {
458
+ switch (p->token) {
459
+ case GREP_PATTERN: /* atom */
460
+ case GREP_PATTERN_HEAD:
461
+ case GREP_PATTERN_BODY:
462
+ hit |= match_next_pattern(p, bol, eol, ctx,
463
+ pmatch, eflags);
464
+ break;
465
+ default:
466
+ break;
467
+ }
468
+ }
469
+ }
470
+ return hit;
471
+}
472
+
473
+static void show_line(struct grep_opt *opt, char *bol, char *eol,
474
+ const char *name, unsigned lno, char sign)
475
+{
476
+ int rest = eol - bol;
477
+
478
+ if (opt->null_following_name)
479
+ sign = '\0';
480
+ if (opt->pathname)
481
+ printf("%s%c", name, sign);
482
+ if (opt->linenum)
483
+ printf("%d%c", lno, sign);
484
+ if (opt->color) {
485
+ regmatch_t match;
486
+ enum grep_context ctx = GREP_CONTEXT_BODY;
487
+ int ch = *eol;
488
+ int eflags = 0;
489
+
490
+ *eol = '\0';
491
+ while (next_match(opt, bol, eol, ctx, &match, eflags)) {
492
+ printf("%.*s%s%.*s%s",
493
+ match.rm_so, bol,
494
+ opt->color_match,
495
+ match.rm_eo - match.rm_so, bol + match.rm_so,
496
+ GIT_COLOR_RESET);
497
+ bol += match.rm_eo;
498
+ rest -= match.rm_eo;
499
+ eflags = REG_NOTBOL;
500
+ }
501
+ *eol = ch;
502
+ }
503
+ printf("%.*s\n", rest, bol);
504
+}
505
+
506
static int grep_buffer_1(struct grep_opt *opt, const char *name,
507
char *buf, unsigned long size, int collect_hits)
508
{
grep.h
+3
@@ -1,5 +1,6 @@
1
#ifndef GREP_H
2
#define GREP_H
3
+#include "color.h"
4
5
enum grep_pat_token {
6
GREP_PATTERN,
@@ -77,6 +78,8 @@ struct grep_opt {
78
unsigned relative:1;
79
unsigned pathname:1;
80
unsigned null_following_name:1;
81
+ int color;
82
+ char color_match[COLOR_MAXLEN];
83
int regflags;
84
unsigned pre_context;
85
unsigned post_context;