blib/lib/Git/Critic.pm

filter: Statementstmt 100% 11 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 15 / 15 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Git::Critic;
2  
3 # ABSTRACT: Only run Perl::Critic on lines changed in the current branch
4 1 ▶use v5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use autodie ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 use Capture::Tiny 'capture_stdout';
10 use Carp;
11 use File::Basename 'basename';
12 use List::Util qw(uniq);
13 use Moo;
14 use Types::Standard qw( ArrayRef Bool Int Str);
15  
16 our $VERSION = '0.3';
17  
18 #
19 # Moo attributes
20 #
21  
22 has primary_target => (
23 is => 'ro',
24 isa => Str,
25 required => 1,
26 );
27  
28 has current_target => (
29 is => 'ro',
30 isa => Str,
31 lazy => 1,
32 builder => '_build_current_target',
33 );
34  
35 has max_file_size => (
36 is => 'ro',
37 isa => Int,
38 default => 0,
39 );
40  
41 has severity => (
42 is => 'ro',
43 isa => Int | Str,
44 default => 5,
45 );
46  
47 has verbose => (
48 is => 'ro',
49 isa => Bool,
50 default => 0,
51 );
52  
53 # this is only for tests
54 has _run_test_queue => (
55 is => 'ro',
56 isa => ArrayRef,
57 default => sub { [] },
58 init_arg => undef,
59 );
60  
61 #
62 # Builders
63 #
64  
65 sub _build_current_target {
66 my $self = shift;
67 return $self->_run( 'git', 'rev-parse', '--abbrev-ref', 'HEAD' );
68 }
69  
70 #
71 # The following methods are for the tests
72 #
73  
74 # return true if we have any data in our test queue
75 sub _run_queue_active {
76 my $self = shift;
77 return scalar @{ $self->_run_test_queue };
78 }
79  
80 sub _add_to_run_queue {
81 my ( $self, $result ) = @_;
82 push @{ $self->_run_test_queue } => $result;
83 }
84  
85 sub _get_next_run_queue_response {
86 my $self = shift;
87 shift @{ $self->_run_test_queue };
88 }
89  
90 #
91 # These call system commands
92 #
93  
94 # if we have a response added to the run queue via _add_to_run_queue, return
95 # that instead of calling the system command. Let it die if the system command
96 # fails
97  
98 sub _run {
99 my ( $self, @command ) = @_;
100 if ( $self->_run_queue_active ) {
101 return $self->_get_next_run_queue_response;
102 }
103  
104 if ( $self->verbose ) {
105 say STDERR "Running command: @command";
106 }
107  
108 # XXX yeah, this needs to be more robust
109 chomp( my $result = capture_stdout { system(@command) } );
110 warn $result;
111 return $result;
112 }
113  
114 # same as _run, but don't let it die
115 sub _run_without_die {
116 my ( $self, @command ) = @_;
117 if ( $self->verbose ) {
118 say STDERR "Running command: @command";
119 }
120 chomp(
121 my $result = capture_stdout {
122 no autodie;
123 system(@command);
124 }
125 );
126 return $result;
127 }
128  
129 # get Perl files which have been changed in the current branch
130 sub _get_modified_perl_files {
131 my $self = shift;
132 my $primary_target = $self->primary_target;
133 my $current_target = $self->current_target;
134 my @files = uniq sort grep { /\S/ && $self->_is_perl($_) }
135 split /\n/ => $self->_run( 'git', 'diff', '--name-only',
136 "$primary_target..$current_target" );
137 return @files;
138 }
139  
140 # get the diff of the current file
141 sub _get_diff {
142 my ( $self, $file ) = @_;
143 my $primary_target = $self->primary_target;
144 my $current_target = $self->current_target;
145 my @diff =
146 split /\n/ =>
147 $self->_run( 'git', 'diff', "$primary_target..$current_target", $file );
148 return @diff;
149 }
150  
151 # remove undefined arguments. This makes a command line
152 # script easier to follow
153 around BUILDARGS => sub {
154 my ( $orig, $class, @args ) = @_;
155  
156 my $arg_for = $class->$orig(@args);
157 foreach my $arg ( keys %$arg_for ) {
158 if ( not defined $arg_for->{$arg} ) {
159 delete $arg_for->{$arg};
160 }
161 }
162 return $arg_for;
163 };
164  
165 sub run {
166 my $self = shift;
167  
168 my $primary_target = $self->primary_target;
169 my $current_target = $self->current_target;
170 if ( $primary_target eq $current_target ) {
171  
172 # in the future, we might want to allow you to check the primary
173 # branch X commits back
174 return;
175 }
176  
177 # We walking through every file you've changed and parse the diff to
178 # figure out the start and end of every change you've made. Any perlcritic
179 # failures which are *not* on those lines are ignored
180 my @files = $self->_get_modified_perl_files;
181 my $found;
182 my %reported;
183 my @failures;
184 FILE: foreach my $file (@files) {
185 next FILE unless -e $file; # it was deleted
186 if ( $self->max_file_size ) {
187 next FILE
188 unless -s _ <= $self->max_file_size; # large files are very slow
189 }
190 my $severity = $self->severity;
191 my $critique =
192 $self->_run_without_die( 'perlcritic', "--severity=$severity",
193 $file );
194 next FILE unless $critique; # should never happen unless perlcritic dies
195 my @critiques = split /\n/, $critique;
196  
197 # unified diff format
198 # @@ -3,8 +3,9 @@
199 # @@ from-file-line-numbers to-file-line-numbers @@
200 my @chunks = map {
201 /^ \@\@\s+ -\d+,\d+\s+
202 \+(?<start>\d+)
203 ,(?<lines>\d+)
204 \s+\@\@/xs
205 ? [ $+{start}, $+{start} + $+{lines} ]
206 : ()
207 } $self->_get_diff($file);
208 CRITIQUE: foreach my $this_critique (@critiques) {
209 next CRITIQUE if $this_critique =~ / source OK$/;
210 $this_critique =~ /\bline\s+(?<line_number>\d+)/;
211 unless ( defined $+{line_number} ) {
212 warn "Could not find line number in critique $critique";
213 next;
214 }
215 foreach my $chunk (@chunks) {
216 my ( $min, $max ) = @$chunk;
217 if ( $+{line_number} >= $min && $+{line_number} <= $max ) {
218 unless ($reported{$critique}) {
219 push @failures => "$file: $this_critique"
220 }
221 $reported{$critique}++;
222 next CRITIQUE;
223 }
224 }
225 }
226 }
227 return @failures;
228 }
229  
230 # a heuristic to determine if the file in question is Perl. We might allow
231 # a client to override this in the future
232 sub _is_perl {
233 my ( $self, $file ) = @_;
234 return unless -e $file; # sometimes we get non-existent files
235 return 1 if $file =~ /\.(?:p[ml]|t)$/;
236  
237 # if we got to here, let's check to see if "perl" is in a shebang
238 open my $fh, '<', $file;
239 my $first_line = <$fh>;
240 close $fh;
241 if ( $first_line =~ /^#!.*\bperl\b/ ) {
242 say STDERR "Found changed Perl file: $file" if $self->verbose;
243 return $file;
244 }
245 return;
246 }
247  
248 # vim: filetype=perl
249  
250 1;
251  
252 __END__
↑ Summary