beyondgrep / ack2

**ack 2 is no longer being maintained. ack 3 is the latest version.**
https://github.com/beyondgrep/ack3/
Other
1.48k stars 140 forks source link

ack -l ignores matches with start-of-line anchor. #498

Closed shlomif closed 10 years ago

shlomif commented 10 years ago

Patch with a test case is here:

http://www.shlomifish.org/Files/files/code/ack-git1.diff

ack -l '^use strict;' for example does not find any Perl files.

Furthermore, with "\A" instead of "^" this patch will likely not find it anyway.

Also included below:

commit 234be65940ef396a77c4c47ae6c3f4427bbb1fdc
Author: Shlomi Fish <shlomif@shlomifish.org>
Date:   Tue Sep 30 15:28:10 2014 +0300

    Fix -l with an "^" start-of-line anchor.

diff --git a/ack b/ack
index 30b2662..3f88509 100644
--- a/ack
+++ b/ack
@@ -331,7 +331,8 @@ sub build_regex {
         $str = "(?i)$str";
     }

-    my $re = eval { qr/$str/ };
+    # We need /m for multi-line matches.
+    my $re = eval { qr/$str/m };
     if ( !$re ) {
         die "Invalid regex '$str':\n  $@";
     }
diff --git a/t/ack-c.t b/t/ack-c.t
index 21cdf06..2b363fd 100644
--- a/t/ack-c.t
+++ b/t/ack-c.t
@@ -1,9 +1,9 @@
-#!perl -T
+#!perl

 use warnings;
 use strict;

-use Test::More tests => 12;
+use Test::More tests => 13;

 use lib 't';
 use Util;
@@ -21,6 +21,18 @@ DASH_L: {
     ack_sets_match( [ @args, @files ], \@expected, 'Looking for religion with -l' );
 }

+DASH_L_WITH_CARET: {
+    my @expected = qw(
+        t/text/science-of-myth.txt
+    );
+
+    my @args  = ('-l', '^But you oughta');
+    my @files = qw( t/text );
+
+    ack_sets_match( [ @args, @files ], \@expected,
+        'Looking for a match at start of line with -l' );
+}
+
 DASH_CAPITAL_L: {
     my @expected = qw(
         t/text/4th-of-july.txt
xtaran commented 10 years ago

Hi,

On Tue, Sep 30, 2014 at 05:34:48AM -0700, Shlomi Fish wrote:

Patch with a test case is here:

http://www.shlomifish.org/Files/files/code/ack-git1.diff

ack -l '^use strict;' for example does not find any Perl files.

Furthermore, with "\A" instead of "^" this patch will likely not find it anyway.

Sounds similar to https://github.com/petdance/ack2/issues/491

    Regards, Axel
petdance commented 10 years ago

ack does not do multiline matches. ack only deals with one line at a time.

shlomif commented 10 years ago

@petdance : you misunderstand. It won't match a single line against the pattern of /^Prefix/ either.

shlomif[Module-Format@default]:$trunk/Module-Format/Module-Format$ ack '^use strict' lib/
lib/Module/Format/PerlMF_App.pm
3:use strict;

lib/Module/Format/Module.pm
4:use strict;

lib/Module/Format/ModuleList.pm
4:use strict;

lib/Module/Format.pm
4:use strict;
shlomif[Module-Format@default]:$trunk/Module-Format/Module-Format$ ack -l '^use strict' lib/
shlomif[Module-Format@default]:$trunk/Module-Format/Module-Format$ grep -rlP '^use strict' lib/
lib/Module/Format/PerlMF_App.pm
lib/Module/Format/Module.pm
lib/Module/Format/ModuleList.pm
lib/Module/Format.pm
shlomif[Module-Format@default]:$trunk/Module-Format/Module-Format$              
hoelzro commented 10 years ago

@xtaran is right; this is a duplicate of #491.