Skip to content

Commit

Permalink
grep -P: Fix matching ^ and $
Browse files Browse the repository at this point in the history
When "git grep" is run with -P/--perl-regexp, it doesn't match ^ and $ at
the beginning/end of the line.  This is because PCRE normally matches ^
and $ at the beginning/end of the whole text, not for each line, and "git
grep" passes a large chunk of text (possibly containing many lines) to
pcre_exec() and then splits the text into lines.

This makes "git grep -P" behave differently from "git grep -E" and also
from "grep -P" and "pcregrep":

	$ cat file
	a
	 b
	$ git grep --no-index -P '^ ' file
	$ git grep --no-index -E '^ ' file
	file: b
	$ grep -c -P '^ ' file
	 b
	$ pcregrep -c '^ ' file
	 b

Reported-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Signed-off-by: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Michał Kiedrowicz authored and Junio C Hamano committed Feb 27, 2012
1 parent f0c5793 commit fba4f12
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
{
const char *error;
int erroffset;
int options = 0;
int options = PCRE_MULTILINE;

if (opt->ignore_case)
options |= PCRE_CASELESS;
Expand Down

0 comments on commit fba4f12

Please sign in to comment.