blib/lib/Git/MoreHooks/CheckIndent.pm

filter: Statementstmt 35.3% 41 / 116 Branchbran 12.5% 3 / 24 Conditioncond 29.1% 7 / 24 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 60.0% 9 / 15 Podpod 0.0% 0 / 3 totaltotal 31.9% 62 / 194 CC 32 SCAR 43.0
CC
32
Cov
31%
do_hook
182.0
_setup_config
72.0
handle_file
56.0
CRAP 73.9
« blib/lib/Git/MoreHooks/TriggerJenkins.pm ↑ Summary blib/lib/Git/MoreHooks/CheckCommitAuthorFromMailmap.pm »
1 ## no critic (Documentation::PodSpelling)
2 ## no critic (Documentation::RequirePodAtEnd)
3 ## no critic (Documentation::RequirePodSections)
4 ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
5 ## no critic (Subroutines::RequireArgUnpacking)
6  
7 package Git::MoreHooks::CheckIndent;
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: Check committed files for problems with indentation.
14  
15 1 our $VERSION = '0.021';
16  
17  
18 1 ▶use Git::MoreHooks::CheckCommitBase \&do_hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Git::Hooks 2.014000; # v2.14.0: oldest release on CPAN
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Log::Any qw{$log};
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 1 my $PKG = __PACKAGE__;
27 1 my ($CFG) = __PACKAGE__ =~ /::([^:]+)$/msx;
28 1 $CFG = 'githooks.' . $CFG;
29  
30  
31 ####################
32 # Hook configuration, check it and set defaults.
33  
34 0 ▶sub _setup_config {
Subroutine _setup_config: not called CC=8 SCAR=42.8
35 0 my ($git) = @_;
36 0 $log->debugf( __PACKAGE__ . '::_setup_config(%s):', '$git' );
37  
38 0 my $config = $git->get_config();
39 0 $log->tracef( __PACKAGE__ . '::_setup_config(): Current Git config:\n%s.', $config );
40  
41 # Put empty hash if there is no config items.
42 0 ▶ $config->{ lc $CFG } //= {};
Condition0%0/2
$$config{lc $CFG} //= {}
l!l
00
MC/DC0%0/1
$$config{lc $CFG} //= {}
$$config{lc $CFG}
Truth table0%0/2
A
A= $$config{lc $CFG}
Aresult
11
00
43  
44 # Set default config values.
45 0 my $default = $config->{ lc $CFG };
46 0 ▶ $default->{'file'} //= [];
Condition0%0/2
$default->{"file"} //= []
l!l
00
MC/DC0%0/1
$default->{"file"} //= []
$default->{"file"}
Truth table0%0/2
A
A= $default->{"file"}
Aresult
11
00
47 0 ▶ $default->{'exception'} //= [];
Condition0%0/2
$default->{"exception"} //= []
l!l
00
MC/DC0%0/1
$default->{"exception"} //= []
$default->{"exception"}
Truth table0%0/2
A
A= $default->{"exception"}
Aresult
11
00
48  
49 # Check validity of config items.
50 0 foreach my $file_def ( @{$default->{'file'}} ) {
51 0 $log->debugf( __PACKAGE__ . q{::} . '_setup_config(): Check for validity, config item: \'%s\'.', $file_def );
52 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_def =~ /^ (?:[[:graph:]]+) (?: (?:[[:space:]]{1,}indent-size:[[:digit:]]+){1,} | (?:[[:space:]]{1,}indent-char:(?:space|tab|both)) ){1,} (?:[[:space:]]{0,}) $/msx) 0 0 0
53 ## no critic (RegularExpressions::ProhibitComplexRegexes)
54 $file_def !~ m{^
55 (?:[[:graph:]]+)
56 (?:
57 (?:[[:space:]]{1,}indent-size:[[:digit:]]+){1,}
58 | (?:[[:space:]]{1,}indent-char:(?:space|tab|both))
59 ){1,}
60 (?:[[:space:]]{0,})
61 $}msx
62 ## use critic (RegularExpressions::ProhibitComplexRegexes)
63 )
64 {
65 0 $git->error( $PKG, 'Faulty config item: \'' . $file_def . '\'.' );
66 0 return 0;
67 }
68 }
69 0 foreach my $exc_def ( @{$default->{'exception'}} ) {
70 0 $log->debugf( __PACKAGE__ . q{::} . '_setup_config(): Check for validity, config item: \'%s\'.', $exc_def );
71 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($exc_def =~ /^ (?:[[:space:]]{0,}) (?# Free spacing before) (?:[[:graph:]]+) (?# File name pattern) (?:[[:space:]]{1,}) (?# Required spacing) (?:[[:graph:]]+) (?# Regular expression) (?:[[:space:]]{0,}) (?# Free spacing after) $/msx) 0 0 0
72 ## no critic (RegularExpressions::ProhibitComplexRegexes)
73 $exc_def !~ m{^
74 (?:[[:space:]]{0,}) (?# Free spacing before)
75 (?:[[:graph:]]+) (?# File name pattern)
76 (?:[[:space:]]{1,}) (?# Required spacing)
77 (?:[[:graph:]]+) (?# Regular expression)
78 (?:[[:space:]]{0,}) (?# Free spacing after)
79 $}msx
80 ## use critic (RegularExpressions::ProhibitComplexRegexes)
81 )
82 {
83 0 $git->error( $PKG, 'Faulty config item: \'' . $exc_def . '\'.' );
84 0 return 0;
85 }
86 }
87 0 return 1;
88 }
89  
90 ####################
91 # Internal functions
92  
93 2 ▶sub check_for_indent {
Subroutine check_for_indent: called CC=7 SCAR=19.9
Pod undocumented
94 2 my %params = validate(
95 @_,
96 {
97 file_as_string => { type => SCALAR, },
98 indent_char => {
99 type => SCALAR,
100 default => q{ },
101 regex => qr/[[:space:]]{1,}/msx,
102 },
103 indent_size => {
104 type => SCALAR,
105 default => 4,
106 regex => qr/[[:digit:]]{1,}/msx,
107 },
108 exceptions => {
109 type => ARRAYREF,
110 default => [],
111 },
112 },
113 );
114 2 my $ic = $params{'indent_char'};
115 2 my $row_nr = 1;
116 2 my %errors;
117 2 foreach my $row ( split qr/\n/msx, $params{'file_as_string'} ) {
118  
119 # Check for faulty tab chars (space/tab)
120 9 my ($indents) = $row =~ m/^([[:space:]]{0,})/msx;
121 9 ▶ if ( length $indents > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (length $indents > 0 and !($indents =~ /^[$ic]{1,}$/msx) || $ic ne "\t" && length($indents) % $params{"indent_size"} != 0) 3 6 9
Condition77%7/9
length $indents > 0 and !($indents =~ /^[$ic]{1,}$/msx) || $ic ne "\t" && length($indents) % $params{"indent_size"} != 0
!ll&&!rl&&r
243
not $indents =~ /^[$ic]{1,}$/msx or $ic ne "\t" and length($indents) % $params{"indent_size"} != 0
l!l&&r!l&&!r
304
$ic ne "\t" and length($indents) % $params{"indent_size"} != 0
!ll&&!rl&&r
220
MC/DC50%2/4
length $indents > 0 and !($indents =~ /^[$ic]{1,}$/msx) || $ic ne "\t" && length($indents) % $params{"indent_size"} != 0
length $indents > 0 not $indents =~ /^[$ic]{1,}$/msx $ic ne "\t" length($indents) % $params{"indent_size"} != 0
Truth table80%4/5
A and not(B) or C and D
A= length $indents > 0B= not $indents =~ /^[$ic]{1,}$/msxC= $ic ne "\t"D= length($indents) % $params{"indent_size"} != 0
ABCDresult
0XXX0
100X0
10100
11XX1
10111
122 (
123 $indents !~ m/^[$ic]{1,}$/msx
124 || (($ic ne qq{\t}) && (length $indents) % $params{'indent_size'} != 0)
125 ) ) {
126 # If there is an exception regexp that matches this row,
127 # then skip logging it as error.
128 0 ▶ if (!map { $row =~ m/$_/msx } @{$params{'exceptions'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not map({$row =~ /$_/msx;} @{$params{'exceptions'};})) { } 3 0 3
129 3 $errors{$row_nr} = $row;
130 } else {
131 0 $log->debugf(__PACKAGE__ . q{::} . 'check_for_indent(): Except this row: \'%s\'', $row);
132 }
133 }
134 9 $row_nr++;
135 }
136 2 return %errors;
137 }
138  
139 ####################
140 # Callback function
141  
142 0 ▶sub do_hook {
Subroutine do_hook: not called CC=13 SCAR=52.0
Pod undocumented
143 0 my ($git, $hook_name, $opts) = @_;
144 0 $log->tracef( 'do_hook(%s)', (join q{:}, @_) );
145  
146 0 ▶ return 1 if $git->im_admin();
Branch: 1 branch
BranchTrueFalseTotal
if $git->im_admin 0 0 0
147 0 ▶ if ( !_setup_config($git) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_setup_config($git)) 0 0 0
148 0 return 0;
149 }
150  
151 0 my $errors = 0;
152 0 ▶ if ( $hook_name eq 'pre-commit' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($hook_name eq 'pre-commit') { } 0 0 0
elsif ($hook_name eq 'patchset-created' or $hook_name eq 'draft-published') { } 0 0 0
elsif ($hook_name eq 'update' or $hook_name eq 'pre-receive' or $hook_name eq 'ref-update') { } 0 0 0
153 0 my @files = $git->filter_files_in_index('AM');
154 0 foreach my $file (@files) {
155 0 ▶ my $read_file_func_ptr = sub { return path(shift)->slurp( { 'binmode' => ':raw' } ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 0 $errors += handle_file($git, $file, $read_file_func_ptr, ':0');
157 }
158 ▶ } elsif ( $hook_name eq 'patchset-created' || $hook_name eq 'draft-published' ) {
Condition0%0/3
$hook_name eq "patchset-created" or $hook_name eq "draft-published"
l!l&&r!l&&!r
000
MC/DC0%0/2
$hook_name eq "patchset-created" or $hook_name eq "draft-published"
$hook_name eq "patchset-created" $hook_name eq "draft-published"
Truth table0%0/3
A or B
A= $hook_name eq "patchset-created"B= $hook_name eq "draft-published"
ABresult
1X1
011
000
159 0 my @files = $git->filter_files_in_commit('AM', $opts->{'gerrit-opts'}->{'--commit'});
160 0 foreach my $file (@files) {
161 0 ▶ my $read_file_func_ptr = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
162 0 my ($file, $commit) = @_;
163 0 my $tmpfile_name = $git->blob($commit, $file);
164 0 return path($tmpfile_name)->slurp( { 'binmode' => ':raw' } );
165 0 };
166 0 $errors += handle_file($git, $file, $read_file_func_ptr, $opts->{'gerrit-opts'}->{'--commit'});
167 }
168 ▶ } elsif ( $hook_name eq 'update' || $hook_name eq 'pre-receive' || $hook_name eq 'ref-update' ) {
Condition0%0/6
$hook_name eq "update" or $hook_name eq "pre-receive" or $hook_name eq "ref-update"
l!l&&r!l&&!r
000
$hook_name eq "update" or $hook_name eq "pre-receive"
l!l&&r!l&&!r
000
MC/DC0%0/3
$hook_name eq "update" or $hook_name eq "pre-receive" or $hook_name eq "ref-update"
$hook_name eq "update" $hook_name eq "pre-receive" $hook_name eq "ref-update"
Truth table0%0/4
A or B or C
A= $hook_name eq "update"B= $hook_name eq "pre-receive"C= $hook_name eq "ref-update"
ABCresult
1XX1
01X1
0011
0000
169 0 foreach my $ref ($git->get_affected_refs()) {
170 0 my ($old_commit, $new_commit) = $git->get_affected_ref_range($ref);
171 0 my @files = $git->filter_files_in_range('AM', $old_commit, $new_commit);
172 0 foreach my $file (@files) {
173 0 ▶ my $read_file_func_ptr = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
174 0 my ($file, $commit) = @_;
175 0 my $tmpfile_name = $git->blob($commit, $file);
176 0 return path($tmpfile_name)->slurp( { 'binmode' => ':raw' } );
177 0 };
178 0 $errors += handle_file($git, $file, $read_file_func_ptr, $new_commit);
179 }
180 }
181 }
182 0 return $errors == 0;
183 }
184 0 ▶sub handle_file {
Subroutine handle_file: not called CC=7 SCAR=40.3
Pod undocumented
185 0 my ($git, $filename, $read_file_func_ptr, $commit) = @_;
186 0 $log->tracef( 'handle_file(%s)', (join q{:}, @_) );
187 0 my @file_defs = $git->get_config($CFG => 'file');
188 0 my %opts;
189 0 my $errors = 0;
190 0 foreach my $file_def (@file_defs) {
191 0 my ($file_regexp, $options) = split q{ }, $file_def, 2;
192 0 ▶ if ( $filename =~ m/$file_regexp/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /$file_regexp/msx) 0 0 0
193 0 ($opts{'indent_size'}) = $options =~ m/indent-size:([[:digit:]]+)/msx;
194 0 ($opts{'indent_char'}) = $options =~ m/indent-char:(space|tab|both)/msx;
195 0 my $file_as_string = &{$read_file_func_ptr}($filename, $commit);
196 0 my %exceptions;
197 0 foreach my $exc_row ($git->get_config($CFG => 'exception')) {
198 0 my ($exc_file_regexp, $exc) = split qr{[[:space:]]+}msx, $exc_row;
199 0 ▶ if ($filename =~ m/$exc_file_regexp/msx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /$exc_file_regexp/msx) 0 0 0
200 0 $exceptions{$exc_file_regexp} = $exc;
201 }
202 }
203 my %results = check_for_indent(
204 'file_as_string' => $file_as_string,
205 'indent_char' => $opts{'indent_char'} eq 'space' ? q{ } : qq{\t},
206 0 ▶ 'indent_size' => $opts{'indent_size'},
Branch: 1 branch
BranchTrueFalseTotal
$opts{'indent_char'} eq 'space' ? : 0 0 0
207 'exceptions' => [values %exceptions],
208 );
209 0 foreach my $row_nr (keys %results) {
210 $git->fault( (sprintf 'Indent error (%s:%s): \'%s\'',
211 0 $filename, $row_nr, $results{$row_nr}),
212 { prefix => $PKG, commit => $commit }
213 );
214 0 $errors++;
215 }
216 0 last;
217 }
218 }
219 0 return $errors;
220 }
221  
222 1 1;
223  
224 __END__
« blib/lib/Git/MoreHooks/TriggerJenkins.pm ↑ Summary blib/lib/Git/MoreHooks/CheckCommitAuthorFromMailmap.pm »