Skip to content

Commit 7e8f59d

Browse files
René Scharfegitster
authored andcommitted
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>
1 parent 7921277 commit 7e8f59d

File tree

5 files changed

+130
-12
lines changed

5 files changed

+130
-12
lines changed

Documentation/config.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,15 @@ color.diff.<slot>::
548548
whitespace errors). The values of these variables may be specified as
549549
in color.branch.<slot>.
550550

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+
551560
color.interactive::
552561
When set to `always`, always use colors for interactive prompts
553562
and displays (such as those used by "git-add --interactive").

Documentation/git-grep.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SYNOPSIS
1717
[-l | --files-with-matches] [-L | --files-without-match]
1818
[-z | --null]
1919
[-c | --count] [--all-match]
20+
[--color | --no-color]
2021
[-A <post-context>] [-B <pre-context>] [-C <context>]
2122
[-f <file>] [-e] <pattern>
2223
[--and|--or|--not|(|)|-e <pattern>...] [<tree>...]
@@ -105,6 +106,13 @@ OPTIONS
105106
Instead of showing every matched line, show the number of
106107
lines that match.
107108

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+
108116
-[ABC] <context>::
109117
Show `context` trailing (`A` -- after), or leading (`B`
110118
-- before), or both (`C` -- context) lines, and place a

builtin-grep.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@
2222

2323
static int builtin_grep;
2424

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+
2543
/*
2644
* git grep pathspecs are somewhat different from diff-tree pathspecs;
2745
* pathname wildcards are allowed.
@@ -536,6 +554,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
536554
opt.pattern_tail = &opt.pattern_list;
537555
opt.regflags = REG_NEWLINE;
538556

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+
539563
/*
540564
* If there is no -- then the paths must exist in the working
541565
* 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)
732756
opt.relative = 0;
733757
continue;
734758
}
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+
}
735767
if (!strcmp("--", arg)) {
736768
/* later processing wants to have this at argv[1] */
737769
argv--;

grep.c

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -253,18 +253,6 @@ static int word_char(char ch)
253253
return isalnum(ch) || ch == '_';
254254
}
255255

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-
268256
static void show_name(struct grep_opt *opt, const char *name)
269257
{
270258
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,
437425
return 0;
438426
}
439427

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+
440506
static int grep_buffer_1(struct grep_opt *opt, const char *name,
441507
char *buf, unsigned long size, int collect_hits)
442508
{

grep.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef GREP_H
22
#define GREP_H
3+
#include "color.h"
34

45
enum grep_pat_token {
56
GREP_PATTERN,
@@ -77,6 +78,8 @@ struct grep_opt {
7778
unsigned relative:1;
7879
unsigned pathname:1;
7980
unsigned null_following_name:1;
81+
int color;
82+
char color_match[COLOR_MAXLEN];
8083
int regflags;
8184
unsigned pre_context;
8285
unsigned post_context;

0 commit comments

Comments
 (0)