blib/lib/Code/TidyAll/Git/Prereceive.pm

filter: Statementstmt 94.3% 117 / 124 Branchbran 60.7% 17 / 28 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 95.8% 23 / 24 Podpod 33.3% 2 / 6 totaltotal 82.2% 162 / 197 CC 21 SCAR 15.3
CC
21
Cov
85%
check_input
9.8
check_repeated_push
7.7
__ANON__
6.0
CRAP 4.6
« blib/lib/Code/TidyAll/Plugin/PodTidy.pm ↑ Summary lib/Code/TidyAll/Plugin.pm »
1 package Code::TidyAll::Git::Prereceive;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Capture::Tiny qw(capture);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Code::TidyAll::Util qw(tempdir_simple);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Code::TidyAll;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Digest::SHA qw(sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use IPC::System::Simple qw(capturex run);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Path::Tiny qw(cwd path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Specio::Library::Builtins;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Specio::Library::Numeric;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Specio::Library::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 4 our $VERSION = '0.85';
20  
21 4 has allow_repeated_push => (
22 is => 'ro',
23 isa => t('PositiveInt'),
24 default => 3,
25 );
26  
27 4 has conf_name => (
28 is => 'ro',
29 isa => t('NonEmptyStr'),
30 );
31  
32 has extra_conf_files => (
33 is => 'ro',
34 isa => t( 'ArrayRef', of => t('NonEmptyStr') ),
35 4 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 4 );
37  
38 4 has git_path => (
39 is => 'ro',
40 isa => t('NonEmptyStr'),
41 default => 'git',
42 );
43  
44 4 has tidyall_class => (
45 is => 'ro',
46 isa => t('ClassName'),
47 default => 'Code::TidyAll',
48 );
49  
50 has tidyall_options => (
51 is => 'ro',
52 isa => t('HashRef'),
53 4 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
54 4 );
55  
56 4 ▶sub check {
Subroutine check: called CC=2 SCAR=7.5
Pod documented
57 4 my ( $class, %params ) = @_;
58  
59 4 my $fail_msg;
60 4 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 4 my $self = $class->new(%params);
62  
63 4 my $root_dir = cwd();
64 4 local $ENV{GIT_DIR} = $root_dir;
65  
66 4 my $input = do { local $/; <STDIN> };
67 4 $fail_msg = $self->check_input($input);
68 }
69 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
70 0 my $error = $_;
71 0 ▶ if ( $params{reject_on_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'reject_on_error'}) { } 0 0 0
72 0 die $error;
73 }
74 else {
75 0 print STDERR "*** Error running pre-receive hook (allowing push to proceed):\n$error";
76 }
77 4 };
78 4 ▶ die "$fail_msg\n" if $fail_msg;
Branch: 1 branch
BranchTrueFalseTotal
if $fail_msg 3 1 4
79 }
80  
81 4 ▶sub check_input {
Subroutine check_input: called CC=9 SCAR=22.8
Pod documented
82 4 my ( $self, $input ) = @_;
83  
84 4 my @lines = split( "\n", $input );
85 4 my ( @results, $tidyall );
86 4 foreach my $line (@lines) {
87 4 chomp($line);
88 4 my ( $base, $commit, $ref ) = split( /\s+/, $line );
89  
90 # Create tidyall using configuration found in first commit
91 #
92 4 ▶ $tidyall ||= $self->create_tidyall($commit);
Condition33%1/3
$tidyall ||= $self->create_tidyall($commit)
l!l&&r!l&&!r
040
MC/DC0%0/2
$tidyall ||= $self->create_tidyall($commit)
$tidyall $self->create_tidyall($commit)
Truth table33%1/3
A ||= B
A= $tidyallB= $self->create_tidyall($commit)
ABresult
1X1
011
000
93  
94 4 my @files = $self->get_changed_files( $base, $commit );
95 4 foreach my $file (@files) {
96 4 my $contents = $self->get_file_contents( $file, $commit );
97 4 ▶ if ( $contents =~ /\S/ && $contents =~ /\n/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /\S/ and $contents =~ /\n/) 4 0 4
Condition33%1/3
$contents =~ /\S/ and $contents =~ /\n/
!ll&&!rl&&r
004
MC/DC0%0/2
$contents =~ /\S/ and $contents =~ /\n/
$contents =~ /\S/ $contents =~ /\n/
Truth table33%1/3
A and B
A= $contents =~ /\S/B= $contents =~ /\n/
ABresult
0X0
100
111
98 4 push( @results, $tidyall->process_source( $contents, $file ) );
99 }
100 }
101 }
102  
103 4 my $fail_msg;
104 4 ▶ if ( my @error_results = grep { $_->error } @results ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@error_results) = grep({$_->error;} @results)) 3 1 4
105 3 ▶ unless ( $self->check_repeated_push($input) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check_repeated_push($input)) 3 0 3
106 3 my $error_count = scalar(@error_results);
107 3 ▶ $fail_msg = sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$error_count > 1 ? : 0 3 3
108 '%d file%s did not pass tidyall check',
109 $error_count, $error_count > 1 ? 's' : q{}
110 );
111 }
112 }
113 4 return $fail_msg;
114 }
115  
116 4 ▶sub create_tidyall {
Subroutine create_tidyall: called CC=5 SCAR=16.3
Pod undocumented
117 4 my ( $self, $commit ) = @_;
118  
119 4 my $temp_dir = tempdir_simple();
120 4 ▶ my @conf_names = $self->conf_name ? ( $self->conf_name ) : Code::TidyAll->default_conf_names;
Branch: 1 branch
BranchTrueFalseTotal
$self->conf_name ? : 0 4 4
121 4 ▶ my ($conf_file) = grep { $self->get_file_contents( $_, $commit ) } @conf_names
Branch: 1 branch
BranchTrueFalseTotal
my($conf_file) = grep({$self->get_file_contents($_, $commit);} @conf_names) or die sprintf('could not find conf file %s', join(" or ", @conf_names)) 4 0 4
122 or die sprintf( 'could not find conf file %s', join( ' or ', @conf_names ) );
123 4 foreach my $rel_file ( $conf_file, @{ $self->extra_conf_files } ) {
124 4 ▶ my $contents = $self->get_file_contents( $rel_file, $commit )
Branch: 1 branch
BranchTrueFalseTotal
my $contents = $self->get_file_contents($rel_file, $commit) or die sprintf(q[could not find file '%s' in repo root], $rel_file) 4 0 4
125 or die sprintf( q{could not find file '%s' in repo root}, $rel_file );
126 4 $temp_dir->child($rel_file)->spew($contents);
127 }
128 my $tidyall = $self->tidyall_class->new_from_conf_file(
129 "$temp_dir/" . $conf_file,
130 mode => 'commit',
131 quiet => 1,
132 4 %{ $self->tidyall_options },
133 no_cache => 1,
134 no_backups => 1,
135 check_only => 1,
136 );
137 4 return $tidyall;
138 }
139  
140 4 ▶sub get_changed_files {
Subroutine get_changed_files: called CC=1 SCAR=0.0
Pod undocumented
141 4 my ( $self, $base, $commit ) = @_;
142 4 my $output = capturex( $self->git_path, 'diff', '--numstat', '--name-only', "$base..$commit" );
143 4 my @files = grep {/\S/} split( "\n", $output );
144 4 return @files;
145 }
146  
147 16 ▶sub get_file_contents {
Subroutine get_file_contents: called CC=1 SCAR=0.0
Pod undocumented
148 16 my ( $self, $file, $commit ) = @_;
149 16 ▶ my ( $contents, $error ) = capture { system( $self->git_path, 'show', "$commit:$file" ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
150 16 return $contents;
151 }
152  
153 3 ▶sub check_repeated_push {
Subroutine check_repeated_push: called CC=7 SCAR=20.4
Pod undocumented
154 3 my ( $self, $input ) = @_;
155  
156 3 my $allow = $self->allow_repeated_push;
157 3 ▶ return 0 unless defined $allow;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allow 0 3 3
158  
159 3 my $cwd = path($0)->realpath->parent;
160 3 my $last_push_file = $cwd->child('.prereceive_lastpush');
161 3 ▶ if ( -w $cwd || -w $last_push_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-w $cwd or -w $last_push_file) 3 0 3
Condition33%1/3
-w $cwd or -w $last_push_file
l!l&&r!l&&!r
300
MC/DC0%0/2
-w $cwd or -w $last_push_file
-w $cwd -w $last_push_file
Truth table33%1/3
A or B
A= -w $cwdB= -w $last_push_file
ABresult
1X1
011
000
162 3 my $push_sig = sha1_hex($input);
163 3 ▶ if ( -f $last_push_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $last_push_file) 2 1 3
164 2 my ( $last_push_sig, $count ) = split( /\s+/, $last_push_file->slurp );
165 2 ▶ if ( $last_push_sig eq $push_sig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_push_sig eq $push_sig) 1 1 2
166 1 ++$count;
167 1 print STDERR "*** Identical push seen $count times\n";
168 1 ▶ if ( $count >= $allow ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count >= $allow) 0 1 1
169 0 print STDERR "*** Allowing push to proceed despite errors\n";
170 0 unlink($last_push_file);
171 0 return 1;
172 }
173 1 $last_push_file->spew( join( q{ }, $push_sig, $count ) );
174 1 return 0;
175 }
176 }
177 2 $last_push_file->spew( join( q{ }, $push_sig, 1 ) );
178 }
179  
180 2 return 0;
181 }
182  
183 4 1;
184  
185 # ABSTRACT: Git pre-receive hook that requires files to be tidyall'd
186  
187 __END__
« blib/lib/Code/TidyAll/Plugin/PodTidy.pm ↑ Summary lib/Code/TidyAll/Plugin.pm »