blib/lib/Code/TidyAll/SVN/Precommit.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 180 CC 15 SCAR 38.3
CC
15
Cov
0%
check
110.0
find_conf_for_file
30.0
cat_file
6.0
CRAP 45.9
1 package Code::TidyAll::SVN::Precommit;
2  
3 use strict;
4 use warnings;
5  
6 use Capture::Tiny qw(capture_stdout capture_stderr);
7 use Code::TidyAll::Util qw(tempdir_simple);
8 use Code::TidyAll;
9 use Log::Any qw($log);
10 use Path::Tiny qw(path);
11 use SVN::Look;
12 use Try::Tiny;
13  
14 use Moo;
15  
16 our $VERSION = '0.83';
17  
18 # Public
19 has conf_name => ( is => 'ro' );
20 has emergency_comment_prefix => ( is => 'ro', default => 'NO TIDYALL' );
21 has extra_conf_files => ( is => 'ro', default => sub { [] } );
22 has reject_on_error => ( is => 'ro' );
23 has repos => ( is => 'ro', default => sub { $ARGV[0] } );
24 has tidyall_class => ( is => 'ro', default => 'Code::TidyAll' );
25 has tidyall_options => ( is => 'ro', default => sub { {} } );
26 has txn => ( is => 'ro', default => sub { $ARGV[1] } );
27  
28 # Private
29 has cat_file_cache => ( init_arg => undef, is => 'ro', default => sub { {} } );
30 has revlook => ( init_arg => undef, is => 'lazy' );
31  
32 sub _build_revlook {
33 my $self = shift;
34 return SVN::Look->new( $self->repos, '-t' => $self->txn );
35 }
36  
37 sub check {
38 my ( $class, %params ) = @_;
39  
40 my $fail_msg;
41  
42 try {
43 my $self = $class->new(%params);
44 my $revlook = $self->revlook;
45  
46 # Skip if emergency comment prefix is present
47 #
48 if ( my $prefix = $self->emergency_comment_prefix ) {
49 if ( index( $revlook->log_msg, $prefix ) == 0 ) {
50 return;
51 }
52 }
53  
54 my @files = ( $self->revlook->added(), $self->revlook->updated() );
55 $log->info('----------------------------');
56 $log->infof(
57 '%s [%s] repos = %s; txn = %s',
58 scalar(localtime), $$, scalar( getpwuid($<) ),
59 $self->repos, $self->txn
60 );
61 $log->infof( 'looking at files: %s', join( ', ', @files ) );
62  
63 my %conf_files;
64 foreach my $file (@files) {
65 if ( my $conf_file = $self->find_conf_for_file($file) ) {
66 my $root = $conf_file->parent;
67 my $rel_file = substr( $file, length($root) + 1 );
68 $conf_files{$conf_file}->{$rel_file}++;
69 }
70 else {
71 my $msg = sprintf( q{** could not find conf file upwards from '%s'}, $file );
72 $log->error($msg);
73 die $msg if $self->reject_on_error;
74 }
75 }
76  
77 my @results;
78 for my $conf_file ( map { path($_) } keys %conf_files ) {
79 my $file_map = $conf_files{$conf_file};
80  
81 my $root = $conf_file->parent;
82 my $conf_name = $conf_file->basename;
83 $log->error("$root, $conf_file");
84 my $tempdir = tempdir_simple();
85 my @files = keys(%$file_map);
86 foreach my $rel_file ( $conf_name, @{ $self->extra_conf_files }, @files ) {
87  
88 # TODO: what if cat fails
89 my $contents = $self->cat_file( $root->child($rel_file) );
90 my $full_path = $tempdir->child($rel_file);
91 $full_path->parent->mkpath( { mode => 0755 } );
92 $full_path->spew_raw($contents);
93 }
94 my $tidyall = $self->tidyall_class->new_from_conf_file(
95 $tempdir->child($conf_name),
96 no_cache => 1,
97 check_only => 1,
98 mode => 'commit',
99 %{ $self->tidyall_options },
100 );
101 my $stdout = capture_stdout {
102 push( @results, $tidyall->process_paths( map { $tempdir->child($_) } @files ) );
103 };
104 if ($stdout) {
105 chomp($stdout);
106 $log->info($stdout);
107 }
108 }
109  
110 if ( my @error_results = grep { $_->error } @results ) {
111 my $error_count = scalar(@error_results);
112 $fail_msg = join(
113 "\n",
114 sprintf(
115 '%d file%s did not pass tidyall check',
116 $error_count, $error_count > 1 ? 's' : q{}
117 ),
118 map { join( ': ', $_->path, $_->error ) } @error_results
119 );
120 }
121 }
122 catch {
123 my $error = $_;
124 $log->error($error);
125 die $error if $params{reject_on_error};
126 };
127 die $fail_msg if $fail_msg;
128 }
129  
130 sub find_conf_for_file {
131 my ( $self, $file ) = @_;
132  
133 my @conf_names = $self->conf_name ? ( $self->conf_name ) : Code::TidyAll->default_conf_names;
134 my $search_dir = $file->parent;
135 my $cnt = 0;
136 while (1) {
137 foreach my $conf_name (@conf_names) {
138 my $conf_file = $search_dir->child($conf_name);
139 return $conf_file if ( $self->cat_file($conf_file) );
140 }
141 if ( $search_dir eq '/' || $search_dir eq '' || $search_dir eq '.' ) {
142 return undef;
143 }
144 else {
145 $search_dir = $search_dir->parent;
146 }
147 die 'inf loop!' if ++$cnt > 100;
148 }
149 }
150  
151 sub cat_file {
152 my ( $self, $file ) = @_;
153 my $contents;
154 if ( exists( $self->cat_file_cache->{$file} ) ) {
155 $contents = $self->cat_file_cache->{$file};
156 }
157 else {
158 try {
159 capture_stderr { $contents = $self->revlook->cat($file) };
160 }
161 catch {
162 $contents = '';
163 };
164 $self->cat_file_cache->{$file} = $contents;
165 }
166 return $contents;
167 }
168  
169 1;
170  
171 # ABSTRACT: Subversion pre-commit hook that requires files to be tidyall'd
172  
173 __END__
174  
175 =pod
176  
177 =encoding UTF-8
178  
179 =head1 NAME
180  
181 Code::TidyAll::SVN::Precommit - Subversion pre-commit hook that requires files to be tidyall'd
182  
183 =head1 VERSION
184  
185 version 0.83
186  
187 =head1 SYNOPSIS
188  
189 In hooks/pre-commit in your svn repo:
190  
191 #!/usr/bin/perl
192 use Code::TidyAll::SVN::Precommit;
193 use Log::Any::Adapter (File => '/path/to/hooks/logs/tidyall.log');
194 use strict;
195 use warnings;
196  
197 Code::TidyAll::SVN::Precommit->check();
198  
199 =head1 DESCRIPTION
200  
201 This module implements a L<Subversion pre-commit
202 hook|http://svnbook.red-bean.com/en/1.7/svn.ref.reposhooks.pre-commit.html>
203 that checks if all files are tidied and valid according to L<tidyall>, and
204 rejects the commit if not.
205  
206 =head1 METHODS
207  
208 This class provides the following methods:
209  
210 =head1 Code::TidyAll::SVN::Precommit->check(%params)
211  
212 Check that all files being added or modified in this commit are tidied and
213 valid according to L<tidyall>. If not, then the entire commit is rejected and
214 the reason(s) are output to the client. e.g.
215  
216 % svn commit -m "fixups" CHI.pm CHI/Driver.pm
217 Sending CHI/Driver.pm
218 Sending CHI.pm
219 Transmitting file data ..svn: Commit failed (details follow):
220 svn: Commit blocked by pre-commit hook (exit code 255) with output:
221 2 files did not pass tidyall check
222 lib/CHI.pm: *** 'PerlTidy': needs tidying
223 lib/CHI/Driver.pm: *** 'PerlCritic': Code before strictures are enabled
224 at /tmp/Code-TidyAll-0e6K/Driver.pm line 2
225 [TestingAndDebugging::RequireUseStrict]
226  
227 In an emergency the hook can be bypassed by prefixing the comment with "NO
228 TIDYALL", e.g.
229  
230 % svn commit -m "NO TIDYALL - this is an emergency!" CHI.pm CHI/Driver.pm
231 Sending CHI/Driver.pm
232 Sending CHI.pm
233 Transmitting file data .
234 Committed revision 7562.
235  
236 The configuration file (C<tidyall.ini> or C<.tidyallrc>) must be checked into
237 svn. For each file, the hook will look upwards from the file's repo location
238 and use the first configuration file it finds.
239  
240 By default, if the configuration file cannot be found, or if a runtime error
241 occurs, a warning is logged (see L</LOGGING> below) but the commit is allowed
242 to proceed. This is so that unexpected problems do not prevent valid commits.
243  
244 Passes mode = "commit" by default; see L<modes|tidyall/MODES>.
245  
246 Key/value parameters:
247  
248 =over 4
249  
250 =item * conf_name
251  
252 Conf file name to search for instead of the defaults.
253  
254 =item * emergency_comment_prefix
255  
256 Commit prefix that will cause this hook to be bypassed. Defaults to "NO
257 TIDYALL". e.g.
258  
259 svn commit -m "NO TIDYALL - must get fix to production!"
260  
261 Set to a false value (e.g. blank or undefined) to disable bypassing.
262  
263 =item * extra_conf_files
264  
265 A listref of other configuration files referred to from the main configuration
266 file, e.g.
267  
268 extra_conf_files => ['perlcriticrc', 'perltidyrc']
269  
270 If you don't list them here then you'll get errors like 'cannot find
271 perlcriticrc' when the hook runs.
272  
273 =item * reject_on_error
274  
275 If the configuration file cannot be found for some/all the files, or if a
276 runtime error occurs, reject the commit.
277  
278 =item * repos
279  
280 Repository path being committed; defaults to C<< $ARGV[0] >>
281  
282 =item * tidyall_class
283  
284 Subclass to use instead of L<Code::TidyAll>
285  
286 =item * tidyall_options
287  
288 Hashref of options to pass to the L<Code::TidyAll> constructor
289  
290 =item * txn
291  
292 Commit transaction; defaults to C<< $ARGV[1] >>
293  
294 =back
295  
296 =head1 LOGGING
297  
298 This module uses L<Log::Any> to log its activity, including all files that were
299 checked, an inability to find the configuration file, and any runtime errors
300 that occur. You can create a simple date-stamped log file with
301  
302 use Log::Any::Adapter (File => "/path/to/hooks/logs/tidyall.log");
303  
304 or do something fancier with one of the other L<Log::Any
305 adapters|Log::Any::Adapter>.
306  
307 Having a log file is especially useful with pre-commit hooks since there is no
308 way for the hook to send back output on a successful commit.
309  
310 =head1 ACKNOWLEDGMENTS
311  
312 Thanks to Alexander Simakov, author of
313 L<perlcritic-checker|http://search.cpan.org/~xdr/perlcritic-checker-1.2.6/>,
314 for some of the ideas here such as emergency_comment_prefix.
315  
316 =head1 SUPPORT
317  
318 Bugs may be submitted at L<https://github.com/houseabsolute/perl-code-tidyall/issues>.
319  
320 =head1 SOURCE
321  
322 The source code repository for Code-TidyAll can be found at L<https://github.com/houseabsolute/perl-code-tidyall>.
323  
324 =head1 AUTHORS
325  
326 =over 4
327  
328 =item *
329  
330 Jonathan Swartz <swartz@pobox.com>
331  
332 =item *
333  
334 Dave Rolsky <autarch@urth.org>
335  
336 =back
337  
338 =head1 COPYRIGHT AND LICENSE
339  
340 This software is copyright (c) 2011 - 2022 by Jonathan Swartz.
341  
342 This is free software; you can redistribute it and/or modify it under
343 the same terms as the Perl 5 programming language system itself.
344  
345 The full text of the license can be found in the
346 F<LICENSE> file included with this distribution.
347  
348 =cut