File Coverage

blib/lib/Parse/Gitignore.pm
Criterion Covered Total %
statement 44 62 70.9
branch 9 20 45.0
condition n/a
subroutine 9 10 90.0
pod 4 4 100.0
total 66 96 68.7


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