blib/lib/Dist/Zilla/Plugin/ChangeStats/Dependencies/Git.pm

filter: Statementstmt 32.5% 52 / 160 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 54.1% 13 / 24 Podpod 0.0% 0 / 5 totaltotal 23.1% 65 / 281 CC 46 SCAR 60.5
CC
46
Cov
22%
munge_files
930.0
add_stats
110.0
phase_relation
30.0
CRAP 423.5
↑ Summary
1 2 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Dist::Zilla::Plugin::ChangeStats::Dependencies::Git;
6  
7 # ABSTRACT: Add dependency changes to the changelog
8 2 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9 2 our $VERSION = '0.0200';
10  
11 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Types::Standard qw/ArrayRef Bool HashRef Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Git::Repository;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Module::CPANfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use CPAN::Changes;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use CPAN::Changes::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use JSON::MaybeXS qw/decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 with qw/
23 Dist::Zilla::Role::Plugin
24 Dist::Zilla::Role::FileMunger
25 /;
26  
27 0 ▶sub mvp_multivalue_args { qw/stats_skip_file stats_skip_match/ }
Subroutine mvp_multivalue_args: not called CC=1 SCAR=6.9
Pod undocumented
28  
29 has repo => (
30 is => 'ro',
31 0 ▶ default => sub { Git::Repository->new(work_tree => '.')},
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 2 );
33 2 has change_file => (
34 is => 'ro',
35 isa => Str,
36 default => 'Changes',
37 );
38 2 has group => (
39 is => 'ro',
40 isa => Str,
41 default => '',
42 );
43 2 has format_tag => (
44 is => 'ro',
45 isa => Str,
46 default => '%s',
47 );
48 2 has add_to_first_release => (
49 is => 'ro',
50 isa => Bool,
51 default => 0,
52 );
53  
54 2 has do_stats => (
55 is => 'ro',
56 isa => Bool,
57 default => 0,
58 );
59 has stats_skip_file => (
60 is => 'ro',
61 isa => ArrayRef[Str],
62 traits => ['Array'],
63 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 2 handles => {
65 all_stats_skip_files => 'elements',
66 has_stats_skip_files => 'count',
67 }
68 );
69 has stats_skip_match => (
70 is => 'ro',
71 isa => ArrayRef[Str],
72 traits => ['Array'],
73 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 2 handles => {
75 all_stats_skip_matches => 'elements',
76 has_stats_skip_matches => 'count',
77 }
78 );
79 2 has stats_text => (
80 is => 'ro',
81 isa => Str,
82 default => 'Code churn',
83 );
84  
85  
86  
87 0 ▶sub munge_files {
Subroutine munge_files: not called CC=30 SCAR=68.4
Pod undocumented
88 0 my $self = shift;
89  
90 0 my($file) = grep { $_->name eq $self->change_file } @{ $self->zilla->files };
91  
92 0 ▶ if(!defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 0 0
93 0 $self->log(['Could not find changelog (%s) - nothing to do', $self->change_file]);
94 0 return;
95 }
96  
97 0 my $changes = CPAN::Changes->load_string($file->content, next_token => $self->_next_token);
98 0 my($this_release) = ($changes->releases)[-1];
99 0 ▶ if($this_release->version ne '{{$NEXT}}') {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_release->version ne "{{\$NEXT}}") 0 0 0
100 0 $self->log(['Could not find {{$NEXT}} token - skips']);
101 0 return;
102 }
103  
104 0 ▶ if(!path('META.json')->exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless (path("META.json")->exists) 0 0 0
105 0 $self->log(['Could not find META.json in distribution root - skips']);
106 0 return;
107 }
108 0 my $current_meta = decode_json(path('META.json')->slurp)->{'prereqs'};
109  
110 0 my($previous_release) = grep { $_->version ne '{{$NEXT}}' } reverse $changes->releases;
111  
112 0 ▶ my $is_first_release = defined $previous_release ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $previous_release ? : 0 0 0
113  
114 0 my $tag_meta;
115 my $git_tag;
116 0 ▶ if($self->add_to_first_release && $is_first_release) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->add_to_first_release and $is_first_release) { } 0 0 0
elsif ($is_first_release) { } 0 0 0
Condition0%0/3
$self->add_to_first_release and $is_first_release
!ll&&!rl&&r
000
MC/DC0%0/2
$self->add_to_first_release and $is_first_release
$self->add_to_first_release $is_first_release
Truth table0%0/3
A and B
A= $self->add_to_first_releaseB= $is_first_release
ABresult
0X0
100
111
117 0 $self->log(['First release - adds all dependencies']);
118 0 $tag_meta = {}; # fake meta
119 }
120 elsif($is_first_release) {
121 0 $self->log(['Has no earlier versions in changelog - no dependency changes']);
122 0 return;
123 }
124 else {
125 0 $self->log_debug(['Will compare dependencies with %s'], $previous_release->version);
126 0 $git_tag = sprintf $self->format_tag, $previous_release->version;
127  
128 0 $tag_meta = $self->get_meta($git_tag);
129 0 ▶ if(!defined $tag_meta || !defined $current_meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag_meta and defined $current_meta) 0 0 0
Condition0%0/3
defined $tag_meta and defined $current_meta
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tag_meta and defined $current_meta
defined $tag_meta defined $current_meta
Truth table0%0/3
A and B
A= defined $tag_metaB= defined $current_meta
ABresult
0X0
100
111
130 0 return;
131 }
132 }
133  
134 0 my @all_requirement_changes = ();
135  
136 PHASE:
137 0 for my $phase (qw/runtime test build configure develop/) {
138 RELATION:
139 0 for my $relation (qw/requires recommends suggests/) {
140 0 my $requirement_changes = {
141 added => [],
142 changed => [],
143 removed => [],
144 };
145  
146 0 ▶ my $prev = $tag_meta->{ $phase }{ $relation } || {};
Condition0%0/2
$tag_meta->{$phase}{$relation} || {}
l!l
00
MC/DC0%0/1
$tag_meta->{$phase}{$relation} || {}
$tag_meta->{$phase}{$relation}
Truth table0%0/2
A
A= $tag_meta->{$phase}{$relation}
Aresult
11
00
147 0 ▶ my $now = $current_meta->{ $phase }{ $relation } || {};
Condition0%0/2
$current_meta->{$phase}{$relation} || {}
l!l
00
MC/DC0%0/1
$current_meta->{$phase}{$relation} || {}
$current_meta->{$phase}{$relation}
Truth table0%0/2
A
A= $current_meta->{$phase}{$relation}
Aresult
11
00
148  
149 0 ▶ next RELATION if !scalar keys %{ $prev } && !scalar keys %{ $now };
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$prev;} or scalar keys %{$now;} 0 0 0
Condition0%0/3
scalar keys %{$prev;} or scalar keys %{$now;}
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar keys %{$prev;} or scalar keys %{$now;}
scalar keys %{$prev;} scalar keys %{$now;}
Truth table0%0/3
A or B
A= scalar keys %{$prev;}B= scalar keys %{$now;}
ABresult
1X1
011
000
150  
151 # What is in the current release that wasn't in (or has changed since) the last release.
152 MODULE:
153 0 for my $module (sort keys %{ $now }) {
154 0 ▶ my $current_version = delete $now->{ $module } || '(any)';
Condition0%0/2
delete $now->{$module} || '(any)'
l!l
00
MC/DC0%0/1
delete $now->{$module} || '(any)'
delete $now->{$module}
Truth table0%0/2
A
A= delete $now->{$module}
Aresult
11
00
155 0 ▶ my $previous_version = exists $prev->{ $module } ? delete $prev->{ $module } : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $prev->{$module} ? : 0 0 0
156  
157 0 ▶ if(!defined $previous_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $previous_version) 0 0 0
158 0 push @{ $requirement_changes->{'added'} } => "$module $current_version";
159 0 next MODULE;
160 }
161  
162 0 ▶ $previous_version = $previous_version || '(any)';
Condition0%0/2
$previous_version || '(any)'
l!l
00
MC/DC0%0/1
$previous_version || '(any)'
$previous_version
Truth table0%0/2
A
A= $previous_version
Aresult
11
00
163 0 ▶ if($current_version ne $previous_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_version ne $previous_version) 0 0 0
164 0 push @{ $requirement_changes->{'changed'} } => "$module $previous_version --> $current_version";
165 }
166 }
167 # What was in the last release that currenly isn't there
168 0 for my $module (sort keys %{ $prev }) {
169 0 push @{ $requirement_changes->{'removed'} } => $module;
170 }
171  
172 # Add requirement changes to overall list
173 0 for my $type (qw/added changed removed/) {
174 0 ▶ my $char = $type eq 'added' ? '+' : $type eq 'changed' ? '~' : $type eq 'removed' ? '-' : '!';
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'added' ? : 0 0 0
$type eq 'changed' ? : 0 0 0
$type eq 'removed' ? : 0 0 0
175  
176 0 for my $module (@{ $requirement_changes->{ $type }}) {
177 0 push @all_requirement_changes => ($self->phase_relation($phase, $relation) . " $char $module");
178 }
179 }
180 }
181 }
182  
183 0 my $group = $this_release->get_group($self->group);
184 0 ▶ $self->add_stats($group, $git_tag) if !$is_first_release && $self->do_stats;
Branch: 1 branch
BranchTrueFalseTotal
if not $is_first_release and $self->do_stats 0 0 0
Condition0%0/3
not $is_first_release and $self->do_stats
!ll&&!rl&&r
000
MC/DC0%0/2
not $is_first_release and $self->do_stats
not $is_first_release $self->do_stats
Truth table0%0/3
not(A) and B
A= not $is_first_releaseB= $self->do_stats
ABresult
0X0
100
111
185 0 $group->add_changes(@all_requirement_changes);
186 0 $file->content($changes->serialize);
187 }
188  
189 0 ▶sub get_meta {
Subroutine get_meta: not called CC=3 SCAR=24.8
Pod undocumented
190 0 my $self = shift;
191 0 my $tag = shift;
192  
193 0 my(@tags) = $self->repo->run('tag');
194 0 my($found) = grep { $_ eq $tag } @tags;
195  
196 0 ▶ if(!$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
197 0 $self->log(['Could not find tag %s - skipping', $tag]);
198 0 return;
199 }
200  
201 0 my $show_output;
202 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
203 0 ($show_output) = join '' => $self->repo->run('show', join ':' => ($tag, 'META.json'));
204 }
205 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
206 0 ▶ if($_ =~ m{^fatal:}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^fatal:/) 0 0 0
207 0 $self->log(['Could not find META.json in %s - skipping', $tag]);
208 }
209 0 die $_;
210 0 };
211 0 ▶ return if !defined $show_output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $show_output 0 0 0
212 0 return decode_json($show_output)->{'prereqs'};
213 }
214  
215 0 ▶sub phase_relation {
Subroutine phase_relation: not called CC=5 SCAR=34.0
Pod undocumented
216 0 my $self = shift;
217 0 my $phase = shift;
218 0 my $relation = shift;
219  
220 0 ▶ $phase = $phase eq 'runtime' ? 'run'
Branch: 4 branches
BranchTrueFalseTotal
$phase eq 'runtime' ? : 0 0 0
$phase eq 'test' ? : 0 0 0
$phase eq 'configure' ? : 0 0 0
$phase eq 'develop' ? : 0 0 0
221 : $phase eq 'test' ? 'test'
222 : $phase eq 'configure' ? 'conf'
223 : $phase eq 'develop' ? 'dev'
224 : $phase
225 ;
226 0 $relation = substr $relation, 0, 3;
227  
228 0 return "($phase $relation)";
229 }
230  
231 0 ▶sub _next_token { qr/\{\{\$NEXT\}\}/ }
Subroutine _next_token: not called CC=1 SCAR=6.9
232  
233 0 ▶sub add_stats {
Subroutine add_stats: not called CC=10 SCAR=47.0
Pod undocumented
234 0 my $self = shift;
235 0 my $group = shift;
236 0 my $git_tag = shift;
237  
238 0 my @numstats = $self->repo->run(qw/diff --numstat/, $git_tag);
239 0 my $counter = {
240 files => 0,
241 insertions => 0,
242 deletions => 0,
243 };
244  
245 FILE:
246 0 for my $file (@numstats) {
247 0 my($insertions, $deletions, $path) = split /\s+/, $file, 3;
248 0 ▶ next FILE if grep { $path eq $_ } $self->all_stats_skip_files;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$path eq $_;} $self->all_stats_skip_files 0 0 0
249 0 ▶ next FILE if grep { $path =~ m{$_}i } $self->all_stats_skip_matches;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$path =~ /$_/i;} $self->all_stats_skip_matches 0 0 0
250  
251 # binary files get '-'
252 0 ++$counter->{'files'};
253 0 ▶ $counter->{'insertions'} += $insertions =~ m{^\d+$} ? $insertions : 0;
Branch: 1 branch
BranchTrueFalseTotal
$insertions =~ /^\d+$/ ? : 0 0 0
254 0 ▶ $counter->{'deletions'} += $deletions =~ m{^\d+$} ? $deletions : 0;
Branch: 1 branch
BranchTrueFalseTotal
$deletions =~ /^\d+$/ ? : 0 0 0
255 }
256  
257 my $output = sprintf '%d file%s changed, %d insertion%s(+), %d deletion%s(-)',
258 $counter->{'files'},
259 $counter->{'files'} == 1 ? '': 's',
260 $counter->{'insertions'},
261 $counter->{'insertions'} == 1 ? '': 's',
262 $counter->{'deletions'},
263 0 ▶ $counter->{'deletions'} == 1 ? '': 's';
Branch: 3 branches
BranchTrueFalseTotal
$counter->{'files'} == 1 ? : 0 0 0
$counter->{'insertions'} == 1 ? : 0 0 0
$counter->{'deletions'} == 1 ? : 0 0 0
264  
265 0 ▶ my $intro = length $self->stats_text ? $self->stats_text . ': ' : '';
Branch: 1 branch
BranchTrueFalseTotal
length $self->stats_text ? : 0 0 0
266  
267 0 $group->add_changes($intro . $output);
268 }
269  
270 2 __PACKAGE__->meta->make_immutable;
271  
272 2 1;
273  
274 __END__
↑ Summary