| 1 | package Parse::Gitignore; | ||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use File::Slurper 'read_lines'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | our $VERSION = '1.0'; | |||||||||
| 9 | |||||||||||
| 10 | sub read_gitignore | ||||||||||
| 11 | 3 | ▶ | { | ||||||||
|
Subroutine
read_gitignore: called CC=14 SCAR=26.5
Pod
documented
| |||||||||||
| 12 | 3 | my ($obj, $gitignore_file) = @_; | |||||||||
| 13 | 3 | ▶ | if (-d $gitignore_file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | 1 | $gitignore_file = "$gitignore_file/.gitignore"; | |||||||||
| 15 | } | ||||||||||
| 16 | 3 | ▶ | if (! -f $gitignore_file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 0 | carp ".gitignore file $gitignore_file doesn't exist"; | |||||||||
| 18 | 0 | return; | |||||||||
| 19 | } | ||||||||||
| 20 | 3 | ▶ | if ($obj->{verbose}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 1 | debugmsg ("Reading $gitignore_file."); | |||||||||
| 22 | } | ||||||||||
| 23 | 3 | push @{$obj->{files}}, $gitignore_file; | |||||||||
| 24 | 3 | my @lines = read_lines ($gitignore_file); | |||||||||
| 25 | 3 | my (undef, $dir, undef) = File::Spec->splitpath ($gitignore_file); | |||||||||
| 26 | 3 | ▶ | if ($obj->{verbose}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 1 | debugmsg ("Directory is '$dir'."); | |||||||||
| 28 | } | ||||||||||
| 29 | # Hash of ignored files. | ||||||||||
| 30 | 3 | for my $line (@lines) { | |||||||||
| 31 | # Skip comment lines and empty lines | ||||||||||
| 32 | 9 | ▶ | if ($line =~ /^\s*(#|$)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 3 | next; | |||||||||
| 34 | } | ||||||||||
| 35 | 6 | ▶ | if ($obj->{verbose}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 4 | debugmsg ("Processing line '$line' in '$dir'."); | |||||||||
| 37 | } | ||||||||||
| 38 | 6 | ▶ | if ($line =~ m!/$!) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 1 | ▶ | if ($obj->{verbose}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 1 | debugmsg ("Looking at a directory"); | |||||||||
| 41 | } | ||||||||||
| 42 | 2 | ▶ | for my $ignored_file (glob ("$line/*")) { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 1 | my $pignored_file = tidyfile ("$dir/$ignored_file"); | |||||||||
| 44 | 1 | ▶ | if ($obj->{verbose}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 1 | debugmsg ("Ignoring '$pignored_file' in '$dir'."); | |||||||||
| 46 | } | ||||||||||
| 47 | 1 | $obj->{ignored}{$pignored_file} = 1; | |||||||||
| 48 | } | ||||||||||
| 49 | 1 | next; | |||||||||
| 50 | } | ||||||||||
| 51 | 5 | for my $ignored_file (glob ($line)) { | |||||||||
| 52 | 5 | my $pignored_file = tidyfile ("$dir/$ignored_file"); | |||||||||
| 53 | 5 | ▶ | if ($obj->{verbose}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 3 | debugmsg ("Ignoring '$pignored_file' in '$dir'."); | |||||||||
| 55 | } | ||||||||||
| 56 | 5 | $obj->{ignored}{$pignored_file} = 1; | |||||||||
| 57 | } | ||||||||||
| 58 | } | ||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | sub tidyfile | ||||||||||
| 62 | 6 | ▶ | { | ||||||||
|
Subroutine
tidyfile: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 63 | 6 | my ($file) = @_; | |||||||||
| 64 | 6 | $file =~ s!//+!/!g; | |||||||||
| 65 | 6 | return $file; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | sub excludesfile | ||||||||||
| 69 | 0 | ▶ | { | ||||||||
|
Subroutine
excludesfile: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 70 | 0 | my ($obj, $excludesfile) = @_; | |||||||||
| 71 | 0 | ▶ | if (! -f $excludesfile) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | carp "No such excludesfile: $excludesfile"; | |||||||||
| 73 | 0 | return; | |||||||||
| 74 | } | ||||||||||
| 75 | 0 | my @lines = read_lines ($excludesfile); | |||||||||
| 76 | 0 | my @oklines; | |||||||||
| 77 | 0 | for (@lines) { | |||||||||
| 78 | # Skip comments and empty lines. | ||||||||||
| 79 | 0 | ▶ | if (/^\s*(#|$)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | next; | |||||||||
| 81 | } | ||||||||||
| 82 | 0 | push @oklines, $_; | |||||||||
| 83 | } | ||||||||||
| 84 | 0 | $obj->{excludesfile} = \@oklines; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | sub new | ||||||||||
| 88 | 2 | ▶ | { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 89 | 2 | 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 | }; | |||||||||
| 98 | 2 | bless $obj, $class; | |||||||||
| 99 | 2 | ▶ | if ($gitignore_file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 2 | ▶ | if ($obj->{verbose}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 1 | debugmsg ("Reading '$gitignore_file'."); | |||||||||
| 102 | } | ||||||||||
| 103 | 2 | $obj->read_gitignore ($gitignore_file); | |||||||||
| 104 | } | ||||||||||
| 105 | 2 | return $obj; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | sub ignored | ||||||||||
| 109 | 7 | ▶ | { | ||||||||
|
Subroutine
ignored: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 110 | 7 | my ($obj, $file) = @_; | |||||||||
| 111 | 7 | $file = File::Spec->rel2abs ($file); | |||||||||
| 112 | 7 | ▶ | if ($obj->{verbose}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 5 | ▶ | if ($obj->{ignored}{$file}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 4 | debugmsg ("$file is marked as ignored"); | |||||||||
| 115 | } | ||||||||||
| 116 | } | ||||||||||
| 117 | 7 | return $obj->{ignored}{$file}; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | sub debugmsg | ||||||||||
| 121 | 16 | ▶ | { | ||||||||
|
Subroutine
debugmsg: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 122 | 16 | my (undef, $file, $line) = caller (0); | |||||||||
| 123 | 16 | $file =~ s!.*/!!; | |||||||||
| 124 | 16 | print "$file:$line: @_\n"; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 2 | 1; | |||||||||