Plugin/GitVersionCheckCJM.pm

filter: Statementstmt 90.7% 59 / 65 Branchbran 82.1% 23 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 84.1% 90 / 107 CC 17 SCAR 21.5
CC
17
Cov
86%
munge_file
13.8
munge_files
5.0
_git0
2.0
CRAP 8.6
↑ Summary Plugin/RecommendedPrereqs.pm »
1 #---------------------------------------------------------------------
2 package Dist::Zilla::Plugin::GitVersionCheckCJM;
3 #
4 # Copyright 2009 Christopher J. Madsen
5 #
6 # Author: Christopher J. Madsen <perl@cjmweb.net>
7 # Created: 15 Nov 2009
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the same terms as Perl itself.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
15 # GNU General Public License or the Artistic License for more details.
16 #
17 # ABSTRACT: Ensure version numbers are up-to-date
18 #---------------------------------------------------------------------
19  
20 2 our $VERSION = '4.27';
21 # This file is part of Dist-Zilla-Plugins-CJM 6.000 (December 17, 2017)
22  
23  
24 2 ▶use version 0.77 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 with(
27 'Dist::Zilla::Role::FileMunger',
28 'Dist::Zilla::Role::ModuleInfo',
29 'Dist::Zilla::Role::FileFinderUser' => {
30 default_finders => [ qw(:InstallModules :IncModules :ExecFiles) ],
31 },
32 );
33  
34  
35 2 has single_version => (
36 is => 'ro',
37 isa => 'Bool',
38 default => 0,
39 );
40  
41 # RECOMMEND PREREQ: Git::Wrapper
42 2 ▶use Git::Wrapper (); # AutoPrereqs skips this
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 #---------------------------------------------------------------------
45 # Helper sub to run a git command and split on NULs:
46  
47 sub _git0
48 18 ▶{
Subroutine _git0: called CC=2 SCAR=6.9
49 18 my ($git, $command, @args) = @_;
50  
51 18 my ($result) = do { local $/; $git->$command(@args) };
52  
53 18 ▶ return unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 10 8 18
54  
55 8 split(/\0/, $result);
56 } # end _git0
57  
58 #---------------------------------------------------------------------
59 # Main entry point:
60  
61 9 ▶sub munge_files {
Subroutine munge_files: called CC=5 SCAR=16.1
Pod undocumented
62 9 my ($self) = @_;
63  
64 # Get the released versions:
65 9 my $git = Git::Wrapper->new( $self->zilla->root->stringify );
66  
67 9 ▶ my %released = map { /^v?([\d._]+)$/ ? ($1, 1) : () } $git->tag;
Branch: 1 branch
BranchTrueFalseTotal
/^v?([\d._]+)$/ ? : 36 0 36
68  
69 # Get the list of modified but not-checked-in files:
70 9 my %modified = map { $self->log_debug("mod: $_"); $_ => 1 } (
71 # Files that need to be committed:
72 _git0($git, qw( diff_index -z HEAD --name-only )),
73 # Files that are not tracked by git yet:
74 _git0($git, qw( ls_files -oz --exclude-standard )),
75 );
76  
77 # Get the list of modules:
78 9 my $files = $self->found_files;
79  
80 # Check each module:
81 9 my $errors = 0;
82 9 foreach my $file (@{ $files }) {
83 25 ▶ ++$errors if $self->munge_file($file, $git, \%modified, \%released);
Branch: 1 branch
BranchTrueFalseTotal
if $self->munge_file($file, $git, \%modified, \%released) 5 19 24
84 } # end foreach $file
85  
86 8 ▶ die "Stopped because of errors\n" if $errors;
Branch: 1 branch
BranchTrueFalseTotal
if $errors 5 3 8
87 } # end munge_files
88  
89 #---------------------------------------------------------------------
90 # Check the version of a module:
91  
92 sub munge_file
93 25 ▶{
Subroutine munge_file: called CC=12 SCAR=26.2
Pod undocumented
94 25 my ($self, $file, $git, $modifiedRef, $releasedRef) = @_;
95  
96 # Extract information from the module:
97 25 my $pmFile = $file->name;
98 25 $self->log_debug("checking $pmFile");
99 25 my $pm_info = $self->get_module_info($file);
100  
101 25 ▶ my $version = $pm_info->version
Branch: 1 branch
BranchTrueFalseTotal
my $version = $pm_info->version or $self->log_fatal("ERROR: Can't find version in $pmFile") 24 1 25
102 or $self->log_fatal("ERROR: Can't find version in $pmFile");
103  
104 24 my $distver = version->parse($self->zilla->version);
105  
106 # If module version matches dist version, it's current:
107 # (unless that dist has already been released)
108 24 ▶ if ($version == $distver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == $distver) 11 13 24
109 11 ▶ return unless $releasedRef->{$version};
Branch: 1 branch
BranchTrueFalseTotal
unless $releasedRef->{$version} 7 4 11
110 }
111  
112 # If the module version is greater than the dist version, that's a problem:
113 17 ▶ if ($version > $distver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version > $distver) 1 16 17
114 1 $self->log("ERROR: $pmFile: $version exceeds dist version $distver");
115 1 return 1;
116 }
117  
118 # If all modules must have the same version,
119 # and the module version is less than the dist version, that's a problem:
120 16 ▶ if ($self->single_version and $version < $distver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->single_version and $version < $distver) 0 16 16
Condition33%1/3
$self->single_version and $version < $distver
!ll&&!rl&&r
1600
MC/DC0%0/2
$self->single_version and $version < $distver
$self->single_version $version < $distver
Truth table33%1/3
A and B
A= $self->single_versionB= $version < $distver
ABresult
0X0
100
111
121 0 $self->log("ERROR: $pmFile: $version needs to be updated");
122 0 return 1;
123 }
124  
125 # If the module hasn't been committed yet, it needs updating:
126 # (since it doesn't match the dist version)
127 16 ▶ if ($modifiedRef->{$pmFile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifiedRef->{$pmFile}) 3 13 16
128 3 ▶ if ($version == $distver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == $distver) { } 1 2 3
129 1 $self->log("ERROR: $pmFile: dist version $version needs to be updated");
130 } else {
131 2 $self->log("ERROR: $pmFile: $version needs to be updated");
132 }
133 3 return 1;
134 }
135  
136 # If the module's version doesn't match the dist, and that version
137 # hasn't been released, that's a problem:
138 13 ▶ unless ($releasedRef->{$version}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($releasedRef->{$version}) 1 12 13
139 1 $self->log("ERROR: $pmFile: $version does not seem to have been released, but is not current");
140 1 return 1;
141 }
142  
143 # See if we checked in the module without updating the version:
144 12 my ($lastChangedRev) = $git->rev_list(qw(-n1 HEAD --) => $pmFile);
145  
146 12 my ($inRelease) = $git->name_rev(
147 qw(--refs), "refs/tags/$version",
148 $lastChangedRev
149 );
150  
151 # We're ok if the last change was part of the indicated release:
152 12 ▶ return if $inRelease =~ m! tags/\Q$version\E!;
Branch: 1 branch
BranchTrueFalseTotal
if $inRelease =~ m[ tags/\Q$version\E] 12 0 12
153  
154 0 ▶ if ($version == $distver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == $distver) { } 0 0 0
155 0 $self->log("ERROR: $pmFile: dist version $version needs to be updated");
156 } else {
157 0 $self->log("ERROR: $pmFile: $version needs to be updated");
158 }
159 0 return 1;
160 } # end munge_file
161  
162 #---------------------------------------------------------------------
163 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 2 __PACKAGE__->meta->make_immutable;
165 2 1;
166  
167 __END__
↑ Summary Plugin/RecommendedPrereqs.pm »