Anonymous committed fba4f12

grep -P: Fix matching ^ and $

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
$ git grep --no-index -P '^ ' file
$ git grep --no-index -E '^ ' file
file: b
$ grep -c -P '^ ' file
$ pcregrep -c '^ ' file

Reported-by: Zbigniew Jędrzejewski-Szmek <>
Signed-off-by: Michał Kiedrowicz <>
Signed-off-by: Junio C Hamano <>

  • Participants
  • Parent commits f0c5793

Comments (0)

Files changed (1)

 	const char *error;
 	int erroffset;
-	int options = 0;
+	int options = PCRE_MULTILINE;
 	if (opt->ignore_case)
 		options |= PCRE_CASELESS;