{"thread":{"id":"29732","subject":"[PATCH] grep -P: Fix matching ^ and $","startedAt":"2012-02-25T09:24:28Z","lastAt":"2012-02-27T20:21:22Z","messageCount":6,"participants":["Michał Kiedrowicz","Zbigniew Jędrzejewski-Szmek","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"185408","messageId":"1330161868-7954-1-git-send-email-michal.kiedrowicz@gmail.com","threadId":"29732","inReplyTo":null,"subject":"[PATCH] grep -P: Fix matching ^ and $","fromName":"Michał Kiedrowicz","fromEmail":"michal.kiedrowicz@gmail.com","sentAt":"2012-02-25T09:24:28Z","receivedAt":"2012-02-25T09:24:28Z","isPatch":true,"sender":{"key":"michal.kiedrowicz@gmail.com","avatar":"https://avatars.githubusercontent.com/u/14072847?v=4"},"body":"When `git-grep` is run with -P/--perl-regexp, it doesn't match ^ and $ at\nthe beginning/end of the line.  This is because PCRE normally matches ^\nand $ at the beginning/end of the whole text, not for each line, and git-grep\nfirstly passes a large chunk of text (possibly containing many lines) to\npcre_exec() before it splits the text into lines.  This makes `git-grep -P`\nbehave differently from `git-grep -E` and also from `grep -P` and `pcregrep`:\n\n\t$ cat file\n\ta\n\t b\n\t$ git --no-pager grep --no-index -P '^ ' file\n\t$ git --no-pager grep --no-index -E '^ ' file\n\tfile: b\n\t$ grep -c -P '^ ' file\n\t b\n\t$ pcregrep -c '^ ' file\n\t b\n\nReported-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>\nSigned-off-by: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>\n---\n grep.c          |    2 +-\n t/t7810-grep.sh |   23 +++++++++++++++++++++++\n 2 files changed, 24 insertions(+), 1 deletions(-)\n\ndiff --git a/grep.c b/grep.c\nindex 3821400..f492d26 100644\n--- a/grep.c\n+++ b/grep.c\n@@ -79,7 +79,7 @@ static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)\n {\n \tconst char *error;\n \tint erroffset;\n-\tint options = 0;\n+\tint options = PCRE_MULTILINE;\n \n \tif (opt->ignore_case)\n \t\toptions |= PCRE_CASELESS;\ndiff --git a/t/t7810-grep.sh b/t/t7810-grep.sh\nindex 75f4716..dd6e6d5 100755\n--- a/t/t7810-grep.sh\n+++ b/t/t7810-grep.sh\n@@ -47,6 +47,13 @@ test_expect_success setup '\n \techo vvv >t/v &&\n \tmkdir t/a &&\n \techo vvv >t/a/v &&\n+\t{\n+\t\techo \"line without leading space1\"\n+\t\techo \" line with leading space1\"\n+\t\techo \" line with leading space2\"\n+\t\techo \" line with leading space3\"\n+\t\techo \"line without leading space2\"\n+\t} >space &&\n \tgit add . &&\n \ttest_tick &&\n \tgit commit -m initial\n@@ -893,4 +900,20 @@ test_expect_success 'mimic ack-grep --group' '\n \ttest_cmp expected actual\n '\n \n+cat >expected <<EOF\n+space: line with leading space1\n+space: line with leading space2\n+space: line with leading space3\n+EOF\n+\n+test_expect_success 'grep -E \"^ \"' '\n+\tgit grep -E \"^ \" space >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success \"grep -P '^ '\" '\n+\tgit grep -P \"^ \" space >actual &&\n+\ttest_cmp expected actual\n+'\n+\n test_done\n-- \n1.7.8.4\n"},{"id":"185411","messageId":"20120225103050.14f52a91@gmail.com","threadId":"29732","inReplyTo":"1330161868-7954-1-git-send-email-michal.kiedrowicz@gmail.com","subject":"Re: [PATCH] grep -P: Fix matching ^ and $","fromName":"Michał Kiedrowicz","fromEmail":"michal.kiedrowicz@gmail.com","sentAt":"2012-02-25T09:30:50Z","receivedAt":"2012-02-25T09:30:50Z","isPatch":true,"sender":{"key":"michal.kiedrowicz@gmail.com","avatar":"https://avatars.githubusercontent.com/u/14072847?v=4"},"body":"Michał Kiedrowicz <michal.kiedrowicz@gmail.com> wrote:\n\n> When `git-grep` is run with -P/--perl-regexp, it doesn't match ^ and $ at\n> the beginning/end of the line.  This is because PCRE normally matches ^\n> and $ at the beginning/end of the whole text, not for each line, and git-grep\n> firstly passes a large chunk of text (possibly containing many lines) to\n> pcre_exec() before it splits the text into lines.  This makes `git-grep -P`\n> behave differently from `git-grep -E` and also from `grep -P` and `pcregrep`:\n> \n> \t$ cat file\n> \ta\n> \t b\n> \t$ git --no-pager grep --no-index -P '^ ' file\n> \t$ git --no-pager grep --no-index -E '^ ' file\n> \tfile: b\n> \t$ grep -c -P '^ ' file\n> \t b\n> \t$ pcregrep -c '^ ' file\n> \t b\n> \n\nOriginal report:\nhttp://permalink.gmane.org/gmane.comp.version-control.git/190830\n\n> Reported-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>\n> Signed-off-by: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>\n> ---\n>  grep.c          |    2 +-\n>  t/t7810-grep.sh |   23 +++++++++++++++++++++++\n>  2 files changed, 24 insertions(+), 1 deletions(-)\n> \n> diff --git a/grep.c b/grep.c\n> index 3821400..f492d26 100644\n> --- a/grep.c\n> +++ b/grep.c\n> @@ -79,7 +79,7 @@ static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)\n>  {\n>  \tconst char *error;\n>  \tint erroffset;\n> -\tint options = 0;\n> +\tint options = PCRE_MULTILINE;\n>  \n>  \tif (opt->ignore_case)\n>  \t\toptions |= PCRE_CASELESS;\n> diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh\n> index 75f4716..dd6e6d5 100755\n> --- a/t/t7810-grep.sh\n> +++ b/t/t7810-grep.sh\n> @@ -47,6 +47,13 @@ test_expect_success setup '\n>  \techo vvv >t/v &&\n>  \tmkdir t/a &&\n>  \techo vvv >t/a/v &&\n> +\t{\n> +\t\techo \"line without leading space1\"\n> +\t\techo \" line with leading space1\"\n> +\t\techo \" line with leading space2\"\n> +\t\techo \" line with leading space3\"\n> +\t\techo \"line without leading space2\"\n> +\t} >space &&\n>  \tgit add . &&\n>  \ttest_tick &&\n>  \tgit commit -m initial\n> @@ -893,4 +900,20 @@ test_expect_success 'mimic ack-grep --group' '\n>  \ttest_cmp expected actual\n>  '\n>  \n> +cat >expected <<EOF\n> +space: line with leading space1\n> +space: line with leading space2\n> +space: line with leading space3\n> +EOF\n> +\n> +test_expect_success 'grep -E \"^ \"' '\n> +\tgit grep -E \"^ \" space >actual &&\n> +\ttest_cmp expected actual\n> +'\n> +\n> +test_expect_success \"grep -P '^ '\" '\n> +\tgit grep -P \"^ \" space >actual &&\n> +\ttest_cmp expected actual\n> +'\n> +\n>  test_done\n"},{"id":"185434","messageId":"4F491FF8.9070609@in.waw.pl","threadId":"29732","inReplyTo":"20120225103050.14f52a91@gmail.com","subject":"Re: [PATCH] grep -P: Fix matching ^ and $","fromName":"Zbigniew Jędrzejewski-Szmek","fromEmail":"zbyszek@in.waw.pl","sentAt":"2012-02-25T17:52:56Z","receivedAt":"2012-02-25T17:52:56Z","isPatch":true,"sender":{"key":"zbyszek@in.waw.pl","avatar":"https://avatars.githubusercontent.com/u/349618?v=4"},"body":"On 02/25/2012 10:30 AM, Michał Kiedrowicz wrote:\n> Michał Kiedrowicz<michal.kiedrowicz@gmail.com>  wrote:\n>\n>> When `git-grep` is run with -P/--perl-regexp, it doesn't match ^ and $ at\n>> the beginning/end of the line.  This is because PCRE normally matches ^\n>> and $ at the beginning/end of the whole text, not for each line, and git-grep\n>> firstly passes a large chunk of text (possibly containing many lines) to\n>> pcre_exec() before it splits the text into lines.  This makes `git-grep -P`\n>> behave differently from `git-grep -E` and also from `grep -P` and `pcregrep`:\n\nThanks! I can confirm that I now get the expected output.\n\nZbyszek\n\n> Original report:\n> http://permalink.gmane.org/gmane.comp.version-control.git/190830\n"},{"id":"185472","messageId":"7vlinpdxsu.fsf@alter.siamese.dyndns.org","threadId":"29732","inReplyTo":"1330161868-7954-1-git-send-email-michal.kiedrowicz@gmail.com","subject":"Re: [PATCH] grep -P: Fix matching ^ and $","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-02-26T22:39:29Z","receivedAt":"2012-02-26T22:39:29Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michał Kiedrowicz <michal.kiedrowicz@gmail.com> writes:\n\n> @@ -893,4 +900,20 @@ test_expect_success 'mimic ack-grep --group' '\n>  \ttest_cmp expected actual\n>  '\n>  \n> +cat >expected <<EOF\n> +space: line with leading space1\n> +space: line with leading space2\n> +space: line with leading space3\n> +EOF\n> +\n> +test_expect_success 'grep -E \"^ \"' '\n> +\tgit grep -E \"^ \" space >actual &&\n> +\ttest_cmp expected actual\n> +'\n> +\n> +test_expect_success \"grep -P '^ '\" '\n> +\tgit grep -P \"^ \" space >actual &&\n> +\ttest_cmp expected actual\n> +'\n\nThis test does not pass for me as I do not usually build with pcre;\nshouldn't it be protected with some test prerequisite?\n\nOtherwise the patch looks good; thanks.\n"},{"id":"185522","messageId":"1330361149-26741-1-git-send-email-zbyszek@in.waw.pl","threadId":"29732","inReplyTo":"7vlinpdxsu.fsf@alter.siamese.dyndns.org","subject":"[PATCH] grep -P: add tests for matching ^ and $","fromName":"Zbigniew Jędrzejewski-Szmek","fromEmail":"zbyszek@in.waw.pl","sentAt":"2012-02-27T16:45:49Z","receivedAt":"2012-02-27T16:45:49Z","isPatch":true,"sender":{"key":"zbyszek@in.waw.pl","avatar":"https://avatars.githubusercontent.com/u/349618?v=4"},"body":"From: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>\n\nWhen a fix for matching ^ and $ with -P was commited to master in\nfba4f1 (grep -P: Fix matching ^ and $), the tests were missing the\nLIBPCRE prerequisite check and were dropped from the patch. Here are\nthe tests guarded with LIBPCRE.\n\nSigned-off-by: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>\nSigned-off-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>\n---\n t/t7810-grep.sh |   23 +++++++++++++++++++++++\n 1 file changed, 23 insertions(+)\n\ndiff --git a/t/t7810-grep.sh b/t/t7810-grep.sh\nindex 75f4716..d9ad633 100755\n--- a/t/t7810-grep.sh\n+++ b/t/t7810-grep.sh\n@@ -47,6 +47,13 @@ test_expect_success setup '\n \techo vvv >t/v &&\n \tmkdir t/a &&\n \techo vvv >t/a/v &&\n+\t{\n+\t\techo \"line without leading space1\"\n+\t\techo \" line with leading space1\"\n+\t\techo \" line with leading space2\"\n+\t\techo \" line with leading space3\"\n+\t\techo \"line without leading space2\"\n+\t} >space &&\n \tgit add . &&\n \ttest_tick &&\n \tgit commit -m initial\n@@ -893,4 +900,20 @@ test_expect_success 'mimic ack-grep --group' '\n \ttest_cmp expected actual\n '\n \n+cat >expected <<EOF\n+space: line with leading space1\n+space: line with leading space2\n+space: line with leading space3\n+EOF\n+\n+test_expect_success LIBPCRE 'grep -E \"^ \"' '\n+\tgit grep -E \"^ \" space >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success LIBPCRE 'grep -P \"^ \"' '\n+\tgit grep -P \"^ \" space >actual &&\n+\ttest_cmp expected actual\n+'\n+\n test_done\n-- \n1.7.9.2.396.ga883d.dirty\n"},{"id":"185556","messageId":"7v4nuc6n99.fsf@alter.siamese.dyndns.org","threadId":"29732","inReplyTo":"1330361149-26741-1-git-send-email-zbyszek@in.waw.pl","subject":"Re: [PATCH] grep -P: add tests for matching ^ and $","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-02-27T20:21:22Z","receivedAt":"2012-02-27T20:21:22Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> writes:\n\n> From: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>\n>\n> When a fix for matching ^ and $ with -P was commited to master in\n> fba4f1 (grep -P: Fix matching ^ and $), the tests were missing the\n> LIBPCRE prerequisite check and were dropped from the patch. Here are\n> the tests guarded with LIBPCRE.\n\nThanks.\n\nThe real reason I separated it was because I didn't want to worry about\nthe test part when merging this down to older maintenance releases.\n"}]}