All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] grep -P: Fix matching ^ and $
@ 2012-02-25  9:24 Michał Kiedrowicz
  2012-02-25  9:30 ` Michał Kiedrowicz
  2012-02-26 22:39 ` Junio C Hamano
  0 siblings, 2 replies; 6+ messages in thread
From: Michał Kiedrowicz @ 2012-02-25  9:24 UTC (permalink / raw)
  To: git; +Cc: Zbigniew Jędrzejewski-Szmek, Michał Kiedrowicz

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
firstly passes a large chunk of text (possibly containing many lines) to
pcre_exec() before it 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 --no-pager grep --no-index -P '^ ' file
	$ git --no-pager 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>
---
 grep.c          |    2 +-
 t/t7810-grep.sh |   23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletions(-)

diff --git a/grep.c b/grep.c
index 3821400..f492d26 100644
--- a/grep.c
+++ b/grep.c
@@ -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;
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 75f4716..dd6e6d5 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -47,6 +47,13 @@ test_expect_success setup '
 	echo vvv >t/v &&
 	mkdir t/a &&
 	echo vvv >t/a/v &&
+	{
+		echo "line without leading space1"
+		echo " line with leading space1"
+		echo " line with leading space2"
+		echo " line with leading space3"
+		echo "line without leading space2"
+	} >space &&
 	git add . &&
 	test_tick &&
 	git commit -m initial
@@ -893,4 +900,20 @@ test_expect_success 'mimic ack-grep --group' '
 	test_cmp expected actual
 '
 
+cat >expected <<EOF
+space: line with leading space1
+space: line with leading space2
+space: line with leading space3
+EOF
+
+test_expect_success 'grep -E "^ "' '
+	git grep -E "^ " space >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success "grep -P '^ '" '
+	git grep -P "^ " space >actual &&
+	test_cmp expected actual
+'
+
 test_done
-- 
1.7.8.4

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] grep -P: Fix matching ^ and $
  2012-02-25  9:24 [PATCH] grep -P: Fix matching ^ and $ Michał Kiedrowicz
@ 2012-02-25  9:30 ` Michał Kiedrowicz
  2012-02-25 17:52   ` Zbigniew Jędrzejewski-Szmek
  2012-02-26 22:39 ` Junio C Hamano
  1 sibling, 1 reply; 6+ messages in thread
From: Michał Kiedrowicz @ 2012-02-25  9:30 UTC (permalink / raw)
  To: git; +Cc: Michał Kiedrowicz, Zbigniew Jędrzejewski-Szmek

Michał Kiedrowicz <michal.kiedrowicz@gmail.com> wrote:

> 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
> firstly passes a large chunk of text (possibly containing many lines) to
> pcre_exec() before it 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 --no-pager grep --no-index -P '^ ' file
> 	$ git --no-pager grep --no-index -E '^ ' file
> 	file: b
> 	$ grep -c -P '^ ' file
> 	 b
> 	$ pcregrep -c '^ ' file
> 	 b
> 

Original report:
http://permalink.gmane.org/gmane.comp.version-control.git/190830

> Reported-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
> Signed-off-by: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>
> ---
>  grep.c          |    2 +-
>  t/t7810-grep.sh |   23 +++++++++++++++++++++++
>  2 files changed, 24 insertions(+), 1 deletions(-)
> 
> diff --git a/grep.c b/grep.c
> index 3821400..f492d26 100644
> --- a/grep.c
> +++ b/grep.c
> @@ -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;
> diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
> index 75f4716..dd6e6d5 100755
> --- a/t/t7810-grep.sh
> +++ b/t/t7810-grep.sh
> @@ -47,6 +47,13 @@ test_expect_success setup '
>  	echo vvv >t/v &&
>  	mkdir t/a &&
>  	echo vvv >t/a/v &&
> +	{
> +		echo "line without leading space1"
> +		echo " line with leading space1"
> +		echo " line with leading space2"
> +		echo " line with leading space3"
> +		echo "line without leading space2"
> +	} >space &&
>  	git add . &&
>  	test_tick &&
>  	git commit -m initial
> @@ -893,4 +900,20 @@ test_expect_success 'mimic ack-grep --group' '
>  	test_cmp expected actual
>  '
>  
> +cat >expected <<EOF
> +space: line with leading space1
> +space: line with leading space2
> +space: line with leading space3
> +EOF
> +
> +test_expect_success 'grep -E "^ "' '
> +	git grep -E "^ " space >actual &&
> +	test_cmp expected actual
> +'
> +
> +test_expect_success "grep -P '^ '" '
> +	git grep -P "^ " space >actual &&
> +	test_cmp expected actual
> +'
> +
>  test_done

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] grep -P: Fix matching ^ and $
  2012-02-25  9:30 ` Michał Kiedrowicz
@ 2012-02-25 17:52   ` Zbigniew Jędrzejewski-Szmek
  0 siblings, 0 replies; 6+ messages in thread
From: Zbigniew Jędrzejewski-Szmek @ 2012-02-25 17:52 UTC (permalink / raw)
  To: git; +Cc: Michał Kiedrowicz

On 02/25/2012 10:30 AM, Michał Kiedrowicz wrote:
> Michał Kiedrowicz<michal.kiedrowicz@gmail.com>  wrote:
>
>> 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
>> firstly passes a large chunk of text (possibly containing many lines) to
>> pcre_exec() before it splits the text into lines.  This makes `git-grep -P`
>> behave differently from `git-grep -E` and also from `grep -P` and `pcregrep`:

Thanks! I can confirm that I now get the expected output.

Zbyszek

> Original report:
> http://permalink.gmane.org/gmane.comp.version-control.git/190830

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] grep -P: Fix matching ^ and $
  2012-02-25  9:24 [PATCH] grep -P: Fix matching ^ and $ Michał Kiedrowicz
  2012-02-25  9:30 ` Michał Kiedrowicz
@ 2012-02-26 22:39 ` Junio C Hamano
  2012-02-27 16:45   ` [PATCH] grep -P: add tests for " Zbigniew Jędrzejewski-Szmek
  1 sibling, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2012-02-26 22:39 UTC (permalink / raw)
  To: Michał Kiedrowicz; +Cc: git, Zbigniew Jędrzejewski-Szmek

Michał Kiedrowicz <michal.kiedrowicz@gmail.com> writes:

> @@ -893,4 +900,20 @@ test_expect_success 'mimic ack-grep --group' '
>  	test_cmp expected actual
>  '
>  
> +cat >expected <<EOF
> +space: line with leading space1
> +space: line with leading space2
> +space: line with leading space3
> +EOF
> +
> +test_expect_success 'grep -E "^ "' '
> +	git grep -E "^ " space >actual &&
> +	test_cmp expected actual
> +'
> +
> +test_expect_success "grep -P '^ '" '
> +	git grep -P "^ " space >actual &&
> +	test_cmp expected actual
> +'

This test does not pass for me as I do not usually build with pcre;
shouldn't it be protected with some test prerequisite?

Otherwise the patch looks good; thanks.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] grep -P: add tests for matching ^ and $
  2012-02-26 22:39 ` Junio C Hamano
@ 2012-02-27 16:45   ` Zbigniew Jędrzejewski-Szmek
  2012-02-27 20:21     ` Junio C Hamano
  0 siblings, 1 reply; 6+ messages in thread
From: Zbigniew Jędrzejewski-Szmek @ 2012-02-27 16:45 UTC (permalink / raw)
  To: git, gitster; +Cc: michal.kiedrowicz, Zbigniew Jędrzejewski-Szmek

From: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>

When a fix for matching ^ and $ with -P was commited to master in
fba4f1 (grep -P: Fix matching ^ and $), the tests were missing the
LIBPCRE prerequisite check and were dropped from the patch. Here are
the tests guarded with LIBPCRE.

Signed-off-by: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>
Signed-off-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
---
 t/t7810-grep.sh |   23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 75f4716..d9ad633 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -47,6 +47,13 @@ test_expect_success setup '
 	echo vvv >t/v &&
 	mkdir t/a &&
 	echo vvv >t/a/v &&
+	{
+		echo "line without leading space1"
+		echo " line with leading space1"
+		echo " line with leading space2"
+		echo " line with leading space3"
+		echo "line without leading space2"
+	} >space &&
 	git add . &&
 	test_tick &&
 	git commit -m initial
@@ -893,4 +900,20 @@ test_expect_success 'mimic ack-grep --group' '
 	test_cmp expected actual
 '
 
+cat >expected <<EOF
+space: line with leading space1
+space: line with leading space2
+space: line with leading space3
+EOF
+
+test_expect_success LIBPCRE 'grep -E "^ "' '
+	git grep -E "^ " space >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success LIBPCRE 'grep -P "^ "' '
+	git grep -P "^ " space >actual &&
+	test_cmp expected actual
+'
+
 test_done
-- 
1.7.9.2.396.ga883d.dirty

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] grep -P: add tests for matching ^ and $
  2012-02-27 16:45   ` [PATCH] grep -P: add tests for " Zbigniew Jędrzejewski-Szmek
@ 2012-02-27 20:21     ` Junio C Hamano
  0 siblings, 0 replies; 6+ messages in thread
From: Junio C Hamano @ 2012-02-27 20:21 UTC (permalink / raw)
  To: Zbigniew Jędrzejewski-Szmek; +Cc: git, gitster, michal.kiedrowicz

Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> writes:

> From: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>
>
> When a fix for matching ^ and $ with -P was commited to master in
> fba4f1 (grep -P: Fix matching ^ and $), the tests were missing the
> LIBPCRE prerequisite check and were dropped from the patch. Here are
> the tests guarded with LIBPCRE.

Thanks.

The real reason I separated it was because I didn't want to worry about
the test part when merging this down to older maintenance releases.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-02-27 20:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-25  9:24 [PATCH] grep -P: Fix matching ^ and $ Michał Kiedrowicz
2012-02-25  9:30 ` Michał Kiedrowicz
2012-02-25 17:52   ` Zbigniew Jędrzejewski-Szmek
2012-02-26 22:39 ` Junio C Hamano
2012-02-27 16:45   ` [PATCH] grep -P: add tests for " Zbigniew Jędrzejewski-Szmek
2012-02-27 20:21     ` Junio C Hamano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.