File Coverage

blib/lib/Dist/Zilla/Plugin/Git/RequireUnixEOL.pm
Criterion Covered Total %
statement 60 69 86.9
branch 11 18 61.1
condition 3 6 50.0
subroutine 14 15 93.3
pod 0 2 0.0
total 88 110 80.0


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::Git::RequireUnixEOL;
2              
3 2     2   2696255 use 5.006;
  2         6  
4 2     2   9 use strict;
  2         4  
  2         37  
5 2     2   10 use warnings;
  2         3  
  2         72  
6              
7             our $VERSION = '0.002';
8              
9 2     2   306 use Moose;
  2         357950  
  2         14  
10              
11             with qw(
12             Dist::Zilla::Role::BeforeBuild
13             );
14              
15             has _git => (
16             is => 'ro',
17             isa => 'Git::Wrapper',
18             lazy => 1,
19             default => sub { Git::Wrapper->new( path( shift->zilla->root )->absolute->stringify ) },
20             );
21              
22 4     4 0 128377 sub mvp_multivalue_args { return (qw( ignore )) }
23              
24             has ignore => (
25             is => 'ro',
26             isa => 'Maybe[ArrayRef]',
27             default => sub { [] },
28             );
29              
30 2     2   12814 use Carp;
  2         5  
  2         139  
31 2     2   446 use Git::Wrapper;
  2         22263  
  2         60  
32 2     2   510 use Path::Tiny;
  2         7634  
  2         92  
33 2     2   555 use Safe::Isa;
  2         700  
  2         206  
34 2     2   13 use Try::Tiny;
  2         4  
  2         88  
35              
36 2     2   249 use namespace::autoclean;
  2         5581  
  2         19  
37              
38             sub before_build {
39 4     4 0 134072 my ($self) = @_;
40              
41 4         17 my %ignored_file = map { $_ => 1 } @{ $self->ignore };
  1         12  
  4         191  
42 4         25 my @files = grep { !exists $ignored_file{$_} } $self->_git_ls_files();
  4         32  
43 4 100       26 return if !@files;
44              
45 3         7 my @errors;
46             FILE:
47 3         11 for my $file (@files) {
48 3 50       84 open my $fh, '<', $file or croak "Could not open $file: $!";
49              
50             # On Windows default is :crlf, which hides \r\n
51 3 50       27 binmode $fh, ':raw' or croak "binmode failed: $!";
52              
53 3         7 my $windows_line_ending_found = 0;
54 3         7 my $line_no = 0;
55             LINE:
56 3         33 while ( my $line = <$fh> ) {
57 12         20 $line_no++;
58              
59 12 100 100     53 if ( ( $windows_line_ending_found == 0 ) and ( $line =~ m{\r$}xsm ) ) {
60 1         7 $windows_line_ending_found = 1;
61 1         7 push @errors, "File $file uses Windows EOL (found on line $line_no)";
62             }
63              
64 12 100       51 if ( $line =~ m{[ \t]+\r?\n$}xsm ) {
65 1         13 push @errors, "File $file has trailing whitespace on line $line_no";
66             }
67             }
68              
69 3 50       26 close $fh or croak "Could not read $file: $!";
70             }
71              
72 3 100       9 if (@errors) {
73 2         24 $self->log_fatal( join "\n", q{-} x 60, @errors );
74             }
75              
76 1         4 return;
77             }
78              
79             sub _git_ls_files {
80 4     4   12 my ($self) = @_;
81              
82 4         135 my $git = $self->_git;
83              
84 4         14 my @files;
85             try {
86 4     4   264 @files = $git->ls_files();
87             }
88             catch {
89 0     0   0 my $fatal = $_;
90 0 0       0 if ( $fatal->$_isa('Git::Wrapper::Exception') ) {
91 0         0 my $err = $git->ERR;
92 0 0 0     0 if ( $err and @{$err} ) {
  0         0  
93 0         0 $self->log( @{$err} );
  0         0  
94             }
95              
96 0         0 $self->log_fatal( $fatal->error );
97             }
98              
99 0         0 $self->log_fatal($fatal);
100 4         61 };
101              
102 4         18408 return @files;
103             }
104              
105             __PACKAGE__->meta->make_immutable;
106              
107             1;
108              
109             __END__
110              
111             =pod
112              
113             =encoding UTF-8
114              
115             =head1 NAME
116              
117             Dist::Zilla::Plugin::Git::RequireUnixEOL - Enforce the correct line endings in your Git repository with Dist::Zilla
118              
119             =head1 VERSION
120              
121             Version 0.002
122              
123             =head1 SYNOPSIS
124              
125             # in dist.ini:
126             [Git::RequireUnixEOL]
127              
128             =head1 DESCRIPTION
129              
130             This plugin checks that all the files in the Git repository where your
131             project is saved use Unix line endings and have no whitespace at the end of
132             a line. Files not in the Git index are ignored. You can ignore additional
133             files with the B<ignore> option in F<dist.ini>.
134              
135             The plugin runs in the before build phase and aborts the build if a violation
136             is found.
137              
138             The plugin should ensure that you always commit your files with the correct
139             line endings and without superfluous whitespace.
140              
141             This plugin checks the files in your repository. To check your build you can
142             use a test based on L<Test::EOL|Test::EOL>.
143              
144             =head1 SUPPORT
145              
146             =head2 Bugs / Feature Requests
147              
148             Please report any bugs or feature requests through the issue tracker
149             at L<https://github.com/skirmess/Dist-Zilla-Plugin-Git-RequireUnixEOL/issues>.
150             You will be notified automatically of any progress on your issue.
151              
152             =head2 Source Code
153              
154             This is open source software. The code repository is available for
155             public review and contribution under the terms of the license.
156              
157             L<https://github.com/skirmess/Dist-Zilla-Plugin-Git-RequireUnixEOL>
158              
159             git clone https://github.com/skirmess/Dist-Zilla-Plugin-Git-RequireUnixEOL.git
160              
161             =head1 AUTHOR
162              
163             Sven Kirmess <sven.kirmess@kzone.ch>
164              
165             =head1 COPYRIGHT AND LICENSE
166              
167             This software is Copyright (c) 2017 by Sven Kirmess.
168              
169             This is free software, licensed under:
170              
171             The (two-clause) FreeBSD License
172              
173             =head1 SEE ALSO
174              
175             L<Dist::Zilla::Plugin::Git::FilePermissions|Dist::Zilla::Plugin::Git::FilePermissions>,
176             L<Test::EOL|Test::EOL>
177              
178             =cut
179              
180             # vim: ts=4 sts=4 sw=4 et: syntax=perl