blib/lib/Git/MoreHooks/CheckTrailers.pm

filter: Statementstmt 95.4% 270 / 283 Branchbran 85.1% 92 / 108 Conditioncond 78.9% 30 / 38 MC/DCmcdc 59.0% 13 / 22 Subroutinesub 97.0% 33 / 34 Podpod 57.1% 4 / 7 totaltotal 89.8% 442 / 492 CC 87 SCAR 21.5
CC
87
Cov
91%
_setup_config
23.6
_trailers
8.6
message_errors
8.1
CRAP 8.6
« blib/lib/Git/MoreHooks/ScanCtags.pm ↑ Summary blib/lib/Git/MoreHooks.pm »
1 package Git::MoreHooks::CheckTrailers;
2  
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: GitHooks plugin to enforce a commit message trailer policy
7  
8 24 our $VERSION = '0.020';
9  
10 24 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Encode qw( encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 ▶use Git::Hooks 4.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 24 ▶use List::Util qw( any first );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 24 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 24 my $PKG = __PACKAGE__;
19 24 my $CFG = __PACKAGE__ =~ s/.*::/githooks./rmsx;
20  
21 # The kinds of commit which may be exempted from the 'require' check.
22 24 my @KINDS = qw( merge revert fixup );
23  
24 # Reported at most once per process, however many commits a push carries.
25 24 my $announced_no_interpret_trailers = 0;
26  
27 #############
28 # Grok hook configuration, check it and set defaults.
29  
30 34 ▶sub _setup_config {
Subroutine _setup_config: called CC=20 SCAR=31.6
31 34 my ($git) = @_;
32  
33 # Drop anything parsed from a previous call. A hook process calls this once,
34 # but a test (or any caller exercising several configurations through one
35 # repository object) calls it again after changing the configuration, and a
36 # cache that outlived the change would answer for the old one.
37 34 delete $git->cache('checktrailers')->{'specs'};
38  
39 34 my $config = $git->get_config();
40 34 ▶ $config->{ lc $CFG } //= {};
Condition100%2/2
$$config{lc $CFG} //= {}
l!l
322
MC/DC100%1/1
$$config{lc $CFG} //= {}
$$config{lc $CFG}
Truth table100%2/2
A
A= $$config{lc $CFG}
Aresult
11
00
41 34 my $default = $config->{ lc $CFG };
42  
43 # NO trailer names are defaulted. An unconfigured plugin does nothing: the
44 # spelling of a disclosure trailer is not settled across projects, so every
45 # repository names its own.
46 34 ▶ $default->{'deny'} //= [];
Condition100%2/2
$default->{"deny"} //= []
l!l
1717
MC/DC100%1/1
$default->{"deny"} //= []
$default->{"deny"}
Truth table100%2/2
A
A= $default->{"deny"}
Aresult
11
00
47 34 ▶ $default->{'require'} //= [];
Condition100%2/2
$default->{"require"} //= []
l!l
1024
MC/DC100%1/1
$default->{"require"} //= []
$default->{"require"}
Truth table100%2/2
A
A= $default->{"require"}
Aresult
11
00
48 34 ▶ $default->{'order'} //= [];
Condition100%2/2
$default->{"order"} //= []
l!l
430
MC/DC100%1/1
$default->{"order"} //= []
$default->{"order"}
Truth table100%2/2
A
A= $default->{"order"}
Aresult
11
00
49 34 ▶ $default->{'deny-in-body'} //= ['false'];
Condition50%1/2
$default->{"deny-in-body"} //= ["false"]
l!l
034
MC/DC0%0/1
$default->{"deny-in-body"} //= ["false"]
$default->{"deny-in-body"}
Truth table50%1/2
A
A= $default->{"deny-in-body"}
Aresult
11
00
50 34 ▶ $default->{'require-skip'} //= [@KINDS];
Condition100%2/2
$default->{"require-skip"} //= [@KINDS]
l!l
133
MC/DC100%1/1
$default->{"require-skip"} //= [@KINDS]
$default->{"require-skip"}
Truth table100%2/2
A
A= $default->{"require-skip"}
Aresult
11
00
51  
52 34 my $errors = 0;
53  
54 # Report a broken specification once, here, rather than per commit.
55 34 foreach my $option (qw( deny require )) {
56 68 foreach my $spec ( $git->get_config( $CFG => $option ) ) {
57 28 my ( undef, $why ) = _parse_spec($spec);
58 28 ▶ next if !defined $why;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $why 28 0 28
59 0 $git->fault( "Cannot understand a $option specification: $why",
60 { prefix => $PKG, option => $option, details => $spec } );
61 0 $errors++;
62 }
63 }
64  
65 34 foreach my $kind ( $git->get_config( $CFG => 'require-skip' ) ) {
66 100 ▶ next if $kind eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "none" 1 99 100
67 99 ▶ next if any { $_ eq $kind } @KINDS;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $kind; } , @KINDS) 99 0 99
68 0 $git->fault( 'Unknown require-skip value. It must be one of: ' . ( join q{, }, @KINDS, 'none' ),
69 { prefix => $PKG, option => 'require-skip', details => $kind } );
70 0 $errors++;
71 }
72  
73 # A token in both lists can never be satisfied.
74 34 my %denied;
75 34 foreach my $spec ( $git->get_config( $CFG => 'deny' ) ) {
76 18 my ($parsed) = _parse_spec($spec);
77 18 ▶ next if !defined $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parsed 0 18 18
78 18 ▶ next if !defined $parsed->{'token'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parsed->{"token"} 0 18 18
79 18 $denied{ lc $parsed->{'token'} } = 1;
80 }
81  
82 34 foreach my $spec ( $git->get_config( $CFG => 'require' ) ) {
83 10 my ($parsed) = _parse_spec($spec);
84 10 ▶ next if !defined $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parsed 0 10 10
85 10 my $token = $parsed->{'token'};
86 10 ▶ next if !defined $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 10 10
87 10 ▶ next if !$denied{ lc $token };
Branch: 1 branch
BranchTrueFalseTotal
unless $denied{lc $token} 10 0 10
88 0 $git->fault( "The trailer '$token' is both denied and required, which no commit message can satisfy.",
89 { prefix => $PKG, option => 'require', details => $spec } );
90 0 $errors++;
91 }
92  
93 34 $log->debugf( "$PKG: configuration has %d problem(s)", $errors );
94 34 return;
95 }
96  
97 # One field of a specification: either qr<delim>...<delim> or a bare trailer
98 # token. Returns ( $matcher, $literal_token_or_undef, $error_or_undef ).
99 #
100 # Both are compiled case-insensitively and with nothing else. A field carrying
101 # trailing flags (qr/x/msx) does not end in its own delimiter, so it falls
102 # through to the token branch and is reported as a configuration error rather
103 # than silently losing the flags. Somebody wanting other flags writes
104 # qr/(?x: ... )/.
105 24 my %CLOSER = ( '{' => '}', '(' => ')', '[' => ']', '<' => '>' );
106  
107 109 ▶sub _parse_field {
Subroutine _parse_field: called CC=6 SCAR=17.9
108 109 my ($field) = @_;
109  
110 # qr<delim>...<delim>, with the paired delimiters spelled the usual way.
111 # A field carrying trailing flags does not end in its own closing
112 # delimiter, so it falls through to the token branch and is reported rather
113 # than silently losing them.
114 109 ▶ if ( my ( $open, $pattern, $closer ) = $field =~ m/\Aqr(.)(.*)(.)\z/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $field =~ /\Aqr(.)(.*)(.)\z/msx) 17 92 109
115 17 ▶ if ( $closer eq ( $CLOSER{$open} // $open ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($closer eq ($CLOSER{$open} // $open)) 16 1 17
Condition66%2/3
$CLOSER{$open} // $open
l!l&&r!l&&!r
1160
MC/DC0%0/2
$CLOSER{$open} // $open
$CLOSER{$open} $open
Truth table66%2/3
A // B
A= $CLOSER{$open}B= $open
ABresult
1X1
011
000
116 ## no critic (RegularExpressions::RequireDotMatchAnything) -- a user pattern; /s would change what their regex means
117 ## no critic (RegularExpressions::RequireExtendedFormatting) -- likewise for /x, which would make their whitespace vanish
118 ## no critic (RegularExpressions::RequireLineBoundaryMatching) -- and likewise for /m
119 16 my $matcher = eval { qr/$pattern/i };
120 16 ▶ return ( undef, undef, "the regular expression qr/$pattern/ does not compile", 1 )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matcher 1 15 16
121 if !defined $matcher;
122 15 return ( $matcher, undef, undef, 1 );
123 }
124 }
125  
126 # A bare token, held to git's own trailer-token character set.
127 93 ▶ if ( my ($token) = $field =~ m/\A([[:alnum:]][[:alnum:]-]*)\z/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($token) = $field =~ /\A([[:alnum:]][[:alnum:]-]*)\z/msx) 90 3 93
128 90 return ( qr/\A\Q$token\E\z/imsx, $token, undef, 0 );
129 }
130  
131 3 return ( undef, undef, "'$field' is neither a trailer token nor a qr// pattern", 0 );
132 }
133  
134 # TOKENMATCH [whitespace VALUEMATCH].
135 #
136 # Split on whitespace first: a trailer token can never contain any, so a token
137 # pattern that did could never match a real trailer, and splitting first makes
138 # each field's qr// unambiguous to parse.
139 94 ▶sub _parse_spec {
Subroutine _parse_spec: called CC=8 SCAR=20.8
140 94 my ($spec) = @_;
141  
142 94 ▶ return ( undef, 'the specification is empty' ) if !defined $spec || $spec !~ m/\S/msx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec and $spec =~ /\S/msx 2 92 94
Condition66%2/3
defined $spec and $spec =~ /\S/msx
!ll&&!rl&&r
0292
MC/DC50%1/2
defined $spec and $spec =~ /\S/msx
defined $spec $spec =~ /\S/msx
Truth table66%2/3
A and B
A= defined $specB= $spec =~ /\S/msx
ABresult
0X0
100
111
143  
144 92 my $trimmed = $spec;
145 92 $trimmed =~ s/\A\s+//msx;
146 92 $trimmed =~ s/\s+\z//msx;
147  
148 92 my ( $token_field, $value_field ) = split /\s+/msx, $trimmed, 2;
149  
150 92 my ( $token_re, $token, $token_error ) = _parse_field($token_field);
151 92 ▶ return ( undef, $token_error ) if defined $token_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token_error 2 90 92
152  
153 90 my $value_re;
154 90 ▶ if ( defined $value_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_field) 17 73 90
155 17 my ( $matcher, undef, $value_error, $was_pattern ) = _parse_field($value_field);
156  
157 # A broken qr// is an error: somebody wrote a pattern and meant it, so
158 # quietly matching their regex source as a literal string would be the
159 # worst of both. Anything that is not a pattern at all is a literal
160 # substring, which is what most value matches want to be.
161 17 ▶ return ( undef, $value_error ) if defined $value_error && $was_pattern;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value_error and $was_pattern 1 16 17
Condition100%3/3
defined $value_error and $was_pattern
!ll&&!rl&&r
1511
MC/DC100%2/2
defined $value_error and $was_pattern
defined $value_error $was_pattern
Truth table100%3/3
A and B
A= defined $value_errorB= $was_pattern
ABresult
0X0
100
111
162 16 ▶ if ( defined $value_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_error) 1 15 16
163 1 $matcher = qr/\Q$value_field\E/imsx;
164 }
165 16 $value_re = $matcher;
166 }
167  
168 89 return ( { raw => $spec, token => $token, token_re => $token_re, value_re => $value_re }, undef );
169 }
170  
171 # The parsed specifications, cached per repository object so that a push of many
172 # commits parses the configuration once.
173 290 ▶sub _specs {
Subroutine _specs: called CC=7 SCAR=19.5
174 290 my ($git) = @_;
175  
176 290 my $cache = $git->cache('checktrailers');
177 290 ▶ return $cache->{'specs'} if exists $cache->{'specs'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cache->{"specs"} 257 33 290
178  
179 33 my %specs = ( deny => [], require => [], order => [] );
180 33 foreach my $option (qw( deny require )) {
181 66 foreach my $spec ( $git->get_config( $CFG => $option ) ) {
182 27 my ($parsed) = _parse_spec($spec);
183  
184 # A broken specification is already reported by _setup_config, which
185 # fails the hook; skip it here rather than complain twice.
186 27 ▶ next if !defined $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parsed 0 27 27
187 27 push @{ $specs{$option} }, $parsed;
188 }
189 }
190  
191 33 foreach my $token ( $git->get_config( $CFG => 'order' ) ) {
192 8 push @{ $specs{'order'} }, $token;
193 }
194  
195 33 ▶ $specs{'deny_in_body'} = $git->get_config_boolean( $CFG => 'deny-in-body' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$git->get_config_boolean($CFG, 'deny-in-body') ? : 0 33 33
196 33 $specs{'skip'} = { map { $_ => 1 } $git->get_config( $CFG => 'require-skip' ) };
197  
198 33 $cache->{'specs'} = \%specs;
199 33 return $cache->{'specs'};
200 }
201  
202 16 ▶sub _commit_encoding {
Subroutine _commit_encoding: called CC=2 SCAR=7.2
203 16 my ($git) = @_;
204 16 ▶ return $git->get_config( i18n => 'commitEncoding' ) || 'utf-8';
Condition50%1/2
$git->get_config('i18n', 'commitEncoding') || 'utf-8'
l!l
016
MC/DC0%0/1
$git->get_config('i18n', 'commitEncoding') || 'utf-8'
$git->get_config('i18n', 'commitEncoding')
Truth table50%1/2
A
A= $git->get_config('i18n', 'commitEncoding')
Aresult
11
00
205 }
206  
207 119 ▶sub _paragraphs {
Subroutine _paragraphs: called CC=1 SCAR=0.0
208 119 my ($msg) = @_;
209 119 return split /\n{2,}/msx, $msg;
210 }
211  
212 # THE TRAILERS, AS GIT ITSELF READS THEM.
213 #
214 # Not Git::Message, which CheckLog uses: its footer parser accepts the final
215 # block only when EVERY line of it is a trailer, so a body line above the
216 # trailers, a space before a colon, or a folded value all make it report no
217 # trailers at all. Git parses those messages fine and the forges mint a
218 # contributor from them, so believing Git::Message would mean missing exactly
219 # the commits this plugin exists to catch -- the GitHub squash-merge shape among
220 # them, since "Squash and merge" collects Co-authored-by from what it squashes.
221 #
222 # `--parse` is `--only-trailers --only-input --unfold`. `--only-input` matters:
223 # without it a repository's own trailer.* configuration could add trailers that
224 # are not in the message, and we would reject a commit for something nobody
225 # wrote.
226 68 ▶sub _trailers {
Subroutine _trailers: called CC=7 SCAR=21.5
227 68 my ( $git, $msg ) = @_;
228  
229 68 my @paragraphs = _paragraphs($msg);
230 68 ▶ return [] if scalar @paragraphs < 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @paragraphs < 2 21 47 68
231  
232 # No subprocess for the common case. `^` with /m, not `\A`: the trailer need
233 # not be the first line of its paragraph.
234 47 ▶ return [] if $paragraphs[-1] !~ m/^[[:alnum:]][[:alnum:]-]*[ \t]*:/msx;
Branch: 1 branch
BranchTrueFalseTotal
unless $paragraphs[-1] =~ /^[[:alnum:]][[:alnum:]-]*[ \t]*:/msx 6 41 47
235  
236 41 my $out = eval { $git->run( qw( interpret-trailers --parse ), { input => $msg } ) };
237 41 ▶ if ( !defined $out ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $out) 0 41 41
238 0 my $error = $EVAL_ERROR;
239 0 ▶ if ( !$announced_no_interpret_trailers ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($announced_no_interpret_trailers) 0 0 0
240 0 $announced_no_interpret_trailers = 1;
241 0 $git->fault(
242 'Cannot read the commit message trailers. `git interpret-trailers --parse` failed, '
243 . 'so this plugin cannot tell whether the message complies.',
244 { prefix => $PKG, details => $error }
245 );
246 }
247 0 return [];
248 }
249  
250 41 my @trailers;
251 41 foreach my $line ( split /\n/msx, $out ) {
252 49 ▶ if ( my ( $token, $trailer_value ) = $line =~ m/\A([^:]+):[ ]?(.*)\z/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $line =~ /\A([^:]+):[ ]?(.*)\z/msx) 49 0 49
253 49 push @trailers, { token => $token, value => $trailer_value };
254 }
255 }
256  
257 41 $log->debugf( "$PKG: git reads %d trailer(s)", scalar @trailers );
258 41 return \@trailers;
259 }
260  
261 # Lines that LOOK like a trailer and that git did not return.
262 #
263 # git does not read these as trailers, so they create no false attribution yet.
264 # They will the moment the message is reflowed, squash-merged, or gains a
265 # Signed-off-by -- and the same scan is what lets the 'require' check say
266 # "you wrote it, but not where git can see it", which is the message that saves
267 # somebody a puzzled quarter of an hour.
268 #
269 # The final paragraph only, by default: the title is never a trailer block, and
270 # widening to the whole body starts matching prose. The commit that adds a
271 # deny rule to a repository will discuss the forbidden token in prose.
272 67 ▶sub _suspect_lines {
Subroutine _suspect_lines: called CC=7 SCAR=19.5
273 67 my ( $git, $msg, $trailers ) = @_;
274  
275 # Everything after the TITLE, which is the first line -- not after the first
276 # paragraph. A trailer written with no blank line above it shares the title's
277 # paragraph, and that shape is one of the ones worth catching: git does not
278 # read it as a trailer, so it is invisible to the authoritative source.
279 67 my $body = $msg;
280 67 $body =~ s/\A[^\n]*\n?//msx;
281 67 $body =~ s/\A\n+//msx;
282 67 ▶ return [] if $body !~ m/\S/msx;
Branch: 1 branch
BranchTrueFalseTotal
unless $body =~ /\S/msx 16 51 67
283  
284 51 my @paragraphs = _paragraphs($body);
285 51 ▶ my @scope = _specs($git)->{'deny_in_body'} ? @paragraphs : ( $paragraphs[-1] );
Branch: 1 branch
BranchTrueFalseTotal
_specs($git)->{'deny_in_body'} ? : 0 51 51
286  
287 51 my @suspects;
288 51 foreach my $paragraph (@scope) {
289 51 foreach my $line ( split /\n/msx, $paragraph ) {
290 61 ▶ if ( my ( $token, $trailer_value ) = $line =~ m/\A([[:alnum:]][[:alnum:]-]*)[ \t]*:[ \t]*(.*)\z/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $line =~ /\A([[:alnum:]][[:alnum:]-]*)[ \t]*:[ \t]*(.*)\z/msx) 53 8 61
291 53 ▶ next if _accounted_for( $trailers, $token, $trailer_value );
Branch: 1 branch
BranchTrueFalseTotal
if _accounted_for($trailers, $token, $trailer_value) 48 5 53
292 5 push @suspects, { token => $token, value => $trailer_value, line => $line };
293 }
294 }
295 }
296 51 return \@suspects;
297 }
298  
299 # Is this line one git already told us about?
300 #
301 # Not simply equality: `--parse` unfolds, so a trailer written over two lines
302 # comes back with the continuation appended, and the raw first line's value is
303 # then a prefix of it. Without allowing for that, every folded trailer would be
304 # reported as a suspect line as well as a real one.
305 53 ▶sub _accounted_for {
Subroutine _accounted_for: called CC=5 SCAR=16.1
306 53 my ( $trailers, $token, $trailer_value ) = @_;
307  
308 53 foreach my $trailer ( @{$trailers} ) {
309 57 ▶ next if lc $trailer->{'token'} ne lc $token;
Branch: 1 branch
BranchTrueFalseTotal
if lc $trailer->{"token"} ne lc $token 9 48 57
310 48 ▶ return 1 if $trailer->{'value'} eq $trailer_value;
Branch: 1 branch
BranchTrueFalseTotal
if $trailer->{"value"} eq $trailer_value 47 1 48
311 1 ▶ return 1 if index( $trailer->{'value'}, $trailer_value . q{ } ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($trailer->{"value"}, $trailer_value . ' ') == 0 1 0 1
312 }
313 5 return 0;
314 }
315  
316 62 ▶sub _commit_kinds {
Subroutine _commit_kinds: called CC=6 SCAR=17.9
317 62 my ( $git, $commit, $msg ) = @_;
318  
319 62 my @kinds;
320 62 ▶ if ( defined $commit ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $commit) { } 25 37 62
elsif (-f path($git->git_dir)->child('MERGE_HEAD')) { } 1 36 37
321 25 my @parents = $commit->parent;
322 25 ▶ if ( scalar @parents > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @parents > 1) 2 23 25
323 2 push @kinds, 'merge';
324 }
325 }
326 elsif ( -f path( $git->git_dir() )->child('MERGE_HEAD') ) {
327 1 push @kinds, 'merge';
328 }
329  
330 62 ▶ if ( $msg =~ m/^This[ ]reverts[ ]commit[ ][[:xdigit:]]{7,40}/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg =~ /^This[ ]reverts[ ]commit[ ][[:xdigit:]]{7,40}/msx) 2 60 62
331 2 push @kinds, 'revert';
332 }
333 62 ▶ if ( $msg =~ m/\A(?:fixup|squash|amend)!\s/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg =~ /\A(?:fixup|squash|amend)!\s/msx) 3 59 62
334 3 push @kinds, 'fixup';
335 }
336  
337 62 return \@kinds;
338 }
339  
340 35 ▶sub _matches {
Subroutine _matches: called CC=4 SCAR=13.9
341 35 my ( $spec, $trailer ) = @_;
342  
343 35 ▶ return 0 if $trailer->{'token'} !~ $spec->{'token_re'};
Branch: 1 branch
BranchTrueFalseTotal
unless $trailer->{"token"} =~ /$spec->{"token_re"}/imsx 14 21 35
344 21 ▶ return 1 if !defined $spec->{'value_re'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"value_re"} 16 5 21
345 5 ▶ return $trailer->{'value'} =~ $spec->{'value_re'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$trailer->{'value'} =~ /$spec->{'value_re'}/i ? : 2 3 5
346 }
347  
348 59 ▶sub deny_errors {
Subroutine deny_errors: called CC=4 SCAR=13.9
Pod undocumented
349 59 my ($ctx) = @_;
350  
351 59 my $git = $ctx->{'git'};
352 59 my $specs = _specs($git);
353 59 my $errors = 0;
354  
355 59 foreach my $spec ( @{ $specs->{'deny'} } ) {
356 42 my @hits = grep { _matches( $spec, $_ ) } @{ $ctx->{'trailers'} };
357 42 ▶ if (@hits) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hits) 13 29 42
358 13 my $details = join "\n", map { $_->{'token'} . q{: } . $_->{'value'} } @hits;
359 $git->fault(
360 <<'EOS',
361 This commit's message carries a trailer this repository forbids.
362 Please, amend your commit to remove it.
363 EOS
364 13 { prefix => $PKG, commit => $ctx->{'id'}, option => 'deny', details => $details }
365 );
366 13 $errors++;
367 }
368  
369 42 my @suspects = grep { _matches( $spec, $_ ) } @{ $ctx->{'suspects'} };
370 42 ▶ next if !scalar @suspects;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @suspects 40 2 42
371 2 my $details = join "\n", map { $_->{'line'} } @suspects;
372 $git->fault(
373 <<'EOS',
374 This commit's message has a line that looks like a trailer this repository
375 forbids. git does not read it as a trailer yet, because it is not in a
376 trailer block, so it creates no false attribution yet. It will, as soon as
377 the message is reflowed, squash-merged, or gains a Signed-off-by.
378 Please, amend your commit to remove the line.
379 EOS
380 2 { prefix => $PKG, commit => $ctx->{'id'}, option => 'deny', details => $details }
381 );
382 2 $errors++;
383 }
384  
385 59 return $errors;
386 }
387  
388 59 ▶sub require_errors {
Subroutine require_errors: called CC=8 SCAR=20.8
Pod undocumented
389 59 my ($ctx) = @_;
390  
391 59 my $git = $ctx->{'git'};
392 59 my $specs = _specs($git);
393 59 ▶ return 0 if !scalar @{ $specs->{'require'} };
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$specs->{"require"};} 47 12 59
394  
395 # A merge, a revert and a fixup are not places to disclose anything: the
396 # first two have no footer of their own and the third is transient.
397 12 ▶ my $skipped = first { $specs->{'skip'}->{$_} } @{ $ctx->{'kinds'} };
Subroutine __ANON__: called CC=1 SCAR=0.0
398 12 ▶ if ( defined $skipped ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $skipped) 3 9 12
399 3 $log->debugf( "$PKG: not requiring a trailer of a %s commit", $skipped );
400 3 return 0;
401 }
402  
403 9 my $errors = 0;
404 9 foreach my $spec ( @{ $specs->{'require'} } ) {
405 9 ▶ next if any { _matches( $spec, $_ ) } @{ $ctx->{'trailers'} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { _matches($spec, $_); } , @{$$ctx{"trailers"};}) 3 6 9
406  
407 6 ▶ my $named = $spec->{'token'} // $spec->{'raw'};
Condition33%1/3
$spec->{"token"} // $spec->{"raw"}
l!l&&r!l&&!r
600
MC/DC0%0/2
$spec->{"token"} // $spec->{"raw"}
$spec->{"token"} $spec->{"raw"}
Truth table33%1/3
A // B
A= $spec->{"token"}B= $spec->{"raw"}
ABresult
1X1
011
000
408  
409 # Written, but not where git can see it. Much the most likely mistake,
410 # and the one worth its own message.
411 6 ▶ my $misplaced = first { $_->{'token'} =~ $spec->{'token_re'} } @{ $ctx->{'suspects'} };
Subroutine __ANON__: called CC=1 SCAR=0.0
412 6 ▶ if ( defined $misplaced ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $misplaced) 2 4 6
413 $git->fault(
414 <<"EOS",
415 This commit's message mentions '$named', but git does not read it as a
416 trailer, because it is not in a paragraph of its own at the end of the
417 message. Please, amend your commit to put a blank line before it.
418 EOS
419 2 { prefix => $PKG, commit => $ctx->{'id'}, option => 'require', details => $misplaced->{'line'} }
420 );
421 2 $errors++;
422 2 next;
423 }
424  
425 # Present, but its value is not one this repository accepts.
426 4 ▶ my $wrong_value = first { $_->{'token'} =~ $spec->{'token_re'} } @{ $ctx->{'trailers'} };
Subroutine __ANON__: called CC=1 SCAR=0.0
427 4 ▶ if ( defined $wrong_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wrong_value) 1 3 4
428 $git->fault(
429 <<"EOS",
430 This commit's trailer '$named' has a value this repository does not accept.
431 EOS
432 {
433 prefix => $PKG,
434 commit => $ctx->{'id'},
435 option => 'require',
436 1 details => $wrong_value->{'token'} . q{: } . $wrong_value->{'value'},
437 }
438 );
439 1 $errors++;
440 1 next;
441 }
442  
443 $git->fault(
444 <<"EOS",
445 This commit's message does not have a trailer this repository requires.
446 Please, amend your commit to add '$named' as the last paragraph.
447 EOS
448 3 { prefix => $PKG, commit => $ctx->{'id'}, option => 'require' }
449 );
450 3 $errors++;
451 }
452  
453 9 return $errors;
454 }
455  
456 # The required relative order of those trailers a repository cares about.
457 #
458 # Git::Message cannot express this at all: it rebuilds the footer in
459 # lexicographic order with signed-off-by forced last, so the order the author
460 # actually wrote is gone before any check could read it. `interpret-trailers`
461 # preserves it.
462 #
463 # Only trailers named by an 'order' entry are constrained; the rest are ignored,
464 # unless '*' is configured, which stands for "any other trailer" and is how
465 # "the sign-off comes last" is said.
466 59 ▶sub order_errors {
Subroutine order_errors: called CC=7 SCAR=19.5
Pod undocumented
467 59 my ($ctx) = @_;
468  
469 59 my $git = $ctx->{'git'};
470 59 my $specs = _specs($git);
471 59 my @order = @{ $specs->{'order'} };
472 59 ▶ return 0 if !scalar @order;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @order 53 6 59
473  
474 6 ▶ my $wildcard = first { $order[$_] eq q{*} } 0 .. $#order;
Subroutine __ANON__: called CC=1 SCAR=0.0
475  
476 6 my @ranked;
477 6 foreach my $trailer ( @{ $ctx->{'trailers'} } ) {
478 12 ▶ my $rank = first { lc $order[$_] eq lc $trailer->{'token'} } 0 .. $#order;
Subroutine __ANON__: called CC=1 SCAR=0.0
479 12 ▶ $rank //= $wildcard;
Condition100%3/3
$rank //= $wildcard
l!l&&r!l&&!r
1011
MC/DC100%2/2
$rank //= $wildcard
$rank $wildcard
Truth table100%3/3
A //= B
A= $rankB= $wildcard
ABresult
1X1
011
000
480 12 ▶ next if !defined $rank;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rank 1 11 12
481 11 push @ranked, { rank => $rank, token => $trailer->{'token'} };
482 }
483  
484 # Report the first inversion only: one fault per commit is enough to act on.
485 6 foreach my $i ( 1 .. $#ranked ) {
486 5 ▶ next if $ranked[$i]->{'rank'} >= $ranked[ $i - 1 ]->{'rank'};
Branch: 1 branch
BranchTrueFalseTotal
if $ranked[$i]{"rank"} >= $ranked[$i - 1]{"rank"} 2 3 5
487 3 my $earlier = $ranked[$i]->{'token'};
488 3 my $later = $ranked[ $i - 1 ]->{'token'};
489 $git->fault(
490 <<"EOS",
491 This commit's message trailers are not in the order this repository requires.
492 '$earlier' must come before '$later'.
493 Please, amend your commit to reorder them.
494 EOS
495 {
496 prefix => $PKG,
497 commit => $ctx->{'id'},
498 option => 'order',
499 details => q{Configured order: }
500 . ( join q{, }, @order )
501 . qq{\nFound order: }
502 3 . ( join q{, }, map { $_->{'token'} } @ranked ),
503 }
504 );
505 3 return 1;
506 }
507  
508 3 return 0;
509 }
510  
511 62 ▶sub message_errors {
Subroutine message_errors: called CC=8 SCAR=20.9
Pod documented
512 62 my ( $git, $commit, $msg ) = @_;
513  
514 62 my $specs = _specs($git);
515  
516 # Unconfigured means inert, and costs nothing: no subprocess, no parsing.
517 return 0
518 62 if !scalar @{ $specs->{'deny'} }
519 21 && !scalar @{ $specs->{'require'} }
520 62 ▶ && !scalar @{ $specs->{'order'} };
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$specs->{"deny"};} or scalar @{$specs->{"require"};} or scalar @{$specs->{"order"};} 3 59 62
Condition100%6/6
scalar @{$specs->{"deny"};} or scalar @{$specs->{"require"};} or scalar @{$specs->{"order"};}
l!l&&r!l&&!r
5363
scalar @{$specs->{"deny"};} or scalar @{$specs->{"require"};}
l!l&&r!l&&!r
41129
MC/DC100%3/3
scalar @{$specs->{"deny"};} or scalar @{$specs->{"require"};} or scalar @{$specs->{"order"};}
scalar @{$specs->{"deny"};} scalar @{$specs->{"require"};} scalar @{$specs->{"order"};}
Truth table100%4/4
A or B or C
A= scalar @{$specs->{"deny"};}B= scalar @{$specs->{"require"};}C= scalar @{$specs->{"order"};}
ABCresult
1XX1
01X1
0011
0000
521  
522 59 ▶ return 0 if !defined $msg || $msg !~ m/\S/msx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg and $msg =~ /\S/msx 0 59 59
Condition33%1/3
defined $msg and $msg =~ /\S/msx
!ll&&!rl&&r
0059
MC/DC0%0/2
defined $msg and $msg =~ /\S/msx
defined $msg $msg =~ /\S/msx
Truth table33%1/3
A and B
A= defined $msgB= $msg =~ /\S/msx
ABresult
0X0
100
111
523  
524 # The client path hands us decoded characters and the server path hands us
525 # bytes. Everything below works in bytes: feeding characters to a
526 # subprocess warns and mangles them, and configuration values are bytes too.
527 59 ▶ my $text = utf8::is_utf8($msg) ? encode( _commit_encoding($git), $msg ) : $msg;
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($msg) ? : 16 43 59
528 59 $text =~ s/\r\n/\n/gmsx;
529  
530 59 my $trailers = _trailers( $git, $text );
531 59 ▶ my $ctx = {
Branch: 1 branch
BranchTrueFalseTotal
defined $commit ? : 25 34 59
532 git => $git,
533 id => defined $commit ? $commit->commit : q{},
534 msg => $text,
535 trailers => $trailers,
536 suspects => _suspect_lines( $git, $text, $trailers ),
537 kinds => _commit_kinds( $git, $commit, $text ),
538 };
539  
540 59 return deny_errors($ctx) + require_errors($ctx) + order_errors($ctx);
541 }
542  
543 18 ▶sub check_message_file {
Subroutine check_message_file: called CC=1 SCAR=0.0
Pod documented
544 18 my ( $git, $msg ) = @_;
545 18 return message_errors( $git, undef, $msg );
546 }
547  
548 4 ▶sub check_ref {
Subroutine check_ref: called CC=2 SCAR=6.9
Pod documented
549 4 my ( $git, $ref ) = @_;
550  
551 4 my $errors = 0;
552 4 foreach my $commit ( $git->get_affected_ref_commits($ref) ) {
553 25 $errors += message_errors( $git, $commit, $commit->message );
554 }
555 4 return $errors;
556 }
557  
558 0 ▶sub check_patchset {
Subroutine check_patchset: not called CC=1 SCAR=6.9
Pod documented
559 0 my ( $git, $branch, $commit ) = @_;
560 0 return message_errors( $git, $commit, $commit->message );
561 }
562  
563 # Install hooks
564 24 my $options = { config => \&_setup_config };
565  
566 24 GITHOOKS_CHECK_AFFECTED_REFS( \&check_ref, $options );
567 24 GITHOOKS_CHECK_PATCHSET( \&check_patchset, $options );
568 24 GITHOOKS_CHECK_MESSAGE_FILE( \&check_message_file, $options );
569  
570 24 1;
571  
572 __END__
« blib/lib/Git/MoreHooks/ScanCtags.pm ↑ Summary blib/lib/Git/MoreHooks.pm »