Skip to content
This repository

Files without an extension #213

Closed
wants to merge 1 commit into from

5 participants

Sebastian Willert Andy Lester suan Xian Rob Hoelz
Sebastian 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.

Sebastian 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
Andy Lester
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.

Andy Lester petdance closed this March 07, 2012
Andy Lester petdance reopened this March 07, 2012
Andy Lester
Owner

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

Sebastian 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 ;)

Andy Lester
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

Sebastian Willert

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

Cheers

suan
suan commented March 07, 2012

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 :)

Xian

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

Rob Hoelz
Collaborator

Moved to the ack2 queue.

Rob Hoelz hoelzro closed this August 28, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 07, 2012
Sebastian 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
This page is out of date. Refresh to see the latest.
15  Ack.pm
@@ -381,6 +381,7 @@ i.e. into %mappings, etc.
381 381
 
382 382
 sub def_types_from_ARGV {
383 383
     my @typedef;
  384
+    my $allow_empty_types;
384 385
 
385 386
     my $parser = Getopt::Long::Parser->new();
386 387
         # pass_through   => leave unrecognized command line arguments alone
@@ -389,10 +390,11 @@ sub def_types_from_ARGV {
389 390
     $parser->getoptions(
390 391
         'type-set=s' => sub { shift; push @typedef, ['c', shift] },
391 392
         'type-add=s' => sub { shift; push @typedef, ['a', shift] },
  393
+        'type-allow-empty' => \$allow_empty_types,
392 394
     ) or App::Ack::die( 'See ack --help or ack --man for options.' );
393 395
 
394 396
     for my $td (@typedef) {
395  
-        my ($type, $ext) = split /=/, $td->[1];
  397
+        my ($type, $ext) = split /=/, $td->[1], -1;
396 398
 
397 399
         if ( $td->[0] eq 'c' ) {
398 400
             # type-set
@@ -415,9 +417,12 @@ sub def_types_from_ARGV {
415 417
                 unless exists $mappings{$type};
416 418
         }
417 419
 
418  
-        my @exts = split /,/, $ext;
  420
+        my @exts = length($ext) ? split /,/, $ext, -1 : '';  # split returns undef on empty string
419 421
         s/^\.// for @exts;
420 422
 
  423
+        # restore old behavior of ignoring empty typedefs unless explicitly asked to do otherwise
  424
+        @exts = grep{ $_ } @exts unless $allow_empty_types;
  425
+
421 426
         if ( !exists $mappings{$type} || ref($mappings{$type}) eq 'ARRAY' ) {
422 427
             push @{$mappings{$type}}, @exts;
423 428
             for my $e ( @exts ) {
@@ -508,6 +513,10 @@ sub filetypes {
508 513
     if ( $filename =~ m{\.([^\.$dir_sep_chars]+)$}o ) {
509 514
         my $ref = $types{lc $1};
510 515
         return (@{$ref},TEXT) if $ref;
  516
+    } elsif ( exists $types{''} ) {
  517
+        # files without extensions could have an user defined type
  518
+        my $ref = $types{''};
  519
+        return (@{$ref},TEXT) if $ref;
511 520
     }
512 521
 
513 522
     # At this point, we can't tell from just the name.  Now we have to
@@ -806,6 +815,8 @@ File inclusion/exclusion:
806 815
   --type-add TYPE=.EXTENSION[,.EXT2[,...]]
807 816
                         Files with the given EXTENSION(s) are recognized as
808 817
                         being of (the existing) type TYPE
  818
+  --type-allow-empty    Allow files without an extension to be recognized as
  819
+                        a distinct type.
809 820
 
810 821
   --[no]follow          Follow symlinks.  Default is off.
811 822
 
15  ack
@@ -1503,6 +1503,7 @@ sub get_command_line_options {
1503 1503
 
1504 1504
 sub def_types_from_ARGV {
1505 1505
     my @typedef;
  1506
+    my $allow_empty_types;
1506 1507
 
1507 1508
     my $parser = Getopt::Long::Parser->new();
1508 1509
         # pass_through   => leave unrecognized command line arguments alone
@@ -1511,10 +1512,11 @@ sub def_types_from_ARGV {
1511 1512
     $parser->getoptions(
1512 1513
         'type-set=s' => sub { shift; push @typedef, ['c', shift] },
1513 1514
         'type-add=s' => sub { shift; push @typedef, ['a', shift] },
  1515
+        'type-allow-empty' => \$allow_empty_types,
1514 1516
     ) or App::Ack::die( 'See ack --help or ack --man for options.' );
1515 1517
 
1516 1518
     for my $td (@typedef) {
1517  
-        my ($type, $ext) = split /=/, $td->[1];
  1519
+        my ($type, $ext) = split /=/, $td->[1], -1;
1518 1520
 
1519 1521
         if ( $td->[0] eq 'c' ) {
1520 1522
             # type-set
@@ -1537,9 +1539,12 @@ sub def_types_from_ARGV {
1537 1539
                 unless exists $mappings{$type};
1538 1540
         }
1539 1541
 
1540  
-        my @exts = split /,/, $ext;
  1542
+        my @exts = length($ext) ? split /,/, $ext, -1 : '';  # split returns undef on empty string
1541 1543
         s/^\.// for @exts;
1542 1544
 
  1545
+        # restore old behavior of ignoring empty typedefs unless explicitly asked to do otherwise
  1546
+        @exts = grep{ $_ } @exts unless $allow_empty_types;
  1547
+
1543 1548
         if ( !exists $mappings{$type} || ref($mappings{$type}) eq 'ARRAY' ) {
1544 1549
             push @{$mappings{$type}}, @exts;
1545 1550
             for my $e ( @exts ) {
@@ -1600,6 +1605,10 @@ sub filetypes {
1600 1605
     if ( $filename =~ m{\.([^\.$dir_sep_chars]+)$}o ) {
1601 1606
         my $ref = $types{lc $1};
1602 1607
         return (@{$ref},TEXT) if $ref;
  1608
+    } elsif ( exists $types{''} ) {
  1609
+        # files without extensions could have an user defined type
  1610
+        my $ref = $types{''};
  1611
+        return (@{$ref},TEXT) if $ref;
1603 1612
     }
1604 1613
 
1605 1614
     # At this point, we can't tell from just the name.  Now we have to
@@ -1848,6 +1857,8 @@ File inclusion/exclusion:
1848 1857
   --type-add TYPE=.EXTENSION[,.EXT2[,...]]
1849 1858
                         Files with the given EXTENSION(s) are recognized as
1850 1859
                         being of (the existing) type TYPE
  1860
+  --type-allow-empty    Allow files without an extension to be recognized as
  1861
+                        a distinct type.
1851 1862
 
1852 1863
   --[no]follow          Follow symlinks.  Default is off.
1853 1864
 
1  t/ack-text.t 100644 → 100755
@@ -48,6 +48,7 @@ ACK_F_TEXT: {
48 48
         t/swamp/Makefile
49 49
         t/swamp/Makefile.PL
50 50
         t/swamp/javascript.js
  51
+        t/swamp/no-extension
51 52
         t/swamp/not-an-#emacs-workfile#
52 53
         t/swamp/notaMakefile
53 54
         t/swamp/notaRakefile
20  t/ack-type.t 100644 → 100755
@@ -3,7 +3,7 @@
3 3
 use warnings;
4 4
 use strict;
5 5
 
6  
-use Test::More tests => 74;
  6
+use Test::More tests => 82;
7 7
 
8 8
 use lib 't';
9 9
 use Util qw( sets_match );
@@ -37,6 +37,15 @@ my $bar = [qw(
37 37
     t/swamp/file.bar
38 38
 )];
39 39
 
  40
+my $none = [
  41
+    't/swamp/0',
  42
+    't/swamp/no-extension',
  43
+    't/swamp/not-an-#emacs-workfile#',
  44
+    't/swamp/notaMakefile',
  45
+    't/swamp/notaRakefile',
  46
+    't/swamp/perl-without-extension',
  47
+];
  48
+
40 49
 my $xml = [qw(
41 50
     t/etc/buttonhook.rss.xxx
42 51
     t/etc/buttonhook.xml.xxx
@@ -109,6 +118,15 @@ check_with( '--type-add xml=.foo,.bar --xml', $foo_bar_xml );
109 118
 # check that --type-set redefines
110 119
 check_with( '--type-set cc=.foo --cc', $foo );
111 120
 
  121
+# check --type-set with empty EXT
  122
+check_with( '--type-set empty= --type empty --no-recurse', [] );
  123
+check_with( '--type-set empty= --type empty --no-recurse --type-allow-empty', $none );
  124
+
  125
+# check --type-add with empty EXT
  126
+check_with( '--type-add cc= --cc --no-recurse', $cc_hh );
  127
+check_with( '--type-add cc= --cc --no-recurse --type-allow-empty', [ sort @$cc_hh, @$none ]);
  128
+
  129
+
112 130
 # check that builtin types cannot be changed
113 131
 BUILTIN: {
114 132
     my @builtins = qw( make skipped text binary );
1  t/swamp/no-extension
... ...
@@ -0,0 +1 @@
  1
+nothing to see here, move along
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.