blib/lib/Dist/Zilla/Plugin/GitFmtChanges.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 120 CC 9 SCAR 40.0
CC
9
Cov
0%
gather_files
90.0
BEGIN
2.0
BEGIN
2.0
CRAP 54.8
1 package Dist::Zilla::Plugin::GitFmtChanges;
2 $Dist::Zilla::Plugin::GitFmtChanges::VERSION = '0.006';
3 =head1 NAME
4  
5 Dist::Zilla::Plugin::GitFmtChanges - Build CHANGES file from a project's git log using git log format.
6  
7 =head1 VERSION
8  
9 version 0.006
10  
11 =head1 SYNOPSIS
12  
13 In your dist.ini:
14  
15 [GitFmtChanges]
16 max_age = 365
17 tag_regexp = ^v\d+\.\d+$
18 file_name = CHANGES
19 log_format = medium
20  
21 The example values are the defaults.
22  
23 =head1 DESCRIPTION
24  
25 This Dist::Zilla plugin writes a CHANGES file that contains formatted
26 commit information from recent git logs. The CHANGES file is formatted
27 using the "--format" option of the git log command. This makes it easy
28 to make the CHANGES file look the way you want it to.
29  
30 This is based on Dist::Zilla::Plugin::ChangelogFromGit.
31  
32 This plugin has the following configuration variables:
33  
34 =over 2
35  
36 =item * max_age
37  
38 It may be impractical to include the full change log in a mature
39 project's distribution. "max_age" limits the changes to the most
40 recent ones within a number of days. The default is about one year.
41  
42 Include two years of changes:
43  
44 max_age = 730
45  
46 =item * tag_regexp
47  
48 This plugin breaks the changelog into sections delineated by releases,
49 which are defined by release tags. "tag_regexp" may be used to focus
50 only on those tags that follow a particular release tagging format.
51 Some of the author's repositories contain multiple projects, each with
52 their own specific release tag formats, so that changelogs can focus
53 on particular projects' tags. For instance, POE::Test::Loops' release
54 tags may be specified as:
55  
56 tag_regexp = ^ptl-
57  
58 =item * file_name
59  
60 Everyone has a preference for their change logs. If you prefer
61 lowercase in your change log file names, you might specify:
62  
63 file_name = Changes
64  
65 =item * log_format
66  
67 Define the format used for the change listing in the CHANGES file.
68 This option is passed through to the B<git log> command.
69 One can use the predefined formats, such as 'oneline', 'short', 'medium' etc.
70  
71 log_format = short
72  
73 Or one can exersize more control by using the "format" formatting.
74 The following example will give the author and date, a newline, and
75 the "subject" of the change.
76  
77 log_format = %ai%n%s
78  
79 =back
80  
81 =cut
82  
83 require 5.6.0;
84 use Moose;
85 with 'Dist::Zilla::Role::FileGatherer';
86  
87 use POSIX qw(strftime);
88 use Date::Simple qw(date today);
89 use Git::Wrapper;
90  
91 has max_age => (
92 is => 'ro',
93 isa => 'Int',
94 default => 365,
95 );
96  
97 has tag_regexp => (
98 is => 'ro',
99 isa => 'Str',
100 default => '^v\\d+\.\\d+$',
101 );
102  
103 has file_name => (
104 is => 'ro',
105 isa => 'Str',
106 default => 'CHANGES',
107 );
108  
109 has log_format => (
110 is => 'ro',
111 isa => 'Str',
112 default => 'medium',
113 );
114  
115 has git => (
116 is =>'ro',
117 default => sub { Git::Wrapper->new('.') }
118 );
119  
120 sub gather_files {
121 my ($self, $arg) = @_;
122  
123 my $earliest_date = strftime(
124 "%FT %T +0000", gmtime(time() - $self->max_age() * 86400)
125 );
126  
127 my @tags = $self->git->tag;
128  
129 {
130 my $tag_pattern = $self->tag_regexp();
131  
132 my $i = @tags;
133 while ($i--) {
134 unless ($tags[$i] =~ /$tag_pattern/o) {
135 splice @tags, $i, 1;
136 next;
137 }
138  
139 my @commit = $self->git->show(
140 { format => 'tformat:(((((%ci)))))' },
141 $tags[$i]
142 );
143 my ($commit) = grep { m#\(\(\(\(\(# } @commit;
144  
145 die $commit unless $commit =~ /\(\(\(\(\((.+?)\)\)\)\)\)/;
146  
147 $tags[$i] = {
148 'time' => $1,
149 'tag' => $tags[$i],
150 };
151 }
152 }
153  
154 push @tags, {'time' => '9999-99-99 99:99:99 +0000', 'tag' => 'HEAD'};
155  
156 @tags = sort { $a->{'time'} cmp $b->{'time'} } @tags;
157  
158 my $changelog = "";
159  
160 {
161 my $log_format = $self->log_format();
162 my $i = @tags;
163 while ($i--) {
164 my $tag_time = $tags[$i]{time};
165 last if $tag_time lt $earliest_date;
166  
167 my @commit;
168 my $prev_tag = $tags[$i-1]{tag};
169 my $curr_tag = $tags[$i]{tag};
170  
171 # Handle initial releases properly
172 $prev_tag = 'HEAD~1'
173 if (!$i && $curr_tag eq 'HEAD' && @tags == 1);
174  
175 open my $commit, "-|", "git log --format=\"$log_format\" $prev_tag..$curr_tag ."
176 or die $!;
177  
178 { local $/ = "\n\n" ; @commit = <$commit> };
179  
180 # Don't display the tag if there's nothing under it.
181 next unless @commit;
182  
183 my $tag_line = "$tag_time $curr_tag";
184 # if this is the HEAD then take the version from
185 # the version, and the date as today
186 if ($curr_tag eq 'HEAD')
187 {
188 my $today = today();
189 my $ver = $self->zilla->version;
190 $tag_line = "v$ver $today";
191 }
192 elsif ($tag_time =~ /(\d+-\d+-\d+)/) # only date
193 {
194 $tag_line = "$curr_tag $1";
195 }
196 $changelog .= (
197 "\n$tag_line\n" .
198 ("-" x length($tag_line)) . "\n\n"
199 );
200  
201 $changelog .= $_ foreach @commit;
202 }
203 }
204  
205 my $epilogue = "End of changes in the last " . $self->max_age() . " day";
206 $epilogue .= "s" unless $self->max_age() == 1;
207  
208 $changelog .= (
209 "\n" .
210 ("=" x length($epilogue)) . "\n" .
211 "$epilogue\n" .
212 ("=" x length($epilogue)) . "\n"
213 );
214 my $dist_name = $self->zilla->name;
215 my $chlog_title = "Revision History for $dist_name";
216 my $prologue = (
217 "$chlog_title\n" .
218 ("=" x length($chlog_title)) . "\n"
219 );
220 $changelog = $prologue . $changelog;
221  
222 my $file = Dist::Zilla::File::InMemory->new({
223 content => $changelog,
224 name => $self->file_name(),
225 });
226  
227 $self->add_file($file);
228 return;
229 }
230  
231 __PACKAGE__->meta->make_immutable;
232 no Moose;
233 1;
234  
235 __END__