blib/lib/Git/Hooks/PerlCritic.pm

filter: Statementstmt 34.3% 22 / 64 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 54.5% 6 / 11 Podpod n/a totaltotal 34.1% 28 / 82 CC 5 SCAR 13.2
CC
5
Cov
31%
_set_critic
6.0
_check_violations
6.0
__ANON__
6.0
CRAP 3.7
↑ Summary
1 package Git::Hooks::PerlCritic;
2 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.001001'; # VERSION
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Module::Load 'load';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Git::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 (my $CFG = __PACKAGE__) =~ s/.*::/githooks./msx;
13  
14 0 ▶sub _changed {
Subroutine _changed: not called CC=1 SCAR=6.9
15 0 my $git = shift;
16  
17 my @changed
18 0 = grep { /\.(p[lm]|t)$/xms }
19 $git->command( qw/diff --cached --name-only --diff-filter=AM/ )
20 ;
21  
22 0 return \@changed;
23 }
24  
25 0 ▶sub _set_critic {
Subroutine _set_critic: not called CC=2 SCAR=17.9
26 0 my ($git) = @_;
27 0 my $pc_rc_filename = $git->get_config($CFG => 'profile');
28  
29 0 load 'Perl::Critic';
30 0 load 'Perl::Critic::Violation';
31 0 load 'Perl::Critic::Utils';
32  
33 0 ▶ my $pc = Perl::Critic->new('-profile' => $pc_rc_filename//q{});
Condition0%0/2
$pc_rc_filename // ""
l!l
00
MC/DC0%0/1
$pc_rc_filename // ""
$pc_rc_filename
Truth table0%0/2
A
A= $pc_rc_filename
Aresult
11
00
34 0 my $verbosity = $pc->config->verbose;
35  
36 # set the format to be a comment
37 0 my $format = Perl::Critic::Utils::verbosity_to_format( $verbosity );
38 0 Perl::Critic::Violation::set_format( "# $format" );
39  
40 0 return $pc;
41 }
42  
43 0 ▶sub _check_violations {
Subroutine _check_violations: not called CC=2 SCAR=17.9
44 0 my $git = shift;
45 0 my $files = shift;
46  
47 0 my @violations;
48 0 foreach my $file ( @{$files} ) {
49 0 state $critic = _set_critic($git);
50  
51 0 @violations = $critic->critique( $file );
52 }
53  
54 0 return \@violations;
55 }
56  
57 0 ▶PREPARE_COMMIT_MSG {
Subroutine __ANON__: not called CC=2 SCAR=17.9
58 0 my ( $git, $commit_msg_file ) = @_;
59  
60 0 my $changed = _changed( $git );
61 0 my $violations = _check_violations( $git, $changed );
62  
63 0 ▶ if ( @{$violations} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$violations;}) 0 0 0
64 0 my $pcf = 'Path::Class::File'; load $pcf;
65 0 my $file = $pcf->new( $commit_msg_file );
66 0 my $contents = $file->slurp;
67  
68 # a space is being prepended, suspect internal join, remove it
69 0 ( $contents .= "@$violations" ) =~ s/^\ #//xmsg;
70  
71 0 $file->spew( $contents );
72 }
73 1 };
74  
75 0 ▶PRE_COMMIT {
Subroutine __ANON__: not called CC=2 SCAR=17.9
76 0 my $git = shift;
77  
78 0 my $changed = _changed( $git );
79 0 my $violations = _check_violations( $git, $changed );
80  
81 0 ▶ if ( @{$violations} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$violations;}) 0 0 0
82 0 print @{$violations};
83 # . operator causes the array ref to give count, otherwise it would
84 # stringify
85 0 croak '# please fix ' . @{$violations} . ' perl critic errors before committing';
86 }
87 1 };
88  
89 1 1;
90  
91 # ABSTRACT: Perl Critic hooks for git
92  
93 __END__
↑ Summary