mirror of
https://github.com/git/git
synced 2024-08-25 02:35:46 +00:00
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>
This commit is contained in:
parent
79212772ce
commit
7e8f59d577
|
@ -548,6 +548,15 @@ color.diff.<slot>::
|
||||||
whitespace errors). The values of these variables may be specified as
|
whitespace errors). The values of these variables may be specified as
|
||||||
in color.branch.<slot>.
|
in color.branch.<slot>.
|
||||||
|
|
||||||
|
color.grep::
|
||||||
|
When set to `always`, always highlight matches. When `false` (or
|
||||||
|
`never`), never. When set to `true` or `auto`, use color only
|
||||||
|
when the output is written to the terminal. Defaults to `false`.
|
||||||
|
|
||||||
|
color.grep.match::
|
||||||
|
Use customized color for matches. The value of this variable
|
||||||
|
may be specified as in color.branch.<slot>.
|
||||||
|
|
||||||
color.interactive::
|
color.interactive::
|
||||||
When set to `always`, always use colors for interactive prompts
|
When set to `always`, always use colors for interactive prompts
|
||||||
and displays (such as those used by "git-add --interactive").
|
and displays (such as those used by "git-add --interactive").
|
||||||
|
|
|
@ -17,6 +17,7 @@ SYNOPSIS
|
||||||
[-l | --files-with-matches] [-L | --files-without-match]
|
[-l | --files-with-matches] [-L | --files-without-match]
|
||||||
[-z | --null]
|
[-z | --null]
|
||||||
[-c | --count] [--all-match]
|
[-c | --count] [--all-match]
|
||||||
|
[--color | --no-color]
|
||||||
[-A <post-context>] [-B <pre-context>] [-C <context>]
|
[-A <post-context>] [-B <pre-context>] [-C <context>]
|
||||||
[-f <file>] [-e] <pattern>
|
[-f <file>] [-e] <pattern>
|
||||||
[--and|--or|--not|(|)|-e <pattern>...] [<tree>...]
|
[--and|--or|--not|(|)|-e <pattern>...] [<tree>...]
|
||||||
|
@ -105,6 +106,13 @@ OPTIONS
|
||||||
Instead of showing every matched line, show the number of
|
Instead of showing every matched line, show the number of
|
||||||
lines that match.
|
lines that match.
|
||||||
|
|
||||||
|
--color::
|
||||||
|
Show colored matches.
|
||||||
|
|
||||||
|
--no-color::
|
||||||
|
Turn off match highlighting, even when the configuration file
|
||||||
|
gives the default to color output.
|
||||||
|
|
||||||
-[ABC] <context>::
|
-[ABC] <context>::
|
||||||
Show `context` trailing (`A` -- after), or leading (`B`
|
Show `context` trailing (`A` -- after), or leading (`B`
|
||||||
-- before), or both (`C` -- context) lines, and place a
|
-- before), or both (`C` -- context) lines, and place a
|
||||||
|
|
|
@ -22,6 +22,24 @@
|
||||||
|
|
||||||
static int builtin_grep;
|
static int builtin_grep;
|
||||||
|
|
||||||
|
static int grep_config(const char *var, const char *value, void *cb)
|
||||||
|
{
|
||||||
|
struct grep_opt *opt = cb;
|
||||||
|
|
||||||
|
if (!strcmp(var, "grep.color") || !strcmp(var, "color.grep")) {
|
||||||
|
opt->color = git_config_colorbool(var, value, -1);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (!strcmp(var, "grep.color.match") ||
|
||||||
|
!strcmp(var, "color.grep.match")) {
|
||||||
|
if (!value)
|
||||||
|
return config_error_nonbool(var);
|
||||||
|
color_parse(value, var, opt->color_match);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return git_color_default_config(var, value, cb);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* git grep pathspecs are somewhat different from diff-tree pathspecs;
|
* git grep pathspecs are somewhat different from diff-tree pathspecs;
|
||||||
* pathname wildcards are allowed.
|
* pathname wildcards are allowed.
|
||||||
|
@ -536,6 +554,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
|
||||||
opt.pattern_tail = &opt.pattern_list;
|
opt.pattern_tail = &opt.pattern_list;
|
||||||
opt.regflags = REG_NEWLINE;
|
opt.regflags = REG_NEWLINE;
|
||||||
|
|
||||||
|
strcpy(opt.color_match, GIT_COLOR_RED GIT_COLOR_BOLD);
|
||||||
|
opt.color = -1;
|
||||||
|
git_config(grep_config, &opt);
|
||||||
|
if (opt.color == -1)
|
||||||
|
opt.color = git_use_color_default;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If there is no -- then the paths must exist in the working
|
* If there is no -- then the paths must exist in the working
|
||||||
* tree. If there is no explicit pattern specified with -e or
|
* 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)
|
||||||
opt.relative = 0;
|
opt.relative = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (!strcmp("--color", arg)) {
|
||||||
|
opt.color = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!strcmp("--no-color", arg)) {
|
||||||
|
opt.color = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (!strcmp("--", arg)) {
|
if (!strcmp("--", arg)) {
|
||||||
/* later processing wants to have this at argv[1] */
|
/* later processing wants to have this at argv[1] */
|
||||||
argv--;
|
argv--;
|
||||||
|
|
90
grep.c
90
grep.c
|
@ -253,18 +253,6 @@ static int word_char(char ch)
|
||||||
return isalnum(ch) || ch == '_';
|
return isalnum(ch) || ch == '_';
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
|
|
||||||
const char *name, unsigned lno, char sign)
|
|
||||||
{
|
|
||||||
if (opt->null_following_name)
|
|
||||||
sign = '\0';
|
|
||||||
if (opt->pathname)
|
|
||||||
printf("%s%c", name, sign);
|
|
||||||
if (opt->linenum)
|
|
||||||
printf("%d%c", lno, sign);
|
|
||||||
printf("%.*s\n", (int)(eol-bol), bol);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void show_name(struct grep_opt *opt, const char *name)
|
static void show_name(struct grep_opt *opt, const char *name)
|
||||||
{
|
{
|
||||||
printf("%s%c", name, opt->null_following_name ? '\0' : '\n');
|
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,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
|
||||||
|
enum grep_context ctx,
|
||||||
|
regmatch_t *pmatch, int eflags)
|
||||||
|
{
|
||||||
|
regmatch_t match;
|
||||||
|
|
||||||
|
if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
|
||||||
|
return 0;
|
||||||
|
if (match.rm_so < 0 || match.rm_eo < 0)
|
||||||
|
return 0;
|
||||||
|
if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
|
||||||
|
if (match.rm_so > pmatch->rm_so)
|
||||||
|
return 1;
|
||||||
|
if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
pmatch->rm_so = match.rm_so;
|
||||||
|
pmatch->rm_eo = match.rm_eo;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int next_match(struct grep_opt *opt, char *bol, char *eol,
|
||||||
|
enum grep_context ctx, regmatch_t *pmatch, int eflags)
|
||||||
|
{
|
||||||
|
struct grep_pat *p;
|
||||||
|
int hit = 0;
|
||||||
|
|
||||||
|
pmatch->rm_so = pmatch->rm_eo = -1;
|
||||||
|
if (bol < eol) {
|
||||||
|
for (p = opt->pattern_list; p; p = p->next) {
|
||||||
|
switch (p->token) {
|
||||||
|
case GREP_PATTERN: /* atom */
|
||||||
|
case GREP_PATTERN_HEAD:
|
||||||
|
case GREP_PATTERN_BODY:
|
||||||
|
hit |= match_next_pattern(p, bol, eol, ctx,
|
||||||
|
pmatch, eflags);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hit;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void show_line(struct grep_opt *opt, char *bol, char *eol,
|
||||||
|
const char *name, unsigned lno, char sign)
|
||||||
|
{
|
||||||
|
int rest = eol - bol;
|
||||||
|
|
||||||
|
if (opt->null_following_name)
|
||||||
|
sign = '\0';
|
||||||
|
if (opt->pathname)
|
||||||
|
printf("%s%c", name, sign);
|
||||||
|
if (opt->linenum)
|
||||||
|
printf("%d%c", lno, sign);
|
||||||
|
if (opt->color) {
|
||||||
|
regmatch_t match;
|
||||||
|
enum grep_context ctx = GREP_CONTEXT_BODY;
|
||||||
|
int ch = *eol;
|
||||||
|
int eflags = 0;
|
||||||
|
|
||||||
|
*eol = '\0';
|
||||||
|
while (next_match(opt, bol, eol, ctx, &match, eflags)) {
|
||||||
|
printf("%.*s%s%.*s%s",
|
||||||
|
match.rm_so, bol,
|
||||||
|
opt->color_match,
|
||||||
|
match.rm_eo - match.rm_so, bol + match.rm_so,
|
||||||
|
GIT_COLOR_RESET);
|
||||||
|
bol += match.rm_eo;
|
||||||
|
rest -= match.rm_eo;
|
||||||
|
eflags = REG_NOTBOL;
|
||||||
|
}
|
||||||
|
*eol = ch;
|
||||||
|
}
|
||||||
|
printf("%.*s\n", rest, bol);
|
||||||
|
}
|
||||||
|
|
||||||
static int grep_buffer_1(struct grep_opt *opt, const char *name,
|
static int grep_buffer_1(struct grep_opt *opt, const char *name,
|
||||||
char *buf, unsigned long size, int collect_hits)
|
char *buf, unsigned long size, int collect_hits)
|
||||||
{
|
{
|
||||||
|
|
3
grep.h
3
grep.h
|
@ -1,5 +1,6 @@
|
||||||
#ifndef GREP_H
|
#ifndef GREP_H
|
||||||
#define GREP_H
|
#define GREP_H
|
||||||
|
#include "color.h"
|
||||||
|
|
||||||
enum grep_pat_token {
|
enum grep_pat_token {
|
||||||
GREP_PATTERN,
|
GREP_PATTERN,
|
||||||
|
@ -77,6 +78,8 @@ struct grep_opt {
|
||||||
unsigned relative:1;
|
unsigned relative:1;
|
||||||
unsigned pathname:1;
|
unsigned pathname:1;
|
||||||
unsigned null_following_name:1;
|
unsigned null_following_name:1;
|
||||||
|
int color;
|
||||||
|
char color_match[COLOR_MAXLEN];
|
||||||
int regflags;
|
int regflags;
|
||||||
unsigned pre_context;
|
unsigned pre_context;
|
||||||
unsigned post_context;
|
unsigned post_context;
|
||||||
|
|
Loading…
Reference in a new issue