« blib/lib/Git/MoreHooks/CheckCommitAuthorFromMailmap.pm
↑ Summary
blib/lib/Git/MoreHooks/GitRepoAdmin.pm »
| 1 | ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) | ||
| 2 | ## no critic (Subroutines::ProhibitCallsToUnexportedSubs) | ||
| 3 | ## no critic (Modules::RequireExplicitInclusion) | ||
| 4 | |||
| 5 | package Git::MoreHooks::CheckPerl; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use utf8; | ||
| 10 | |||
| 11 | # ABSTRACT: Check committed files for Perl::Critic correctness. | ||
| 12 | |||
| 13 | our $VERSION = '0.021'; | ||
| 14 | |||
| 15 | |||
| 16 | # use Git::MoreHooks::CheckCommitBase \&do_hook; | ||
| 17 | |||
| 18 | use Git::Hooks 4.000_000; | ||
| 19 | use Path::Tiny qw( path ); | ||
| 20 | use Log::Any qw{$log}; | ||
| 21 | use Module::Load qw{ load }; | ||
| 22 | use Text::Glob qw{ glob_to_regex }; | ||
| 23 | use List::Util 1.33 qw( any none ); | ||
| 24 | |||
| 25 | |||
| 26 | my $PKG = __PACKAGE__; | ||
| 27 | my ($HOOK) = __PACKAGE__ =~ /::([^:]+)$/msx; | ||
| 28 | my $CFG = 'githooks.' . $HOOK; | ||
| 29 | |||
| 30 | |||
| 31 | #################### | ||
| 32 | # Hook configuration, check it and set defaults. | ||
| 33 | |||
| 34 | sub _setup_config { | ||
| 35 | my ($git) = @_; | ||
| 36 | $log->debugf( __PACKAGE__ . '::_setup_config(%s):', $git ); | ||
| 37 | |||
| 38 | # The configuration is cached during the first invocation | ||
| 39 | # in the object Git::Repository object. | ||
| 40 | # https://metacpan.org/pod/Git::Repository::Plugin::GitHooks#get_config-[SECTION-[VARIABLE]] | ||
| 41 | my $config = $git->get_config(); | ||
| 42 | $log->tracef( __PACKAGE__ . '::_setup_config(): Current Git config:\n%s.', $config ); | ||
| 43 | |||
| 44 | # Put empty hash if there is no config items. | ||
| 45 | $config->{ lc $CFG } //= {}; | ||
| 46 | |||
| 47 | # Set default config values. | ||
| 48 | my $default = $config->{ lc $CFG }; | ||
| 49 | $default->{'use-temp-files'} //= [0]; | ||
| 50 | $default->{'name'} //= [ | ||
| 51 | '*.p[lm]', | ||
| 52 | '*.psgi', | ||
| 53 | '*.t', | ||
| 54 | ]; | ||
| 55 | $default->{'dirpath'} //= [ | ||
| 56 | 'bin', | ||
| 57 | 'script', | ||
| 58 | ]; | ||
| 59 | $default->{'filepath'} //= []; | ||
| 60 | |||
| 61 | $default->{'critic'} //= {}; | ||
| 62 | $default->{'perl-c'} //= {}; | ||
| 63 | |||
| 64 | # Perl::Critic | ||
| 65 | $default->{'critic'}->{'active'} //= ['true']; | ||
| 66 | $default->{'critic'}->{'profile'} //= []; | ||
| 67 | |||
| 68 | # Perl -c | ||
| 69 | $default->{'perl-c'}->{'active'} //= [0]; | ||
| 70 | |||
| 71 | $log->tracef( __PACKAGE__ . '::_setup_config(): Final Git config:\n%s.', $git->get_config() ); | ||
| 72 | |||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | #################### | ||
| 77 | # Internal | ||
| 78 | |||
| 79 | use Config::Tiny; | ||
| 80 | |||
| 81 | sub _get_critic_profile { | ||
| 82 | my ($git, $profile) = @_; | ||
| 83 | $log->debugf( __PACKAGE__ . q{::} . '_get_critic_profile():profile=%s', $profile); | ||
| 84 | |||
| 85 | my $content = $git->run(qw/cat-file -p/, $profile); | ||
| 86 | $log->tracef( __PACKAGE__ . q{::} . '_get_critic_profile():content=%s', $content); | ||
| 87 | |||
| 88 | return $content; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _read_critic_profile { | ||
| 92 | my ($git, $content) = @_; | ||
| 93 | my $c = Config::Tiny->read_string( $content ); | ||
| 94 | return $c; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _set_critic { | ||
| 98 | my ($git) = @_; | ||
| 99 | my $cfg_section = $CFG . q{.} . 'critic'; | ||
| 100 | |||
| 101 | eval { | ||
| 102 | load 'Perl::Critic'; | ||
| 103 | load 'Perl::Critic::Utils'; | ||
| 104 | load 'Perl::Critic::Violation'; | ||
| 105 | 1; # To cover the fact that operation correctly returns a false value. | ||
| 106 | } or do { | ||
| 107 | $log->errorf( __PACKAGE__ . q{::} | ||
| 108 | . '_set_critic():Cannot load Perl::Critic' | ||
| 109 | ); | ||
| 110 | return { 'error' => 'Cannot load Perl::Critic' }; | ||
| 111 | }; | ||
| 112 | |||
| 113 | my $pc_rc_filename = $git->get_config( $cfg_section => 'profile'); | ||
| 114 | $log->tracef( __PACKAGE__ . q{::} . '_set_critic():pc_rc_filename=%s', $pc_rc_filename); | ||
| 115 | |||
| 116 | my @critic_cfg_props = qw( severity theme top only force verbose allow-unsafe ); | ||
| 117 | my @critic_cfg_list_props = qw( include exclude ); | ||
| 118 | my %cfg; | ||
| 119 | if( $pc_rc_filename ) { | ||
| 120 | my $content = _get_critic_profile( $git, $pc_rc_filename ); | ||
| 121 | my $rc = _read_critic_profile( $git, $content ); | ||
| 122 | $log->debugf( __PACKAGE__ . q{::} | ||
| 123 | . '_set_critic():(rc)=(%s)', $rc | ||
| 124 | ); | ||
| 125 | |||
| 126 | $cfg{'-profile'} = q{}; # Do not read a .perlcriticrc file! | ||
| 127 | ## no critic (ControlStructures::ProhibitPostfixControls) | ||
| 128 | my $props = $rc->{_}; | ||
| 129 | $log->debugf( __PACKAGE__ . q{::} . '_set_critic():(props)=%s', $props); | ||
| 130 | foreach my $key ( keys %{ $props } ) { | ||
| 131 | my $item_name = ( any { $key } @critic_cfg_props ) ? "-$key" : $key; | ||
| 132 | my $val = $props->{ $key }; | ||
| 133 | $cfg{ $item_name } = $val; | ||
| 134 | } | ||
| 135 | # These two require special handling. | ||
| 136 | foreach my $key ( @critic_cfg_list_props ) { | ||
| 137 | my $val = $props->{ $key }; | ||
| 138 | $cfg{ "-$key" } = [ split qr/\s+/msx, $val ] if( $val ); ## no critic (ControlStructures::ProhibitPostfixControls) | ||
| 139 | } | ||
| 140 | } else { | ||
| 141 | my $props = $git->get_config( "$cfg_section.cfg" ); | ||
| 142 | $log->debugf( __PACKAGE__ . q{::} . '_set_critic():(props)=%s', $props); | ||
| 143 | foreach my $key ( keys %{ $props } ) { | ||
| 144 | my $item_name = ( any { $key } @critic_cfg_props ) ? "-$key" : $key; | ||
| 145 | my $val = $props->{ $key }->[-1]; # The value is a list, take last item from it. | ||
| 146 | $cfg{ $item_name } = $val; | ||
| 147 | } | ||
| 148 | # These two require special handling. | ||
| 149 | foreach my $key ( @critic_cfg_list_props ) { | ||
| 150 | my $val = $props->{ $key }->[-1]; | ||
| 151 | $cfg{ "-$key" } = [ split qr/\s+/msx, $val ] if( $val ); ## no critic (ControlStructures::ProhibitPostfixControls) | ||
| 152 | } | ||
| 153 | } | ||
| 154 | $log->debugf( __PACKAGE__ . q{::} . '_set_critic():(cfg)=(%s)', \%cfg); | ||
| 155 | |||
| 156 | my $pc = Perl::Critic->new( %cfg ); | ||
| 157 | $log->debugf( __PACKAGE__ . q{::} . '_set_critic():(Perl::Critic config)=(%s)', $pc->config); | ||
| 158 | |||
| 159 | # set the format to be a comment | ||
| 160 | my $verbosity = $pc->config->verbose; | ||
| 161 | my $format = Perl::Critic::Utils::verbosity_to_format( $verbosity ); | ||
| 162 | Perl::Critic::Violation::set_format( "# $format" ); | ||
| 163 | |||
| 164 | return { 'critic' => $pc }; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub _check_perl_critic_violations { | ||
| 168 | my ($git, $commit, $cache, $content, $filename) = @_; | ||
| 169 | |||
| 170 | my ($critic) = $cache->{'critic'}; | ||
| 171 | # Source code should only be passed to PPI::Document->new as a SCALAR reference | ||
| 172 | my @violations = $critic->critique( \$content ); | ||
| 173 | $log->debugf( __PACKAGE__ . q{::} | ||
| 174 | . '_check_perl_critic_violations():file=%s, violations=%s', $filename, \@violations); | ||
| 175 | |||
| 176 | return @violations; | ||
| 177 | } | ||
| 178 | |||
| 179 | # TODO Make this dynamic, add more checkers. | ||
| 180 | my @PERL_CHECKERS = ( | ||
| 181 | { | ||
| 182 | name => q{Perl::Critic}, | ||
| 183 | cfg => q{critic}, | ||
| 184 | f_do => \&_check_perl_critic_violations, | ||
| 185 | input => 'FILE_CONTENT', | ||
| 186 | f_pre => \&_set_critic, | ||
| 187 | cache => undef, | ||
| 188 | } | ||
| 189 | ); | ||
| 190 | |||
| 191 | #################### | ||
| 192 | # Main functions | ||
| 193 | |||
| 194 | sub check_new_or_modified_files { | ||
| 195 | my ($git, $commit, @files) = @_; | ||
| 196 | $log->debugf( __PACKAGE__ . q{::} . 'check_new_or_modified_files(%s, %s, %s)', 'git', $commit, \@files); | ||
| 197 | my $errors = 0; | ||
| 198 | |||
| 199 | my $config = $git->get_config( $CFG ); | ||
| 200 | $log->tracef( __PACKAGE__ . q{::} . 'check_new_or_modified_files():config=%s', $config); | ||
| 201 | my @name_patterns; | ||
| 202 | foreach my $pattern (@{ $config->{'name'} }) { | ||
| 203 | # TODO Refactor regex. | ||
| 204 | ## no critic (RegularExpressions::RequireDotMatchAnything) | ||
| 205 | ## no critic (RegularExpressions::RequireExtendedFormatting) | ||
| 206 | ## no critic (RegularExpressions::RequireLineBoundaryMatching) | ||
| 207 | if ($pattern =~ m/^qr(.)(.*)\g{1}/) { | ||
| 208 | $pattern = qr/$2/; | ||
| 209 | } else { | ||
| 210 | $pattern = glob_to_regex($pattern); | ||
| 211 | } | ||
| 212 | push @name_patterns, $pattern; | ||
| 213 | } | ||
| 214 | my @dirpaths = @{ $config->{'dirpath'} }; | ||
| 215 | my @filepaths = @{ $config->{'filepath'} }; | ||
| 216 | $log->debugf( __PACKAGE__ . q{::} . 'check_new_or_modified_files():name_patterns=%s, dirpaths=%s, filepaths=%s', \@name_patterns, \@dirpaths, \@filepaths); | ||
| 217 | foreach my $file ($git->run(qw/ls-files -s/, @files)) { | ||
| 218 | my ($mode, $sha, $n, $filename) = split q{ }, $file; | ||
| 219 | $log->tracef( __PACKAGE__ . q{::} | ||
| 220 | . 'check_new_or_modified_files():(mode, sha, n, name)=(%s, %s, %s, %s)', | ||
| 221 | ($mode, $sha, $n, $filename) | ||
| 222 | ); | ||
| 223 | my $basename = path($filename)->basename; | ||
| 224 | my $dir = path($filename)->parent->stringify; | ||
| 225 | $log->debugf( __PACKAGE__ . q{::} . 'check_new_or_modified_files():basename=%s, dir=%s, filename=%s', $basename, $dir, $filename); | ||
| 226 | # The regexes generated from globs might not be /msx compatible. | ||
| 227 | ## no critic (RegularExpressions::RequireDotMatchAnything) | ||
| 228 | ## no critic (RegularExpressions::RequireExtendedFormatting) | ||
| 229 | ## no critic (RegularExpressions::RequireLineBoundaryMatching) | ||
| 230 | next if( | ||
| 231 | ( none { $basename =~ m/$_/ } @name_patterns ) | ||
| 232 | && ( none { $dir eq $_ } @dirpaths ) | ||
| 233 | && ( none { $filename eq $_ } @filepaths ) | ||
| 234 | ); | ||
| 235 | $log->tracef( __PACKAGE__ . q{::} . 'check_new_or_modified_files(): match found'); | ||
| 236 | my $content = $git->run(qw/cat-file -p/, $sha); | ||
| 237 | $log->tracef( __PACKAGE__ . q{::} . 'check_new_or_modified_files():content=%s', $content); | ||
| 238 | |||
| 239 | foreach my $checker ( @PERL_CHECKERS ) { | ||
| 240 | $log->debugf( __PACKAGE__ . q{::} . 'check_new_or_modified_files():checker=%s', $checker); | ||
| 241 | my @violations; | ||
| 242 | my $active = $config->{'critic'}->{'active'}->[0]; | ||
| 243 | $log->debugf( __PACKAGE__ . q{::} . 'check_new_or_modified_files():active=%s', $active); | ||
| 244 | next if( ! $active ); | ||
| 245 | |||
| 246 | if( ! defined $checker->{'cache'} ) { | ||
| 247 | $checker->{'cache'} = &{ $checker->{'f_pre'} }( $git ); | ||
| 248 | } | ||
| 249 | if( defined $checker->{'cache'}->{'error'} ) { | ||
| 250 | $git->fault($checker->{'name'} . ' Error: ' . $checker->{'cache'}->{'error'}); | ||
| 251 | ++$errors; | ||
| 252 | return $errors; | ||
| 253 | } | ||
| 254 | if( $checker->{'input'} eq q{FILE_CONTENT} ) { | ||
| 255 | @violations = &{$checker->{'f_do'}}( | ||
| 256 | $git, $commit, $checker->{'cache'}, $content, $filename | ||
| 257 | ); | ||
| 258 | } else { | ||
| 259 | # TODO Save file to /tmp dir. Not implemented! | ||
| 260 | } | ||
| 261 | $log->debugf( __PACKAGE__ . q{::} | ||
| 262 | . 'check_new_or_modified_files():file=%s, violations=%s', $filename, \@violations); | ||
| 263 | |||
| 264 | # Report errors | ||
| 265 | foreach my $violation (@violations) { | ||
| 266 | $git->fault($checker->{'name'} . " error in file '$filename':\n" . $violation, | ||
| 267 | {prefix => 'CheckPerl', commit => $commit}); | ||
| 268 | ++$errors; | ||
| 269 | } | ||
| 270 | } | ||
| 271 | } | ||
| 272 | return $errors; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub check_commit { | ||
| 276 | my ($git) = @_; | ||
| 277 | return check_new_or_modified_files($git, ':0', $git->filter_files_in_index('AM')); | ||
| 278 | } | ||
| 279 | sub check_ref { | ||
| 280 | my ($git, $ref) = @_; | ||
| 281 | my ($old_commit, $new_commit) = $git->get_affected_ref_range($ref); | ||
| 282 | return check_new_or_modified_files( | ||
| 283 | $git, | ||
| 284 | $new_commit, | ||
| 285 | $git->filter_files_in_range('AM', $old_commit, $new_commit), | ||
| 286 | ); | ||
| 287 | } | ||
| 288 | sub check_patchset { | ||
| 289 | my ($git, $branch, $commit) = @_; | ||
| 290 | return check_everything($git, $branch, $commit->commit, $commit->extra); | ||
| 291 | } | ||
| 292 | |||
| 293 | # Install hooks | ||
| 294 | my $options = {config => \&_setup_config}; | ||
| 295 | |||
| 296 | GITHOOKS_CHECK_PRE_COMMIT (\&check_commit, $options); | ||
| 297 | GITHOOKS_CHECK_AFFECTED_REFS (\&check_ref, $options); | ||
| 298 | GITHOOKS_CHECK_PATCHSET (\&check_patchset, $options); | ||
| 299 | |||
| 300 | 1; | ||
| 301 | |||
| 302 | __END__ |