Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Files without an extension #213

Closed
wants to merge 1 commit into from

5 participants

@willert

Allow files without an extension to be recognized as a distinct type. Keep old behavior unless --type-allow-empty is explicitly specified to ensure backwards-compatibility. Added tests and help text for this change.

@willert willert Allow files without an extension to be recognized as a distinct type.…
… Keep old behavior unless --type-allow-empty is explicitly specified. Added tests and help text for this change.
4ffcc40
@petdance
Owner

Thanks for the patch. At this point, I don't want ack 1.x to have any radically new functionality. ack 2.0 (the ack2 project here on github) will address this.

@petdance petdance closed this
@petdance petdance reopened this
@petdance
Owner

Hmmm, maybe it wouldn't be horrible to add this. Have you had anyone else use it?

@willert

No I haven't had anyone using it and no one would come to mind. But I tried to keep the changes to be minimally invasive and not alter any of the existing behavior. The fact that I didn't have to adapt any test cases (apart from the additional text file in t/swamp) reflects this quite nicely :)

To give some motivation for this change: I am an heavy user of ack-and-a-half.el and eproject.el in emacs. A very useful feature is 'ack-same, i.E. search in all files of the same type. I do a lot of editing of HTML::Mason templates which carry no extension. Without this patch ack doesn't recognize files without extension as having the same extension as the current one, i.e. none. I'd have to have three distinct ack commands (ack-in-eproject, ack-same-in-this-directory, ack-all-in-this-directory) instead of two, and you know how overloaded the keymap and muscle memory in emacs already is ;)

@petdance
Owner

I forwarded your pull request for public commentary to ack-users. If you're not on that list, I welcome you to join it.

https://groups.google.com/forum/?fromgroups#!topic/ack-users/4tbdPtbeguY

@willert

Thanks. I have joined and will keep an eye on the discussion.

Cheers

@suan

I am actively using this and have a personal fork just for this functionality: https://github.com/suan/ack/compare/master

In my case I want to ignore "lock" files, which don't have read permissions and throw annoying "permission denied" warnings. Another use-case I have (but have been too lazy to add) is to recognize Vagrantfile as a Ruby file.

Would love to see this get merged in :)

@xianxu

I use this. In Twitter's pants build system, build files are all named BUILD in arbitrary directories.

@hoelzro
Collaborator

Moved to the ack2 queue.

@hoelzro hoelzro closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 7, 2012
  1. @willert

    Allow files without an extension to be recognized as a distinct type.…

    willert authored
    … Keep old behavior unless --type-allow-empty is explicitly specified. Added tests and help text for this change.
This page is out of date. Refresh to see the latest.
View
15 Ack.pm
@@ -381,6 +381,7 @@ i.e. into %mappings, etc.
sub def_types_from_ARGV {
my @typedef;
+ my $allow_empty_types;
my $parser = Getopt::Long::Parser->new();
# pass_through => leave unrecognized command line arguments alone
@@ -389,10 +390,11 @@ sub def_types_from_ARGV {
$parser->getoptions(
'type-set=s' => sub { shift; push @typedef, ['c', shift] },
'type-add=s' => sub { shift; push @typedef, ['a', shift] },
+ 'type-allow-empty' => \$allow_empty_types,
) or App::Ack::die( 'See ack --help or ack --man for options.' );
for my $td (@typedef) {
- my ($type, $ext) = split /=/, $td->[1];
+ my ($type, $ext) = split /=/, $td->[1], -1;
if ( $td->[0] eq 'c' ) {
# type-set
@@ -415,9 +417,12 @@ sub def_types_from_ARGV {
unless exists $mappings{$type};
}
- my @exts = split /,/, $ext;
+ my @exts = length($ext) ? split /,/, $ext, -1 : ''; # split returns undef on empty string
s/^\.// for @exts;
+ # restore old behavior of ignoring empty typedefs unless explicitly asked to do otherwise
+ @exts = grep{ $_ } @exts unless $allow_empty_types;
+
if ( !exists $mappings{$type} || ref($mappings{$type}) eq 'ARRAY' ) {
push @{$mappings{$type}}, @exts;
for my $e ( @exts ) {
@@ -508,6 +513,10 @@ sub filetypes {
if ( $filename =~ m{\.([^\.$dir_sep_chars]+)$}o ) {
my $ref = $types{lc $1};
return (@{$ref},TEXT) if $ref;
+ } elsif ( exists $types{''} ) {
+ # files without extensions could have an user defined type
+ my $ref = $types{''};
+ return (@{$ref},TEXT) if $ref;
}
# At this point, we can't tell from just the name. Now we have to
@@ -806,6 +815,8 @@ File inclusion/exclusion:
--type-add TYPE=.EXTENSION[,.EXT2[,...]]
Files with the given EXTENSION(s) are recognized as
being of (the existing) type TYPE
+ --type-allow-empty Allow files without an extension to be recognized as
+ a distinct type.
--[no]follow Follow symlinks. Default is off.
View
15 ack
@@ -1503,6 +1503,7 @@ sub get_command_line_options {
sub def_types_from_ARGV {
my @typedef;
+ my $allow_empty_types;
my $parser = Getopt::Long::Parser->new();
# pass_through => leave unrecognized command line arguments alone
@@ -1511,10 +1512,11 @@ sub def_types_from_ARGV {
$parser->getoptions(
'type-set=s' => sub { shift; push @typedef, ['c', shift] },
'type-add=s' => sub { shift; push @typedef, ['a', shift] },
+ 'type-allow-empty' => \$allow_empty_types,
) or App::Ack::die( 'See ack --help or ack --man for options.' );
for my $td (@typedef) {
- my ($type, $ext) = split /=/, $td->[1];
+ my ($type, $ext) = split /=/, $td->[1], -1;
if ( $td->[0] eq 'c' ) {
# type-set
@@ -1537,9 +1539,12 @@ sub def_types_from_ARGV {
unless exists $mappings{$type};
}
- my @exts = split /,/, $ext;
+ my @exts = length($ext) ? split /,/, $ext, -1 : ''; # split returns undef on empty string
s/^\.// for @exts;
+ # restore old behavior of ignoring empty typedefs unless explicitly asked to do otherwise
+ @exts = grep{ $_ } @exts unless $allow_empty_types;
+
if ( !exists $mappings{$type} || ref($mappings{$type}) eq 'ARRAY' ) {
push @{$mappings{$type}}, @exts;
for my $e ( @exts ) {
@@ -1600,6 +1605,10 @@ sub filetypes {
if ( $filename =~ m{\.([^\.$dir_sep_chars]+)$}o ) {
my $ref = $types{lc $1};
return (@{$ref},TEXT) if $ref;
+ } elsif ( exists $types{''} ) {
+ # files without extensions could have an user defined type
+ my $ref = $types{''};
+ return (@{$ref},TEXT) if $ref;
}
# At this point, we can't tell from just the name. Now we have to
@@ -1848,6 +1857,8 @@ File inclusion/exclusion:
--type-add TYPE=.EXTENSION[,.EXT2[,...]]
Files with the given EXTENSION(s) are recognized as
being of (the existing) type TYPE
+ --type-allow-empty Allow files without an extension to be recognized as
+ a distinct type.
--[no]follow Follow symlinks. Default is off.
View
1  t/ack-text.t 100644 → 100755
@@ -48,6 +48,7 @@ ACK_F_TEXT: {
t/swamp/Makefile
t/swamp/Makefile.PL
t/swamp/javascript.js
+ t/swamp/no-extension
t/swamp/not-an-#emacs-workfile#
t/swamp/notaMakefile
t/swamp/notaRakefile
View
20 t/ack-type.t 100644 → 100755
@@ -3,7 +3,7 @@
use warnings;
use strict;
-use Test::More tests => 74;
+use Test::More tests => 82;
use lib 't';
use Util qw( sets_match );
@@ -37,6 +37,15 @@ my $bar = [qw(
t/swamp/file.bar
)];
+my $none = [
+ 't/swamp/0',
+ 't/swamp/no-extension',
+ 't/swamp/not-an-#emacs-workfile#',
+ 't/swamp/notaMakefile',
+ 't/swamp/notaRakefile',
+ 't/swamp/perl-without-extension',
+];
+
my $xml = [qw(
t/etc/buttonhook.rss.xxx
t/etc/buttonhook.xml.xxx
@@ -109,6 +118,15 @@ check_with( '--type-add xml=.foo,.bar --xml', $foo_bar_xml );
# check that --type-set redefines
check_with( '--type-set cc=.foo --cc', $foo );
+# check --type-set with empty EXT
+check_with( '--type-set empty= --type empty --no-recurse', [] );
+check_with( '--type-set empty= --type empty --no-recurse --type-allow-empty', $none );
+
+# check --type-add with empty EXT
+check_with( '--type-add cc= --cc --no-recurse', $cc_hh );
+check_with( '--type-add cc= --cc --no-recurse --type-allow-empty', [ sort @$cc_hh, @$none ]);
+
+
# check that builtin types cannot be changed
BUILTIN: {
my @builtins = qw( make skipped text binary );
View
1  t/swamp/no-extension
@@ -0,0 +1 @@
+nothing to see here, move along
Something went wrong with that request. Please try again.