grep: support newline separated pattern list
Currently, patterns that contain newline characters don't match anything when given to git grep. Regular grep(1) interprets patterns as lists of newline separated search strings instead. Implement this functionality by creating and inserting extra grep_pat structures for patterns consisting of multiple lines when appending to the pattern lists. For simplicity, all pattern strings are duplicated. The original pattern is truncated in place to make it contain only the first line. Requested-by: Torne (Richard Coles) <torne@google.com> 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
2b3873ff34
commit
526a858a99
@ -29,7 +29,9 @@ SYNOPSIS
|
|||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
-----------
|
-----------
|
||||||
Look for specified patterns in the tracked files in the work tree, blobs
|
Look for specified patterns in the tracked files in the work tree, blobs
|
||||||
registered in the index file, or blobs in given tree objects.
|
registered in the index file, or blobs in given tree objects. Patterns
|
||||||
|
are lists of one or more search expressions separated by newline
|
||||||
|
characters. An empty string as search expression matches all lines.
|
||||||
|
|
||||||
|
|
||||||
CONFIGURATION
|
CONFIGURATION
|
||||||
|
33
grep.c
33
grep.c
@ -9,7 +9,7 @@ static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
|
|||||||
enum grep_header_field field)
|
enum grep_header_field field)
|
||||||
{
|
{
|
||||||
struct grep_pat *p = xcalloc(1, sizeof(*p));
|
struct grep_pat *p = xcalloc(1, sizeof(*p));
|
||||||
p->pattern = pat;
|
p->pattern = xmemdupz(pat, patlen);
|
||||||
p->patternlen = patlen;
|
p->patternlen = patlen;
|
||||||
p->origin = origin;
|
p->origin = origin;
|
||||||
p->no = no;
|
p->no = no;
|
||||||
@ -23,6 +23,36 @@ static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
|
|||||||
**tail = p;
|
**tail = p;
|
||||||
*tail = &p->next;
|
*tail = &p->next;
|
||||||
p->next = NULL;
|
p->next = NULL;
|
||||||
|
|
||||||
|
switch (p->token) {
|
||||||
|
case GREP_PATTERN: /* atom */
|
||||||
|
case GREP_PATTERN_HEAD:
|
||||||
|
case GREP_PATTERN_BODY:
|
||||||
|
for (;;) {
|
||||||
|
struct grep_pat *new_pat;
|
||||||
|
size_t len = 0;
|
||||||
|
char *cp = p->pattern + p->patternlen, *nl = NULL;
|
||||||
|
while (++len <= p->patternlen) {
|
||||||
|
if (*(--cp) == '\n') {
|
||||||
|
nl = cp;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!nl)
|
||||||
|
break;
|
||||||
|
new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
|
||||||
|
p->no, p->token, p->field);
|
||||||
|
new_pat->next = p->next;
|
||||||
|
if (!p->next)
|
||||||
|
*tail = &new_pat->next;
|
||||||
|
p->next = new_pat;
|
||||||
|
*nl = '\0';
|
||||||
|
p->patternlen -= len;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void append_header_grep_pattern(struct grep_opt *opt,
|
void append_header_grep_pattern(struct grep_opt *opt,
|
||||||
@ -408,6 +438,7 @@ void free_grep_patterns(struct grep_opt *opt)
|
|||||||
free_pcre_regexp(p);
|
free_pcre_regexp(p);
|
||||||
else
|
else
|
||||||
regfree(&p->regexp);
|
regfree(&p->regexp);
|
||||||
|
free(p->pattern);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
2
grep.h
2
grep.h
@ -35,7 +35,7 @@ struct grep_pat {
|
|||||||
const char *origin;
|
const char *origin;
|
||||||
int no;
|
int no;
|
||||||
enum grep_pat_token token;
|
enum grep_pat_token token;
|
||||||
const char *pattern;
|
char *pattern;
|
||||||
size_t patternlen;
|
size_t patternlen;
|
||||||
enum grep_header_field field;
|
enum grep_header_field field;
|
||||||
regex_t regexp;
|
regex_t regexp;
|
||||||
|
@ -322,6 +322,11 @@ test_expect_success 'grep -f, multiple patterns' '
|
|||||||
test_cmp expected actual
|
test_cmp expected actual
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success 'grep, multiple patterns' '
|
||||||
|
git grep "$(cat patterns)" >actual &&
|
||||||
|
test_cmp expected actual
|
||||||
|
'
|
||||||
|
|
||||||
cat >expected <<EOF
|
cat >expected <<EOF
|
||||||
file:foo mmap bar
|
file:foo mmap bar
|
||||||
file:foo_mmap bar
|
file:foo_mmap bar
|
||||||
|
Loading…
Reference in New Issue
Block a user