File Coverage

blib/lib/Parse/Gitignore.pm
Criterion Covered Total %
statement 66 79 83.5
branch 24 32 75.0
condition n/a
subroutine 10 11 90.9
pod 4 6 66.6
total 104 128 81.2


line stmt bran cond sub pod time code
1             package Parse::Gitignore;
2 2     2   267747 use warnings;
  2         5  
  2         145  
3 2     2   26 use strict;
  2         4  
  2         59  
4 2     2   13 use Carp;
  2         4  
  2         151  
5 2     2   1187 use File::Slurper 'read_lines';
  2         53793  
  2         198  
6 2     2   22 use File::Spec;
  2         5  
  2         2643  
7              
8             our $VERSION = '1.0';
9              
10             sub read_gitignore
11             {
12 3     3 1 15 my ($obj, $gitignore_file) = @_;
13 3 100       105 if (-d $gitignore_file) {
14 1         5 $gitignore_file = "$gitignore_file/.gitignore";
15             }
16 3 50       50 if (! -f $gitignore_file) {
17 0         0 carp ".gitignore file $gitignore_file doesn't exist";
18 0         0 return;
19             }
20 3 100       12 if ($obj->{verbose}) {
21 1         6 debugmsg ("Reading $gitignore_file.");
22             }
23 3         7 push @{$obj->{files}}, $gitignore_file;
  3         13  
24 3         16 my @lines = read_lines ($gitignore_file);
25 3         672 my (undef, $dir, undef) = File::Spec->splitpath ($gitignore_file);
26 3 100       19 if ($obj->{verbose}) {
27 1         4 debugmsg ("Directory is '$dir'.");
28             }
29             # Hash of ignored files.
30 3         11 for my $line (@lines) {
31             # Skip comment lines and empty lines
32 9 100       42 if ($line =~ /^\s*(#|$)/) {
33 3         7 next;
34             }
35 6 100       21 if ($obj->{verbose}) {
36 4         16 debugmsg ("Processing line '$line' in '$dir'.");
37             }
38 6 100       32 if ($line =~ m!/$!) {
39 1 50       18 if ($obj->{verbose}) {
40 1         3 debugmsg ("Looking at a directory");
41             }
42 1         87 for my $ignored_file (glob ("$line/*")) {
43 1         7 my $pignored_file = tidyfile ("$dir/$ignored_file");
44 1 50       5 if ($obj->{verbose}) {
45 1         4 debugmsg ("Ignoring '$pignored_file' in '$dir'.");
46             }
47 1         5 $obj->{ignored}{$pignored_file} = 1;
48             }
49 1         4 next;
50             }
51 5         437 for my $ignored_file (glob ($line)) {
52 5         33 my $pignored_file = tidyfile ("$dir/$ignored_file");
53 5 100       29 if ($obj->{verbose}) {
54 3         10 debugmsg ("Ignoring '$pignored_file' in '$dir'.");
55             }
56 5         31 $obj->{ignored}{$pignored_file} = 1;
57             }
58             }
59             }
60              
61             sub tidyfile
62             {
63 6     6 0 21 my ($file) = @_;
64 6         49 $file =~ s!//+!/!g;
65 6         18 return $file;
66             }
67              
68             sub excludesfile
69             {
70 0     0 1 0 my ($obj, $excludesfile) = @_;
71 0 0       0 if (! -f $excludesfile) {
72 0         0 carp "No such excludesfile: $excludesfile";
73 0         0 return;
74             }
75 0         0 my @lines = read_lines ($excludesfile);
76 0         0 my @oklines;
77 0         0 for (@lines) {
78             # Skip comments and empty lines.
79 0 0       0 if (/^\s*(#|$)/) {
80 0         0 next;
81             }
82 0         0 push @oklines, $_;
83             }
84 0         0 $obj->{excludesfile} = \@oklines;
85             }
86              
87             sub new
88             {
89 2     2 1 546347 my ($class, $gitignore_file, %options) = @_;
90             my $obj = {
91             # The globs which should be ignored.
92             ignored => {},
93             # The .gitignore files we have read so far.
94             file_list => [],
95             #
96             verbose => $options{verbose},
97 2         16 };
98 2         6 bless $obj, $class;
99 2 50       13 if ($gitignore_file) {
100 2 100       19 if ($obj->{verbose}) {
101 1         10 debugmsg ("Reading '$gitignore_file'.");
102             }
103 2         14 $obj->read_gitignore ($gitignore_file);
104             }
105 2         15 return $obj;
106             }
107              
108             sub ignored
109             {
110 7     7 1 32 my ($obj, $file) = @_;
111 7         195 $file = File::Spec->rel2abs ($file);
112 7 100       30 if ($obj->{verbose}) {
113 5 100       16 if ($obj->{ignored}{$file}) {
114 4         13 debugmsg ("$file is marked as ignored");
115             }
116             }
117 7         79 return $obj->{ignored}{$file};
118             }
119              
120             sub debugmsg
121             {
122 16     16 0 85 my (undef, $file, $line) = caller (0);
123 16         82 $file =~ s!.*/!!;
124 16         233 print "$file:$line: @_\n";
125             }
126              
127             1;