File Coverage

blib/lib/Dist/Zilla/Plugin/Git/Check.pm
Criterion Covered Total %
statement 47 48 97.9
branch 17 22 77.2
condition n/a
subroutine 9 10 90.0
pod 0 2 0.0
total 73 82 89.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 1     1   1571818 use 5.008;
  1         4  
10 1     1   9 use strict;
  1         3  
  1         25  
11 1     1   6 use warnings;
  1         3  
  1         75  
12              
13             package Dist::Zilla::Plugin::Git::Check;
14             # ABSTRACT: Check your git repository before releasing
15              
16             our $VERSION = '2.048';
17              
18 1     1   8 use Moose;
  1         2  
  1         8  
19 1     1   6619 use namespace::autoclean 0.09;
  1         22  
  1         7  
20 1     1   742 use Types::Standard qw(Bool Enum);;
  1         90367  
  1         11  
21              
22             with 'Dist::Zilla::Role::AfterBuild',
23             'Dist::Zilla::Role::BeforeRelease',
24             'Dist::Zilla::Role::Git::Repo';
25             with 'Dist::Zilla::Role::Git::DirtyFiles',
26             'Dist::Zilla::Role::GitConfig';
27              
28             has build_warnings => ( is=>'ro', isa => Bool, default => 0 );
29              
30             has untracked_files => ( is=>'ro', isa => Enum([qw(die warn ignore)]), default => 'die' );
31              
32             sub _git_config_mapping { +{
33 0     0   0 changelog => '%{changelog}s',
34             } }
35              
36             # -- public methods
37              
38             around dump_config => sub
39             {
40             my $orig = shift;
41             my $self = shift;
42              
43             my $config = $self->$orig;
44              
45             $config->{+__PACKAGE__} = {
46             # build_warnings does not affect the build outcome; do not need to track it
47             untracked_files => $self->untracked_files,
48             blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
49             };
50              
51             return $config;
52             };
53              
54             sub _perform_checks {
55 25     25   378 my ($self, $log_method) = @_;
56              
57 25         92 my @issues;
58 25         642 my $git = $self->git;
59 25         216 my @output;
60              
61             # fetch current branch
62 25 50       522 my ($branch) =
63             map /^\*\s+(.+)/ ? $1 : (),
64             $git->branch;
65              
66             # check if some changes are staged for commit
67 25         277603 @output = $git->diff( { cached=>1, 'name-status'=>1 } );
68 25 100       267365 if ( @output ) {
69 5 50       193 push @issues, @output . " staged change" . (@output == 1 ? '' : 's');
70              
71 5         145 my $errmsg =
72             "branch $branch has some changes staged for commit:\n" .
73             join "\n", map "\t$_", @output;
74 5         151 $self->$log_method($errmsg);
75             }
76              
77             # everything but files listed in allow_dirty should be in a
78             # clean state
79 20         810 @output = $self->list_dirty_files($git);
80 20 100       205415 if ( @output ) {
81 10 50       332 push @issues, @output . " uncommitted file" . (@output == 1 ? '' : 's');
82              
83 10         316 my $errmsg =
84             "branch $branch has some uncommitted files:\n" .
85             join "\n", map "\t$_", @output;
86 10         353 $self->$log_method($errmsg);
87             }
88              
89             # no files should be untracked
90 10         430 @output = $git->ls_files( { others=>1, 'exclude-standard'=>1 } );
91 10 100       94617 if ( @output ) {
92 5 50       202 push @issues, @output . " untracked file" . (@output == 1 ? '' : 's');
93              
94 5         790 my $untracked = $self->untracked_files;
95 5 100       41 if ($untracked ne 'ignore') {
96             # If $log_method is log_fatal, switch to log unless
97             # untracked files are fatal. If $log_method is already log,
98             # this is a no-op.
99 4 100       39 $log_method = 'log' unless $untracked eq 'die';
100              
101 4         120 my $errmsg =
102             "branch $branch has some untracked files:\n" .
103             join "\n", map "\t$_", @output;
104 4         111 $self->$log_method($errmsg);
105             }
106             }
107              
108 7 100       1049 if (@issues) {
109 2         88 $self->log( "branch $branch has " . join(', ', @issues));
110             } else {
111 5         207 $self->log( "branch $branch is in a clean state" );
112             }
113             } # end _perform_checks
114              
115             sub after_build {
116 5     5 0 59594 my $self = shift;
117              
118 5 50       218 $self->_perform_checks('log') if $self->build_warnings;
119             }
120              
121             sub before_release {
122 25     25 0 1363699 my $self = shift;
123              
124 25         656 $self->_perform_checks('log_fatal');
125             }
126              
127             __PACKAGE__->meta->make_immutable;
128             1;
129              
130             __END__
131              
132             =pod
133              
134             =encoding UTF-8
135              
136             =head1 NAME
137              
138             Dist::Zilla::Plugin::Git::Check - Check your git repository before releasing
139              
140             =head1 VERSION
141              
142             version 2.048
143              
144             =head1 SYNOPSIS
145              
146             In your F<dist.ini>:
147              
148             [Git::Check]
149             allow_dirty = dist.ini
150             allow_dirty = README
151             changelog = Changes ; this is the default
152             build_warnings = 0 ; this is the default
153             untracked_files = die ; default value (can also be "warn" or "ignore")
154              
155             =head1 DESCRIPTION
156              
157             This plugin checks that your Git repo is in a clean state before releasing.
158             The following checks are performed before releasing:
159              
160             =over 4
161              
162             =item * there should be no files in the index (staged copy)
163              
164             =item * there should be no untracked files in the working copy
165              
166             =item * the working copy should be clean. The files listed in
167             C<allow_dirty> can be modified locally, though.
168              
169             =back
170              
171             If those conditions are not met, the plugin will die, and the release
172             will thus be aborted. This lets you fix the problems before continuing.
173              
174             The plugin accepts the following options:
175              
176             =over 4
177              
178             =item * changelog - the name of your changelog file. defaults to F<Changes>.
179              
180             =item * allow_dirty - a file that is allowed to have local
181             modifications. This option may appear multiple times. The default
182             list is F<dist.ini> and the changelog file given by C<changelog>. You
183             can use C<allow_dirty => to prohibit all local modifications.
184              
185             =item * allow_dirty_match - works the same as allow_dirty, but
186             matching as a regular expression instead of an exact filename.
187              
188             =item * build_warnings - if 1, perform the same checks after every build,
189             but as warnings, not errors. Defaults to 0.
190              
191             =item * untracked_files - indicates what to do if there are untracked
192             files. Must be either C<die> (the default), C<warn>, or C<ignore>.
193             C<warn> lists the untracked files, while C<ignore> only prints the
194             total number of untracked files.
195              
196             =back
197              
198             =for Pod::Coverage after_build
199             before_release
200              
201             =head1 SUPPORT
202              
203             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-Git>
204             (or L<bug-Dist-Zilla-Plugin-Git@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-Git@rt.cpan.org>).
205              
206             There is also a mailing list available for users of this distribution, at
207             L<http://dzil.org/#mailing-list>.
208              
209             There is also an irc channel available for users of this distribution, at
210             L<C<#distzilla> on C<irc.perl.org>|irc://irc.perl.org/#distzilla>.
211              
212             =head1 AUTHOR
213              
214             Jerome Quelin
215              
216             =head1 COPYRIGHT AND LICENCE
217              
218             This software is copyright (c) 2009 by Jerome Quelin.
219              
220             This is free software; you can redistribute it and/or modify it under
221             the same terms as the Perl 5 programming language system itself.
222              
223             =cut