blib/lib/Git/Hooks/CheckCommit.pm untested

filter: Statementstmt 0.0% 0 / 212 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 29 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 352 CC 38 SCAR 37.4
CC
38
Cov
0%
code_errors
132.0
email_valid_errors
56.0
match_errors
42.0
CRAP 42.1
1 use warnings;
2  
3 package Git::Hooks::CheckCommit;
4 # ABSTRACT: Git::Hooks plugin to enforce commit policies
5 $Git::Hooks::CheckCommit::VERSION = '3.3.1';
6 use v5.16.0;
7 use utf8;
8 use Carp;
9 use Log::Any '$log';
10 use Git::Hooks;
11 use Git::Repository::Log;
12 use List::MoreUtils qw/any none/;
13  
14 my $PKG = __PACKAGE__;
15 my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
16  
17 #############
18 # Grok hook configuration, check it and set defaults.
19  
20 sub _setup_config {
21 my ($git) = @_;
22  
23 my $config = $git->get_config();
24  
25 $config->{lc $CFG} //= {};
26  
27 my $default = $config->{lc $CFG};
28  
29 $default->{'push-limit'} //= [0];
30  
31 return;
32 }
33  
34 ##########
35  
36 # Return common help messages to fix author or committer name/email. This
37 # routine is used to compose some error messages in the routine match_errors
38 # below. The "no critic" exemption below is a false positive.
39 sub _amend_help { ## no critic (ProhibitUnusedPrivateSubroutines)
40 my ($who) = @_;
41 if ($who eq 'author') {
42 return 'Please, amend your commit using the --author option to fix the author name/email.';
43 } elsif ($who eq 'committer') {
44 return 'Please, amend your commit after fixing your user.name and/or user.email configuration options.';
45 } else {
46 croak "Internal error: invalid who ($who)";
47 }
48 }
49  
50 sub match_errors {
51 my ($git, $commit) = @_;
52  
53 my $errors = 0;
54  
55 my $cache = $git->cache($PKG);
56  
57 unless (exists $cache->{identity}) {
58 $cache->{identity} = {};
59 foreach my $info (qw/name email/) {
60 foreach my $regexp ($git->get_config($CFG => $info)) {
61 $regexp =~ s/^(\!?)//;
62 push @{$cache->{identity}{$info}{$1}}, qr/$regexp/; ## no critic (ProhibitCaptureWithoutTest)
63 }
64 }
65 }
66  
67 if (keys %{$cache->{identity}}) {
68 foreach my $info (qw/name email/) {
69 if (my $checks = $cache->{identity}{$info}) {
70 foreach my $who (qw/author committer/) {
71 my $who_info = "${who}_${info}";
72 my $data = $commit->$who_info;
73  
74 if (none { $data =~ $_ } @{$checks->{''}}) {
75 $git->fault(<<"EOS", {commit => $commit, option => $info});
76 The commit $who $info ($data) is invalid.
77 It must match at least one positive option.
78 @{[_amend_help($who)]}
79 EOS
80 ++$errors;
81 }
82  
83 if (any { $data =~ $_ } @{$checks->{'!'}}) {
84 $git->fault(<<"EOS", {commit => $commit, option => $info});
85 The commit $who $info ($data) is invalid.
86 It matches some negative option.
87 @{[_amend_help($who)]}
88 EOS
89 ++$errors;
90 }
91 }
92 }
93 }
94 }
95  
96 return $errors;
97 }
98  
99 sub merge_errors {
100 my ($git, $commit) = @_;
101  
102 if ($commit->parent() > 1) { # it's a merge commit
103 if (my @mergers = $git->get_config($CFG => 'merger')) {
104 if (none {$git->match_user($_)} @mergers) {
105 $git->fault(<<"EOS", {commit => $commit, option => 'merger'});
106 Authorization error: you cannot push this commit.
107  
108 I'm sorry, but you (@{[$git->authenticated_user]}) are not authorized to push
109 *merge commits* because you're not included in the configuration option. You
110 must either include yourself in that option or ask somebody else to push the
111 commit for you.
112 EOS
113 return 1;
114 }
115 }
116 }
117  
118 return 0;
119 }
120  
121 sub email_valid_errors {
122 my ($git, $commit) = @_;
123  
124 my $errors = 0;
125  
126 my $cache = $git->cache($PKG);
127  
128 if ($git->get_config_boolean($CFG => 'email-valid')) {
129 # Let's also cache the Email::Valid object
130 unless (exists $cache->{email_valid}) {
131 $cache->{email_valid} = undef;
132 if (eval { require Email::Valid; }) {
133 my @checks;
134 foreach my $check (qw/mxcheck tldcheck fqdn allow_ip/) {
135 if (my $value = $git->get_config_boolean($CFG => "email-valid.$check")) {
136 push @checks, "-$check" => $value;
137 }
138 }
139 $cache->{email_valid} = Email::Valid->new(@checks);
140 } else {
141 $git->fault(<<'EOS', {option => 'email-valid.*'});
142 I could not load the Email::Valid Perl module.
143  
144 I need it to validate your commit's author and committer as requested by your
145 configuration options.
146  
147 Please, install the module or disable the options to proceed.
148 EOS
149 ++$errors;
150 }
151 }
152  
153 if (my $ev = $cache->{email_valid}) {
154 foreach my $who (qw/author committer/) {
155 my $who_email = "${who}_email";
156 my $email = $commit->$who_email;
157 unless ($ev->address($email)) {
158 my $details = $ev->details();
159 $git->fault(<<"EOS", {commit => $commit});
160 The commit $who email ($email) failed the $details check.
161 @{[_amend_help($who)]}
162 EOS
163 ++$errors;
164 }
165 }
166 }
167 }
168  
169 return $errors;
170 }
171  
172 sub _canonical_identity {
173 my ($git, $mailmap, $identity) = @_;
174  
175 my $cache = $git->cache($PKG);
176  
177 unless (exists $cache->{canonical}{$identity}) {
178 $cache->{canonical}{$identity} =
179 $git->run('-c', "mailmap.file=$mailmap", 'check-mailmap', $identity);
180 }
181  
182 return $cache->{canonical}{$identity};
183 }
184  
185 sub canonical_errors {
186 my ($git, $commit) = @_;
187  
188 my $errors = 0;
189  
190 if (my $mailmap = $git->get_config($CFG => 'canonical')) {
191 foreach my $who (qw/author committer/) {
192 my $who_name = "${who}_name";
193 my $who_email = "${who}_email";
194 my $identity = $commit->$who_name . ' <' . $commit->$who_email . '>';
195 my $canonical = eval {_canonical_identity($git, $mailmap, $identity) };
196 unless (defined $canonical) {
197 $git->fault(<<'EOS', {option => 'canonical'});
198 Git error: could not run command git-check-mailmap.
199 The configuration option requires it.
200 It's available since Git 1.8.4.
201 Please, either upgrade your Git or disable this option.
202 EOS
203 ++$errors;
204 }
205  
206 if ($identity ne $canonical) {
207 $git->fault(<<"EOS", {commit => $commit, option => 'canonical'});
208 The commit $who identity isn't canonical.
209 It's '$identity' but its canonical form is '$canonical'.
210 @{[_amend_help($who)]}
211 EOS
212 ++$errors;
213 }
214 }
215 }
216  
217 return $errors;
218 }
219  
220 sub signature_errors {
221 my ($git, $commit) = @_;
222  
223 my $errors = 0;
224  
225 my $signature = $git->get_config($CFG => 'signature');
226  
227 if (defined $signature && $signature ne 'nocheck') {
228 my $status = $git->run(qw/log -1 --format=%G?/, $commit->commit);
229  
230 if ($status eq 'B') {
231 $git->fault(<<'EOS', {commit => $commit, option => 'signature'});
232 The commit has a BAD GPG signature.
233 Please, amend your commit with the -S option to fix it.
234 EOS
235 ++$errors;
236 } elsif ($status eq 'N' && $signature ne 'optional') {
237 $git->fault(<<'EOS', {commit => $commit, option => 'signature'});
238 The commit has NO GPG signature.
239 Please, amend your commit with the -S option to add one.
240 EOS
241 ++$errors;
242 } elsif ($status eq 'U' && $signature eq 'trusted') {
243 $git->fault(<<'EOS', {commit => $commit, option => 'signature'});
244 The commit has an UNTRUSTED GPG signature.
245 Please, amend your commit with the -S option to add another one.
246 EOS
247 ++$errors;
248 }
249 }
250  
251 return $errors;
252 }
253  
254 sub code_errors {
255 my ($git, $commit, $ref) = @_;
256  
257 my $errors = 0;
258  
259 my $cache = $git->cache($PKG);
260  
261 unless (exists $cache->{codes}) {
262 $cache->{codes} = [];
263 CODE:
264 foreach my $check ($git->get_config($CFG => 'check-code')) {
265 my $code;
266 if ($check =~ s/^file://) {
267 $code = do $check;
268 unless ($code) {
269 if (length $@) {
270 $git->fault("I couldn't parse the file ($check):",
271 {commit => $commit, option => 'check-code', details => $@});
272 } elsif (! defined $code) {
273 $git->fault("I couldn't read the file ($check):",
274 {commit => $commit, option => 'check-code', details => $@});
275 } else {
276 $git->fault("The file ($check) returned FALSE",
277 {commit => $commit, option => 'check-code', details => $@});
278 }
279 ++$errors;
280 next CODE;
281 }
282 } else {
283 $code = eval $check; ## no critic (BuiltinFunctions::ProhibitStringyEval)
284 if (length $@) {
285 $git->fault("I couldn't parse the option value ($check):",
286 {commit => $commit, option => 'check-code', details => $@});
287 ++$errors;
288 next CODE;
289 }
290 }
291 if (defined $code && ref $code && ref $code eq 'CODE') {
292 push @{$cache->{codes}}, $code;
293 } else {
294 $git->fault("The option value must end with a code ref.",
295 {commit => $commit, option => 'check-code'});
296 ++$errors;
297 }
298 }
299 }
300  
301 foreach my $code (@{$cache->{codes}}) {
302 my $ok = eval { $code->($git, $commit, $ref) };
303 if (defined $ok) {
304 unless ($ok) {
305 $git->fault("Error detected while evaluating the option.",
306 {commit => $commit, option => 'check-code'});
307 ++$errors;
308 }
309 } elsif (length $@) {
310 $git->fault('Error detected while evaluating the option',
311 {commit => $commit, option => 'check-code', details => $@});
312 ++$errors;
313 }
314 } continue {
315 $git->check_timeout();
316 }
317  
318 return $errors;
319 }
320  
321 sub commit_errors {
322 my ($git, $commit, $ref) = @_;
323  
324 return
325 match_errors($git, $commit) +
326 merge_errors($git, $commit) +
327 email_valid_errors($git, $commit) +
328 canonical_errors($git, $commit) +
329 signature_errors($git, $commit) +
330 code_errors($git, $commit, $ref);
331 }
332  
333 sub check_ref {
334 my ($git, $ref) = @_;
335  
336 my $errors = 0;
337  
338 my @commits = $git->get_affected_ref_commits($ref);
339  
340 if (my $limit = $git->get_config_integer($CFG => 'push-limit')) {
341 if (@commits > $limit) {
342 $git->fault(<<"EOS", {ref => $ref, option => 'push-limit'});
343 Are you sure you want to push @{[scalar @commits]} commits to this reference at
344 once?
345  
346 The configuration option currently allows one to push at most $limit commits to
347 a reference at once.
348  
349 If you're sure about this you can break the whole commit sequence in smaller
350 subsequences and push them one at a time.
351 EOS
352 ++$errors;
353 }
354 }
355  
356 foreach my $commit (@commits) {
357 $errors += commit_errors($git, $commit, $ref);
358 }
359  
360 return $errors;
361 }
362  
363 sub check_commit {
364 my ($git) = @_;
365  
366 # Grok author and committer information from git's environment variables, if
367 # they're defined. Sometimes they aren't...
368  
369 my $author_name = $ENV{GIT_AUTHOR_NAME} || 'nobody';
370 my $author_email = $ENV{GIT_AUTHOR_EMAIL} || 'nobody@example.net';
371 my $committer_name = $ENV{GIT_COMMITTER_NAME} || $author_name;
372 my $committer_email = $ENV{GIT_COMMITTER_EMAIL} || $author_email;
373  
374 # Construct a fake commit object to pass to the error checking routines.
375 my $commit = Git::Repository::Log->new(
376 commit => '<new>',
377 author => "$author_name <$author_email> 1234567890 -0300",
378 committer => "$committer_name <$committer_email> 1234567890 -0300",
379 message => "Fake\n",
380 );
381  
382 return
383 match_errors($git, $commit) +
384 email_valid_errors($git, $commit) +
385 canonical_errors($git, $commit) +
386 code_errors($git, $commit);
387 }
388  
389 sub check_post_commit {
390 my ($git) = @_;
391  
392 $log->debug(__PACKAGE__ . "::check_post_commit");
393  
394 _setup_config($git);
395  
396 my $current_branch = $git->get_current_branch();
397  
398 return 1 unless $git->is_reference_enabled($current_branch);
399  
400 return signature_errors($git, $git->get_commit('HEAD'));
401 }
402  
403 sub check_patchset {
404 my ($git, $branch, $commit) = @_;
405  
406 return commit_errors($git, $commit, $git->get_commit($branch));
407 }
408  
409 # Install hooks
410 my $options = {config => \&_setup_config};
411  
412 GITHOOKS_CHECK_AFFECTED_REFS(\&check_ref, $options);
413 GITHOOKS_CHECK_PRE_COMMIT(\&check_commit, $options);
414 GITHOOKS_CHECK_PATCHSET(\&check_patchset, $options);
415  
416 POST_APPLYPATCH(\&check_post_commit);
417 POST_COMMIT(\&check_post_commit);
418  
419 1;
420  
421 __END__