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

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 122 CC 8 SCAR 33.6
CC
8
Cov
0%
check
72.0
BEGIN
2.0
BEGIN
2.0
CRAP 28.7
1 package Code::TidyAll::Git::Precommit;
2  
3 use strict;
4 use warnings;
5  
6 use Capture::Tiny qw(capture_stdout capture_stderr);
7 use Code::TidyAll::Git::Util qw(git_files_to_commit);
8 use Code::TidyAll;
9 use IPC::System::Simple qw(capturex run);
10 use Log::Any qw($log);
11 use Path::Tiny qw(path);
12 use Scope::Guard qw(guard);
13 use Specio::Library::Builtins;
14 use Specio::Library::String;
15 use Try::Tiny;
16  
17 use Moo;
18  
19 our $VERSION = '0.83';
20  
21 has conf_name => (
22 is => 'ro',
23 isa => t('NonEmptyStr'),
24 );
25  
26 has git_path => (
27 is => 'ro',
28 isa => t('NonEmptyStr'),
29 default => 'git'
30 );
31  
32 has no_stash => (
33 is => 'ro',
34 isa => t('Bool'),
35 default => 0,
36 );
37  
38 has tidyall_class => (
39 is => 'ro',
40 isa => t('ClassName'),
41 default => 'Code::TidyAll'
42 );
43  
44 has tidyall_options => (
45 is => 'ro',
46 isa => t('HashRef'),
47 default => sub { {} }
48 );
49  
50 sub check {
51 my ( $class, %params ) = @_;
52  
53 my $fail_msg;
54  
55 try {
56 my $self = $class->new(%params);
57 my $tidyall_class = $self->tidyall_class;
58  
59 # Find conf file at git root
60 my $root_dir = capturex( $self->git_path, qw( rev-parse --show-toplevel ) );
61 chomp($root_dir);
62 $root_dir = path($root_dir);
63  
64 my @conf_names
65 = $self->conf_name ? ( $self->conf_name ) : Code::TidyAll->default_conf_names;
66 my ($conf_file) = grep { $_->is_file } map { $root_dir->child($_) } @conf_names
67 or die sprintf( 'could not find conf file %s', join( ' or ', @conf_names ) );
68  
69 my $guard;
70 unless ( $self->no_stash || $root_dir->child( '.git', 'MERGE_HEAD' )->exists ) {
71  
72 # We stash things to make sure that we only attempt to run tidyall
73 # on changes in the index while ensuring that after the hook runs
74 # the working directory is in the same state it was before the
75 # commit.
76 #
77 # If there's nothing to stash there's no stash entry, in which
78 # case popping would be very bad.
79 my $pre_stash_state
80 = capturex( [ 0, 1 ], $self->git_path, qw( rev-parse -q --verify refs/stash ) );
81 run(
82 $self->git_path, qw( stash save --keep-index --include-untracked ),
83 'TidyAll pre-commit guard'
84 );
85 my $post_stash_state
86 = capturex( [ 0, 1 ], $self->git_path, qw( rev-parse -q --verify refs/stash ) );
87 unless ( $pre_stash_state eq $post_stash_state ) {
88 $guard = guard {
89 my ($version) = capturex(qw( git version )) =~ /([0-9]+\.[0-9]+\.[0-9]+)/
90 or die 'Cannot determine version number from git version output!';
91 my $minor = ( split /\./, $version )[1];
92  
93 # When pop is run quietly in 2.24.x it deletes files! See
94 # https://public-inbox.org/git/CAMcnqp22tEFva4vYHYLzY83JqDHGzDbDGoUod21Dhtnvv=h_Pg@mail.gmail.com/
95 # for the initial bug report. This was fixed in 2.25.
96 my @args = $minor == 24 ? () : ('-q');
97 run( $self->git_path, 'stash', 'pop', @args );
98 }
99 }
100 }
101  
102 # Gather file paths to be committed
103 my @files = git_files_to_commit($root_dir);
104  
105 my $tidyall = $tidyall_class->new_from_conf_file(
106 $conf_file,
107 no_cache => 1,
108 check_only => 1,
109 mode => 'commit',
110 %{ $self->tidyall_options },
111 );
112 my @results = $tidyall->process_paths(@files);
113  
114 if ( my @error_results = grep { $_->error } @results ) {
115 my $error_count = scalar(@error_results);
116 $fail_msg = sprintf(
117 "%d file%s did not pass tidyall check\n",
118 $error_count, $error_count > 1 ? 's' : q{}
119 );
120 }
121 }
122 catch {
123 my $error = $_;
124 die "Error during pre-commit hook (use --no-verify to skip hook):\n$error";
125 };
126 die "$fail_msg\n" if $fail_msg;
127 }
128  
129 1;
130  
131 # ABSTRACT: Git pre-commit hook that requires files to be tidyall'd
132  
133 __END__
134  
135 =pod
136  
137 =encoding UTF-8
138  
139 =head1 NAME
140  
141 Code::TidyAll::Git::Precommit - Git pre-commit hook that requires files to be tidyall'd
142  
143 =head1 VERSION
144  
145 version 0.83
146  
147 =head1 SYNOPSIS
148  
149 In .git/hooks/pre-commit:
150  
151 #!/usr/bin/env perl
152 use strict;
153 use warnings;
154  
155 use Code::TidyAll::Git::Precommit;
156 Code::TidyAll::Git::Precommit->check();
157  
158 =head1 DESCRIPTION
159  
160 This module implements a L<Git pre-commit
161 hook|http://git-scm.com/book/en/Customizing-Git-Git-Hooks> that checks if all
162 files are tidied and valid according to L<tidyall>, and rejects the commit if
163 not. Files/commits are never modified by this hook.
164  
165 See also L<Code::TidyAll::Git::Prereceive>, which validates pushes to a shared
166 repo.
167  
168 The tidyall configuration file (F<tidyall.ini> or F<.tidyallrc>) must be
169 checked into git in the repo root directory i.e. next to the .git directory.
170  
171 By default, the hook will stash any changes not in the index beforehand, and
172 restore them afterwards, via
173  
174 git stash save --keep-index --include-untracked
175 ....
176 git stash pop
177  
178 This means that if the configuration file has uncommitted changes that are not
179 in the index, they will not affect the tidyall run.
180  
181 =head1 METHODS
182  
183 This class provides one method:
184  
185 =head2 Code::TidyAll::Git::Precommit->check(%params)
186  
187 Checks that all files being added or modified in this commit are tidied and
188 valid according to L<tidyall>. If not, then the entire commit is rejected and
189 the reason(s) are output to the client. e.g.
190  
191 % git commit -m "fixups" CHI.pm CHI/Driver.pm
192 2 files did not pass tidyall check
193 lib/CHI.pm: *** 'PerlTidy': needs tidying
194 lib/CHI/Driver.pm: *** 'PerlCritic': Code before strictures are enabled
195 at /tmp/Code-TidyAll-0e6K/Driver.pm line 2
196 [TestingAndDebugging::RequireUseStrict]
197  
198 In an emergency the hook can be bypassed by passing --no-verify to commit:
199  
200 % git commit --no-verify ...
201  
202 or you can just move F<.git/hooks/pre-commit> out of the way temporarily.
203  
204 This class passes mode = "commit" by default to tidyall; see
205 L<modes|tidyall/MODES>.
206  
207 Key/value parameters:
208  
209 =over 4
210  
211 =item * conf_name
212  
213 A conf file name to search for instead of the defaults.
214  
215 =item * git_path
216  
217 Path to git to use in commands, e.g. '/usr/bin/git' or '/usr/local/bin/git'. By
218 default, it just uses 'git', which will search the user's C<PATH>.
219  
220 =item * no_stash
221  
222 Don't attempt to stash changes not in the index. This means the hook will
223 process files that are not going to be committed.
224  
225 =item * tidyall_class
226  
227 Subclass to use instead of L<Code::TidyAll>.
228  
229 =item * tidyall_options
230  
231 A hashref of options to pass to the L<Code::TidyAll> constructor.
232  
233 =back
234  
235 =head1 USING AND (NOT) ENFORCING THIS HOOK
236  
237 This hook must be placed manually in each copy of the repo - there is no way to
238 automatically distribute or enforce it. However, you can make things easier on
239 yourself or your developers as follows:
240  
241 =over
242  
243 =item *
244  
245 Create a directory called F<git/hooks> at the top of your repo (note no dot
246 prefix).
247  
248 mkdir -p git/hooks
249  
250 =item *
251  
252 Commit your pre-commit script in F<git/hooks/pre-commit> containing:
253  
254 #!/usr/bin/env perl
255  
256 use strict;
257 use warnings;
258  
259 use Code::TidyAll::Git::Precommit;
260 Code::TidyAll::Git::Precommit->check();
261  
262 =item *
263  
264 Add a setup script in F<git/setup.sh> containing
265  
266 #!/bin/bash
267 chmod +x git/hooks/pre-commit
268 cd .git/hooks
269 ln -s ../../git/hooks/pre-commit
270  
271 =item *
272  
273 Run C<git/setup.sh> (or tell your developers to run it) once for each new clone
274 of the repo
275  
276 =back
277  
278 See L<this Stack Overflow
279 question||http://stackoverflow.com/questions/3703159/git-remote-shared-pre-commit-hook>
280 for more information on pre-commit hooks and the impossibility of enforcing
281 their use.
282  
283 See also L<Code::TidyAll::Git::Prereceive>, which enforces tidyall on pushes to
284 a remote shared repository.
285  
286 =head1 SUPPORT
287  
288 Bugs may be submitted at L<https://github.com/houseabsolute/perl-code-tidyall/issues>.
289  
290 =head1 SOURCE
291  
292 The source code repository for Code-TidyAll can be found at L<https://github.com/houseabsolute/perl-code-tidyall>.
293  
294 =head1 AUTHORS
295  
296 =over 4
297  
298 =item *
299  
300 Jonathan Swartz <swartz@pobox.com>
301  
302 =item *
303  
304 Dave Rolsky <autarch@urth.org>
305  
306 =back
307  
308 =head1 COPYRIGHT AND LICENSE
309  
310 This software is copyright (c) 2011 - 2022 by Jonathan Swartz.
311  
312 This is free software; you can redistribute it and/or modify it under
313 the same terms as the Perl 5 programming language system itself.
314  
315 The full text of the license can be found in the
316 F<LICENSE> file included with this distribution.
317  
318 =cut