blib/lib/Dist/Zilla/Plugin/Git/NextVersion.pm

filter: Statementstmt 89.9% 107 / 119 Branchbran 70.5% 24 / 34 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.3% 24 / 26 Podpod 0.0% 0 / 4 totaltotal 83.5% 157 / 188 CC 24 SCAR 14.0
CC
24
Cov
85%
__ANON__
12.0
_last_version
8.0
__ANON__
6.1
CRAP 4.1
« blib/lib/Dist/Zilla/Plugin/Git/Check.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/Git/Push.pm »
1 #
2 # This file is part of Dist-Zilla-Plugin-Git
3 #
4 # This software is copyright (c) 2009 by Jerome Quelin.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package Dist::Zilla::Plugin::Git::NextVersion;
13 # ABSTRACT: Provide a version number by bumping the last git release tag
14  
15 4 our $VERSION = '2.052';
16  
17 4 ▶use Dist::Zilla 4 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use version 0.80 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use namespace::autoclean 0.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Types::Standard qw(Str RegexpRef Bool ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Type::Utils qw(coerce from as via subtype);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 4 ▶use constant _cache_fn => '.gitnxtver_cache';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 4 with 'Dist::Zilla::Role::BeforeRelease',
30 'Dist::Zilla::Role::AfterRelease',
31 'Dist::Zilla::Role::FilePruner',
32 'Dist::Zilla::Role::VersionProvider',
33 'Dist::Zilla::Role::Git::Repo';
34  
35 # -- attributes
36  
37 4 ▶use constant _CoercedRegexp => do {
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 my $tc = subtype as RegexpRef;
39 4 ▶ coerce $tc, from Str, via { qr/$_/ };
Subroutine __ANON__: called CC=1 SCAR=0.0
40 4 $tc;
41 4 };
42  
43 has version_regexp => ( is => 'ro', isa => _CoercedRegexp, coerce => 1,
44 19 ▶ default => sub { qr/^v(.+)$/ } );
Subroutine __ANON__: called CC=1 SCAR=0.0
45  
46 4 has first_version => ( is => 'ro', isa => Str, default => '0.001' );
47  
48 4 has version_by_branch => ( is => 'ro', isa => Bool, default => 0 );
49  
50 31 ▶sub _versions_from_tags {
Subroutine _versions_from_tags: called CC=2 SCAR=6.9
51 31 my ($regexp, $tags) = @_;
52  
53 # WARNING: The quotes in "$1" are necessary, because version doesn't
54 # call get magic properly.
55 31 ▶ return [ sort map /$regexp/ ? try { version->parse("$1") } : (), @$tags ];
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
/$regexp/ ? : 64 24 88
56 } # end _versions_from_tags
57  
58 has _all_versions => (
59 is => 'ro', isa => ArrayRef,
60 init_arg => undef,
61 lazy => 1,
62 12 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
63 12 my $self = shift;
64 12 my $v = _versions_from_tags($self->version_regexp, [ $self->git->tag ]);
65 12 ▶ if ($self->logger->get_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->logger->get_debug) 0 12 12
66 0 $self->log_debug("Found version $_") for @$v;
67 }
68 12 $v;
69 }
70 4 );
71  
72 30 ▶sub _max_version {
Subroutine _max_version: called CC=2 SCAR=6.9
73 30 my $versions = shift; # arrayref of versions sorted in ascending order
74  
75 30 ▶ return $versions->[-1]->stringify if @$versions;
Branch: 1 branch
BranchTrueFalseTotal
if @$versions 19 11 30
76  
77 11 return undef;
78 } # end _max_version
79  
80 20 ▶sub _last_version {
Subroutine _last_version: called CC=8 SCAR=20.8
81 20 my ($self) = @_;
82  
83 20 my $last_ver;
84 20 my $by_branch = $self->version_by_branch;
85 20 my $git = $self->git;
86  
87 20 local $/ = "\n"; # Force record separator to be single newline
88  
89 20 ▶ if ($by_branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($by_branch) 12 8 20
90 12 my $head;
91 12 my $cachefile = path($self->zilla->root)->child(_cache_fn);
92 12 ▶ if (-f $cachefile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cachefile) 3 9 12
93 3 ($head) = $git->rev_parse('HEAD');
94 3 ▶ return $1 if $cachefile->slurp =~ /^\Q$head\E (.+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $cachefile->slurp =~ /^\Q$head\E (.+)/ 2 1 3
95 }
96 10 ▶ try {
Subroutine __ANON__: called CC=6 SCAR=18.1
97 # Note: git < 1.6.1 doesn't understand --simplify-by-decoration or %d
98 10 my @tags;
99 10 for ($git->rev_list(qw(--simplify-by-decoration --pretty=%d HEAD))) {
100 48 ▶ /^\s*\((.+)\)/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*\((.+)\)/ or next 24 24 48
101 24 push @tags, split /,\s*/, $1;
102 } # end for lines from git log
103 9 s/^tag:\s+// for @tags; # Git 1.8.3 says "tag: X" instead of "X"
104 9 my $versions = _versions_from_tags($self->version_regexp, \@tags);
105 9 ▶ if ($self->logger->get_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->logger->get_debug) 0 9 9
106 0 $self->log_debug("Found version $_ on branch") for @$versions;
107 }
108 9 $last_ver = _max_version($versions);
109 10 };
110 10 ▶ if (defined $last_ver) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_ver) 7 3 10
111 7 ▶ ($head) = $git->rev_parse('HEAD') unless $head;
Branch: 1 branch
BranchTrueFalseTotal
unless $head 6 1 7
112 7 print { $cachefile->openw } "$head $last_ver\n";
113 7 return $last_ver;
114 }
115 } # end if version_by_branch
116  
117 # Consider versions from all branches:
118 11 $last_ver = _max_version($self->_all_versions);
119  
120 11 ▶ $self->log("WARNING: Unable to find version on current branch")
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_ver and $by_branch 0 11 11
Condition66%2/3
defined $last_ver and $by_branch
!ll&&!rl&&r
740
MC/DC0%0/2
defined $last_ver and $by_branch
defined $last_ver $by_branch
Truth table66%2/3
A and B
A= defined $last_verB= $by_branch
ABresult
0X0
100
111
121 if defined($last_ver) and $by_branch;
122  
123 11 return $last_ver;
124 }
125  
126 # -- role implementation
127  
128 around dump_config => sub
129 0 ▶{
Subroutine __ANON__: not called CC=3 SCAR=24.8
130 0 my $orig = shift;
131 0 my $self = shift;
132  
133 0 my $config = $self->$orig;
134  
135 0 ▶ $config->{+__PACKAGE__} = {
Branch: 2 branches
BranchTrueFalseTotal
$self->version_by_branch ? : 0 0 0
builtin::blessed($self) ne __PACKAGE__ ? : 0 0 0
136 (map +($_ => $self->$_), qw(version_regexp first_version)),
137 version_by_branch => $self->version_by_branch ? 1 : 0,
138 blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
139 };
140  
141 0 return $config;
142 4 };
143  
144 1 ▶sub before_release {
Subroutine before_release: called CC=2 SCAR=7.0
Pod undocumented
145 1 my $self = shift;
146  
147 # Make sure we're not duplicating a version:
148 1 my $version = version->parse( $self->zilla->version );
149  
150 $self->log_fatal("version $version has already been tagged")
151 1 ▶ if grep +($_ == $version), @{ $self->_all_versions };
Branch: 1 branch
BranchTrueFalseTotal
if grep(($_ == $version), @{$self->_all_versions;}) 1 0 1
152 }
153  
154 0 ▶sub after_release {
Subroutine after_release: not called CC=1 SCAR=6.9
Pod undocumented
155 0 my $self = shift;
156  
157 # Remove the cache file, just in case:
158 0 path($self->zilla->root)->child(_cache_fn)->remove;
159 }
160  
161 23 ▶sub provide_version {
Subroutine provide_version: called CC=3 SCAR=11.0
Pod undocumented
162 23 my ($self) = @_;
163  
164 # override (or maybe needed to initialize)
165 23 ▶ return $ENV{V} if exists $ENV{V};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"V"} 3 20 23
166  
167 20 my $last_ver = $self->_last_version;
168  
169 20 ▶ return $self->first_version
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last_ver 7 13 20
170 unless defined $last_ver;
171  
172 13 require Version::Next;
173 13 my $new_ver = Version::Next::next_version($last_ver);
174 13 $self->log("Bumping version from $last_ver to $new_ver");
175  
176 13 return "$new_ver";
177 }
178  
179 1 ▶sub prune_files {
Subroutine prune_files: called CC=3 SCAR=15.1
Pod undocumented
180 1 my $self = shift;
181  
182 1 for my $file (@{ $self->zilla->files }) {
183  
184 # Ensure we don't distribute .gitnxtver_cache:
185 0 ▶ next unless $file->name eq _cache_fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $file->name eq ".gitnxtver_cache" 0 0 0
186  
187 0 $self->log_debug([ 'pruning %s', $file->name ]);
188  
189 0 $self->zilla->prune_file($file);
190 }
191  
192 1 return;
193 } # end prune_files
194  
195 4 __PACKAGE__->meta->make_immutable;
196 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
197 4 1;
198  
199 __END__
« blib/lib/Dist/Zilla/Plugin/Git/Check.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/Git/Push.pm »