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

filter: Statementstmt 28.8% 39 / 135 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 41.6% 10 / 24 Podpod 100% 7 / 7 totaltotal 25.8% 56 / 217 CC 29 SCAR 32.8
CC
29
Cov
22%
trailers_in
90.0
last_release_tag
30.0
items
30.0
CRAP 26.7
↑ Summary
1 package Dist::Zilla::Plugin::Git::ChangelogFromTrailers 0.002;
2  
3 # ABSTRACT: Build the Changes entry for a release from the Changelog trailers of its commits
4  
5 1 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use re '/aasx';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Moose; ## no critic (ProhibitUnusedImports) imports has, extends and with, which the policy does not see as calls
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 extends 'Dist::Zilla::Plugin::NextRelease';
12 1 with 'Dist::Zilla::Role::FileGatherer', 'Dist::Zilla::Role::BeforeRelease';
13  
14 1 ▶use Dist::Zilla::File::InMemory ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Git::Wrapper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use List::Util qw{any first max uniq};
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use namespace::autoclean; ## no critic (ProhibitUnusedImports) loaded for its effect, which removes the imports from the class
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 1 has trailer => (
22 is => 'ro',
23 isa => 'Str',
24 default => 'Changelog',
25 );
26  
27  
28 1 has version_regexp => (
29 is => 'ro',
30 isa => 'Str',
31 default => '^v?(\d+(?:\.\d+)*(?:_\d+)?)$',
32 );
33  
34 1 has _entry => (
35 is => 'ro',
36 isa => 'Maybe[Str]',
37 lazy => 1,
38 init_arg => undef,
39 builder => '_build_entry',
40 );
41  
42 1 has _created => (
43 is => 'rw',
44 isa => 'Bool',
45 default => 0,
46 init_arg => undef,
47 );
48  
49 1 has _changes => (
50 is => 'rw',
51 isa => 'Str',
52 init_arg => undef,
53 );
54  
55 has _git => (
56 is => 'ro',
57 isa => 'Git::Wrapper',
58 lazy => 1,
59 init_arg => undef,
60 0 ▶ default => sub { Git::Wrapper->new( $_[0]->zilla->root->stringify ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 1 );
62  
63 # The name of the last release tag: of the tags that are reachable from HEAD
64 # and match version_regexp, the one with the highest version.
65 # @returns Str|Undef the name of the tag, or undef if no tag matches
66 # @throws dies with a Git::Wrapper::Exception when git tag fails
67 # @example my $tag = $plugin->last_release_tag;
68 0 ▶sub last_release_tag {
Subroutine last_release_tag: not called CC=5 SCAR=34.0
Pod documented
69 0 my ($self) = @_;
70 0 my $regexp = $self->version_regexp;
71  
72 0 my %version_of;
73 0 foreach my $tag ( $self->_git->tag( { merged => 'HEAD' } ) ) {
74 0 my ($v) = $tag =~ m/$regexp/;
75 0 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
76 0 my $parsed = eval { version->parse($v) };
77 0 ▶ $version_of{$tag} = $parsed if defined $parsed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parsed 0 0 0
78 }
79 0 ▶ return unless %version_of;
Branch: 1 branch
BranchTrueFalseTotal
unless %version_of 0 0 0
80  
81 0 my $highest = max values %version_of;
82 0 ▶ return first { $version_of{$_} == $highest } sort keys %version_of;
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 }
84  
85 # The values of the trailers in the commits since the last release tag, in the
86 # order that L</Trailers> describes. Nothing when the repository has no commit.
87 # @returns Str the values, as a list
88 # @throws dies with a Git::Wrapper::Exception when git tag or git log fails
89 # @example my @items = $plugin->items;
90 0 ▶sub items {
Subroutine items: not called CC=5 SCAR=34.0
Pod documented
91 0 my ($self) = @_;
92 0 my $git = $self->_git;
93  
94 # A repository with no commits has no HEAD to read a log from.
95 0 ▶ return unless eval { $git->rev_parse( { verify => 1, quiet => 1 }, 'HEAD' ); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $git->rev_parse({"verify", 1, "quiet", 1}, "HEAD"); 1 } } 0 0 0
96  
97 0 my $tag = $self->last_release_tag;
98 0 my $key = $self->trailer;
99  
100 # Git::Wrapper refuses --format in log, so RUN gives the raw lines. Each
101 # commit starts with a record separator, and a unit separator divides the
102 # trailers that git reads from the whole message.
103 0 ▶ my $log = join "\n", $git->RUN(
Branch: 1 branch
BranchTrueFalseTotal
defined $tag ? : 0 0 0
104 log => { reverse => 1, format => "%x1e%(trailers:key=$key,valueonly,unfold)%x1f%B" },
105 defined $tag ? "$tag..HEAD" : 'HEAD',
106 );
107 0 my @values;
108 0 foreach my $commit ( grep { $_ ne q{} } split /\x1e/, $log ) {
109 0 my ( $trailers, $message ) = split /\x1f/, $commit, 2;
110 0 ▶ push @values, $self->trailers_in( $message // q{} ), split /\n/, $trailers;
Condition0%0/2
$message // ""
l!l
00
MC/DC0%0/1
$message // ""
$message
Truth table0%0/2
A
A= $message
Aresult
11
00
111 }
112 0 return uniq grep { $_ ne q{} } map { s/^\s+//r =~ s/\s+\z//r } @values;
113 }
114  
115 # The values of the trailers of L</trailer> in each paragraph of the commit
116 # message C<$message> that is a block of trailers, in their order. A paragraph
117 # is such a block when its first line is a trailer, and each line after it is a
118 # trailer or continues the one above it with leading whitespace. A continued
119 # trailer is joined into one line. git reads trailers only from the last
120 # paragraph, so this finds those that a squash merge left above it.
121 # @param message Str the whole commit message
122 # @returns Str the values, as a list
123 # @example my @values = $plugin->trailers_in("Subject\n\nChangelog: A change\n\nCo-authored-by: A. Tester <tester\@test.test>");
124 0 ▶sub trailers_in {
Subroutine trailers_in: not called CC=9 SCAR=45.0
Pod documented
125 0 my ( $self, $message ) = @_;
126 0 my $key = lc $self->trailer;
127 0 my @values;
128 0 foreach my $paragraph ( split /\n[ \t]*\n/, $message ) {
129 0 my @lines = grep { $_ ne q{} } split /\n/, $paragraph;
130 0 ▶ next if !@lines || $lines[0] !~ m/\A[[:alnum:]][[:alnum:]-]*\s*:/;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines and $lines[0] =~ /\A[[:alnum:]][[:alnum:]-]*\s*:/aasx 0 0 0
Condition0%0/3
@lines and $lines[0] =~ /\A[[:alnum:]][[:alnum:]-]*\s*:/aasx
!ll&&!rl&&r
000
MC/DC0%0/2
@lines and $lines[0] =~ /\A[[:alnum:]][[:alnum:]-]*\s*:/aasx
@lines $lines[0] =~ /\A[[:alnum:]][[:alnum:]-]*\s*:/aasx
Truth table0%0/3
A and B
A= @linesB= $lines[0] =~ /\A[[:alnum:]][[:alnum:]-]*\s*:/aasx
ABresult
0X0
100
111
131 0 ▶ next if any { !m/\A(?:[[:alnum:]][[:alnum:]-]*\s*:|\s+\S)/ } @lines;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not /\A(?:[[:alnum:]][[:alnum:]-]*\s*:|\s+\S)/aasx; } , @lines) 0 0 0
132 0 my @trailers;
133 0 foreach my $line (@lines) {
134 0 ▶ if ( $line =~ m/\A\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\s/aasx) 0 0 0
135 0 $trailers[-1] .= q{ } . ( $line =~ s/\A\s+//r );
136 0 next;
137 }
138 0 push @trailers, $line;
139 }
140 0 foreach my $trailer (@trailers) {
141 0 my ( $name, $value ) = $trailer =~ m/\A([[:alnum:]][[:alnum:]-]*)\s*:\s*(.*)\z/;
142 0 ▶ push @values, $value if lc $name eq $key;
Branch: 1 branch
BranchTrueFalseTotal
if lc $name eq $key 0 0 0
143 }
144 }
145 0 return @values;
146 }
147  
148 # Add an empty F<Changes> to the build if the repository has none, as
149 # L<Dist::Zilla::Role::FileGatherer> asks.
150 # @returns nothing
151 # @example $plugin->gather_files;
152 0 ▶sub gather_files {
Subroutine gather_files: not called CC=2 SCAR=17.9
Pod documented
153 0 my ($self) = @_;
154 0 ▶ return if -e $self->zilla->root->child( $self->filename );
Branch: 1 branch
BranchTrueFalseTotal
if -e $self->zilla->root->child($self->filename) 0 0 0
155  
156 0 $self->add_file(
157 Dist::Zilla::File::InMemory->new(
158 name => $self->filename,
159 content => sprintf( "Revision history for %s\n", $self->zilla->name ),
160 )
161 );
162 0 $self->_created(1);
163 0 return;
164 }
165  
166 0 ▶sub _changes_file {
Subroutine _changes_file: not called CC=3 SCAR=24.8
167 0 my ($self) = @_;
168 0 my $name = $self->filename;
169 0 foreach my $file ( @{ $self->zilla->files } ) {
170 0 ▶ return $file if $file->name eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $file->name eq $name 0 0 0
171 }
172 0 return;
173 }
174  
175 0 ▶sub _build_entry {
Subroutine _build_entry: not called CC=2 SCAR=17.9
176 0 my ($self) = @_;
177 0 my @items = $self->items;
178 0 ▶ return unless @items;
Branch: 1 branch
BranchTrueFalseTotal
unless @items 0 0 0
179 0 return join q{}, $self->section_header, "\n", map { " - $_\n" } @items;
180 }
181  
182 # Add the entry to F<Changes> in the build, as L<Dist::Zilla::Role::FileMunger>
183 # asks. If no commit has a trailer, leave it as it is.
184 # @returns nothing
185 # @throws by log_fatal when the build has no F<Changes>
186 # @example $plugin->munge_files;
187 0 ▶sub munge_files {
Subroutine munge_files: not called CC=4 SCAR=30.0
Pod documented
188 0 my ($self) = @_;
189 0 my $file = $self->_changes_file;
190 0 ▶ $self->log_fatal( [ 'failed to find %s in the distribution', $self->filename ] ) unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
191  
192 0 my $entry = $self->_entry;
193 0 ▶ if ( !defined $entry ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $entry) 0 0 0
194 0 $self->log( [ 'no %s trailers since the last release, so %s is unchanged', $self->trailer, $self->filename ] );
195 0 return;
196 }
197  
198 0 my $content = $file->content;
199 0 ▶ $content =~ s/\n*\z/\n\n$entry/ unless $content =~ s/^(?=v?\d)/$entry\n/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ s/^(?=v?\d)/$entry\n/aamsx 0 0 0
200  
201 0 $self->_changes($content);
202 0 $file->content($content);
203 0 return;
204 }
205  
206 # Check that the release can write its entry, as
207 # L<Dist::Zilla::Role::BeforeRelease> asks.
208 # @returns nothing
209 # @throws by log_fatal when no commit since the last release has a trailer, or
210 # when this plugin is below C<[Git::Commit]> in F<dist.ini>
211 # @example $plugin->before_release;
212 0 ▶sub before_release {
Subroutine before_release: not called CC=4 SCAR=30.0
Pod documented
213 0 my ($self) = @_;
214 0 ▶ $self->log_fatal( [ 'no %s trailers in the commits since the last release', $self->trailer ] )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_entry 0 0 0
215 unless defined $self->_entry;
216  
217 0 my @plugins = @{ $self->zilla->plugins };
218 0 ▶ my $me = first { $plugins[$_] == $self } 0 .. $#plugins;
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 ▶ my $commit = first { $plugins[$_]->isa('Dist::Zilla::Plugin::Git::Commit') } 0 .. $#plugins;
Subroutine __ANON__: not called CC=1 SCAR=6.9
220 0 ▶ $self->log_fatal('[Git::ChangelogFromTrailers] must be above [Git::Commit] in dist.ini, or the release commit does not contain Changes')
Branch: 1 branch
BranchTrueFalseTotal
if defined $commit and $commit < $me 0 0 0
Condition0%0/3
defined $commit and $commit < $me
!ll&&!rl&&r
000
MC/DC0%0/2
defined $commit and $commit < $me
defined $commit $commit < $me
Truth table0%0/3
A and B
A= defined $commitB= $commit < $me
ABresult
0X0
100
111
221 if defined $commit && $commit < $me;
222 0 return;
223 }
224  
225  
226 0 ▶sub after_release {
Subroutine after_release: not called CC=3 SCAR=24.8
Pod documented
227 0 my ($self) = @_;
228 0 ▶ return unless defined $self->_changes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_changes 0 0 0
229  
230 0 my $file = $self->_changes_file;
231 0 my $iolayer = sprintf ':raw:encoding(%s)', $file->encoding;
232 0 $self->log_debug( [ 'updating contents of %s on disk', $self->update_filename ] );
233 0 my $changes = $self->zilla->root->child( $self->update_filename );
234 0 $changes->spew( { binmode => $iolayer }, $self->_changes );
235  
236 # Git::Commit commits only tracked files that changed, and an intent-to-add
237 # entry makes the new file one of those.
238 0 ▶ $self->_git->add( { intent_to_add => 1 }, $self->update_filename ) if $self->_created;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_created 0 0 0
239 0 return;
240 }
241  
242 1 __PACKAGE__->meta->make_immutable;
243  
244 1 1;
245  
246 __END__
↑ Summary