blib/lib/Perl/Lint/Git.pm

filter: Statementstmt 100% 89 / 89 Branchbran 70.8% 17 / 24 Conditioncond 43.7% 7 / 16 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 85.1% 138 / 162 CC 21 SCAR 14.6
CC
21
Cov
88%
report_violations
8.5
get_blame_line
6.6
new
5.0
CRAP 4.3
↑ Summary
1 package Perl::Lint::Git;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use File::Basename qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Git::Repository qw( Blame );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Perl::Lint qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 =head1 NAME
14  
15 Perl::Lint::Git - Connect git and Perl::Lint to blame the right people for violations.
16  
17  
18 =head1 VERSION
19  
20 Version 1.1.0
21  
22 =cut
23  
24 10 our $VERSION = '1.1.0';
25  
26  
27 =head1 SYNOPSIS
28  
29 use Perl::Lint::Git;
30 my $git_linter = Perl::Lint::Git->new(
31 file => $file,
32 );
33  
34 my $violations = $git_linter->report_violations(
35 author => $author, # or undef for all
36 since => $date, # to lint only recent changes
37 );
38  
39  
40 =head1 METHODS
41  
42 =head2 new()
43  
44 Create a new Perl::Lint::Git object.
45  
46 my $git_linter = Perl::Lint::Git->new(
47 file => $file,
48 );
49  
50 Parameters:
51  
52 =over 4
53  
54 =item * 'file' I<(mandatory)>
55  
56 The path to a file in a Git repository.
57  
58 =back
59  
60 =cut
61  
62 sub new
63 11 ▶{
Subroutine new: called CC=5 SCAR=16.2
Pod documented
64 11 my ( $class, %args ) = @_;
65 11 my $file = delete( $args{'file'} );
66  
67 # Check parameters.
68 11 ▶ croak "Argument 'file' is needed to create a Perl::Lint::Git object"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or $file eq "" 1 10 11
Condition66%2/3
not defined $file or $file eq ""
l!l&&r!l&&!r
1010
MC/DC50%1/2
not defined $file or $file eq ""
not defined $file $file eq ""
Truth table66%2/3
not(A) or B
A= not defined $fileB= $file eq ""
ABresult
1X1
011
000
69 if !defined( $file ) || ( $file eq '' );
70 10 ▶ croak "Argument 'file' is not a valid file path"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 1 9 10
71 unless -e $file;
72 9 ▶ croak "The following argument(s) are not valid: " . join( ', ', keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 9 9
73 if scalar( keys %args ) != 0;
74  
75 # Create the object.
76 9 my $self = bless(
77 {
78 'file' => $file,
79 'analysis_completed' => 0,
80 'git_output' => undef,
81 'perl_lint_output' => undef,
82 'authors' => undef,
83 },
84 $class
85 );
86  
87 9 return $self;
88 }
89  
90  
91 =head2 get_authors()
92  
93 Return an arrayref of all the authors found in git blame for the file analyzed.
94  
95 my $authors = $git_linter->get_authors();
96  
97 =cut
98  
99 sub get_authors
100 3 ▶{
Subroutine get_authors: called CC=3 SCAR=11.0
Pod documented
101 3 my ( $self ) = @_;
102  
103 3 ▶ unless ( defined( $self->{'authors'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"authors"}) 3 0 3
104 {
105 3 my $blame_lines = $self->get_blame_lines();
106  
107 # Find all the authors listed.
108 3 my $authors = {};
109 3 foreach my $blame_line ( @$blame_lines )
110 {
111 33 my $commit_attributes = $blame_line->get_commit_attributes();
112 33 $authors->{ $commit_attributes->{'author-mail'} } = 1;
113 }
114 3 $self->{'authors'} = [ keys %$authors ];
115 }
116  
117 3 return $self->{'authors'};
118 }
119  
120  
121 =head2 report_violations()
122  
123 Report the violations for a given Git author.
124  
125 my $violations = $git_linter->report_violations(
126 author => $author, # or undef for all
127 since => $date, # to lint only recent changes
128 );
129  
130 Parameters:
131  
132 =over 4
133  
134 =item * author I<(mandatory)>
135  
136 The name of the author to search violations for.
137  
138 =item * since I<(optional)>
139  
140 A date (format YYYY-MM-DD) for which violations that are older will
141 author to fix an entire legacy file at once if only one line needs to be
142 be ignored. This allows linting only recent changes, instead of forcing your
143 modified.
144  
145 =item * use_cache I<(default: 0)>
146  
147 Use a cached version of C<git diff> when available. See
148 L<Git::Repository::Plugin::Blame::Cache> for more information.
149  
150 =back
151  
152 =cut
153  
154 sub report_violations
155 2 ▶{
Subroutine report_violations: called CC=8 SCAR=21.4
Pod documented
156 2 my ( $self, %args ) = @_;
157 2 my $author = delete( $args{'author'} );
158 2 my $since = delete( $args{'since'} );
159 2 ▶ my $use_cache = delete( $args{'use_cache'} ) || 0;
Condition50%1/2
delete $args{'use_cache'} || 0
l!l
02
MC/DC0%0/1
delete $args{'use_cache'} || 0
delete $args{'use_cache'}
Truth table50%1/2
A
A= delete $args{'use_cache'}
Aresult
11
00
160  
161 # Verify parameters.
162 2 ▶ croak 'The argument "author" must be passed'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $author 0 2 2
163 if !defined( $author );
164 2 ▶ croak "The following argument(s) are not valid: " . join( ', ', keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 2 2
165 if scalar( keys %args ) != 0;
166  
167 # Analyze the file.
168 2 $self->_analyze_file(
169 use_cache => $use_cache,
170 );
171  
172 # Run through all the violations and find the ones from the author we're
173 # interested in.
174 2 my $author_violations = [];
175 2 my $perl_lint_violations = $self->get_perl_lint_violations();
176 2 foreach my $violation ( @$perl_lint_violations )
177 {
178 10 my $line_number = $violation->{line};
179 10 my $blame_line = $self->get_blame_line( $line_number );
180 10 my $commit_attributes = $blame_line->get_commit_attributes();
181  
182 # If the author doesn't match, skip.
183 10 ▶ next unless $commit_attributes->{'author-mail'} eq $author;
Branch: 1 branch
BranchTrueFalseTotal
unless $commit_attributes->{"author-mail"} eq $author 5 5 10
184  
185 # If the parameters require filtering by time, do this here before we
186 # add it to the list of violations.
187 5 ▶ next if defined( $since ) && $commit_attributes->{'author-time'} < $since;
Branch: 1 branch
BranchTrueFalseTotal
if defined $since and $commit_attributes->{"author-time"} < $since 0 5 5
Condition33%1/3
defined $since and $commit_attributes->{"author-time"} < $since
!ll&&!rl&&r
500
MC/DC0%0/2
defined $since and $commit_attributes->{"author-time"} < $since
defined $since $commit_attributes->{"author-time"} < $since
Truth table33%1/3
A and B
A= defined $sinceB= $commit_attributes->{"author-time"} < $since
ABresult
0X0
100
111
188  
189 # It passes all the search criteria, add it to the list.
190 5 push( @$author_violations, $violation );
191 }
192  
193 2 return $author_violations;
194 }
195  
196  
197 =head2 force_reanalyzing()
198  
199 Force reanalyzing the file specified by the current object. This is useful
200 if the file has been modified since the Perl::Lint::Git object has been
201 created.
202  
203 $git_critic->force_reanalyzing();
204  
205 =cut
206  
207 sub force_reanalyzing
208 1 ▶{
Subroutine force_reanalyzing: called CC=1 SCAR=0.0
Pod documented
209 1 my ( $self ) = @_;
210  
211 1 $self->_is_analyzed( 0 );
212  
213 1 return 1;
214 }
215  
216  
217 =head1 ACCESSORS
218  
219 =head2 get_perl_lint_violations()
220  
221 Return an arrayref of all the Perl::Lint::Violation objects found by running
222 Perl::Lint on the file specified by the current object.
223  
224 my $perlcritic_violations = $git_critic->get_perlcritic_violations();
225  
226 =cut
227  
228 sub get_perl_lint_violations
229 3 ▶{
Subroutine get_perl_lint_violations: called CC=1 SCAR=0.0
Pod documented
230 3 my ( $self ) = @_;
231  
232 # Analyze the file.
233 3 $self->_analyze_file();
234  
235 3 return $self->{'perl_lint_violations'}
236 }
237  
238  
239 =head2 get_blame_lines()
240  
241 Return an arrayref of Git::Repository::Plugin::Blame::Line objects corresponding
242 to the lines in the file analyzed.
243  
244 my $blame_lines = $self->get_blame_lines();
245  
246 =cut
247  
248 sub get_blame_lines
249 26 ▶{
Subroutine get_blame_lines: called CC=1 SCAR=0.0
Pod documented
250 26 my ( $self ) = @_;
251  
252 # Analyze the file.
253 26 $self->_analyze_file();
254  
255 26 return $self->{'git_blame_lines'};
256 }
257  
258  
259 =head2 get_blame_line()
260  
261 Return a Git::Repository::Plugin::Blame::Line object corresponding to the line
262 number passed as parameter.
263  
264 my $blame_line = $git_critic->get_blame_line( 5 );
265  
266 =cut
267  
268 sub get_blame_line
269 21 ▶{
Subroutine get_blame_line: called CC=5 SCAR=18.9
Pod documented
270 21 my ( $self, $line_number ) = @_;
271  
272 # Verify parameters.
273 21 ▶ croak 'The first parameter must be an integer representing a line number in the file analyzed'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $line_number && $line_number =~ /^\d+$/x or $line_number == 0 0 21 21
Condition33%2/6
not defined $line_number && $line_number =~ /^\d+$/x or $line_number == 0
l!l&&r!l&&!r
0021
defined $line_number && $line_number =~ /^\d+$/x
!ll&&!rl&&r
0021
MC/DC0%0/3
not defined $line_number && $line_number =~ /^\d+$/x or $line_number == 0
defined $line_number $line_number =~ /^\d+$/x $line_number == 0
Truth table25%1/4
not(A && B) or C
A= defined $line_numberB= $line_number =~ /^\d+$/xC= $line_number == 0
ABCresult
0XX1
10X1
1111
1100
274 if !defined( $line_number ) || $line_number !~ m/^\d+$/x || $line_number == 0;
275  
276 21 my $blame_lines = $self->get_blame_lines();
277 21 ▶ croak 'The line number requested does not exist'
Branch: 1 branch
BranchTrueFalseTotal
if $line_number > scalar @$blame_lines 0 21 21
278 if $line_number > scalar( @$blame_lines );
279  
280 21 return $blame_lines->[ $line_number - 1 ];
281 }
282  
283  
284 =head1 INTERNAL METHODS
285  
286 =head2 _analyze_file()
287  
288 Run C<git blame> and L<Perl::Lint> on the file specified by the current object
289 and caches the results to speed reports later.
290  
291 $git_critic->_analyze_file();
292  
293 Arguments:
294  
295 =over 4
296  
297 =item * use_cache (default: 0)
298  
299 Use a cached version of C<git diff> when available.
300  
301 =back
302  
303 =cut
304  
305 sub _analyze_file
306 31 ▶{
Subroutine _analyze_file: called CC=3 SCAR=11.0
307 31 my ( $self, %args ) = @_;
308 31 ▶ my $use_cache = delete( $args{'use_cache'} ) || 0;
Condition50%1/2
delete $args{'use_cache'} || 0
l!l
031
MC/DC0%0/1
delete $args{'use_cache'} || 0
delete $args{'use_cache'}
Truth table50%1/2
A
A= delete $args{'use_cache'}
Aresult
11
00
309  
310 # If the file has already been analyzed, no need to do it again.
311 return
312 31 ▶ if $self->_is_analyzed();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_analyzed 24 7 31
313  
314 7 my $file = $self->_get_file();
315  
316 # Git::Repository uses GIT_DIR and GIT_WORK_TREE to determine the path
317 # to the git repository when those environment variables are present.
318 # This however poses problems here, when those variables point to a
319 # different repository then the one the file to analyze belongs to,
320 # or when they use relative paths.
321 # To force Git::Repository to derive the git repository's path from
322 # the file path, we thus locally delete GIT_DIR and GIT_WORK_TREE.
323 7 local %ENV = %ENV;
324 7 delete( $ENV{'GIT_DIR'} );
325 7 delete( $ENV{'GIT_WORK_TREE'} );
326  
327 # Do a git blame on the file.
328 7 my ( undef, $directory, undef ) = File::Basename::fileparse( $file );
329 7 my $repository = Git::Repository->new( work_tree => $directory );
330 7 $self->{'git_blame_lines'} = $repository->blame(
331 $file,
332 use_cache => $use_cache,
333 );
334  
335 # Run Perl::Lint on the file.
336 7 my $linter = Perl::Lint->new();
337 7 $self->{'perl_lint_violations'} = $linter->lint( [ $file ] );
338  
339 # Flag the file as analyzed.
340 7 $self->_is_analyzed( 1 );
341  
342 7 return;
343 }
344  
345  
346 =head2 _is_analyzed()
347  
348 Return whether the file specified by the current object has already been
349 analyzed with "git blame" and "PerlLint".
350  
351 my $is_analyzed = $git_critic->_is_analyzed();
352  
353 =cut
354  
355 sub _is_analyzed
356 43 ▶{
Subroutine _is_analyzed: called CC=2 SCAR=6.9
357 43 my ( $self, $value ) = @_;
358  
359 43 ▶ $self->{'analysis_completed'} = $value
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 8 35 43
360 if defined( $value );
361  
362 43 return $self->{'analysis_completed'};
363 }
364  
365  
366 =head2 _get_file()
367  
368 Return the path to the file to analyze for the current object.
369  
370 my $file = $git_critic->_get_file();
371  
372 =cut
373  
374 sub _get_file
375 8 ▶{
Subroutine _get_file: called CC=1 SCAR=0.0
376 8 my ( $self ) = @_;
377  
378 8 return $self->{'file'};
379 }
380  
381  
382 =head1 SEE ALSO
383  
384 =over 4
385  
386 =item * L<Perl::Lint>
387  
388 =back
389  
390  
391 =head1 BUGS
392  
393 Please report any bugs or feature requests through the web interface at
394 L<https://github.com/guillaumeaubert/Perl-Lint-Git/issues>.
395 I will be notified, and then you'll automatically be notified of progress on
396 your bug as I make changes.
397  
398  
399 =head1 SUPPORT
400  
401 You can find documentation for this module with the perldoc command.
402  
403 perldoc Perl::Lint::Git
404  
405  
406 You can also look for information at:
407  
408 =over 4
409  
410 =item * GitHub (report bugs there)
411  
412 L<https://github.com/guillaumeaubert/Perl-Lint-Git/issues>
413  
414 =item * AnnoCPAN: Annotated CPAN documentation
415  
416 L<http://annocpan.org/dist/Perl-Lint-Git>
417  
418 =item * CPAN Ratings
419  
420 L<http://cpanratings.perl.org/d/Perl-Lint-Git>
421  
422 =item * MetaCPAN
423  
424 L<https://metacpan.org/release/Perl-Lint-Git>
425  
426 =back
427  
428  
429 =head1 AUTHOR
430  
431 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
432 C<< <aubertg at cpan.org> >>.
433  
434  
435 =head1 COPYRIGHT & LICENSE
436  
437 Copyright 2015-2017 Guillaume Aubert.
438  
439 This code is free software; you can redistribute it and/or modify it under the
440 same terms as Perl 5 itself.
441  
442 This program is distributed in the hope that it will be useful, but WITHOUT ANY
443 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
444 PARTICULAR PURPOSE. See the LICENSE file for more details.
445  
446 =cut
447  
448 10 1;
↑ Summary