| 1 | package Dist::Zilla::Plugin::Git::ChangelogFromTrailers 0.001; | ||||||||||||||||||||||||||||
| 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{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 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub last_release_tag { | ||||||||||||||||||||||||||
|
Subroutine
last_release_tag: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 66 | 0 | my $regexp = $self->version_regexp; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my %version_of; | |||||||||||||||||||||||||||
| 69 | 0 | foreach my $tag ( $self->_git->tag( { merged => 'HEAD' } ) ) { | |||||||||||||||||||||||||||
| 70 | 0 | my ($v) = $tag =~ m/$regexp/; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | next unless defined $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | my $parsed = eval { version->parse($v) }; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | $version_of{$tag} = $parsed if defined $parsed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return unless %version_of; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $highest = max values %version_of; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | return first { $version_of{$_} == $highest } sort keys %version_of; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub items { | ||||||||||||||||||||||||||
|
Subroutine
items: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 84 | 0 | my $git = $self->_git; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # A repository with no commits has no HEAD to read a log from. | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | return unless eval { $git->rev_parse( { verify => 1, quiet => 1 }, 'HEAD' ); 1 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $tag = $self->last_release_tag; | |||||||||||||||||||||||||||
| 90 | 0 | my $key = $self->trailer; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Git::Wrapper refuses --format in log, so RUN gives the raw lines: one value | ||||||||||||||||||||||||||||
| 93 | # to a line, and an empty line for a commit with no trailer. | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my @values = $git->RUN( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | log => { reverse => 1, format => "%(trailers:key=$key,valueonly,unfold)" }, | ||||||||||||||||||||||||||||
| 96 | defined $tag ? "$tag..HEAD" : 'HEAD', | ||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | 0 | return uniq grep { $_ ne q{} } map { s/^\s+//r =~ s/\s+\z//r } @values; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub gather_files { | ||||||||||||||||||||||||||
|
Subroutine
gather_files: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | return if -e $self->zilla->root->child( $self->filename ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | $self->add_file( | |||||||||||||||||||||||||||
| 107 | Dist::Zilla::File::InMemory->new( | ||||||||||||||||||||||||||||
| 108 | name => $self->filename, | ||||||||||||||||||||||||||||
| 109 | content => sprintf( "Revision history for %s\n", $self->zilla->name ), | ||||||||||||||||||||||||||||
| 110 | ) | ||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||
| 112 | 0 | $self->_created(1); | |||||||||||||||||||||||||||
| 113 | 0 | return; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub _build_entry { | ||||||||||||||||||||||||||
|
Subroutine
_build_entry: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 117 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 118 | 0 | my @items = $self->items; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | return unless @items; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | return join q{}, $self->section_header, "\n", map { " - $_\n" } @items; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub munge_files { | ||||||||||||||||||||||||||
|
Subroutine
munge_files: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 126 | 0 | ▶ | my $file = first { $_->name eq $self->filename } @{ $self->zilla->files }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | $self->log_fatal( [ 'failed to find %s in the distribution', $self->filename ] ) unless $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my $entry = $self->_entry; | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | if ( !defined $entry ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | $self->log( [ 'no %s trailers since the last release, so %s is unchanged', $self->trailer, $self->filename ] ); | |||||||||||||||||||||||||||
| 132 | 0 | return; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | my $content = $file->content; | |||||||||||||||||||||||||||
| 136 | 0 | ▶ | $content =~ s/\n*\z/\n\n$entry/ unless $content =~ s/^(?=v?\d)/$entry\n/m; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | $self->_changes($content); | |||||||||||||||||||||||||||
| 139 | 0 | $file->content($content); | |||||||||||||||||||||||||||
| 140 | 0 | return; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | sub before_release { | ||||||||||||||||||||||||||
|
Subroutine
before_release: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | $self->log_fatal( [ 'no %s trailers in the commits since the last release', $self->trailer ] ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | unless defined $self->_entry; | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | my @plugins = @{ $self->zilla->plugins }; | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | my $me = first { $plugins[$_] == $self } 0 .. $#plugins; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | my $commit = first { $plugins[$_]->isa('Dist::Zilla::Plugin::Git::Commit') } 0 .. $#plugins; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 152 | 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
Condition0%0/3
defined $commit and $commit < $me
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
| |||||||||||||||||||||||||||||
| 153 | if defined $commit && $commit < $me; | ||||||||||||||||||||||||||||
| 154 | 0 | return; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub after_release { | ||||||||||||||||||||||||||
|
Subroutine
after_release: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | return unless defined $self->_changes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | my $file = first { $_->name eq $self->filename } @{ $self->zilla->files }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 163 | 0 | my $iolayer = sprintf ':raw:encoding(%s)', $file->encoding; | |||||||||||||||||||||||||||
| 164 | 0 | $self->log_debug( [ 'updating contents of %s on disk', $self->update_filename ] ); | |||||||||||||||||||||||||||
| 165 | 0 | my $changes = $self->zilla->root->child( $self->update_filename ); | |||||||||||||||||||||||||||
| 166 | 0 | $changes->spew( { binmode => $iolayer }, $self->_changes ); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Git::Commit commits only tracked files that changed, and an intent-to-add | ||||||||||||||||||||||||||||
| 169 | # entry makes the new file one of those. | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | $self->_git->add( { intent_to_add => 1 }, $self->update_filename ) if $self->_created; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | return; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 1 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||