| 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 File::Temp 'tempfile'; | ||
| 13 | use List::Util qw(uniq); | ||
| 14 | use Moo; | ||
| 15 | use Types::Standard qw( ArrayRef Bool Int Str); | ||
| 16 | |||
| 17 | our $VERSION = '0.4'; | ||
| 18 | |||
| 19 | # | ||
| 20 | # Moo attributes | ||
| 21 | # | ||
| 22 | |||
| 23 | has primary_target => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => Str, | ||
| 26 | required => 1, | ||
| 27 | ); | ||
| 28 | |||
| 29 | has current_target => ( | ||
| 30 | is => 'ro', | ||
| 31 | isa => Str, | ||
| 32 | lazy => 1, | ||
| 33 | builder => '_build_current_target', | ||
| 34 | ); | ||
| 35 | |||
| 36 | has max_file_size => ( | ||
| 37 | is => 'ro', | ||
| 38 | isa => Int, | ||
| 39 | default => 0, | ||
| 40 | ); | ||
| 41 | |||
| 42 | has severity => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => Int | Str, | ||
| 45 | default => 5, | ||
| 46 | ); | ||
| 47 | |||
| 48 | has verbose => ( | ||
| 49 | is => 'ro', | ||
| 50 | isa => Bool, | ||
| 51 | default => 0, | ||
| 52 | ); | ||
| 53 | |||
| 54 | # this is only for tests | ||
| 55 | has _run_test_queue => ( | ||
| 56 | is => 'ro', | ||
| 57 | isa => ArrayRef, | ||
| 58 | default => sub { [] }, | ||
| 59 | init_arg => undef, | ||
| 60 | ); | ||
| 61 | |||
| 62 | # | ||
| 63 | # Builders | ||
| 64 | # | ||
| 65 | |||
| 66 | sub _build_current_target { | ||
| 67 | my $self = shift; | ||
| 68 | return $self->_run( 'git', 'rev-parse', '--abbrev-ref', 'HEAD' ); | ||
| 69 | } | ||
| 70 | |||
| 71 | # | ||
| 72 | # The following methods are for the tests | ||
| 73 | # | ||
| 74 | |||
| 75 | # return true if we have any data in our test queue | ||
| 76 | sub _run_queue_active { | ||
| 77 | my $self = shift; | ||
| 78 | return scalar @{ $self->_run_test_queue }; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _add_to_run_queue { | ||
| 82 | my ( $self, $result ) = @_; | ||
| 83 | push @{ $self->_run_test_queue } => $result; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _get_next_run_queue_response { | ||
| 87 | my $self = shift; | ||
| 88 | shift @{ $self->_run_test_queue }; | ||
| 89 | } | ||
| 90 | |||
| 91 | # | ||
| 92 | # These call system commands | ||
| 93 | # | ||
| 94 | |||
| 95 | # if we have a response added to the run queue via _add_to_run_queue, return | ||
| 96 | # that instead of calling the system command. Let it die if the system command | ||
| 97 | # fails | ||
| 98 | |||
| 99 | sub _run { | ||
| 100 | my ( $self, @command ) = @_; | ||
| 101 | if ( $self->_run_queue_active ) { | ||
| 102 | return $self->_get_next_run_queue_response; | ||
| 103 | } | ||
| 104 | |||
| 105 | if ( $self->verbose ) { | ||
| 106 | say STDERR "Running command: @command"; | ||
| 107 | } | ||
| 108 | |||
| 109 | # XXX yeah, this needs to be more robust | ||
| 110 | chomp( my $result = capture_stdout { system(@command) } ); | ||
| 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 %reported; | ||
| 182 | my @failures; | ||
| 183 | FILE: foreach my $file (@files) { | ||
| 184 | my $file_text = $self->_run( 'git', 'show', "${current_target}:$file" ) | ||
| 185 | or next FILE; | ||
| 186 | if ( $self->max_file_size ) { | ||
| 187 | # we want the lenght in bytes, not characters | ||
| 188 | use bytes; | ||
| 189 | next FILE | ||
| 190 | unless length($file_text) <= $self->max_file_size; # large files are very slow | ||
| 191 | } | ||
| 192 | |||
| 193 | my ($fh, $filename) = tempfile(); | ||
| 194 | print $fh $file_text; | ||
| 195 | close $fh; | ||
| 196 | my $severity = $self->severity; | ||
| 197 | my $critique = | ||
| 198 | $self->_run_without_die( 'perlcritic', "--severity=$severity", | ||
| 199 | $filename ); | ||
| 200 | next FILE unless $critique; # should never happen unless perlcritic dies | ||
| 201 | my @critiques = split /\n/, $critique; | ||
| 202 | |||
| 203 | # unified diff format | ||
| 204 | # @@ -3,8 +3,9 @@ | ||
| 205 | # @@ from-file-line-numbers to-file-line-numbers @@ | ||
| 206 | my @chunks = map { | ||
| 207 | /^ \@\@\s+ -\d+,\d+\s+ | ||
| 208 | \+(?<start>\d+) | ||
| 209 | ,(?<lines>\d+) | ||
| 210 | \s+\@\@/xs | ||
| 211 | ? [ $+{start}, $+{start} + $+{lines} ] | ||
| 212 | : () | ||
| 213 | } $self->_get_diff($file); | ||
| 214 | my $max_line_number = $chunks[-1][-1]; | ||
| 215 | CRITIQUE: foreach my $this_critique (@critiques) { | ||
| 216 | next CRITIQUE if $this_critique =~ / source OK$/; | ||
| 217 | $this_critique =~ /\bline\s+(?<line_number>\d+)/; | ||
| 218 | unless ( defined $+{line_number} ) { | ||
| 219 | warn "Could not find line number in critique $this_critique"; | ||
| 220 | next; | ||
| 221 | } | ||
| 222 | |||
| 223 | # no need to keep processing | ||
| 224 | last CRITIQUE if $+{line_number} > $max_line_number; | ||
| 225 | |||
| 226 | foreach my $chunk (@chunks) { | ||
| 227 | my ( $min, $max ) = @$chunk; | ||
| 228 | if ( $+{line_number} >= $min && $+{line_number} <= $max ) { | ||
| 229 | unless ($reported{$this_critique}) { | ||
| 230 | push @failures => "$file: $this_critique" | ||
| 231 | } | ||
| 232 | $reported{$this_critique}++; | ||
| 233 | next CRITIQUE; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | return @failures; | ||
| 239 | } | ||
| 240 | |||
| 241 | # a heuristic to determine if the file in question is Perl. We might allow | ||
| 242 | # a client to override this in the future | ||
| 243 | sub _is_perl { | ||
| 244 | my ( $self, $file ) = @_; | ||
| 245 | return unless -e $file; # sometimes we get non-existent files | ||
| 246 | return 1 if $file =~ /\.(?:p[ml]|t)$/; | ||
| 247 | |||
| 248 | # if we got to here, let's check to see if "perl" is in a shebang | ||
| 249 | open my $fh, '<', $file; | ||
| 250 | my $first_line = <$fh>; | ||
| 251 | close $fh; | ||
| 252 | if ( $first_line =~ /^#!.*\bperl\b/ ) { | ||
| 253 | say STDERR "Found changed Perl file: $file" if $self->verbose; | ||
| 254 | return $file; | ||
| 255 | } | ||
| 256 | return; | ||
| 257 | } | ||
| 258 | |||
| 259 | # vim: filetype=perl | ||
| 260 | |||
| 261 | 1; | ||
| 262 | |||
| 263 | __END__ | ||