lib/App/githook/perltidy/pre_commit.pm

filter: Statementstmt 82.2% 130 / 158 Branchbran 64.5% 40 / 62 Conditioncond 48.1% 13 / 27 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 8 totaltotal 70.1% 200 / 285 CC 47 SCAR 30.6
CC
47
Cov
74%
run
37.7
critic_perl
20.0
tidy_perl
6.6
CRAP 21.4
↑ Summary lib/App/githook/perltidy/install.pm »
1 package App::githook::perltidy::pre_commit;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use App::githook::perltidy::pre_commit_CI
6 11 ▶ isa => 'App::githook::perltidy',
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 has => {};
8 11 ▶use OptArgs2::StatusLine '$status', '$v_status', 'RS';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = '1.0.1';
12  
13 9 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=14.2
Pod undocumented
14 9 my $self = shift;
15 9 my @hook = $self->pre_commit->slurp;
16 9 ▶ unless (grep( /NO_GITHOOK_PERLTIDY/, @hook )
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /NO_GITHOOK_PERLTIDY/, @hook and grep /PERL5LIB/, @hook) 0 9 9
Condition33%1/3
grep /NO_GITHOOK_PERLTIDY/, @hook and grep /PERL5LIB/, @hook
!ll&&!rl&&r
009
MC/DC0%0/2
grep /NO_GITHOOK_PERLTIDY/, @hook and grep /PERL5LIB/, @hook
grep /NO_GITHOOK_PERLTIDY/, @hook grep /PERL5LIB/, @hook
Truth table33%1/3
A and B
A= grep /NO_GITHOOK_PERLTIDY/, @hookB= grep /PERL5LIB/, @hook
ABresult
0X0
100
111
17 and grep( /PERL5LIB/, @hook ) )
18 {
19 0 my $loc = $self->pre_commit->relative( $self->repo );
20 0 warn qq{githook-perltidy: You have an old "$loc" hook.\n}
21 . qq{githook-perltidy: Consider using }
22 . qq{"githook-perltidy install --force" to rebuild\n};
23 }
24 }
25  
26 11 our $temp_dir;
27  
28 15 ▶sub sys {
Subroutine sys: called CC=3 SCAR=11.4
Pod undocumented
29 15 my $self = shift;
30 15 ▶ $v_status = join( ' ', map { defined $_ ? $_ : '*UNDEF*' } @_ ) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 46 0 46
31 15 ▶ system(@_) == 0 or Carp::croak "@_ failed: $?";
Branch: 1 branch
BranchTrueFalseTotal
system(@_) == 0 or Carp::croak("@_ failed: $?") 15 0 15
32 }
33  
34 15 ▶sub tmp_sys {
Subroutine tmp_sys: called CC=1 SCAR=0.0
Pod undocumented
35 15 my $self = shift;
36 15 local $ENV{GIT_WORK_TREE} = $temp_dir;
37 15 $self->sys(@_);
38 }
39  
40 5 ▶sub tidy_perl {
Subroutine tidy_perl: called CC=6 SCAR=18.9
Pod undocumented
41 5 my $self = shift;
42 5 ▶ my $tmp_file = shift || die 'tidy_perl($TMP_FILE, $file)';
Condition50%1/2
shift() || die('tidy_perl($TMP_FILE, $file)')
l!l
50
MC/DC0%0/1
shift() || die('tidy_perl($TMP_FILE, $file)')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
43 5 ▶ my $file = shift || die 'tidy_perl($tmp_file, $FILE)';
Condition50%1/2
shift() || die('tidy_perl($tmp_file, $FILE)')
l!l
50
MC/DC0%0/1
shift() || die('tidy_perl($tmp_file, $FILE)')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
44 5 my $where = shift;
45  
46 5 state $req = do {
47 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶ if ( $self->sweetened ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sweetened) { } 0 5 5
49 0 require Perl::Tidy::Sweetened;
50 0 *perltidy = \&Perl::Tidy::Sweetened::perltidy;
51 }
52 else {
53 5 require Perl::Tidy;
54 5 *perltidy = \&Perl::Tidy::perltidy;
55 }
56 };
57  
58 5 $status = "(perltidy) ($where)";
59  
60 5 my $errormsg;
61 5 my $error = perltidy(
62 argv => [ qw{-nst -b -bext=/}, "$tmp_file" ],
63 errorfile => \$errormsg,
64 perltidyrc => $self->perltidyrc->stringify,
65 );
66  
67 5 ▶ if ( length($errormsg) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $errormsg) { } 1 4 5
elsif ($error) { } 0 4 4
68 1 $status .= ":\n";
69 1 die $errormsg;
70 }
71 elsif ($error) {
72 0 $status .= ":\n";
73 0 die "An unknown perltidy error occurred.";
74 }
75 }
76  
77 1 ▶sub tidy_pod {
Subroutine tidy_pod: called CC=3 SCAR=11.1
Pod undocumented
78 1 my $self = shift;
79 1 ▶ my $tmp_file = shift || die 'tidy_pod($TMP_FILE, $file)';
Condition50%1/2
shift() || die('tidy_pod($TMP_FILE, $file)')
l!l
10
MC/DC0%0/1
shift() || die('tidy_pod($TMP_FILE, $file)')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
80 1 ▶ my $file = shift || die 'tidy_pod($tmp_file, $FILE)';
Condition50%1/2
shift() || die('tidy_pod($tmp_file, $FILE)')
l!l
10
MC/DC0%0/1
shift() || die('tidy_pod($tmp_file, $FILE)')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
81 1 my $where = shift;
82  
83 1 state $req = require Pod::Tidy;
84 1 $status = "(podtidy) ($where)";
85  
86 Pod::Tidy::tidy_files(
87 files => [$tmp_file],
88 recursive => 0,
89 verbose => 0,
90 inplace => 1,
91 nobackup => 1,
92 columns => 72,
93 1 %{ $self->podtidyrc_opts },
94 );
95 }
96  
97 0 ▶sub critic_perl {
Subroutine critic_perl: not called CC=4 SCAR=30.0
Pod undocumented
98 0 my $self = shift;
99 0 ▶ my $tmp_file = shift || die 'critic_perl($TMP_FILE, $file)';
Condition0%0/2
shift() || die('critic_perl($TMP_FILE, $file)')
l!l
00
MC/DC0%0/1
shift() || die('critic_perl($TMP_FILE, $file)')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
100 0 ▶ my $file = shift || die 'critic_perl($tmp_file, $FILE)';
Condition0%0/2
shift() || die('critic_perl($tmp_file, $FILE)')
l!l
00
MC/DC0%0/1
shift() || die('critic_perl($tmp_file, $FILE)')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
101 0 my $where = shift;
102  
103 0 state $req = require Perl::Critic;
104 0 $status = "(perlcritic) ($where)";
105  
106 0 my @violations =
107 Perl::Critic::critique( { -profile => $self->perlcriticrc->stringify },
108 $tmp_file->stringify );
109  
110 0 ▶ if (@violations) {
Branch: 1 branch
BranchTrueFalseTotal
if (@violations) 0 0 0
111 0 $status .= ":\n";
112 0 die join( '', @violations );
113 }
114 }
115  
116 1 ▶sub convert_readme {
Subroutine convert_readme: called CC=4 SCAR=14.8
Pod undocumented
117 1 my $self = shift;
118 1 ▶ my $file = shift || die 'convert_readme($FILE)';
Condition50%1/2
shift() || die('convert_readme($FILE)')
l!l
10
MC/DC0%0/1
shift() || die('convert_readme($FILE)')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
119  
120 1 $status = '-> README';
121  
122 1 ▶ my $width = $self->podtidyrc_opts->{columns} // 72;
Condition50%1/2
$self->podtidyrc_opts->{"columns"} // 72
l!l
10
MC/DC0%0/1
$self->podtidyrc_opts->{"columns"} // 72
$self->podtidyrc_opts->{"columns"}
Truth table50%1/2
A
A= $self->podtidyrc_opts->{"columns"}
Aresult
11
00
123  
124 1 require Pod::Text;
125 1 Pod::Text->new( sentence => 0, width => $width )
126 ->parse_from_file( "$file", 'README' );
127  
128 1 ▶ if ( system("git ls-files --error-unmatch README > /dev/null 2>&1") == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (system("git ls-files --error-unmatch README > /dev/null 2>&1") == 0) 0 1 1
129 0 $self->sys(qw/git add README/);
130 }
131 }
132  
133 9 ▶sub run {
Subroutine run: called CC=30 SCAR=36.3
Pod undocumented
134 9 my $self = shift;
135 9 my @perlfiles = ();
136 9 my %partial = ();
137  
138 9 ▶ untie $v_status unless $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->verbose 9 0 9
139  
140 9 ▶ return if $ENV{NO_GITHOOK_PERLTIDY};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NO_GITHOOK_PERLTIDY"} 0 9 9
141 9 $temp_dir = Path::Tiny->tempdir('githook-perltidy-XXXXXXXX');
142  
143 9 $v_status = "TMP=$temp_dir\n";
144  
145 9 my @index;
146 9 my $force_readme = 0;
147 9 ▶ my $readme_from = $self->readme_from // '';
Condition100%2/2
$self->readme_from // ""
l!l
18
MC/DC100%1/1
$self->readme_from // ""
$self->readme_from
Truth table100%2/2
A
A= $self->readme_from
Aresult
11
00
148  
149 # Use the -z flag to get clean filenames with no escaping or quoting
150 # "lines" are separated with NUL, so set input record separator
151 # appropriately
152 {
153 9 local $/ = "\0";
154 9 ▶ open( my $fh, '-|', 'git status --porcelain -z' ) || die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", "git status --porcelain -z" or die "open: $!" 9 0 9
155  
156 9 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 61 9 70
157 61 chomp $line;
158 61 ▶ next unless $line =~ m/^[ACMR](.) (.*)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^[ACMR](.) (.*)/ 52 9 61
159 9 my ( $wtree, $file ) = ( $1, $2 );
160  
161 9 push( @index, $file );
162 9 $partial{$file} = $wtree eq 'M';
163  
164 9 ▶ $force_readme++ if $file eq '.readme_from';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq ".readme_from" 1 8 9
165 9 ▶ $force_readme-- if $file eq $readme_from;
Branch: 1 branch
BranchTrueFalseTotal
if $file eq $readme_from 0 9 9
166 }
167 }
168  
169 9 ▶ if ( $force_readme > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_readme > 0) 1 8 9
170 1 ▶ $v_status = "force .readme_from " . $self->readme_from . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
171 if $self->verbose;
172 1 push( @index, $self->readme_from );
173 }
174  
175 9 ▶ unless (@index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@index) 0 9 9
176 0 $status = "(0)\n";
177 0 return 0;
178 }
179  
180 9 $self->tmp_sys( qw/git checkout-index/, @index );
181  
182 9 foreach my $file (@index) {
183 10 ▶ if ( $file !~ m/\.(pl|pod|pm|t)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file =~ /\.(pl|pod|pm|t)$/i) 5 5 10
184 5 my ($first) = $temp_dir->child($file)->lines( { count => 1 } );
185 next
186 5 ▶ unless $first =~ m/^#!.*perl/
Branch: 1 branch
BranchTrueFalseTotal
unless $first =~ /^#!.*perl/ or $first =~ /^#!.*\.plenv/ 3 2 5
Condition66%2/3
$first =~ /^#!.*perl/ or $first =~ /^#!.*\.plenv/
l!l&&r!l&&!r
203
MC/DC50%1/2
$first =~ /^#!.*perl/ or $first =~ /^#!.*\.plenv/
$first =~ /^#!.*perl/ $first =~ /^#!.*\.plenv/
Truth table66%2/3
A or B
A= $first =~ /^#!.*perl/B= $first =~ /^#!.*\.plenv/
ABresult
1X1
011
000
187 or $first =~ m/^#!.*\.plenv/;
188 }
189  
190 7 push( @perlfiles, path($file) );
191 }
192  
193 9 ▶ unless (@perlfiles) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@perlfiles) 2 7 9
194 2 $status = "files touched: 0\n";
195 2 exit 0;
196 }
197  
198 7 ▶ $v_status = "no .podtidy-opts - skipping podtidy calls\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->podtidyrc 5 2 7
199 if not $self->podtidyrc;
200  
201 7 my $i = 1;
202 7 my $total = scalar @perlfiles;
203 7 foreach my $file (@perlfiles) {
204 7 my $base = $file->basename;
205 7 $status = $v_status = '';
206 7 local $status = local $v_status = "$status ($i/$total) $base " . RS;
207  
208 7 my $tmp_file = $temp_dir->child($file);
209  
210 # If the README conversion is forced then we don't need to tidy
211 # the source file
212 7 ▶ if ( $file eq $readme_from and $force_readme > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq $readme_from and $force_readme > 0) 1 6 7
Condition66%2/3
$file eq $readme_from and $force_readme > 0
!ll&&!rl&&r
601
MC/DC50%1/2
$file eq $readme_from and $force_readme > 0
$file eq $readme_from $force_readme > 0
Truth table66%2/3
A and B
A= $file eq $readme_fromB= $force_readme > 0
ABresult
0X0
100
111
213 1 $self->convert_readme($tmp_file);
214 1 $force_readme--;
215 1 next;
216 }
217  
218 # Critique first to avoid unecessary tidying
219 6 ▶ if ( $self->perlcriticrc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->perlcriticrc) 0 6 6
220 0 $self->critic_perl( $tmp_file, $file, 'INDEX' );
221 }
222  
223 6 ▶ unless ( $file =~ m/\.pod$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file =~ /\.pod$/i) 5 1 6
224 5 $self->tidy_perl( $tmp_file, $file, 'INDEX' );
225 }
226  
227 5 ▶ if ( $self->podtidyrc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->podtidyrc) 1 4 5
228 1 $self->tidy_pod( $tmp_file, $file, 'INDEX' );
229 }
230  
231 5 ▶ if ( $file eq $readme_from ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq $readme_from) 0 5 5
232 0 $self->convert_readme($tmp_file);
233 }
234  
235 5 $i++;
236 }
237  
238 6 ▶ warn "Failed to convert to README: $readme_from\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $force_readme <= 0 0 6 6
239 unless $force_readme <= 0;
240  
241 6 $self->tmp_sys( qw/git add /, @perlfiles );
242  
243 6 $i = 1;
244 6 foreach my $file (@perlfiles) {
245 6 my $base = $file->basename;
246 6 $status = $v_status = '';
247 6 local $status = local $v_status = "$status ($i/$total) $base " . RS;
248  
249 6 my $tmp_file = $temp_dir->child($file);
250  
251 # Redo the whole thing again for partially modified files
252 6 ▶ if ( $partial{$file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($partial{$file}) 0 6 6
253 0 $v_status = "copy to TMP\n";
254 0 $file->copy($tmp_file);
255  
256 0 ▶ unless ( $file =~ m/\.pod$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file =~ /\.pod$/i) 0 0 0
257 0 $self->tidy_perl( $tmp_file, $file, 'WORK_TREE' );
258 }
259  
260 0 ▶ if ( $self->podtidyrc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->podtidyrc) 0 0 0
261 0 $self->tidy_pod( $tmp_file, $file, 'INDEX' );
262 0 $self->tidy_pod( $tmp_file, $file, 'WORK_TREE' );
263 }
264 }
265  
266 # Move the tidied file back to the real working directory
267 6 $status = 'move TMP to REPO';
268 6 my $mtime = $file->stat->mtime;
269 6 $tmp_file->move($file);
270 6 utime $file->stat->atime, $mtime, $file;
271 }
272  
273 6 $status = "files touched: $total\n";
274 }
275  
276 11 1;
277 __END__
↑ Summary lib/App/githook/perltidy/install.pm »