File Coverage

blib/lib/Dist/Zilla/Plugin/Git/RequireUnixEOL.pm
Criterion Covered Total %
statement 54 54 100.0
branch 12 16 75.0
condition 3 3 100.0
subroutine 11 11 100.0
pod 0 2 0.0
total 80 86 93.0


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