File Coverage

blib/lib/Parse/Gitignore.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Parse::Gitignore;
2 1     1   46342 use warnings;
  1         2  
  1         27  
3 1     1   5 use strict;
  1         1  
  1         15  
4 1     1   3 use Carp;
  1         2  
  1         39  
5             #use Path::Tiny;
6 1     1   5 use File::Basename;
  1         1  
  1         54  
7 1     1   648 use File::Slurper 'read_lines';
  0            
  0            
8             use File::Spec;
9             our $VERSION = '0.03';
10              
11             sub read_gitignore
12             {
13             my ($obj, $gitignore_file, %options) = @_;
14             if (! -f $gitignore_file) {
15             carp ".gitignore file $gitignore_file doesn't exist";
16             return;
17             }
18             if ($obj->{verbose}) {
19             print "Reading $gitignore_file.\n";
20             }
21             push @{$obj->{files}}, $gitignore_file;
22             my @lines = read_lines ($gitignore_file);
23             my (undef, $dir, undef) = File::Spec->splitpath ($gitignore_file);
24             if ($obj->{verbose}) {
25             print "Directory is $dir.\n";
26             }
27             # Hash of ignored files.
28             for my $line (@lines) {
29             # Skip comment lines and empty lines
30             if ($line =~ /^\s*(#|$)/) {
31             next;
32             }
33             if ($obj->{verbose}) {
34             print "Processing $line in $dir.\n";
35             }
36             for my $ignored_file (glob ($line)) {
37             # print "ignore '$ignored_file'\n";
38             my $pignored_file = File::Spec->rel2abs ($ignored_file);
39             if ($obj->{verbose}) {
40             print "$dir Ignoring '$pignored_file'.\n";
41             }
42             $obj->{ignored}{$pignored_file} = 1;
43             }
44             }
45             }
46              
47             sub excludesfile
48             {
49             my ($obj, $excludesfile) = @_;
50             if (! -f $excludesfile) {
51             carp "No such excludesfile: $excludesfile";
52             return;
53             }
54             my @lines = read_lines ($excludesfile);
55             my @oklines;
56             for (@lines) {
57             # Skip comments and empty lines.
58             if (/^\s*(#|$)/) {
59             next;
60             }
61             push @oklines, $_;
62             }
63             $obj->{excludesfile} = \@oklines;
64             }
65              
66             sub new
67             {
68             my ($class, $gitignore_file, %options) = @_;
69             my $obj = {
70             # The globs which should be ignored.
71             ignored => {},
72             # The .gitignore files we have read so far.
73             file_list => [],
74             #
75             verbose => $options{verbose},
76             };
77             bless $obj, $class;
78             if ($gitignore_file) {
79             if ($obj->{verbose}) {
80             print "Reading '$gitignore_file'.\n";
81             }
82             $obj->read_gitignore ($gitignore_file);
83             }
84             return $obj;
85             }
86              
87             sub ignored
88             {
89             my ($obj, $file) = @_;
90             $file = File::Spec->rel2abs ($file);
91             return $obj->{ignored}{$file};
92             }
93              
94             1;