| 1 | # | ||||||||||
| 2 | # Copyright (c) 2003,2004,2005 Alexander Taler (dissent@0--0.org) | ||||||||||
| 3 | # | ||||||||||
| 4 | # All rights reserved. This program is free software; you can redistribute it | ||||||||||
| 5 | # and/or modify it under the same terms as Perl itself. | ||||||||||
| 6 | # | ||||||||||
| 7 | |||||||||||
| 8 | package VCS::LibCVS::IgnoreChecker; | ||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | VCS::LibCVS::IgnoreChecker - Ask it if a file should be ignored. | ||||||||||
| 16 | |||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||
| 18 | |||||||||||
| 19 | my $ignorer = VCS::LibCVS::IgnoreChecker->new($repository); | ||||||||||
| 20 | if ( $ignorer->ignore_check("dir1/file1") ) { | ||||||||||
| 21 | |||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||
| 23 | |||||||||||
| 24 | CVS has an involved way of deciding which files should be ignored, and which | ||||||||||
| 25 | shouldn't. Create an IgnoreChecker, and ask it if a file should be ignored. | ||||||||||
| 26 | |||||||||||
| 27 | The CVSROOT directory in the repository contains some information about what to | ||||||||||
| 28 | ignore, so you'll need a Repository to create an IgnoreChecker. | ||||||||||
| 29 | |||||||||||
| 30 | See the CVS info page for which files are ignored. | ||||||||||
| 31 | |||||||||||
| 32 | =cut | ||||||||||
| 33 | |||||||||||
| 34 | ############################################################################### | ||||||||||
| 35 | # Class constants | ||||||||||
| 36 | ############################################################################### | ||||||||||
| 37 | |||||||||||
| 38 | 1 | ▶ | use constant REVISION => '$Header: /cvsroot/libcvs-perl/libcvs-perl/VCS/LibCVS/IgnoreChecker.pm,v 1.9 2005/10/10 12:52:11 dissent Exp $ '; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | |||||||||||
| 40 | 1 | ▶ | use constant DEFAULT_IGNORE_LIST => | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | ( 'RCS', 'SCCS', 'CVS', 'CVS.adm', | ||||||||||
| 42 | 'RCSLOG', 'cvslog.*', | ||||||||||
| 43 | 'tags', 'TAGS', | ||||||||||
| 44 | '.make.state', '.nse_depinfo', | ||||||||||
| 45 | '*~', '#*', '.#*', ',*', '_$*', '*$', | ||||||||||
| 46 | '*.old', '*.bak', '*.BAK', '*.orig', '*.rej', '.del-*', | ||||||||||
| 47 | '*.a', '*.olb', '*.o', '*.obj', '*.so', '*.exe', | ||||||||||
| 48 | '*.Z', '*.elc', '*.ln', | ||||||||||
| 49 | 'core', | ||||||||||
| 50 | 1 | ); | |||||||||
| 51 | |||||||||||
| 52 | ############################################################################### | ||||||||||
| 53 | # Class variables | ||||||||||
| 54 | ############################################################################### | ||||||||||
| 55 | |||||||||||
| 56 | ############################################################################### | ||||||||||
| 57 | # Private variables | ||||||||||
| 58 | ############################################################################### | ||||||||||
| 59 | |||||||||||
| 60 | # $self->{Repository} VCS::LibCVS::Repository | ||||||||||
| 61 | # $self->{GlobalList} @list ref of perl regexps. Checked for all files | ||||||||||
| 62 | |||||||||||
| 63 | ############################################################################### | ||||||||||
| 64 | # Class routines | ||||||||||
| 65 | ############################################################################### | ||||||||||
| 66 | |||||||||||
| 67 | =head1 CLASS ROUTINES | ||||||||||
| 68 | |||||||||||
| 69 | =head2 B<new()> | ||||||||||
| 70 | |||||||||||
| 71 | $ignorer = VCS::LibCVS::IgnoreChecker->new($repository) | ||||||||||
| 72 | |||||||||||
| 73 | =over 4 | ||||||||||
| 74 | |||||||||||
| 75 | =item return type: VCS::LibCVS::IgnoreChecker | ||||||||||
| 76 | |||||||||||
| 77 | =item argument 1 type: VCS::LibCVS::Repository | ||||||||||
| 78 | |||||||||||
| 79 | The repository from which to retrieve global information. | ||||||||||
| 80 | |||||||||||
| 81 | =back | ||||||||||
| 82 | |||||||||||
| 83 | Creates a new IgnoreChecker, for a specific repository. | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 88 | 0 | my $class = shift; | |||||||||
| 89 | 0 | my $that = bless {}, $class; | |||||||||
| 90 | |||||||||||
| 91 | 0 | $that->{Repository} = shift; | |||||||||
| 92 | 0 | $that->{GlobalList} = []; | |||||||||
| 93 | |||||||||||
| 94 | # Build up the global ignore list | ||||||||||
| 95 | |||||||||||
| 96 | # Start with the default ignore list | ||||||||||
| 97 | 0 | $that->_append_patterns($that->{GlobalList}, DEFAULT_IGNORE_LIST); | |||||||||
| 98 | |||||||||||
| 99 | # Get CVSROOT/cvsignore from the repository | ||||||||||
| 100 | # if it's not there, we catch the exception | ||||||||||
| 101 | 0 | eval { | |||||||||
| 102 | 0 | my $r_ig = VCS::LibCVS::RepositoryFile->new($that->{Repository}, "CVSROOT/cvsignore"); | |||||||||
| 103 | 0 | my $r_ig_file_rev = $r_ig->get_revision("HEAD"); | |||||||||
| 104 | 0 | my $r_patterns = $r_ig_file_rev->get_contents()->as_string(); | |||||||||
| 105 | 0 | $that->_append_patterns($that->{GlobalList}, split(/\s/,$r_patterns)); | |||||||||
| 106 | }; | ||||||||||
| 107 | |||||||||||
| 108 | # Home directory | ||||||||||
| 109 | 0 | $that->_append_patterns_file($that->{GlobalList}, "$ENV{HOME}/.cvsignore"); | |||||||||
| 110 | |||||||||||
| 111 | # Environment | ||||||||||
| 112 | 0 | ▶ | if ($ENV{CVSIGNORE}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 0 | $that->_append_patterns($that->{GlobalList}, split(/\s/,$ENV{CVSIGNORE})); | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 0 | return $that; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | ############################################################################### | ||||||||||
| 120 | # Instance routines | ||||||||||
| 121 | ############################################################################### | ||||||||||
| 122 | |||||||||||
| 123 | =head1 INSTANCE ROUTINES | ||||||||||
| 124 | |||||||||||
| 125 | =head2 B<ignore_check()> | ||||||||||
| 126 | |||||||||||
| 127 | if ($ignorer->ignore_check("module/file1")) { | ||||||||||
| 128 | |||||||||||
| 129 | =over 4 | ||||||||||
| 130 | |||||||||||
| 131 | =item return type: boolean scalar | ||||||||||
| 132 | |||||||||||
| 133 | Returns true if the file is ignored by CVS | ||||||||||
| 134 | |||||||||||
| 135 | =item argument 1 type: scalar string | ||||||||||
| 136 | |||||||||||
| 137 | Filename to check. The filename must be qualified with its directory name so | ||||||||||
| 138 | that the .cvsignore in that directory may be checked. The path may be relative | ||||||||||
| 139 | or absolute. | ||||||||||
| 140 | |||||||||||
| 141 | =back | ||||||||||
| 142 | |||||||||||
| 143 | Checks if the file should be ignored. See CVS info page for how this is done. | ||||||||||
| 144 | |||||||||||
| 145 | =cut | ||||||||||
| 146 | |||||||||||
| 147 | 0 | ▶ | sub ignore_check { | ||||||||
|
Subroutine
ignore_check: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 148 | 0 | my $self = shift; | |||||||||
| 149 | 0 | my $qual_filename = shift; | |||||||||
| 150 | |||||||||||
| 151 | # Look for a .cvsignore in the same directory as the file | ||||||||||
| 152 | 0 | my ($dirname, $filename) = $qual_filename =~ /^(.*?)([^\/]*)$/; | |||||||||
| 153 | 0 | my $cvsignore_filename = $dirname . ".cvsignore"; | |||||||||
| 154 | |||||||||||
| 155 | # Duplicate the GlobalList so that we can add the local things to it | ||||||||||
| 156 | 0 | my $local_list = [ @{$self->{GlobalList}} ]; | |||||||||
| 157 | 0 | $self->_append_patterns_file($local_list, $cvsignore_filename); | |||||||||
| 158 | |||||||||||
| 159 | 0 | foreach my $pat (@$local_list) { | |||||||||
| 160 | 0 | ▶ | return 1 if $filename =~ /$pat/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | } | ||||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | ############################################################################### | ||||||||||
| 165 | # Private routines | ||||||||||
| 166 | ############################################################################### | ||||||||||
| 167 | |||||||||||
| 168 | # Append a list of patterns to the global list | ||||||||||
| 169 | 0 | ▶ | sub _append_patterns { | ||||||||
|
Subroutine
_append_patterns: not called CC=3 SCAR=24.8
| |||||||||||
| 170 | 0 | my $self = shift; | |||||||||
| 171 | 0 | my $list = shift; | |||||||||
| 172 | |||||||||||
| 173 | 0 | foreach my $next (@_) { | |||||||||
| 174 | 0 | ▶ | if ($next eq "-I") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 175 | 0 | @$list = (); | |||||||||
| 176 | } else { | ||||||||||
| 177 | 0 | push (@$list, _sh_pattern_2_perl_re($next)); | |||||||||
| 178 | } | ||||||||||
| 179 | } | ||||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | # Append a list of patterns found in the named file, (if it exists) to the | ||||||||||
| 183 | # global list | ||||||||||
| 184 | 0 | ▶ | sub _append_patterns_file { | ||||||||
|
Subroutine
_append_patterns_file: not called CC=2 SCAR=17.9
| |||||||||||
| 185 | 0 | my $self = shift; | |||||||||
| 186 | 0 | my $list = shift; | |||||||||
| 187 | 0 | my $filename = shift; | |||||||||
| 188 | |||||||||||
| 189 | 0 | ▶ | if (-f $filename) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 0 | open FDHOME, $filename; | |||||||||
| 191 | 0 | my @more_patterns = <FDHOME>; | |||||||||
| 192 | 0 | $self->_append_patterns($list, map { split(/\s/,$_) } @more_patterns); | |||||||||
| 193 | } | ||||||||||
| 194 | } | ||||||||||
| 195 | |||||||||||
| 196 | ######################################## | ||||||||||
| 197 | # Convert a CVS / sh(1) filematch pattern into a perl regexp | ||||||||||
| 198 | # Differences: | ||||||||||
| 199 | # + sh(1) allows any character to be escaped. In Perl, only alphas should | ||||||||||
| 200 | # not be escaped. Also true inside [..] | ||||||||||
| 201 | # + sh(1) interprets a leading ! or ^ in a character class as negation | ||||||||||
| 202 | # Perl only interprets for this ^ | ||||||||||
| 203 | |||||||||||
| 204 | # Note also that CVS differs from sh(1) in that it doesn't require a leading | ||||||||||
| 205 | # . to be specified explicitly | ||||||||||
| 206 | |||||||||||
| 207 | # Perl and sh/CVS appear to use character classes ([:class:]) in the same way. | ||||||||||
| 208 | |||||||||||
| 209 | 0 | ▶ | sub _sh_pattern_2_perl_re { | ||||||||
|
Subroutine
_sh_pattern_2_perl_re: not called CC=7 SCAR=40.3
| |||||||||||
| 210 | 0 | my $sh_pattern = shift; | |||||||||
| 211 | 0 | my $perl_re = "^"; | |||||||||
| 212 | |||||||||||
| 213 | # Traverse the sh pattern with regexp matches. Find special characters | ||||||||||
| 214 | # (*, ?, [) not escaped by a slash | ||||||||||
| 215 | 0 | ▶ | while ($sh_pattern) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 216 | 0 | $sh_pattern =~ /^(([^[*?\\]|\\.)*)([[*?\\])?(.*)/; | |||||||||
| 217 | 0 | my $non_special_bit = $1; | |||||||||
| 218 | 0 | my $optional_special = $3; | |||||||||
| 219 | 0 | $sh_pattern = $4; | |||||||||
| 220 | |||||||||||
| 221 | # Transform the non-special bit by changing escaping schemes | ||||||||||
| 222 | 0 | $perl_re .= _sh_pattern_escapes_2_perl_re_escapes($non_special_bit); | |||||||||
| 223 | |||||||||||
| 224 | # Act on the special bit: | ||||||||||
| 225 | 0 | ▶ | next unless $optional_special; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 226 | |||||||||||
| 227 | # matched a trailing \. Ignore it like CVS. | ||||||||||
| 228 | 0 | ▶ | next if $optional_special eq "\\"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 229 | |||||||||||
| 230 | # replace ? with . | ||||||||||
| 231 | 0 | ▶ | $perl_re .= "." if $optional_special eq "?"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 232 | |||||||||||
| 233 | # replace * with .* | ||||||||||
| 234 | 0 | ▶ | $perl_re .= ".*" if $optional_special eq "*"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 235 | |||||||||||
| 236 | # classes are a bit more work | ||||||||||
| 237 | 0 | ▶ | if ($optional_special eq "[") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 238 | # Grab the whole class, consists of: | ||||||||||
| 239 | # Optional ^ or !; optional ]; non-] characters or [:class:]'es; final ] | ||||||||||
| 240 | 0 | $sh_pattern =~ /^([\!\^]?\]?(\[:[^:]*:\]|[^]]*?)*)\](.*)/; | |||||||||
| 241 | 0 | my $class = $1; | |||||||||
| 242 | 0 | $sh_pattern = $3; | |||||||||
| 243 | |||||||||||
| 244 | # Replace leading ! with ^ | ||||||||||
| 245 | 0 | $class =~ s/^!/^/; | |||||||||
| 246 | |||||||||||
| 247 | # Remove escapes on alphas (count odd number of \) | ||||||||||
| 248 | 0 | $class =~ s/((^|[^\\])(\\\\)*)\\([[:alpha:]])/$1$4/g; | |||||||||
| 249 | |||||||||||
| 250 | # Append to perl_re | ||||||||||
| 251 | 0 | $perl_re .= "[$class]"; | |||||||||
| 252 | } | ||||||||||
| 253 | } | ||||||||||
| 254 | 0 | $perl_re .= "\$"; | |||||||||
| 255 | 0 | return $perl_re; | |||||||||
| 256 | } | ||||||||||
| 257 | |||||||||||
| 258 | # Change the slash escape scheme being used | ||||||||||
| 259 | |||||||||||
| 260 | # Trailing slashes are ignored by CVS. To copy this behaviour they are removed | ||||||||||
| 261 | # before quoting with quotemeta | ||||||||||
| 262 | |||||||||||
| 263 | 0 | ▶ | sub _sh_pattern_escapes_2_perl_re_escapes { | ||||||||
|
Subroutine
_sh_pattern_escapes_2_perl_re_escapes: not called CC=1 SCAR=6.9
| |||||||||||
| 264 | 0 | my $sh_pattern = shift; | |||||||||
| 265 | # Remove all backslashes that are escaping something | ||||||||||
| 266 | 0 | $sh_pattern =~ s/\\(.)/$1/g; | |||||||||
| 267 | # Remove a trailing backslash | ||||||||||
| 268 | 0 | $sh_pattern =~ s/\\$//; | |||||||||
| 269 | 0 | return quotemeta($sh_pattern); | |||||||||
| 270 | } | ||||||||||
| 271 | |||||||||||
| 272 | =head1 SEE ALSO | ||||||||||
| 273 | |||||||||||
| 274 | VCS::LibCVS | ||||||||||
| 275 | |||||||||||
| 276 | =cut | ||||||||||
| 277 | |||||||||||
| 278 | 1 | 1; | |||||||||