File Coverage

blib/lib/Dist/Zilla/Plugin/Git/Commit.pm
Criterion Covered Total %
statement 57 57 100.0
branch 5 8 62.5
condition n/a
subroutine 14 14 100.0
pod 1 2 50.0
total 77 81 95.0


line stmt bran cond sub pod time code
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 9     9   15888547 use 5.008;
  9         47  
10 9     9   61 use strict;
  9         20  
  9         327  
11 9     9   62 use warnings;
  9         19  
  9         696  
12              
13             package Dist::Zilla::Plugin::Git::Commit;
14             # ABSTRACT: Commit dirty files
15              
16             our $VERSION = '2.046';
17              
18 9     9   66 use namespace::autoclean;
  9         21  
  9         84  
19 9     9   1579 use File::Temp qw{ tempfile };
  9         18642  
  9         849  
20 9     9   67 use Moose;
  9         37  
  9         81  
21 9     9   65952 use MooseX::Has::Sugar;
  9         7248  
  9         45  
22 9     9   5752 use Types::Standard qw(Str ArrayRef);
  9         718665  
  9         101  
23 9     9   12303 use Types::Path::Tiny 'Path';
  9         209451  
  9         102  
24 9     9   4275 use Path::Tiny 0.048 qw(); # subsumes
  9         234  
  9         538  
25 9     9   52 use Cwd;
  9         26  
  9         6257  
26              
27             with 'Dist::Zilla::Role::AfterRelease',
28             'Dist::Zilla::Role::Git::Repo';
29             with 'Dist::Zilla::Role::Git::DirtyFiles';
30             with 'Dist::Zilla::Role::Git::StringFormatter';
31             with 'Dist::Zilla::Role::GitConfig';
32              
33             sub _git_config_mapping { +{
34 2     2   340 changelog => '%{changelog}s',
35             } }
36              
37             # -- attributes
38              
39             has commit_msg => ( ro, isa=>Str, default => 'v%V%n%n%c' );
40             has add_files_in => ( ro, isa=> ArrayRef[Path], coerce => 1, default => sub { [] });
41              
42              
43             # -- public methods
44              
45             sub mvp_multivalue_args { qw( add_files_in ) }
46              
47             around dump_config => sub
48             {
49             my $orig = shift;
50             my $self = shift;
51              
52             my $config = $self->$orig;
53              
54             $config->{+__PACKAGE__} = {
55             commit_msg => $self->commit_msg,
56             add_files_in => [ sort @{ $self->add_files_in } ],
57             blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
58             };
59              
60             return $config;
61             };
62              
63             sub after_release {
64 8     8 0 679908 my $self = shift;
65              
66 8         215 my $git = $self->git;
67 8         79 my @output;
68              
69             # check if there are dirty files that need to be committed.
70             # at this time, we know that only those 2 files may remain modified,
71             # otherwise before_release would have failed, ending the release
72             # process.
73 8         201 @output = sort { lc $a cmp lc $b } $self->list_dirty_files($git, 1);
  8         151  
74              
75             # add any other untracked files to the commit list
76 8 100       90 if ( @{ $self->add_files_in } ) {
  8         1072  
77 1         36 my @untracked_files = $git->ls_files( { others=>1, 'exclude-standard'=>1 } );
78 1         7852 foreach my $f ( @untracked_files ) {
79 1         8 foreach my $path ( @{ $self->add_files_in } ) {
  1         122  
80 1 50       28 if ( Path::Tiny::path( $path )->subsumes( $f ) ) {
81 1         363 push( @output, $f );
82 1         11 last;
83             }
84             }
85             }
86             }
87              
88             # if nothing to commit, we're done!
89 8 50       132 return unless @output;
90              
91             # write commit message in a temp file
92 8         396 my ($fh, $filename) = tempfile( getcwd . '/DZP-git.XXXX', UNLINK => 1 );
93 8 50       6844 binmode $fh, ':utf8' unless Dist::Zilla->VERSION < 5;
94 8         131 print $fh $self->get_commit_message;
95 8         1343 close $fh;
96              
97             # commit the files in git
98 8         204 $git->add( @output );
99 8         71545 $self->log_debug($_) for $git->commit( { file=>$filename } );
100 8         96044 $self->log("Committed @output");
101             }
102              
103              
104             #pod =method get_commit_message
105             #pod
106             #pod This method returns the commit message. The default implementation
107             #pod reads the Changes file to get the list of changes in the just-released version.
108             #pod
109             #pod =cut
110              
111             sub get_commit_message {
112 18     18 1 116 my $self = shift;
113              
114 18         784 return $self->_format_string($self->commit_msg);
115             } # end get_commit_message
116              
117             __PACKAGE__->meta->make_immutable;
118             1;
119              
120             __END__
121              
122             =pod
123              
124             =encoding UTF-8
125              
126             =head1 NAME
127              
128             Dist::Zilla::Plugin::Git::Commit - Commit dirty files
129              
130             =head1 VERSION
131              
132             version 2.046
133              
134             =head1 SYNOPSIS
135              
136             In your F<dist.ini>:
137              
138             [Git::Commit]
139             changelog = Changes ; this is the default
140              
141             =head1 DESCRIPTION
142              
143             Once the release is done, this plugin will record this fact in git by
144             committing changelog and F<dist.ini>. The commit message will be taken
145             from the changelog for this release. It will include lines between
146             the current version and timestamp and the next non-indented line,
147             except that blank lines at the beginning or end are removed.
148              
149             B<Warning:> If you are using Git::Commit in conjunction with the
150             L<NextRelease|Dist::Zilla::Plugin::NextRelease> plugin,
151             C<[NextRelease]> must come before C<[Git::Commit]> (or C<[@Git]>) in
152             your F<dist.ini> or plugin bundle. Otherwise, Git::Commit will commit
153             the F<Changes> file before NextRelease has updated it.
154              
155             The plugin accepts the following options:
156              
157             =over 4
158              
159             =item * changelog - the name of your changelog file. Defaults to F<Changes>.
160              
161             =item * allow_dirty - a file that will be checked in if it is locally
162             modified. This option may appear multiple times. The default
163             list is F<dist.ini> and the changelog file given by C<changelog>.
164              
165             =item * allow_dirty_match - works the same as allow_dirty, but
166             matching as a regular expression(s) instead of an exact filename(s).
167              
168             =item * add_files_in - a path that will have its new files checked in.
169             This option may appear multiple times. This is used to add files
170             generated during build-time to the repository, for example. The default
171             list is empty.
172              
173             Note: The files have to be generated between the phases BeforeRelease
174             E<lt>-E<gt> AfterRelease, and after Git::Check + before Git::Commit.
175              
176             =item * commit_msg - the commit message to use. Defaults to
177             C<v%V%n%n%c>, meaning the version number and the list of changes.
178             The L<formatting codes|Dist::Zilla::Role::Git::StringFormatter/DESCRIPTION>
179             are documented under L<Dist::Zilla::Role::Git::StringFormatter>.
180              
181             =item * time_zone - the time zone to use with C<%d>. Can be any
182             time zone name accepted by DateTime. Defaults to C<local>.
183              
184             =back
185              
186             =head1 METHODS
187              
188             =head2 get_commit_message
189              
190             This method returns the commit message. The default implementation
191             reads the Changes file to get the list of changes in the just-released version.
192              
193             =for Pod::Coverage after_release mvp_multivalue_args
194              
195             =head1 SUPPORT
196              
197             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-Git>
198             (or L<bug-Dist-Zilla-Plugin-Git@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-Git@rt.cpan.org>).
199              
200             There is also a mailing list available for users of this distribution, at
201             L<http://dzil.org/#mailing-list>.
202              
203             There is also an irc channel available for users of this distribution, at
204             L<C<#distzilla> on C<irc.perl.org>|irc://irc.perl.org/#distzilla>.
205              
206             =head1 AUTHOR
207              
208             Jerome Quelin
209              
210             =head1 COPYRIGHT AND LICENCE
211              
212             This software is copyright (c) 2009 by Jerome Quelin.
213              
214             This is free software; you can redistribute it and/or modify it under
215             the same terms as the Perl 5 programming language system itself.
216              
217             =cut