blib/lib/Git/MoreHooks/CheckTrailers.pm

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