| 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 | our $VERSION = '0.019'; # VERSION: generated by DZP::OurPkgVersion | ||
| 16 | |||
| 17 | |||
| 18 | 1 | ▶ | use Git::MoreHooks::CheckCommitBase \&do_hook; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 19 | |||
| 20 | use Git::Hooks 3.000000; | ||
| 21 | use Path::Tiny; | ||
| 22 | use Log::Any qw{$log}; | ||
| 23 | use Params::Validate qw(:all); | ||
| 24 | |||
| 25 | |||
| 26 | my $PKG = __PACKAGE__; | ||
| 27 | my ($CFG) = __PACKAGE__ =~ /::([^:]+)$/msx; | ||
| 28 | $CFG = 'githooks.' . $CFG; | ||
| 29 | |||
| 30 | |||
| 31 | #################### | ||
| 32 | # Hook configuration, check it and set defaults. | ||
| 33 | |||
| 34 | sub _setup_config { | ||
| 35 | my ($git) = @_; | ||
| 36 | $log->debugf( __PACKAGE__ . '::_setup_config(%s):', '$git' ); | ||
| 37 | |||
| 38 | my $config = $git->get_config(); | ||
| 39 | $log->tracef( __PACKAGE__ . '::_setup_config(): Current Git config:\n%s.', $config ); | ||
| 40 | |||
| 41 | # Put empty hash if there is no config items. | ||
| 42 | $config->{ lc $CFG } //= {}; | ||
| 43 | |||
| 44 | # Set default config values. | ||
| 45 | my $default = $config->{ lc $CFG }; | ||
| 46 | $default->{'file'} //= []; | ||
| 47 | $default->{'exception'} //= []; | ||
| 48 | |||
| 49 | # Check validity of config items. | ||
| 50 | foreach my $file_def ( @{$default->{'file'}} ) { | ||
| 51 | $log->debugf( __PACKAGE__ . q{::} . '_setup_config(): Check for validity, config item: \'%s\'.', $file_def ); | ||
| 52 | if ( | ||
| 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 | $git->error( $PKG, 'Faulty config item: \'' . $file_def . '\'.' ); | ||
| 66 | return 0; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | foreach my $exc_def ( @{$default->{'exception'}} ) { | ||
| 70 | $log->debugf( __PACKAGE__ . q{::} . '_setup_config(): Check for validity, config item: \'%s\'.', $exc_def ); | ||
| 71 | if ( | ||
| 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 | $git->error( $PKG, 'Faulty config item: \'' . $exc_def . '\'.' ); | ||
| 84 | return 0; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | return 1; | ||
| 88 | } | ||
| 89 | |||
| 90 | #################### | ||
| 91 | # Internal functions | ||
| 92 | |||
| 93 | sub check_for_indent { | ||
| 94 | 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 | my $ic = $params{'indent_char'}; | ||
| 115 | my $row_nr = 1; | ||
| 116 | my %errors; | ||
| 117 | foreach my $row ( split qr/\n/msx, $params{'file_as_string'} ) { | ||
| 118 | |||
| 119 | # Check for faulty tab chars (space/tab) | ||
| 120 | my ($indents) = $row =~ m/^([[:space:]]{0,})/msx; | ||
| 121 | if ( length $indents > 0 && | ||
| 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 | if (!map { $row =~ m/$_/msx } @{$params{'exceptions'}}) { | ||
| 129 | $errors{$row_nr} = $row; | ||
| 130 | } else { | ||
| 131 | $log->debugf(__PACKAGE__ . q{::} . 'check_for_indent(): Except this row: \'%s\'', $row); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | $row_nr++; | ||
| 135 | } | ||
| 136 | return %errors; | ||
| 137 | } | ||
| 138 | |||
| 139 | #################### | ||
| 140 | # Callback function | ||
| 141 | |||
| 142 | sub do_hook { | ||
| 143 | my ($git, $hook_name, $opts) = @_; | ||
| 144 | $log->tracef( 'do_hook(%s)', (join q{:}, @_) ); | ||
| 145 | |||
| 146 | return 1 if $git->im_admin(); | ||
| 147 | if ( !_setup_config($git) ) { | ||
| 148 | return 0; | ||
| 149 | } | ||
| 150 | |||
| 151 | my $errors = 0; | ||
| 152 | if ( $hook_name eq 'pre-commit' ) { | ||
| 153 | my @files = $git->filter_files_in_index('AM'); | ||
| 154 | foreach my $file (@files) { | ||
| 155 | my $read_file_func_ptr = sub { return path(shift)->slurp( { 'binmode' => ':raw' } ) }; | ||
| 156 | $errors += handle_file($git, $file, $read_file_func_ptr, ':0'); | ||
| 157 | } | ||
| 158 | } elsif ( $hook_name eq 'patchset-created' || $hook_name eq 'draft-published' ) { | ||
| 159 | my @files = $git->filter_files_in_commit('AM', $opts->{'gerrit-opts'}->{'--commit'}); | ||
| 160 | foreach my $file (@files) { | ||
| 161 | my $read_file_func_ptr = sub { | ||
| 162 | my ($file, $commit) = @_; | ||
| 163 | my $tmpfile_name = $git->blob($commit, $file); | ||
| 164 | return path($tmpfile_name)->slurp( { 'binmode' => ':raw' } ); | ||
| 165 | }; | ||
| 166 | $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' ) { | ||
| 169 | foreach my $ref ($git->get_affected_refs()) { | ||
| 170 | my ($old_commit, $new_commit) = $git->get_affected_ref_range($ref); | ||
| 171 | my @files = $git->filter_files_in_range('AM', $old_commit, $new_commit); | ||
| 172 | foreach my $file (@files) { | ||
| 173 | my $read_file_func_ptr = sub { | ||
| 174 | my ($file, $commit) = @_; | ||
| 175 | my $tmpfile_name = $git->blob($commit, $file); | ||
| 176 | return path($tmpfile_name)->slurp( { 'binmode' => ':raw' } ); | ||
| 177 | }; | ||
| 178 | $errors += handle_file($git, $file, $read_file_func_ptr, $new_commit); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } | ||
| 182 | return $errors == 0; | ||
| 183 | } | ||
| 184 | sub handle_file { | ||
| 185 | my ($git, $filename, $read_file_func_ptr, $commit) = @_; | ||
| 186 | $log->tracef( 'handle_file(%s)', (join q{:}, @_) ); | ||
| 187 | my @file_defs = $git->get_config($CFG => 'file'); | ||
| 188 | my %opts; | ||
| 189 | my $errors = 0; | ||
| 190 | foreach my $file_def (@file_defs) { | ||
| 191 | my ($file_regexp, $options) = split q{ }, $file_def, 2; | ||
| 192 | if ( $filename =~ m/$file_regexp/msx ) { | ||
| 193 | ($opts{'indent_size'}) = $options =~ m/indent-size:([[:digit:]]+)/msx; | ||
| 194 | ($opts{'indent_char'}) = $options =~ m/indent-char:(space|tab|both)/msx; | ||
| 195 | my $file_as_string = &{$read_file_func_ptr}($filename, $commit); | ||
| 196 | my %exceptions; | ||
| 197 | foreach my $exc_row ($git->get_config($CFG => 'exception')) { | ||
| 198 | my ($exc_file_regexp, $exc) = split qr{[[:space:]]+}msx, $exc_row; | ||
| 199 | if ($filename =~ m/$exc_file_regexp/msx) { | ||
| 200 | $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 | 'indent_size' => $opts{'indent_size'}, | ||
| 207 | 'exceptions' => [values %exceptions], | ||
| 208 | ); | ||
| 209 | foreach my $row_nr (keys %results) { | ||
| 210 | $git->fault( (sprintf 'Indent error (%s:%s): \'%s\'', | ||
| 211 | $filename, $row_nr, $results{$row_nr}), | ||
| 212 | { prefix => $PKG, commit => $commit } | ||
| 213 | ); | ||
| 214 | $errors++; | ||
| 215 | } | ||
| 216 | last; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | return $errors; | ||
| 220 | } | ||
| 221 | |||
| 222 | 1; | ||
| 223 | |||
| 224 | __END__ | ||