Skip to content

Commit

Permalink
Merge remote branch 'daxim/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
petdance committed Oct 7, 2010
2 parents c25bd15 + 2aa6fa5 commit 01ef566
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
18 changes: 14 additions & 4 deletions Ack.pm
Expand Up @@ -223,6 +223,7 @@ sub get_command_line_options {
'h|no-filename' => \$opt{h},
'H|with-filename' => \$opt{H},
'i|ignore-case' => \$opt{i},
'invert-file-match' => \$opt{invert_file_match},
'lines=s' => sub { shift; my $val = shift; push @{$opt{lines}}, $val },
'l|files-with-matches' => \$opt{l},
'L|files-without-matches' => sub { $opt{l} = $opt{v} = 1 },
Expand Down Expand Up @@ -496,7 +497,7 @@ sub filetypes {
return 'skipped' unless is_searchable( $basename );

my $lc_basename = lc $basename;
return ('make',TEXT) if $lc_basename eq 'makefile' || $lc_basename eq 'gnumakefile';
return ('make',TEXT) if $lc_basename eq 'makefile' || $lc_basename eq 'gnumakefile';
return ('rake','ruby',TEXT) if $lc_basename eq 'rakefile';

# If there's an extension, look it up
Expand Down Expand Up @@ -774,6 +775,7 @@ File finding:
The PATTERN must not be specified.
-g REGEX Same as -f, but only print files matching REGEX.
--sort-files Sort the found files lexically.
--invert-file-match print/search handle files that do not match REGEX (-g/-G)
--show-types Show which types each file has.
File inclusion/exclusion:
Expand Down Expand Up @@ -1503,6 +1505,14 @@ sub get_starting_points {
return \@what;
}

sub _match {
my ( $target, $expression, $invert_flag ) = @_;
if ( $invert_flag ) {
return $target !~ $expression;
} else {
return $target =~ $expression;
}
}

=head2 get_iterator
Expand All @@ -1522,9 +1532,9 @@ sub get_iterator {

if ( $g_regex ) {
$file_filter
= $opt->{u} ? sub { $File::Next::name =~ /$g_regex/ } # XXX Maybe this should be a 1, no?
: $opt->{all} ? sub { $starting_point{ $File::Next::name } || ( $File::Next::name =~ /$g_regex/ && is_searchable( $_ ) ) }
: sub { $starting_point{ $File::Next::name } || ( $File::Next::name =~ /$g_regex/ && is_interesting( @_ ) ) }
= $opt->{u} ? sub { _match( $File::Next::name, qr/$g_regex/, $opt->{invert_file_match} ) } # XXX Maybe this should be a 1, no?
: $opt->{all} ? sub { $starting_point{ $File::Next::name } || ( _match( $File::Next::name, qr/$g_regex/, $opt->{invert_file_match} ) && is_searchable( $_ ) ) }
: sub { $starting_point{ $File::Next::name } || ( _match( $File::Next::name, qr/$g_regex/, $opt->{invert_file_match} ) && is_interesting( @ _) ) }
;
}
else {
Expand Down
17 changes: 16 additions & 1 deletion t/ack-g.t
Expand Up @@ -3,7 +3,7 @@
use warnings;
use strict;

use Test::More tests => 55;
use Test::More tests => 57;

use lib 't';
use Util;
Expand Down Expand Up @@ -329,6 +329,21 @@ INVERT_MATCH_CONTENT_SWAPPED: {
sets_match( \@results, \@expected, "Looking for files without $content_regex in files matching $file_regex - swapped" );
}

INVERT_FILE_MATCH: {
my @expected = qw(
t/text/boy-named-sue.txt
t/text/me-and-bobbie-mcgee.txt
t/text/shut-up-be-happy.txt
);
my $file_regex = 'of';

my @files = qw( t/text/ );
my @args = ( '-a', '--invert-file-match', '-g', $file_regex );
my @results = run_ack( @args, @files );

sets_match( \@results, \@expected, "Looking for file names that do not match $file_regex" );
}

G_WITH_REGEX: {
# specifying both -g and a regex should result in an error
my @files = qw( t/text );
Expand Down

0 comments on commit 01ef566

Please sign in to comment.