blib/lib/File/Ignore.pm

filter: Statementstmt 85.8% 85 / 99 Branchbran 56.2% 27 / 48 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 90.0% 9 / 10 Podpod 100% 4 / 4 totaltotal 71.4% 130 / 182 CC 34 SCAR 27.6
CC
34
Cov
73%
_collect
33.2
include
7.6
exclude
7.6
CRAP 15.8
↑ Summary
1 package File::Ignore;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 File::Ignore - Ignore files that are good to ignore
9  
10 =head1 VERSION
11  
12 Version 0.021
13  
14 =cut
15  
16 3 our $VERSION = '0.021';
17  
18  
19 =head1 SYNOPSIS
20  
21 use File::Ignore;
22  
23 if (File::Ignore->ignore($file)) {
24 # ... Skip ...
25 }
26 else {
27 # Continue to process...
28 }
29  
30 my $good = File::Ignore->include(qw(src/RCS apple.Z doc/apple.txt tags .svn banana.html core));
31 for my $file (@$good) {
32 # doc/apple.txt
33 # banana.html
34 }
35  
36 my $bad = File::Ignore->exclude([qw(src/RCS apple.Z doc/apple.txt tags .svn banana.html core)]);
37 for my $file (@$bad) {
38 # src/RCS
39 # apple.Z
40 # tags
41 # .svn
42 # core
43 }
44  
45 =head1 METHODS
46  
47 =head2 File::Ignore->ignore( <file> )
48  
49 Returns true if <file> is one of the ignoreable.
50  
51 =head2 File::Ignore->include( <file>, <file>, ... )
52  
53 Returns an array reference of each <file> that is NOT ignoreable (should be included)
54  
55 =head2 File::Ignore->exclude( <file>, <file>, ... )
56  
57 Returns an array reference of each <file> that IS ignoreable (should be excluded)
58  
59 =head2 File::Ignore->ignoreable
60  
61 Returns a list of what is ignoreable. Currently, this is:
62  
63 regexp category
64  
65 RCS/ RCS rcs revision rsync
66 SCCS/ SCCS revision rsync sccs
67 CVS/ CVS cvs revision rsync
68 CVS.adm CVS\.adm cvs revision rsync
69 RCSLOG RCSLOG rcs revision rsync
70 cvslog.* cvslog\..* cvs revision rsync
71 tags tags ctags etags rsync
72 TAGS TAGS ctags etags rsync
73 .make.state \.make\.state make rsync
74 .nse_depinfo \.nse_depinfo rsync
75 *~ .*~ rsync
76 #* #.* rsync
77 .#* \.#.* rsync
78 ,* ,.* rsync
79 _$* _\$.* rsync
80 *$ .*\$ rsync
81 *.old .*\.old backup rsync
82 *.bak .*\.bak backup rsync
83 *.BAK .*\.BAK backup rsync
84 *.orig .*\.orig backup rsync
85 *.rej .*\.rej patch rsync
86 .del-* \.del-.* rsync
87 *.a .*\.a object rsync
88 *.olb .*\.olb object rsync
89 *.o .*\.o object rsync
90 *.obj .*\.obj object rsync
91 *.so .*\.so object rsync
92 .exe \.exe object rsync
93 *.Z .*\.Z rsync
94 *.elc .*\.elc rsync
95 *.ln .*\.ln rsync
96 core core core rsync
97 .svn/ \.svn revision rsync svn
98 .sw[p-z] \.sw[p-z] swap vim
99  
100 The above list was taken from C<rsync -C>
101  
102 Let me know if you have any thoughts on additions to this list or categorization.
103  
104 =head1 AUTHOR
105  
106 Robert Krimen, C<< <rkrimen at cpan.org> >>
107  
108 =head1 BUGS
109  
110 Please report any bugs or feature requests to C<bug-file-ignore at rt.cpan.org>, or through
111 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Ignore>. I will be notified, and then you'll
112 automatically be notified of progress on your bug as I make changes.
113  
114  
115  
116  
117 =head1 SUPPORT
118  
119 You can find documentation for this module with the perldoc command.
120  
121 perldoc File::Ignore
122  
123  
124 You can also look for information at:
125  
126 =over 4
127  
128 =item * RT: CPAN's request tracker
129  
130 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Ignore>
131  
132 =item * AnnoCPAN: Annotated CPAN documentation
133  
134 L<http://annocpan.org/dist/File-Ignore>
135  
136 =item * CPAN Ratings
137  
138 L<http://cpanratings.perl.org/d/File-Ignore>
139  
140 =item * Search CPAN
141  
142 L<http://search.cpan.org/dist/File-Ignore>
143  
144 =back
145  
146  
147 =head1 ACKNOWLEDGEMENTS
148  
149  
150 =head1 COPYRIGHT & LICENSE
151  
152 Copyright 2007 Robert Krimen, all rights reserved.
153  
154 This program is free software; you can redistribute it and/or modify it
155 under the same terms as Perl itself.
156  
157  
158 =cut
159  
160 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 102 ▶sub _make_entry {
Subroutine _make_entry: called CC=5 SCAR=16.1
163 102 local $_ = shift;
164 102 my $original = $_;
165 102 my $tags = "";
166 102 ▶ $tags = $1 if s/:(.*)$//;
Branch: 1 branch
BranchTrueFalseTotal
if s/:(.*)$// 102 0 102
167 102 my $specification = $_;
168 102 my @tags = split m/\s*,\s*/, $tags;
169 102 my %tag = map { $_ => 1 } @tags;
170 102 my $scope = "basename";
171 102 ▶ $scope = "path" if m/^\//;
Branch: 1 branch
BranchTrueFalseTotal
if m[^/] 0 102 102
172 102 my $prune = 0;
173 102 ▶ $prune = 1 if s/\/$//;
Branch: 1 branch
BranchTrueFalseTotal
if s[/$][] 12 90 102
174 102 my ($prunere, $pruneqr);
175 102 ▶ if ($prune) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prune) 12 90 102
176 12 $prunere = "(?:^|\\/)$_(?:$|\\/)";
177 12 $pruneqr = qr/$prunere/;
178 }
179
180 102 $_ =~ s/\$/\\\$/g;
181 102 $_ =~ s/\./\\./g;
182 102 $_ =~ s/\*/\.\*/g;
183  
184 102 return { specification => $specification, original => $original, re => $_, qr => qr/$_/, scope => $scope, prune => $prune, prunere => $prunere, pruneqr => $pruneqr, tags => \@tags, tag => \%tag };
185 }
186  
187 3 my @_ignore;
188 {
189 3 ▶ no warnings qw/qw/;
Subroutine BEGIN: called CC=1 SCAR=0.0
190 3 push @_ignore, map { _make_entry $_ } (qw(
191 RCS/:revision,rcs,rsync
192 SCCS/:revision,sccs,rsync
193 CVS/:revision,cvs,rsync
194 CVS.adm:revision,cvs,rsync
195 RCSLOG:revision,rcs,rsync
196 cvslog.*:revision,cvs,rsync
197 tags:etags,ctags,rsync
198 TAGS:etags,ctags,rsync
199 .make.state:make,rsync
200 .nse_depinfo:rsync
201 *~:rsync
202 #*:rsync
203 .#*:rsync
204 ,*:rsync
205 _$*:rsync
206 *$:rsync
207 *.old:backup,rsync
208 *.bak:backup,rsync
209 *.BAK:backup,rsync
210 *.orig:backup,rsync
211 *.rej:patch,rsync
212 .del-*:rsync
213 *.a:object,rsync
214 *.olb:object,rsync
215 *.o:object,rsync
216 *.obj:object,rsync
217 *.so:object,rsync
218 .exe:object,rsync
219 *.Z:rsync
220 *.elc:rsync
221 *.ln:rsync
222 core:core,rsync
223 .svn/:revision,svn,rsync
224 .sw[p-z]:vim,swap
225 ));
226 }
227  
228 3 my @_path = grep { $_->{scope} eq "path" } @_ignore;
229 3 my @_basename = @_ignore;
230 3 my @_prune = grep { $_->{prune} } @_ignore;
231  
232 173 ▶sub ignore {
Subroutine ignore: called CC=5 SCAR=17.0
Pod documented
233 173 ▶ shift if $_[0] && $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 173 0 173
Condition33%1/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
00173
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table33%1/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
234 173 my $self = __PACKAGE__;
235 173 my $option = {};
236 173 ▶ $option = shift if ref $_[0] eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 173 173
237 173 my $file = shift;
238  
239 173 ▶ return $self->_collect(1, $option, [ $file ]) ? 1 : 0; # Should we exclude this file?
Branch: 1 branch
BranchTrueFalseTotal
$self->_collect(1, $option, [$file]) ? : 73 100 173
240 }
241  
242 1 ▶sub include {
Subroutine include: called CC=6 SCAR=20.3
Pod documented
243 1 ▶ shift if $_[0] && $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 1 0 1
Condition33%1/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
001
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table33%1/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
244 1 my $self = __PACKAGE__;
245 1 my $option = {};
246 1 ▶ $option = shift if ref $_[0] eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 1 1
247 1 ▶ my $each = ref $_[0] eq "ARRAY" ? $_[0] : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 1 1
248  
249 1 ▶ return $self->_collect(0, $option, $each) || [];
Condition50%1/2
$self->_collect(0, $option, $each) || []
l!l
10
MC/DC0%0/1
$self->_collect(0, $option, $each) || []
$self->_collect(0, $option, $each)
Truth table50%1/2
A
A= $self->_collect(0, $option, $each)
Aresult
11
00
250 }
251  
252 1 ▶sub exclude {
Subroutine exclude: called CC=6 SCAR=20.3
Pod documented
253 1 ▶ shift if $_[0] && $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq __PACKAGE__ 1 0 1
Condition33%1/3
$_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
001
MC/DC0%0/2
$_[0] and $_[0] eq __PACKAGE__
$_[0] $_[0] eq __PACKAGE__
Truth table33%1/3
A and B
A= $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
254 1 my $self = __PACKAGE__;
255 1 my $option = {};
256 1 ▶ $option = shift if ref $_[0] eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 1 1
257 1 ▶ my $each = ref $_[0] eq "ARRAY" ? $_[0] : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 1 1
258  
259 1 ▶ return $self->_collect(1, $option, $each) || [];
Condition50%1/2
$self->_collect(1, $option, $each) || []
l!l
10
MC/DC0%0/1
$self->_collect(1, $option, $each) || []
$self->_collect(1, $option, $each)
Truth table50%1/2
A
A= $self->_collect(1, $option, $each)
Aresult
11
00
260 }
261  
262 175 ▶sub _collect {
Subroutine _collect: called CC=16 SCAR=35.0
263 175 my $self = shift;
264 175 my $collect_ignoreable = shift;
265 175 my $option = {};
266 175 ▶ $option = shift if ref $_[0] eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 175 0 175
267 175 my $each = shift;
268  
269 175 my @collection;
270 PATH:
271 175 for my $path (@$each) {
272 187 my $original_path = $path = "$path";
273 187 $path =~ s/\/$//;
274 187 my ($volume, $directory_path, $basename) = File::Spec->splitpath($path);
275  
276 187 my (@ign_basename, @ign_path, @ign_prune);
277 187 ▶ if (my $tag = $option->{tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $option->{'tag'}) { } 0 187 187
278 0 @ign_basename = map { $_->{tag}->{$tag} } @_basename;
279 0 @ign_path = map { $_->{tag}->{$tag} } @_path;
280 0 @ign_prune = map { $_->{tag}->{$tag} } @_prune;
281 }
282 else {
283 187 @ign_basename = @_basename;
284 187 @ign_path = @_path;
285 187 @ign_prune = @_prune;
286 }
287  
288 187 for (@ign_basename) {
289 4989 ▶ if ($basename =~ $_->{qr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basename =~ /$_->{"qr"}/) 83 4906 4989
290 83 ▶ push @collection, $original_path if $collect_ignoreable;
Branch: 1 branch
BranchTrueFalseTotal
if $collect_ignoreable 78 5 83
291 83 next PATH;
292 }
293 }
294  
295 104 for (@ign_path) {
296 0 ▶ if ($path =~ $_->{qr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$_->{"qr"}/) 0 0 0
297 0 ▶ push @collection, $original_path if $collect_ignoreable;
Branch: 1 branch
BranchTrueFalseTotal
if $collect_ignoreable 0 0 0
298 0 next PATH;
299 }
300 }
301  
302 104 ▶ if ($option->{pruneable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{"pruneable"}) 0 104 104
303 0 for (@ign_prune) {
304 0 ▶ if ($path =~ $_->{pruneqr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$_->{"pruneqr"}/) 0 0 0
305 0 ▶ push @collection, $original_path if $collect_ignoreable;
Branch: 1 branch
BranchTrueFalseTotal
if $collect_ignoreable 0 0 0
306 0 next PATH;
307 }
308 }
309 }
310  
311 104 ▶ push @collection, $original_path unless $collect_ignoreable;
Branch: 1 branch
BranchTrueFalseTotal
unless $collect_ignoreable 2 102 104
312 }
313  
314 175 ▶ return unless @collection;
Branch: 1 branch
BranchTrueFalseTotal
unless @collection 100 75 175
315 75 return \@collection;
316 }
317  
318 0 ▶sub ignoreable {
Subroutine ignoreable: not called CC=1 SCAR=6.9
Pod documented
319 0 return [ @_ignore ];
320 }
321  
322 3 1; # End of File::Ignore
323  
324 __END__
↑ Summary