2006-09-17 23:02:52 +00:00
|
|
|
#include "cache.h"
|
|
|
|
#include "grep.h"
|
2009-07-01 22:07:24 +00:00
|
|
|
#include "userdiff.h"
|
2007-06-05 02:36:11 +00:00
|
|
|
#include "xdiff-interface.h"
|
2006-09-17 23:02:52 +00:00
|
|
|
|
log --author/--committer: really match only with name part
When we tried to find commits done by AUTHOR, the first implementation
tried to pattern match a line with "^author .*AUTHOR", which later was
enhanced to strip leading caret and look for "^author AUTHOR" when the
search pattern was anchored at the left end (i.e. --author="^AUTHOR").
This had a few problems:
* When looking for fixed strings (e.g. "git log -F --author=x --grep=y"),
the regexp internally used "^author .*x" would never match anything;
* To match at the end (e.g. "git log --author='google.com>$'"), the
generated regexp has to also match the trailing timestamp part the
commit header lines have. Also, in order to determine if the '$' at
the end means "match at the end of the line" or just a literal dollar
sign (probably backslash-quoted), we would need to parse the regexp
ourselves.
An earlier alternative tried to make sure that a line matches "^author "
(to limit by field name) and the user supplied pattern at the same time.
While it solved the -F problem by introducing a special override for
matching the "^author ", it did not solve the trailing timestamp nor tail
match problem. It also would have matched every commit if --author=author
was asked for, not because the author's email part had this string, but
because every commit header line that talks about the author begins with
that field name, regardleses of who wrote it.
Instead of piling more hacks on top of hacks, this rethinks the grep
machinery that is used to look for strings in the commit header, and makes
sure that (1) field name matches literally at the beginning of the line,
followed by a SP, and (2) the user supplied pattern is matched against the
remainder of the line, excluding the trailing timestamp data.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-09-05 05:15:02 +00:00
|
|
|
void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field field, const char *pat)
|
|
|
|
{
|
|
|
|
struct grep_pat *p = xcalloc(1, sizeof(*p));
|
|
|
|
p->pattern = pat;
|
|
|
|
p->origin = "header";
|
|
|
|
p->no = 0;
|
|
|
|
p->token = GREP_PATTERN_HEAD;
|
|
|
|
p->field = field;
|
"log --author=me --grep=it" should find intersection, not union
Historically, any grep filter in "git log" family of commands were taken
as restricting to commits with any of the words in the commit log message.
However, the user almost always want to find commits "done by this person
on that topic". With "--all-match" option, a series of grep patterns can
be turned into a requirement that all of them must produce a match, but
that makes it impossible to ask for "done by me, on either this or that"
with:
log --author=me --committer=him --grep=this --grep=that
because it will require both "this" and "that" to appear.
Change the "header" parser of grep library to treat the headers specially,
and parse it as:
(all-match-OR (HEADER-AUTHOR me)
(HEADER-COMMITTER him)
(OR
(PATTERN this)
(PATTERN that) ) )
Even though the "log" command line parser doesn't give direct access to
the extended grep syntax to group terms with parentheses, this change will
cover the majority of the case the users would want.
This incidentally revealed that one test in t7002 was bogus. It ran:
log --author=Thor --grep=Thu --format='%s'
and expected (wrongly) "Thu" to match "Thursday" in the author/committer
date, but that would never match, as the timestamp in raw commit buffer
does not have the name of the day-of-the-week.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-01-18 04:09:06 +00:00
|
|
|
*opt->header_tail = p;
|
|
|
|
opt->header_tail = &p->next;
|
log --author/--committer: really match only with name part
When we tried to find commits done by AUTHOR, the first implementation
tried to pattern match a line with "^author .*AUTHOR", which later was
enhanced to strip leading caret and look for "^author AUTHOR" when the
search pattern was anchored at the left end (i.e. --author="^AUTHOR").
This had a few problems:
* When looking for fixed strings (e.g. "git log -F --author=x --grep=y"),
the regexp internally used "^author .*x" would never match anything;
* To match at the end (e.g. "git log --author='google.com>$'"), the
generated regexp has to also match the trailing timestamp part the
commit header lines have. Also, in order to determine if the '$' at
the end means "match at the end of the line" or just a literal dollar
sign (probably backslash-quoted), we would need to parse the regexp
ourselves.
An earlier alternative tried to make sure that a line matches "^author "
(to limit by field name) and the user supplied pattern at the same time.
While it solved the -F problem by introducing a special override for
matching the "^author ", it did not solve the trailing timestamp nor tail
match problem. It also would have matched every commit if --author=author
was asked for, not because the author's email part had this string, but
because every commit header line that talks about the author begins with
that field name, regardleses of who wrote it.
Instead of piling more hacks on top of hacks, this rethinks the grep
machinery that is used to look for strings in the commit header, and makes
sure that (1) field name matches literally at the beginning of the line,
followed by a SP, and (2) the user supplied pattern is matched against the
remainder of the line, excluding the trailing timestamp data.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-09-05 05:15:02 +00:00
|
|
|
p->next = NULL;
|
|
|
|
}
|
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
void append_grep_pattern(struct grep_opt *opt, const char *pat,
|
|
|
|
const char *origin, int no, enum grep_pat_token t)
|
|
|
|
{
|
|
|
|
struct grep_pat *p = xcalloc(1, sizeof(*p));
|
|
|
|
p->pattern = pat;
|
|
|
|
p->origin = origin;
|
|
|
|
p->no = no;
|
|
|
|
p->token = t;
|
|
|
|
*opt->pattern_tail = p;
|
|
|
|
opt->pattern_tail = &p->next;
|
|
|
|
p->next = NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-25 22:51:39 +00:00
|
|
|
struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
|
|
|
|
{
|
|
|
|
struct grep_pat *pat;
|
|
|
|
struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
|
|
|
|
*ret = *opt;
|
|
|
|
|
|
|
|
ret->pattern_list = NULL;
|
|
|
|
ret->pattern_tail = &ret->pattern_list;
|
|
|
|
|
|
|
|
for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
|
|
|
|
{
|
|
|
|
if(pat->token == GREP_PATTERN_HEAD)
|
|
|
|
append_header_grep_pattern(ret, pat->field,
|
|
|
|
pat->pattern);
|
|
|
|
else
|
|
|
|
append_grep_pattern(ret, pat->pattern, pat->origin,
|
|
|
|
pat->no, pat->token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
|
|
|
|
{
|
2009-01-09 23:18:34 +00:00
|
|
|
int err;
|
|
|
|
|
2009-03-07 12:28:40 +00:00
|
|
|
p->word_regexp = opt->word_regexp;
|
2009-11-06 09:22:35 +00:00
|
|
|
p->ignore_case = opt->ignore_case;
|
2010-02-03 18:16:30 +00:00
|
|
|
p->fixed = opt->fixed;
|
2009-03-07 12:28:40 +00:00
|
|
|
|
2009-01-09 23:18:34 +00:00
|
|
|
if (p->fixed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
err = regcomp(&p->regexp, p->pattern, opt->regflags);
|
2006-09-17 23:02:52 +00:00
|
|
|
if (err) {
|
|
|
|
char errbuf[1024];
|
|
|
|
char where[1024];
|
|
|
|
if (p->no)
|
|
|
|
sprintf(where, "In '%s' at %d, ",
|
|
|
|
p->origin, p->no);
|
|
|
|
else if (p->origin)
|
|
|
|
sprintf(where, "%s, ", p->origin);
|
|
|
|
else
|
|
|
|
where[0] = 0;
|
|
|
|
regerror(err, &p->regexp, errbuf, 1024);
|
|
|
|
regfree(&p->regexp);
|
|
|
|
die("%s'%s': %s", where, p->pattern, errbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-28 00:50:52 +00:00
|
|
|
static struct grep_expr *compile_pattern_or(struct grep_pat **);
|
2006-09-17 23:02:52 +00:00
|
|
|
static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
|
|
|
|
{
|
|
|
|
struct grep_pat *p;
|
|
|
|
struct grep_expr *x;
|
|
|
|
|
|
|
|
p = *list;
|
2009-04-27 18:10:24 +00:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
2006-09-17 23:02:52 +00:00
|
|
|
switch (p->token) {
|
|
|
|
case GREP_PATTERN: /* atom */
|
2006-09-20 19:39:46 +00:00
|
|
|
case GREP_PATTERN_HEAD:
|
|
|
|
case GREP_PATTERN_BODY:
|
2006-09-17 23:02:52 +00:00
|
|
|
x = xcalloc(1, sizeof (struct grep_expr));
|
|
|
|
x->node = GREP_NODE_ATOM;
|
|
|
|
x->u.atom = p;
|
|
|
|
*list = p->next;
|
|
|
|
return x;
|
|
|
|
case GREP_OPEN_PAREN:
|
|
|
|
*list = p->next;
|
2006-09-28 00:50:52 +00:00
|
|
|
x = compile_pattern_or(list);
|
2006-09-17 23:02:52 +00:00
|
|
|
if (!*list || (*list)->token != GREP_CLOSE_PAREN)
|
|
|
|
die("unmatched parenthesis");
|
|
|
|
*list = (*list)->next;
|
|
|
|
return x;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct grep_expr *compile_pattern_not(struct grep_pat **list)
|
|
|
|
{
|
|
|
|
struct grep_pat *p;
|
|
|
|
struct grep_expr *x;
|
|
|
|
|
|
|
|
p = *list;
|
2009-04-27 18:10:24 +00:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
2006-09-17 23:02:52 +00:00
|
|
|
switch (p->token) {
|
|
|
|
case GREP_NOT:
|
|
|
|
if (!p->next)
|
|
|
|
die("--not not followed by pattern expression");
|
|
|
|
*list = p->next;
|
|
|
|
x = xcalloc(1, sizeof (struct grep_expr));
|
|
|
|
x->node = GREP_NODE_NOT;
|
|
|
|
x->u.unary = compile_pattern_not(list);
|
|
|
|
if (!x->u.unary)
|
|
|
|
die("--not followed by non pattern expression");
|
|
|
|
return x;
|
|
|
|
default:
|
|
|
|
return compile_pattern_atom(list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct grep_expr *compile_pattern_and(struct grep_pat **list)
|
|
|
|
{
|
|
|
|
struct grep_pat *p;
|
|
|
|
struct grep_expr *x, *y, *z;
|
|
|
|
|
|
|
|
x = compile_pattern_not(list);
|
|
|
|
p = *list;
|
|
|
|
if (p && p->token == GREP_AND) {
|
|
|
|
if (!p->next)
|
|
|
|
die("--and not followed by pattern expression");
|
|
|
|
*list = p->next;
|
|
|
|
y = compile_pattern_and(list);
|
|
|
|
if (!y)
|
|
|
|
die("--and not followed by pattern expression");
|
|
|
|
z = xcalloc(1, sizeof (struct grep_expr));
|
|
|
|
z->node = GREP_NODE_AND;
|
|
|
|
z->u.binary.left = x;
|
|
|
|
z->u.binary.right = y;
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct grep_expr *compile_pattern_or(struct grep_pat **list)
|
|
|
|
{
|
|
|
|
struct grep_pat *p;
|
|
|
|
struct grep_expr *x, *y, *z;
|
|
|
|
|
|
|
|
x = compile_pattern_and(list);
|
|
|
|
p = *list;
|
|
|
|
if (x && p && p->token != GREP_CLOSE_PAREN) {
|
|
|
|
y = compile_pattern_or(list);
|
|
|
|
if (!y)
|
|
|
|
die("not a pattern expression %s", p->pattern);
|
|
|
|
z = xcalloc(1, sizeof (struct grep_expr));
|
|
|
|
z->node = GREP_NODE_OR;
|
|
|
|
z->u.binary.left = x;
|
|
|
|
z->u.binary.right = y;
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
|
|
|
|
{
|
|
|
|
return compile_pattern_or(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void compile_grep_patterns(struct grep_opt *opt)
|
|
|
|
{
|
|
|
|
struct grep_pat *p;
|
"log --author=me --grep=it" should find intersection, not union
Historically, any grep filter in "git log" family of commands were taken
as restricting to commits with any of the words in the commit log message.
However, the user almost always want to find commits "done by this person
on that topic". With "--all-match" option, a series of grep patterns can
be turned into a requirement that all of them must produce a match, but
that makes it impossible to ask for "done by me, on either this or that"
with:
log --author=me --committer=him --grep=this --grep=that
because it will require both "this" and "that" to appear.
Change the "header" parser of grep library to treat the headers specially,
and parse it as:
(all-match-OR (HEADER-AUTHOR me)
(HEADER-COMMITTER him)
(OR
(PATTERN this)
(PATTERN that) ) )
Even though the "log" command line parser doesn't give direct access to
the extended grep syntax to group terms with parentheses, this change will
cover the majority of the case the users would want.
This incidentally revealed that one test in t7002 was bogus. It ran:
log --author=Thor --grep=Thu --format='%s'
and expected (wrongly) "Thu" to match "Thursday" in the author/committer
date, but that would never match, as the timestamp in raw commit buffer
does not have the name of the day-of-the-week.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-01-18 04:09:06 +00:00
|
|
|
struct grep_expr *header_expr = NULL;
|
2006-09-17 23:02:52 +00:00
|
|
|
|
"log --author=me --grep=it" should find intersection, not union
Historically, any grep filter in "git log" family of commands were taken
as restricting to commits with any of the words in the commit log message.
However, the user almost always want to find commits "done by this person
on that topic". With "--all-match" option, a series of grep patterns can
be turned into a requirement that all of them must produce a match, but
that makes it impossible to ask for "done by me, on either this or that"
with:
log --author=me --committer=him --grep=this --grep=that
because it will require both "this" and "that" to appear.
Change the "header" parser of grep library to treat the headers specially,
and parse it as:
(all-match-OR (HEADER-AUTHOR me)
(HEADER-COMMITTER him)
(OR
(PATTERN this)
(PATTERN that) ) )
Even though the "log" command line parser doesn't give direct access to
the extended grep syntax to group terms with parentheses, this change will
cover the majority of the case the users would want.
This incidentally revealed that one test in t7002 was bogus. It ran:
log --author=Thor --grep=Thu --format='%s'
and expected (wrongly) "Thu" to match "Thursday" in the author/committer
date, but that would never match, as the timestamp in raw commit buffer
does not have the name of the day-of-the-week.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-01-18 04:09:06 +00:00
|
|
|
if (opt->header_list) {
|
|
|
|
p = opt->header_list;
|
|
|
|
header_expr = compile_pattern_expr(&p);
|
|
|
|
if (p)
|
|
|
|
die("incomplete pattern expression: %s", p->pattern);
|
|
|
|
for (p = opt->header_list; p; p = p->next) {
|
|
|
|
switch (p->token) {
|
|
|
|
case GREP_PATTERN: /* atom */
|
|
|
|
case GREP_PATTERN_HEAD:
|
|
|
|
case GREP_PATTERN_BODY:
|
|
|
|
compile_regexp(p, opt);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
opt->extended = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-09-28 00:50:52 +00:00
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
for (p = opt->pattern_list; p; p = p->next) {
|
2006-09-20 19:39:46 +00:00
|
|
|
switch (p->token) {
|
|
|
|
case GREP_PATTERN: /* atom */
|
|
|
|
case GREP_PATTERN_HEAD:
|
|
|
|
case GREP_PATTERN_BODY:
|
2009-01-09 23:18:34 +00:00
|
|
|
compile_regexp(p, opt);
|
2006-09-20 19:39:46 +00:00
|
|
|
break;
|
|
|
|
default:
|
2006-09-17 23:02:52 +00:00
|
|
|
opt->extended = 1;
|
2006-09-20 19:39:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-09-17 23:02:52 +00:00
|
|
|
}
|
|
|
|
|
"log --author=me --grep=it" should find intersection, not union
Historically, any grep filter in "git log" family of commands were taken
as restricting to commits with any of the words in the commit log message.
However, the user almost always want to find commits "done by this person
on that topic". With "--all-match" option, a series of grep patterns can
be turned into a requirement that all of them must produce a match, but
that makes it impossible to ask for "done by me, on either this or that"
with:
log --author=me --committer=him --grep=this --grep=that
because it will require both "this" and "that" to appear.
Change the "header" parser of grep library to treat the headers specially,
and parse it as:
(all-match-OR (HEADER-AUTHOR me)
(HEADER-COMMITTER him)
(OR
(PATTERN this)
(PATTERN that) ) )
Even though the "log" command line parser doesn't give direct access to
the extended grep syntax to group terms with parentheses, this change will
cover the majority of the case the users would want.
This incidentally revealed that one test in t7002 was bogus. It ran:
log --author=Thor --grep=Thu --format='%s'
and expected (wrongly) "Thu" to match "Thursday" in the author/committer
date, but that would never match, as the timestamp in raw commit buffer
does not have the name of the day-of-the-week.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-01-18 04:09:06 +00:00
|
|
|
if (opt->all_match || header_expr)
|
|
|
|
opt->extended = 1;
|
|
|
|
else if (!opt->extended)
|
2006-09-17 23:02:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Then bundle them up in an expression.
|
|
|
|
* A classic recursive descent parser would do.
|
|
|
|
*/
|
|
|
|
p = opt->pattern_list;
|
2009-03-18 20:53:27 +00:00
|
|
|
if (p)
|
|
|
|
opt->pattern_expression = compile_pattern_expr(&p);
|
2006-09-17 23:02:52 +00:00
|
|
|
if (p)
|
|
|
|
die("incomplete pattern expression: %s", p->pattern);
|
"log --author=me --grep=it" should find intersection, not union
Historically, any grep filter in "git log" family of commands were taken
as restricting to commits with any of the words in the commit log message.
However, the user almost always want to find commits "done by this person
on that topic". With "--all-match" option, a series of grep patterns can
be turned into a requirement that all of them must produce a match, but
that makes it impossible to ask for "done by me, on either this or that"
with:
log --author=me --committer=him --grep=this --grep=that
because it will require both "this" and "that" to appear.
Change the "header" parser of grep library to treat the headers specially,
and parse it as:
(all-match-OR (HEADER-AUTHOR me)
(HEADER-COMMITTER him)
(OR
(PATTERN this)
(PATTERN that) ) )
Even though the "log" command line parser doesn't give direct access to
the extended grep syntax to group terms with parentheses, this change will
cover the majority of the case the users would want.
This incidentally revealed that one test in t7002 was bogus. It ran:
log --author=Thor --grep=Thu --format='%s'
and expected (wrongly) "Thu" to match "Thursday" in the author/committer
date, but that would never match, as the timestamp in raw commit buffer
does not have the name of the day-of-the-week.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-01-18 04:09:06 +00:00
|
|
|
|
|
|
|
if (!header_expr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (opt->pattern_expression) {
|
|
|
|
struct grep_expr *z;
|
|
|
|
z = xcalloc(1, sizeof(*z));
|
|
|
|
z->node = GREP_NODE_OR;
|
|
|
|
z->u.binary.left = opt->pattern_expression;
|
|
|
|
z->u.binary.right = header_expr;
|
|
|
|
opt->pattern_expression = z;
|
|
|
|
} else {
|
|
|
|
opt->pattern_expression = header_expr;
|
|
|
|
}
|
|
|
|
opt->all_match = 1;
|
2006-09-17 23:02:52 +00:00
|
|
|
}
|
|
|
|
|
2006-09-27 23:27:10 +00:00
|
|
|
static void free_pattern_expr(struct grep_expr *x)
|
|
|
|
{
|
|
|
|
switch (x->node) {
|
|
|
|
case GREP_NODE_ATOM:
|
|
|
|
break;
|
|
|
|
case GREP_NODE_NOT:
|
|
|
|
free_pattern_expr(x->u.unary);
|
|
|
|
break;
|
|
|
|
case GREP_NODE_AND:
|
|
|
|
case GREP_NODE_OR:
|
|
|
|
free_pattern_expr(x->u.binary.left);
|
|
|
|
free_pattern_expr(x->u.binary.right);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_grep_patterns(struct grep_opt *opt)
|
|
|
|
{
|
|
|
|
struct grep_pat *p, *n;
|
|
|
|
|
|
|
|
for (p = opt->pattern_list; p; p = n) {
|
|
|
|
n = p->next;
|
|
|
|
switch (p->token) {
|
|
|
|
case GREP_PATTERN: /* atom */
|
|
|
|
case GREP_PATTERN_HEAD:
|
|
|
|
case GREP_PATTERN_BODY:
|
|
|
|
regfree(&p->regexp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opt->extended)
|
|
|
|
return;
|
|
|
|
free_pattern_expr(opt->pattern_expression);
|
|
|
|
}
|
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
static char *end_of_line(char *cp, unsigned long *left)
|
|
|
|
{
|
|
|
|
unsigned long l = *left;
|
|
|
|
while (l && *cp != '\n') {
|
|
|
|
l--;
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
*left = l;
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int word_char(char ch)
|
|
|
|
{
|
|
|
|
return isalnum(ch) || ch == '_';
|
|
|
|
}
|
|
|
|
|
grep: Colorize filename, line number, and separator
Colorize the filename, line number, and separator in git grep output, as
GNU grep does. The colors are customizable through color.grep.<slot>.
The default is to only color the separator (in cyan), since this gives
the biggest legibility increase without overwhelming the user with
colors. GNU grep also defaults cyan for the separator, but defaults to
magenta for the filename and to green for the line number, as well.
There is one difference from GNU grep: When a binary file matches
without -a, GNU grep does not color the <file> in "Binary file <file>
matches", but we do.
Like GNU grep, if --null is given, the null separators are not colored.
For config.txt, use a a sub-list to describe the slots, rather than
a single paragraph with parentheses, since this is much more readable.
Remove the cast to int for `rm_eo - rm_so` since it is not necessary.
Signed-off-by: Mark Lodato <lodatom@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-03-07 16:52:46 +00:00
|
|
|
static void output_color(struct grep_opt *opt, const void *data, size_t size,
|
|
|
|
const char *color)
|
|
|
|
{
|
|
|
|
if (opt->color && color && color[0]) {
|
|
|
|
opt->output(opt, color, strlen(color));
|
|
|
|
opt->output(opt, data, size);
|
|
|
|
opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
|
|
|
|
} else
|
|
|
|
opt->output(opt, data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void output_sep(struct grep_opt *opt, char sign)
|
|
|
|
{
|
|
|
|
if (opt->null_following_name)
|
|
|
|
opt->output(opt, "\0", 1);
|
|
|
|
else
|
|
|
|
output_color(opt, &sign, 1, opt->color_sep);
|
|
|
|
}
|
|
|
|
|
2008-10-01 16:11:15 +00:00
|
|
|
static void show_name(struct grep_opt *opt, const char *name)
|
|
|
|
{
|
grep: Colorize filename, line number, and separator
Colorize the filename, line number, and separator in git grep output, as
GNU grep does. The colors are customizable through color.grep.<slot>.
The default is to only color the separator (in cyan), since this gives
the biggest legibility increase without overwhelming the user with
colors. GNU grep also defaults cyan for the separator, but defaults to
magenta for the filename and to green for the line number, as well.
There is one difference from GNU grep: When a binary file matches
without -a, GNU grep does not color the <file> in "Binary file <file>
matches", but we do.
Like GNU grep, if --null is given, the null separators are not colored.
For config.txt, use a a sub-list to describe the slots, rather than
a single paragraph with parentheses, since this is much more readable.
Remove the cast to int for `rm_eo - rm_so` since it is not necessary.
Signed-off-by: Mark Lodato <lodatom@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-03-07 16:52:46 +00:00
|
|
|
output_color(opt, name, strlen(name), opt->color_filename);
|
2010-01-25 22:51:39 +00:00
|
|
|
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
|
2008-10-01 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
2009-11-06 09:22:35 +00:00
|
|
|
|
|
|
|
static int fixmatch(const char *pattern, char *line, int ignore_case, regmatch_t *match)
|
2006-09-17 23:02:52 +00:00
|
|
|
{
|
2009-11-06 09:22:35 +00:00
|
|
|
char *hit;
|
|
|
|
if (ignore_case)
|
|
|
|
hit = strcasestr(line, pattern);
|
|
|
|
else
|
|
|
|
hit = strstr(line, pattern);
|
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
if (!hit) {
|
|
|
|
match->rm_so = match->rm_eo = -1;
|
|
|
|
return REG_NOMATCH;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
match->rm_so = hit - line;
|
|
|
|
match->rm_eo = match->rm_so + strlen(pattern);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
log --author/--committer: really match only with name part
When we tried to find commits done by AUTHOR, the first implementation
tried to pattern match a line with "^author .*AUTHOR", which later was
enhanced to strip leading caret and look for "^author AUTHOR" when the
search pattern was anchored at the left end (i.e. --author="^AUTHOR").
This had a few problems:
* When looking for fixed strings (e.g. "git log -F --author=x --grep=y"),
the regexp internally used "^author .*x" would never match anything;
* To match at the end (e.g. "git log --author='google.com>$'"), the
generated regexp has to also match the trailing timestamp part the
commit header lines have. Also, in order to determine if the '$' at
the end means "match at the end of the line" or just a literal dollar
sign (probably backslash-quoted), we would need to parse the regexp
ourselves.
An earlier alternative tried to make sure that a line matches "^author "
(to limit by field name) and the user supplied pattern at the same time.
While it solved the -F problem by introducing a special override for
matching the "^author ", it did not solve the trailing timestamp nor tail
match problem. It also would have matched every commit if --author=author
was asked for, not because the author's email part had this string, but
because every commit header line that talks about the author begins with
that field name, regardleses of who wrote it.
Instead of piling more hacks on top of hacks, this rethinks the grep
machinery that is used to look for strings in the commit header, and makes
sure that (1) field name matches literally at the beginning of the line,
followed by a SP, and (2) the user supplied pattern is matched against the
remainder of the line, excluding the trailing timestamp data.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-09-05 05:15:02 +00:00
|
|
|
static int strip_timestamp(char *bol, char **eol_p)
|
|
|
|
{
|
|
|
|
char *eol = *eol_p;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while (bol < --eol) {
|
|
|
|
if (*eol != '>')
|
|
|
|
continue;
|
|
|
|
*eol_p = ++eol;
|
|
|
|
ch = *eol;
|
|
|
|
*eol = '\0';
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *field;
|
|
|
|
size_t len;
|
|
|
|
} header_field[] = {
|
|
|
|
{ "author ", 7 },
|
|
|
|
{ "committer ", 10 },
|
|
|
|
};
|
|
|
|
|
2009-03-07 12:28:40 +00:00
|
|
|
static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
|
2009-03-07 12:30:27 +00:00
|
|
|
enum grep_context ctx,
|
|
|
|
regmatch_t *pmatch, int eflags)
|
2006-09-17 23:02:52 +00:00
|
|
|
{
|
|
|
|
int hit = 0;
|
log --author/--committer: really match only with name part
When we tried to find commits done by AUTHOR, the first implementation
tried to pattern match a line with "^author .*AUTHOR", which later was
enhanced to strip leading caret and look for "^author AUTHOR" when the
search pattern was anchored at the left end (i.e. --author="^AUTHOR").
This had a few problems:
* When looking for fixed strings (e.g. "git log -F --author=x --grep=y"),
the regexp internally used "^author .*x" would never match anything;
* To match at the end (e.g. "git log --author='google.com>$'"), the
generated regexp has to also match the trailing timestamp part the
commit header lines have. Also, in order to determine if the '$' at
the end means "match at the end of the line" or just a literal dollar
sign (probably backslash-quoted), we would need to parse the regexp
ourselves.
An earlier alternative tried to make sure that a line matches "^author "
(to limit by field name) and the user supplied pattern at the same time.
While it solved the -F problem by introducing a special override for
matching the "^author ", it did not solve the trailing timestamp nor tail
match problem. It also would have matched every commit if --author=author
was asked for, not because the author's email part had this string, but
because every commit header line that talks about the author begins with
that field name, regardleses of who wrote it.
Instead of piling more hacks on top of hacks, this rethinks the grep
machinery that is used to look for strings in the commit header, and makes
sure that (1) field name matches literally at the beginning of the line,
followed by a SP, and (2) the user supplied pattern is matched against the
remainder of the line, excluding the trailing timestamp data.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-09-05 05:15:02 +00:00
|
|
|
int saved_ch = 0;
|
2009-05-20 21:31:53 +00:00
|
|
|
const char *start = bol;
|
2006-09-17 23:02:52 +00:00
|
|
|
|
2006-09-20 19:39:46 +00:00
|
|
|
if ((p->token != GREP_PATTERN) &&
|
|
|
|
((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
|
|
|
|
return 0;
|
|
|
|
|
log --author/--committer: really match only with name part
When we tried to find commits done by AUTHOR, the first implementation
tried to pattern match a line with "^author .*AUTHOR", which later was
enhanced to strip leading caret and look for "^author AUTHOR" when the
search pattern was anchored at the left end (i.e. --author="^AUTHOR").
This had a few problems:
* When looking for fixed strings (e.g. "git log -F --author=x --grep=y"),
the regexp internally used "^author .*x" would never match anything;
* To match at the end (e.g. "git log --author='google.com>$'"), the
generated regexp has to also match the trailing timestamp part the
commit header lines have. Also, in order to determine if the '$' at
the end means "match at the end of the line" or just a literal dollar
sign (probably backslash-quoted), we would need to parse the regexp
ourselves.
An earlier alternative tried to make sure that a line matches "^author "
(to limit by field name) and the user supplied pattern at the same time.
While it solved the -F problem by introducing a special override for
matching the "^author ", it did not solve the trailing timestamp nor tail
match problem. It also would have matched every commit if --author=author
was asked for, not because the author's email part had this string, but
because every commit header line that talks about the author begins with
that field name, regardleses of who wrote it.
Instead of piling more hacks on top of hacks, this rethinks the grep
machinery that is used to look for strings in the commit header, and makes
sure that (1) field name matches literally at the beginning of the line,
followed by a SP, and (2) the user supplied pattern is matched against the
remainder of the line, excluding the trailing timestamp data.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-09-05 05:15:02 +00:00
|
|
|
if (p->token == GREP_PATTERN_HEAD) {
|
|
|
|
const char *field;
|
|
|
|
size_t len;
|
|
|
|
assert(p->field < ARRAY_SIZE(header_field));
|
|
|
|
field = header_field[p->field].field;
|
|
|
|
len = header_field[p->field].len;
|
|
|
|
if (strncmp(bol, field, len))
|
|
|
|
return 0;
|
|
|
|
bol += len;
|
|
|
|
saved_ch = strip_timestamp(bol, &eol);
|
|
|
|
}
|
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
again:
|
2009-03-07 12:30:27 +00:00
|
|
|
if (p->fixed)
|
2009-11-06 09:22:35 +00:00
|
|
|
hit = !fixmatch(p->pattern, bol, p->ignore_case, pmatch);
|
2009-03-07 12:30:27 +00:00
|
|
|
else
|
|
|
|
hit = !regexec(&p->regexp, bol, 1, pmatch, eflags);
|
2006-09-17 23:02:52 +00:00
|
|
|
|
2009-03-07 12:28:40 +00:00
|
|
|
if (hit && p->word_regexp) {
|
2006-09-17 23:02:52 +00:00
|
|
|
if ((pmatch[0].rm_so < 0) ||
|
2009-06-03 16:19:01 +00:00
|
|
|
(eol - bol) < pmatch[0].rm_so ||
|
2006-09-17 23:02:52 +00:00
|
|
|
(pmatch[0].rm_eo < 0) ||
|
|
|
|
(eol - bol) < pmatch[0].rm_eo)
|
|
|
|
die("regexp returned nonsense");
|
|
|
|
|
|
|
|
/* Match beginning must be either beginning of the
|
|
|
|
* line, or at word boundary (i.e. the last char must
|
|
|
|
* not be a word char). Similarly, match end must be
|
|
|
|
* either end of the line, or at word boundary
|
|
|
|
* (i.e. the next char must not be a word char).
|
|
|
|
*/
|
2009-01-09 23:08:40 +00:00
|
|
|
if ( ((pmatch[0].rm_so == 0) ||
|
2006-09-17 23:02:52 +00:00
|
|
|
!word_char(bol[pmatch[0].rm_so-1])) &&
|
|
|
|
((pmatch[0].rm_eo == (eol-bol)) ||
|
|
|
|
!word_char(bol[pmatch[0].rm_eo])) )
|
|
|
|
;
|
|
|
|
else
|
|
|
|
hit = 0;
|
|
|
|
|
2009-06-03 16:19:01 +00:00
|
|
|
/* Words consist of at least one character. */
|
|
|
|
if (pmatch->rm_so == pmatch->rm_eo)
|
|
|
|
hit = 0;
|
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
|
|
|
|
/* There could be more than one match on the
|
|
|
|
* line, and the first match might not be
|
|
|
|
* strict word match. But later ones could be!
|
2009-01-09 23:08:40 +00:00
|
|
|
* Forward to the next possible start, i.e. the
|
|
|
|
* next position following a non-word char.
|
2006-09-17 23:02:52 +00:00
|
|
|
*/
|
|
|
|
bol = pmatch[0].rm_so + bol + 1;
|
2009-01-09 23:08:40 +00:00
|
|
|
while (word_char(bol[-1]) && bol < eol)
|
|
|
|
bol++;
|
2009-05-23 11:45:26 +00:00
|
|
|
eflags |= REG_NOTBOL;
|
2009-01-09 23:08:40 +00:00
|
|
|
if (bol < eol)
|
|
|
|
goto again;
|
2006-09-17 23:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
log --author/--committer: really match only with name part
When we tried to find commits done by AUTHOR, the first implementation
tried to pattern match a line with "^author .*AUTHOR", which later was
enhanced to strip leading caret and look for "^author AUTHOR" when the
search pattern was anchored at the left end (i.e. --author="^AUTHOR").
This had a few problems:
* When looking for fixed strings (e.g. "git log -F --author=x --grep=y"),
the regexp internally used "^author .*x" would never match anything;
* To match at the end (e.g. "git log --author='google.com>$'"), the
generated regexp has to also match the trailing timestamp part the
commit header lines have. Also, in order to determine if the '$' at
the end means "match at the end of the line" or just a literal dollar
sign (probably backslash-quoted), we would need to parse the regexp
ourselves.
An earlier alternative tried to make sure that a line matches "^author "
(to limit by field name) and the user supplied pattern at the same time.
While it solved the -F problem by introducing a special override for
matching the "^author ", it did not solve the trailing timestamp nor tail
match problem. It also would have matched every commit if --author=author
was asked for, not because the author's email part had this string, but
because every commit header line that talks about the author begins with
that field name, regardleses of who wrote it.
Instead of piling more hacks on top of hacks, this rethinks the grep
machinery that is used to look for strings in the commit header, and makes
sure that (1) field name matches literally at the beginning of the line,
followed by a SP, and (2) the user supplied pattern is matched against the
remainder of the line, excluding the trailing timestamp data.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-09-05 05:15:02 +00:00
|
|
|
if (p->token == GREP_PATTERN_HEAD && saved_ch)
|
|
|
|
*eol = saved_ch;
|
2009-05-20 21:31:53 +00:00
|
|
|
if (hit) {
|
|
|
|
pmatch[0].rm_so += bol - start;
|
|
|
|
pmatch[0].rm_eo += bol - start;
|
|
|
|
}
|
2006-09-17 23:02:52 +00:00
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
|
2009-03-07 12:28:40 +00:00
|
|
|
static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
|
|
|
|
enum grep_context ctx, int collect_hits)
|
2006-09-17 23:02:52 +00:00
|
|
|
{
|
2006-09-28 00:50:52 +00:00
|
|
|
int h = 0;
|
2009-03-07 12:30:27 +00:00
|
|
|
regmatch_t match;
|
2006-09-28 00:50:52 +00:00
|
|
|
|
2009-04-27 18:10:24 +00:00
|
|
|
if (!x)
|
|
|
|
die("Not a valid grep expression");
|
2006-09-17 23:02:52 +00:00
|
|
|
switch (x->node) {
|
|
|
|
case GREP_NODE_ATOM:
|
2009-03-07 12:30:27 +00:00
|
|
|
h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
|
2006-09-17 23:02:52 +00:00
|
|
|
break;
|
|
|
|
case GREP_NODE_NOT:
|
2009-03-07 12:28:40 +00:00
|
|
|
h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
|
2006-09-28 00:50:52 +00:00
|
|
|
break;
|
2006-09-17 23:02:52 +00:00
|
|
|
case GREP_NODE_AND:
|
2009-03-07 12:28:40 +00:00
|
|
|
if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
|
2009-03-07 12:27:15 +00:00
|
|
|
return 0;
|
2009-03-07 12:28:40 +00:00
|
|
|
h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
|
2006-09-28 00:50:52 +00:00
|
|
|
break;
|
2006-09-17 23:02:52 +00:00
|
|
|
case GREP_NODE_OR:
|
2006-09-28 00:50:52 +00:00
|
|
|
if (!collect_hits)
|
2009-03-07 12:28:40 +00:00
|
|
|
return (match_expr_eval(x->u.binary.left,
|
2006-09-28 00:50:52 +00:00
|
|
|
bol, eol, ctx, 0) ||
|
2009-03-07 12:28:40 +00:00
|
|
|
match_expr_eval(x->u.binary.right,
|
2006-09-28 00:50:52 +00:00
|
|
|
bol, eol, ctx, 0));
|
2009-03-07 12:28:40 +00:00
|
|
|
h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
|
2006-09-28 00:50:52 +00:00
|
|
|
x->u.binary.left->hit |= h;
|
2009-03-07 12:28:40 +00:00
|
|
|
h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
|
2006-09-28 00:50:52 +00:00
|
|
|
break;
|
|
|
|
default:
|
2009-01-04 18:38:41 +00:00
|
|
|
die("Unexpected node type (internal error) %d", x->node);
|
2006-09-17 23:02:52 +00:00
|
|
|
}
|
2006-09-28 00:50:52 +00:00
|
|
|
if (collect_hits)
|
|
|
|
x->hit |= h;
|
|
|
|
return h;
|
2006-09-17 23:02:52 +00:00
|
|
|
}
|
|
|
|
|
2006-09-20 19:39:46 +00:00
|
|
|
static int match_expr(struct grep_opt *opt, char *bol, char *eol,
|
2006-09-28 00:50:52 +00:00
|
|
|
enum grep_context ctx, int collect_hits)
|
2006-09-17 23:02:52 +00:00
|
|
|
{
|
|
|
|
struct grep_expr *x = opt->pattern_expression;
|
2009-03-07 12:28:40 +00:00
|
|
|
return match_expr_eval(x, bol, eol, ctx, collect_hits);
|
2006-09-17 23:02:52 +00:00
|
|
|
}
|
|
|
|
|
2006-09-20 19:39:46 +00:00
|
|
|
static int match_line(struct grep_opt *opt, char *bol, char *eol,
|
2006-09-28 00:50:52 +00:00
|
|
|
enum grep_context ctx, int collect_hits)
|
2006-09-17 23:02:52 +00:00
|
|
|
{
|
|
|
|
struct grep_pat *p;
|
2009-03-07 12:30:27 +00:00
|
|
|
regmatch_t match;
|
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
if (opt->extended)
|
2006-09-28 00:50:52 +00:00
|
|
|
return match_expr(opt, bol, eol, ctx, collect_hits);
|
|
|
|
|
|
|
|
/* we do not call with collect_hits without being extended */
|
2006-09-17 23:02:52 +00:00
|
|
|
for (p = opt->pattern_list; p; p = p->next) {
|
2009-03-07 12:30:27 +00:00
|
|
|
if (match_one_pattern(p, bol, eol, ctx, &match, 0))
|
2006-09-17 23:02:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-07 12:32:32 +00:00
|
|
|
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;
|
2010-03-07 16:52:47 +00:00
|
|
|
char *line_color = NULL;
|
2009-03-07 12:32:32 +00:00
|
|
|
|
2009-07-01 22:06:34 +00:00
|
|
|
if (opt->pre_context || opt->post_context) {
|
2009-07-01 22:03:44 +00:00
|
|
|
if (opt->last_shown == 0) {
|
grep: Colorize filename, line number, and separator
Colorize the filename, line number, and separator in git grep output, as
GNU grep does. The colors are customizable through color.grep.<slot>.
The default is to only color the separator (in cyan), since this gives
the biggest legibility increase without overwhelming the user with
colors. GNU grep also defaults cyan for the separator, but defaults to
magenta for the filename and to green for the line number, as well.
There is one difference from GNU grep: When a binary file matches
without -a, GNU grep does not color the <file> in "Binary file <file>
matches", but we do.
Like GNU grep, if --null is given, the null separators are not colored.
For config.txt, use a a sub-list to describe the slots, rather than
a single paragraph with parentheses, since this is much more readable.
Remove the cast to int for `rm_eo - rm_so` since it is not necessary.
Signed-off-by: Mark Lodato <lodatom@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-03-07 16:52:46 +00:00
|
|
|
if (opt->show_hunk_mark) {
|
|
|
|
output_color(opt, "--", 2, opt->color_sep);
|
|
|
|
opt->output(opt, "\n", 1);
|
|
|
|
} else
|
2009-07-01 22:03:44 +00:00
|
|
|
opt->show_hunk_mark = 1;
|
grep: Colorize filename, line number, and separator
Colorize the filename, line number, and separator in git grep output, as
GNU grep does. The colors are customizable through color.grep.<slot>.
The default is to only color the separator (in cyan), since this gives
the biggest legibility increase without overwhelming the user with
colors. GNU grep also defaults cyan for the separator, but defaults to
magenta for the filename and to green for the line number, as well.
There is one difference from GNU grep: When a binary file matches
without -a, GNU grep does not color the <file> in "Binary file <file>
matches", but we do.
Like GNU grep, if --null is given, the null separators are not colored.
For config.txt, use a a sub-list to describe the slots, rather than
a single paragraph with parentheses, since this is much more readable.
Remove the cast to int for `rm_eo - rm_so` since it is not necessary.
Signed-off-by: Mark Lodato <lodatom@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-03-07 16:52:46 +00:00
|
|
|
} else if (lno > opt->last_shown + 1) {
|
|
|
|
output_color(opt, "--", 2, opt->color_sep);
|
|
|
|
opt->output(opt, "\n", 1);
|
|
|
|
}
|
2009-07-01 22:02:38 +00:00
|
|
|
}
|
|
|
|
opt->last_shown = lno;
|
|
|
|
|
2010-01-25 22:51:39 +00:00
|
|
|
if (opt->pathname) {
|
grep: Colorize filename, line number, and separator
Colorize the filename, line number, and separator in git grep output, as
GNU grep does. The colors are customizable through color.grep.<slot>.
The default is to only color the separator (in cyan), since this gives
the biggest legibility increase without overwhelming the user with
colors. GNU grep also defaults cyan for the separator, but defaults to
magenta for the filename and to green for the line number, as well.
There is one difference from GNU grep: When a binary file matches
without -a, GNU grep does not color the <file> in "Binary file <file>
matches", but we do.
Like GNU grep, if --null is given, the null separators are not colored.
For config.txt, use a a sub-list to describe the slots, rather than
a single paragraph with parentheses, since this is much more readable.
Remove the cast to int for `rm_eo - rm_so` since it is not necessary.
Signed-off-by: Mark Lodato <lodatom@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-03-07 16:52:46 +00:00
|
|
|
output_color(opt, name, strlen(name), opt->color_filename);
|
|
|
|
output_sep(opt, sign);
|
2010-01-25 22:51:39 +00:00
|
|
|
}
|
|
|
|
if (opt->linenum) {
|
|
|
|
char buf[32];
|
|
|
|
snprintf(buf, sizeof(buf), "%d", lno);
|
grep: Colorize filename, line number, and separator
Colorize the filename, line number, and separator in git grep output, as
GNU grep does. The colors are customizable through color.grep.<slot>.
The default is to only color the separator (in cyan), since this gives
the biggest legibility increase without overwhelming the user with
colors. GNU grep also defaults cyan for the separator, but defaults to
magenta for the filename and to green for the line number, as well.
There is one difference from GNU grep: When a binary file matches
without -a, GNU grep does not color the <file> in "Binary file <file>
matches", but we do.
Like GNU grep, if --null is given, the null separators are not colored.
For config.txt, use a a sub-list to describe the slots, rather than
a single paragraph with parentheses, since this is much more readable.
Remove the cast to int for `rm_eo - rm_so` since it is not necessary.
Signed-off-by: Mark Lodato <lodatom@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-03-07 16:52:46 +00:00
|
|
|
output_color(opt, buf, strlen(buf), opt->color_lineno);
|
|
|
|
output_sep(opt, sign);
|
2010-01-25 22:51:39 +00:00
|
|
|
}
|
2009-03-07 12:32:32 +00:00
|
|
|
if (opt->color) {
|
|
|
|
regmatch_t match;
|
|
|
|
enum grep_context ctx = GREP_CONTEXT_BODY;
|
|
|
|
int ch = *eol;
|
|
|
|
int eflags = 0;
|
|
|
|
|
2010-03-07 16:52:47 +00:00
|
|
|
if (sign == ':')
|
|
|
|
line_color = opt->color_selected;
|
|
|
|
else if (sign == '-')
|
|
|
|
line_color = opt->color_context;
|
|
|
|
else if (sign == '=')
|
|
|
|
line_color = opt->color_function;
|
2009-03-07 12:32:32 +00:00
|
|
|
*eol = '\0';
|
|
|
|
while (next_match(opt, bol, eol, ctx, &match, eflags)) {
|
2009-06-01 21:53:05 +00:00
|
|
|
if (match.rm_so == match.rm_eo)
|
|
|
|
break;
|
2010-01-25 22:51:39 +00:00
|
|
|
|
2010-03-07 16:52:47 +00:00
|
|
|
output_color(opt, bol, match.rm_so, line_color);
|
grep: Colorize filename, line number, and separator
Colorize the filename, line number, and separator in git grep output, as
GNU grep does. The colors are customizable through color.grep.<slot>.
The default is to only color the separator (in cyan), since this gives
the biggest legibility increase without overwhelming the user with
colors. GNU grep also defaults cyan for the separator, but defaults to
magenta for the filename and to green for the line number, as well.
There is one difference from GNU grep: When a binary file matches
without -a, GNU grep does not color the <file> in "Binary file <file>
matches", but we do.
Like GNU grep, if --null is given, the null separators are not colored.
For config.txt, use a a sub-list to describe the slots, rather than
a single paragraph with parentheses, since this is much more readable.
Remove the cast to int for `rm_eo - rm_so` since it is not necessary.
Signed-off-by: Mark Lodato <lodatom@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-03-07 16:52:46 +00:00
|
|
|
output_color(opt, bol + match.rm_so,
|
|
|
|
match.rm_eo - match.rm_so,
|
|
|
|
opt->color_match);
|
2009-03-07 12:32:32 +00:00
|
|
|
bol += match.rm_eo;
|
|
|
|
rest -= match.rm_eo;
|
|
|
|
eflags = REG_NOTBOL;
|
|
|
|
}
|
|
|
|
*eol = ch;
|
|
|
|
}
|
2010-03-07 16:52:47 +00:00
|
|
|
output_color(opt, bol, rest, line_color);
|
2010-01-25 22:51:39 +00:00
|
|
|
opt->output(opt, "\n", 1);
|
2009-03-07 12:32:32 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:07:24 +00:00
|
|
|
static int match_funcname(struct grep_opt *opt, char *bol, char *eol)
|
2009-07-01 22:06:34 +00:00
|
|
|
{
|
2009-07-01 22:07:24 +00:00
|
|
|
xdemitconf_t *xecfg = opt->priv;
|
|
|
|
if (xecfg && xecfg->find_func) {
|
|
|
|
char buf[1];
|
|
|
|
return xecfg->find_func(bol, eol - bol, buf, 1,
|
|
|
|
xecfg->find_func_priv) >= 0;
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:06:34 +00:00
|
|
|
if (bol == eol)
|
|
|
|
return 0;
|
|
|
|
if (isalpha(*bol) || *bol == '_' || *bol == '$')
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_funcname_line(struct grep_opt *opt, const char *name,
|
|
|
|
char *buf, char *bol, unsigned lno)
|
|
|
|
{
|
|
|
|
while (bol > buf) {
|
|
|
|
char *eol = --bol;
|
|
|
|
|
|
|
|
while (bol > buf && bol[-1] != '\n')
|
|
|
|
bol--;
|
|
|
|
lno--;
|
|
|
|
|
|
|
|
if (lno <= opt->last_shown)
|
|
|
|
break;
|
|
|
|
|
2009-07-01 22:07:24 +00:00
|
|
|
if (match_funcname(opt, bol, eol)) {
|
2009-07-01 22:06:34 +00:00
|
|
|
show_line(opt, bol, eol, name, lno, '=');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:05:17 +00:00
|
|
|
static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
|
|
|
|
char *bol, unsigned lno)
|
|
|
|
{
|
2009-07-01 22:06:34 +00:00
|
|
|
unsigned cur = lno, from = 1, funcname_lno = 0;
|
|
|
|
int funcname_needed = opt->funcname;
|
2009-07-01 22:05:17 +00:00
|
|
|
|
|
|
|
if (opt->pre_context < lno)
|
|
|
|
from = lno - opt->pre_context;
|
|
|
|
if (from <= opt->last_shown)
|
|
|
|
from = opt->last_shown + 1;
|
|
|
|
|
|
|
|
/* Rewind. */
|
|
|
|
while (bol > buf && cur > from) {
|
2009-07-01 22:06:34 +00:00
|
|
|
char *eol = --bol;
|
|
|
|
|
2009-07-01 22:05:17 +00:00
|
|
|
while (bol > buf && bol[-1] != '\n')
|
|
|
|
bol--;
|
|
|
|
cur--;
|
2009-07-01 22:07:24 +00:00
|
|
|
if (funcname_needed && match_funcname(opt, bol, eol)) {
|
2009-07-01 22:06:34 +00:00
|
|
|
funcname_lno = cur;
|
|
|
|
funcname_needed = 0;
|
|
|
|
}
|
2009-07-01 22:05:17 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 22:06:34 +00:00
|
|
|
/* We need to look even further back to find a function signature. */
|
|
|
|
if (opt->funcname && funcname_needed)
|
|
|
|
show_funcname_line(opt, name, buf, bol, cur);
|
|
|
|
|
2009-07-01 22:05:17 +00:00
|
|
|
/* Back forward. */
|
|
|
|
while (cur < lno) {
|
2009-07-01 22:06:34 +00:00
|
|
|
char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
|
2009-07-01 22:05:17 +00:00
|
|
|
|
|
|
|
while (*eol != '\n')
|
|
|
|
eol++;
|
2009-07-01 22:06:34 +00:00
|
|
|
show_line(opt, bol, eol, name, cur, sign);
|
2009-07-01 22:05:17 +00:00
|
|
|
bol = eol + 1;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-11 06:39:36 +00:00
|
|
|
static int should_lookahead(struct grep_opt *opt)
|
|
|
|
{
|
|
|
|
struct grep_pat *p;
|
|
|
|
|
|
|
|
if (opt->extended)
|
|
|
|
return 0; /* punt for too complex stuff */
|
|
|
|
if (opt->invert)
|
|
|
|
return 0;
|
|
|
|
for (p = opt->pattern_list; p; p = p->next) {
|
|
|
|
if (p->token != GREP_PATTERN)
|
|
|
|
return 0; /* punt for "header only" and stuff */
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int look_ahead(struct grep_opt *opt,
|
|
|
|
unsigned long *left_p,
|
|
|
|
unsigned *lno_p,
|
|
|
|
char **bol_p)
|
|
|
|
{
|
|
|
|
unsigned lno = *lno_p;
|
|
|
|
char *bol = *bol_p;
|
|
|
|
struct grep_pat *p;
|
|
|
|
char *sp, *last_bol;
|
|
|
|
regoff_t earliest = -1;
|
|
|
|
|
|
|
|
for (p = opt->pattern_list; p; p = p->next) {
|
|
|
|
int hit;
|
|
|
|
regmatch_t m;
|
|
|
|
|
|
|
|
if (p->fixed)
|
2010-01-12 08:56:15 +00:00
|
|
|
hit = !fixmatch(p->pattern, bol, p->ignore_case, &m);
|
2010-01-26 17:48:36 +00:00
|
|
|
else {
|
|
|
|
#ifdef REG_STARTEND
|
|
|
|
m.rm_so = 0;
|
|
|
|
m.rm_eo = *left_p;
|
|
|
|
hit = !regexec(&p->regexp, bol, 1, &m, REG_STARTEND);
|
|
|
|
#else
|
2010-01-11 06:39:36 +00:00
|
|
|
hit = !regexec(&p->regexp, bol, 1, &m, 0);
|
2010-01-26 17:48:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
2010-01-11 06:39:36 +00:00
|
|
|
if (!hit || m.rm_so < 0 || m.rm_eo < 0)
|
|
|
|
continue;
|
|
|
|
if (earliest < 0 || m.rm_so < earliest)
|
|
|
|
earliest = m.rm_so;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (earliest < 0) {
|
|
|
|
*bol_p = bol + *left_p;
|
|
|
|
*left_p = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
|
|
|
|
; /* find the beginning of the line */
|
|
|
|
last_bol = sp;
|
|
|
|
|
|
|
|
for (sp = bol; sp < last_bol; sp++) {
|
|
|
|
if (*sp == '\n')
|
|
|
|
lno++;
|
|
|
|
}
|
|
|
|
*left_p -= last_bol - bol;
|
|
|
|
*bol_p = last_bol;
|
|
|
|
*lno_p = lno;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-25 22:51:39 +00:00
|
|
|
int grep_threads_ok(const struct grep_opt *opt)
|
|
|
|
{
|
|
|
|
/* If this condition is true, then we may use the attribute
|
|
|
|
* machinery in grep_buffer_1. The attribute code is not
|
|
|
|
* thread safe, so we disable the use of threads.
|
|
|
|
*/
|
|
|
|
if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
|
|
|
|
!opt->name_only)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* If we are showing hunk marks, we should not do it for the
|
|
|
|
* first match. The synchronization problem we get for this
|
|
|
|
* constraint is not yet solved, so we disable threading in
|
|
|
|
* this case.
|
|
|
|
*/
|
|
|
|
if (opt->pre_context || opt->post_context)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void std_output(struct grep_opt *opt, const void *buf, size_t size)
|
|
|
|
{
|
|
|
|
fwrite(buf, size, 1, stdout);
|
|
|
|
}
|
|
|
|
|
2006-09-28 00:50:52 +00:00
|
|
|
static int grep_buffer_1(struct grep_opt *opt, const char *name,
|
|
|
|
char *buf, unsigned long size, int collect_hits)
|
2006-09-17 23:02:52 +00:00
|
|
|
{
|
|
|
|
char *bol = buf;
|
|
|
|
unsigned long left = size;
|
|
|
|
unsigned lno = 1;
|
|
|
|
unsigned last_hit = 0;
|
|
|
|
int binary_match_only = 0;
|
|
|
|
unsigned count = 0;
|
2010-01-11 06:39:36 +00:00
|
|
|
int try_lookahead = 0;
|
2006-09-20 19:39:46 +00:00
|
|
|
enum grep_context ctx = GREP_CONTEXT_HEAD;
|
2009-07-01 22:07:24 +00:00
|
|
|
xdemitconf_t xecfg;
|
2006-09-17 23:02:52 +00:00
|
|
|
|
2009-07-01 22:02:38 +00:00
|
|
|
opt->last_shown = 0;
|
|
|
|
|
2010-01-25 22:51:39 +00:00
|
|
|
if (!opt->output)
|
|
|
|
opt->output = std_output;
|
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
if (buffer_is_binary(buf, size)) {
|
|
|
|
switch (opt->binary) {
|
|
|
|
case GREP_BINARY_DEFAULT:
|
|
|
|
binary_match_only = 1;
|
|
|
|
break;
|
|
|
|
case GREP_BINARY_NOMATCH:
|
|
|
|
return 0; /* Assume unmatch */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:07:24 +00:00
|
|
|
memset(&xecfg, 0, sizeof(xecfg));
|
|
|
|
if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
|
|
|
|
!opt->name_only && !binary_match_only && !collect_hits) {
|
|
|
|
struct userdiff_driver *drv = userdiff_find_by_path(name);
|
|
|
|
if (drv && drv->funcname.pattern) {
|
|
|
|
const struct userdiff_funcname *pe = &drv->funcname;
|
|
|
|
xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
|
|
|
|
opt->priv = &xecfg;
|
|
|
|
}
|
|
|
|
}
|
2010-01-11 06:39:36 +00:00
|
|
|
try_lookahead = should_lookahead(opt);
|
2009-07-01 22:07:24 +00:00
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
while (left) {
|
|
|
|
char *eol, ch;
|
2006-09-28 00:50:52 +00:00
|
|
|
int hit;
|
2006-09-17 23:02:52 +00:00
|
|
|
|
2010-01-11 06:39:36 +00:00
|
|
|
/*
|
|
|
|
* look_ahead() skips quicly to the line that possibly
|
|
|
|
* has the next hit; don't call it if we need to do
|
|
|
|
* something more than just skipping the current line
|
|
|
|
* in response to an unmatch for the current line. E.g.
|
|
|
|
* inside a post-context window, we will show the current
|
|
|
|
* line as a context around the previous hit when it
|
|
|
|
* doesn't hit.
|
|
|
|
*/
|
|
|
|
if (try_lookahead
|
|
|
|
&& !(last_hit
|
|
|
|
&& lno <= last_hit + opt->post_context)
|
|
|
|
&& look_ahead(opt, &left, &lno, &bol))
|
|
|
|
break;
|
2006-09-17 23:02:52 +00:00
|
|
|
eol = end_of_line(bol, &left);
|
|
|
|
ch = *eol;
|
|
|
|
*eol = 0;
|
|
|
|
|
2006-09-20 19:39:46 +00:00
|
|
|
if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
|
|
|
|
ctx = GREP_CONTEXT_BODY;
|
|
|
|
|
2006-09-28 00:50:52 +00:00
|
|
|
hit = match_line(opt, bol, eol, ctx, collect_hits);
|
2006-09-17 23:02:52 +00:00
|
|
|
*eol = ch;
|
|
|
|
|
2006-09-28 00:50:52 +00:00
|
|
|
if (collect_hits)
|
|
|
|
goto next_line;
|
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
/* "grep -v -e foo -e bla" should list lines
|
|
|
|
* that do not have either, so inversion should
|
|
|
|
* be done outside.
|
|
|
|
*/
|
|
|
|
if (opt->invert)
|
|
|
|
hit = !hit;
|
|
|
|
if (opt->unmatch_name_only) {
|
|
|
|
if (hit)
|
|
|
|
return 0;
|
|
|
|
goto next_line;
|
|
|
|
}
|
|
|
|
if (hit) {
|
|
|
|
count++;
|
|
|
|
if (opt->status_only)
|
|
|
|
return 1;
|
|
|
|
if (binary_match_only) {
|
2010-01-25 22:51:39 +00:00
|
|
|
opt->output(opt, "Binary file ", 12);
|
grep: Colorize filename, line number, and separator
Colorize the filename, line number, and separator in git grep output, as
GNU grep does. The colors are customizable through color.grep.<slot>.
The default is to only color the separator (in cyan), since this gives
the biggest legibility increase without overwhelming the user with
colors. GNU grep also defaults cyan for the separator, but defaults to
magenta for the filename and to green for the line number, as well.
There is one difference from GNU grep: When a binary file matches
without -a, GNU grep does not color the <file> in "Binary file <file>
matches", but we do.
Like GNU grep, if --null is given, the null separators are not colored.
For config.txt, use a a sub-list to describe the slots, rather than
a single paragraph with parentheses, since this is much more readable.
Remove the cast to int for `rm_eo - rm_so` since it is not necessary.
Signed-off-by: Mark Lodato <lodatom@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-03-07 16:52:46 +00:00
|
|
|
output_color(opt, name, strlen(name),
|
|
|
|
opt->color_filename);
|
2010-01-25 22:51:39 +00:00
|
|
|
opt->output(opt, " matches\n", 9);
|
2006-09-17 23:02:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (opt->name_only) {
|
2008-10-01 16:11:15 +00:00
|
|
|
show_name(opt, name);
|
2006-09-17 23:02:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* Hit at this line. If we haven't shown the
|
|
|
|
* pre-context lines, we would need to show them.
|
|
|
|
* When asked to do "count", this still show
|
|
|
|
* the context which is nonsense, but the user
|
|
|
|
* deserves to get that ;-).
|
|
|
|
*/
|
2009-07-01 22:05:17 +00:00
|
|
|
if (opt->pre_context)
|
|
|
|
show_pre_context(opt, name, buf, bol, lno);
|
2009-07-01 22:06:34 +00:00
|
|
|
else if (opt->funcname)
|
|
|
|
show_funcname_line(opt, name, buf, bol, lno);
|
2006-09-17 23:02:52 +00:00
|
|
|
if (!opt->count)
|
|
|
|
show_line(opt, bol, eol, name, lno, ':');
|
2009-07-01 22:02:38 +00:00
|
|
|
last_hit = lno;
|
2006-09-17 23:02:52 +00:00
|
|
|
}
|
|
|
|
else if (last_hit &&
|
|
|
|
lno <= last_hit + opt->post_context) {
|
|
|
|
/* If the last hit is within the post context,
|
|
|
|
* we need to show this line.
|
|
|
|
*/
|
|
|
|
show_line(opt, bol, eol, name, lno, '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
next_line:
|
|
|
|
bol = eol + 1;
|
|
|
|
if (!left)
|
|
|
|
break;
|
|
|
|
left--;
|
|
|
|
lno++;
|
|
|
|
}
|
|
|
|
|
2006-09-28 00:50:52 +00:00
|
|
|
if (collect_hits)
|
|
|
|
return 0;
|
2006-09-27 23:27:10 +00:00
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
if (opt->status_only)
|
|
|
|
return 0;
|
|
|
|
if (opt->unmatch_name_only) {
|
|
|
|
/* We did not see any hit, so we want to show this */
|
2008-10-01 16:11:15 +00:00
|
|
|
show_name(opt, name);
|
2006-09-17 23:02:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-07-01 22:07:24 +00:00
|
|
|
xdiff_clear_find_func(&xecfg);
|
|
|
|
opt->priv = NULL;
|
|
|
|
|
2006-09-17 23:02:52 +00:00
|
|
|
/* NEEDSWORK:
|
|
|
|
* The real "grep -c foo *.c" gives many "bar.c:0" lines,
|
|
|
|
* which feels mostly useless but sometimes useful. Maybe
|
|
|
|
* make it another option? For now suppress them.
|
|
|
|
*/
|
2010-01-25 22:51:39 +00:00
|
|
|
if (opt->count && count) {
|
|
|
|
char buf[32];
|
grep: Colorize filename, line number, and separator
Colorize the filename, line number, and separator in git grep output, as
GNU grep does. The colors are customizable through color.grep.<slot>.
The default is to only color the separator (in cyan), since this gives
the biggest legibility increase without overwhelming the user with
colors. GNU grep also defaults cyan for the separator, but defaults to
magenta for the filename and to green for the line number, as well.
There is one difference from GNU grep: When a binary file matches
without -a, GNU grep does not color the <file> in "Binary file <file>
matches", but we do.
Like GNU grep, if --null is given, the null separators are not colored.
For config.txt, use a a sub-list to describe the slots, rather than
a single paragraph with parentheses, since this is much more readable.
Remove the cast to int for `rm_eo - rm_so` since it is not necessary.
Signed-off-by: Mark Lodato <lodatom@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-03-07 16:52:46 +00:00
|
|
|
output_color(opt, name, strlen(name), opt->color_filename);
|
|
|
|
output_sep(opt, ':');
|
|
|
|
snprintf(buf, sizeof(buf), "%u\n", count);
|
2010-01-25 22:51:39 +00:00
|
|
|
opt->output(opt, buf, strlen(buf));
|
|
|
|
}
|
2006-09-17 23:02:52 +00:00
|
|
|
return !!last_hit;
|
|
|
|
}
|
|
|
|
|
2006-09-28 00:50:52 +00:00
|
|
|
static void clr_hit_marker(struct grep_expr *x)
|
|
|
|
{
|
|
|
|
/* All-hit markers are meaningful only at the very top level
|
|
|
|
* OR node.
|
|
|
|
*/
|
|
|
|
while (1) {
|
|
|
|
x->hit = 0;
|
|
|
|
if (x->node != GREP_NODE_OR)
|
|
|
|
return;
|
|
|
|
x->u.binary.left->hit = 0;
|
|
|
|
x = x->u.binary.right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int chk_hit_marker(struct grep_expr *x)
|
|
|
|
{
|
|
|
|
/* Top level nodes have hit markers. See if they all are hits */
|
|
|
|
while (1) {
|
|
|
|
if (x->node != GREP_NODE_OR)
|
|
|
|
return x->hit;
|
|
|
|
if (!x->u.binary.left->hit)
|
|
|
|
return 0;
|
|
|
|
x = x->u.binary.right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* we do not have to do the two-pass grep when we do not check
|
|
|
|
* buffer-wide "all-match".
|
|
|
|
*/
|
|
|
|
if (!opt->all_match)
|
|
|
|
return grep_buffer_1(opt, name, buf, size, 0);
|
|
|
|
|
|
|
|
/* Otherwise the toplevel "or" terms hit a bit differently.
|
|
|
|
* We first clear hit markers from them.
|
|
|
|
*/
|
|
|
|
clr_hit_marker(opt->pattern_expression);
|
|
|
|
grep_buffer_1(opt, name, buf, size, 1);
|
|
|
|
|
|
|
|
if (!chk_hit_marker(opt->pattern_expression))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return grep_buffer_1(opt, name, buf, size, 0);
|
|
|
|
}
|