blib/lib/Git/MoreHooks/CheckCommitAuthorFromMailmap.pm

filter: Statementstmt 25.0% 33 / 132 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 44.4% 8 / 18 Podpod 80.0% 4 / 5 totaltotal 21.7% 45 / 207 CC 26 SCAR 39.4
CC
26
Cov
18%
_check_mailmap
156.0
_setup_config
20.0
check_ref
20.0
CRAP 51.4
« blib/lib/Git/MoreHooks/CheckIndent.pm ↑ Summary blib/lib/Git/MoreHooks/CheckPerl.pm »
1 ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
2 ## no critic (ControlStructures::ProhibitPostfixControls)
3  
4 package Git::MoreHooks::CheckCommitAuthorFromMailmap;
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Check Git commit author by using the mailmap file.
11  
12 1 our $VERSION = '0.021';
13  
14  
15 1 ▶use Git::Hooks 3.001001; # v3.1.1: GITHOOKS_CHECK_* drivers
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Log::Any qw{$log};
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Module::Load qw{ load };
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 my $PKG = __PACKAGE__;
22 1 my ($CFG) = __PACKAGE__ =~ /::([^:]+)$/msx;
23 1 $CFG = 'githooks.' . $CFG;
24  
25 #############
26 # Grok hook configuration, check it and set defaults.
27  
28 0 ▶sub _setup_config {
Subroutine _setup_config: not called CC=4 SCAR=30.0
29 0 my ($git) = @_;
30 0 $log->debugf( __PACKAGE__ . '::_setup_config(%s):', '$git' );
31  
32 0 my $config = $git->get_config();
33 0 $log->tracef( __PACKAGE__ . '::_setup_config(): Current Git config:\n%s.', $config );
34  
35 0 ▶ $config->{ lc $CFG } //= {};
Condition0%0/2
$$config{lc $CFG} //= {}
l!l
00
MC/DC0%0/1
$$config{lc $CFG} //= {}
$$config{lc $CFG}
Truth table0%0/2
A
A= $$config{lc $CFG}
Aresult
11
00
36  
37 0 my $default = $config->{ lc $CFG };
38 0 ▶ $default->{'match-mailmap-name'} //= ['1'];
Condition0%0/2
$default->{"match-mailmap-name"} //= [1]
l!l
00
MC/DC0%0/1
$default->{"match-mailmap-name"} //= [1]
$default->{"match-mailmap-name"}
Truth table0%0/2
A
A= $default->{"match-mailmap-name"}
Aresult
11
00
39 0 ▶ $default->{'allow-mailmap-aliases'} //= ['1'];
Condition0%0/2
$default->{"allow-mailmap-aliases"} //= [1]
l!l
00
MC/DC0%0/1
$default->{"allow-mailmap-aliases"} //= [1]
$default->{"allow-mailmap-aliases"}
Truth table0%0/2
A
A= $default->{"allow-mailmap-aliases"}
Aresult
11
00
40  
41 0 return;
42 }
43  
44 ##########
45  
46 0 ▶sub check_commit_at_client {
Subroutine check_commit_at_client: not called CC=2 SCAR=17.9
Pod documented
47 0 my ($git) = @_;
48  
49 0 my $current_branch = $git->get_current_branch();
50 0 ▶ return 1 unless $git->is_reference_enabled($current_branch);
Branch: 1 branch
BranchTrueFalseTotal
unless $git->is_reference_enabled($current_branch) 0 0 0
51  
52 0 my $author_name = $ENV{'GIT_AUTHOR_NAME'};
53 0 my $author_email = '<' . $ENV{'GIT_AUTHOR_EMAIL'} . '>';
54  
55 0 my $commit = ':0';
56 0 return _check_author( $git, $commit, $author_name, $author_email );
57 }
58  
59 0 ▶sub check_commit_at_server {
Subroutine check_commit_at_server: not called CC=1 SCAR=6.9
Pod documented
60 0 my ( $git, $commit ) = @_;
61  
62 0 my $author_name = $commit->{'author_name'};
63 0 my $author_email = '<' . $commit->{'author_email'} . '>';
64  
65 0 return _check_author( $git, $commit, $author_name, $author_email );
66 }
67  
68 0 ▶sub _check_author {
Subroutine _check_author: not called CC=3 SCAR=24.8
69 0 my ( $git, $commit, $author_name, $author_email ) = @_;
70  
71 0 _setup_config($git);
72  
73 0 ▶ return 1 if $git->im_admin();
Branch: 1 branch
BranchTrueFalseTotal
if $git->im_admin 0 0 0
74  
75 0 my $errors = 0;
76 0 ▶ _check_mailmap( $git, $commit, $author_name, $author_email ) or ++$errors;
Branch: 1 branch
BranchTrueFalseTotal
_check_mailmap($git, $commit, $author_name, $author_email) or ++$errors 0 0 0
77  
78 0 return $errors == 0;
79 }
80  
81 0 ▶sub _check_mailmap {
Subroutine _check_mailmap: not called CC=12 SCAR=50.5
82 0 my ( $git, $commit, $author_name, $author_email ) = @_;
83  
84 0 my $errors = 0;
85 eval {
86 0 load 'Git::Mailmap';
87 0 1; # To cover the fact that operation correctly returns a false value.
88 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &load("Git::Mailmap"); 1 } } or do { $Git::MoreHooks::CheckCommitAuthorFromMailmap::log->errorf("Git::MoreHooks::CheckCommitAuthorFromMailmap::_set_critic():Cannot load Git::Mailmap"); $git->fault("Cannot load Git::Mailmap", {"prefix", $PKG, "commit", $commit}); ++$errors; return $errors } 0 0 0
89 0 $log->errorf( __PACKAGE__ . q{::}
90 . '_set_critic():Cannot load Git::Mailmap'
91 );
92 0 $git->fault( 'Cannot load Git::Mailmap', { prefix => $PKG, commit => $commit } );
93 0 ++$errors;
94 0 return $errors;
95 };
96  
97 # TODO Move config checking to _setup_config() or elsewhere so it can break early on.
98 0 my $author = $author_name . q{ } . $author_email;
99 0 my $mailmap = Git::Mailmap->new();
100 0 my $mailmap_as_string;
101 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 $mailmap_as_string = $git->run( 'cat-file', '-p', 'HEAD:.mailmap' );
103 0 };
104 0 ▶ if ( defined $mailmap_as_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mailmap_as_string) 0 0 0
105 0 $mailmap->from_string( 'mailmap' => $mailmap_as_string );
106 0 $log->debugf( __PACKAGE__ . q{::} . '_check_mailmap(): HEAD:.mailmap read in.' . ' Content from Git::Mailmap:\n%s',
107 $mailmap->to_string() );
108 }
109  
110 # 2) Config variable mailmap.file
111 0 my $mapfile_location = $git->get_config( 'mailmap' => 'file' );
112 0 ▶ if ( defined $mapfile_location ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mapfile_location) 0 0 0
113 0 ▶ if ( -e $mapfile_location ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $mapfile_location) { } 0 0 0
114 0 my $file_as_str = path($mapfile_location)->slurp_utf8;
115 0 $mailmap->from_string( 'mailmap' => $file_as_str );
116 0 $log->debugf( __PACKAGE__ . q{::} . '_check_mailmap(): mailmap.file (%s) read in.' . ' Content from Git::Mailmap:\n%s',
117 $mapfile_location, $mailmap->to_string() );
118 }
119 else {
120 0 $git->fault( (sprintf 'Config variable \'mailmap.file\' (value: %s) does not point to a file.', $mapfile_location),
121 { prefix => $PKG, commit => $commit }
122 );
123 0 ++$errors;
124 }
125 }
126  
127 # 3) Config variable mailmap.blob
128 0 my $mapfile_blob = $git->get_config( 'mailmap' => 'blob' );
129 0 ▶ if ( defined $mapfile_blob ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mapfile_blob) 0 0 0
130 0 my $blob_as_str;
131 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 $blob_as_str = $git->run( 'cat-file', '-p', $mapfile_blob );
133 0 };
134 0 ▶ if ( defined $blob_as_str ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $blob_as_str) { } 0 0 0
135 0 $mailmap->from_string( 'mailmap' => $blob_as_str );
136 0 $log->debugf( __PACKAGE__ . q{::} . '_check_mailmap(): mailmap.blob (%s) read in.' . ' Content from Git::Mailmap:\n%s',
137 $mapfile_blob, $mailmap->to_string() );
138 }
139 else {
140 0 $git->fault( (sprintf 'Config variable \'mailmap.blob\' (value: %s) does not point to a file.', $mapfile_blob),
141 { prefix => $PKG, commit => $commit }
142 );
143 0 ++$errors;
144 }
145 }
146  
147 0 my $verified = 0;
148  
149 # Always search (first) among proper emails (and names if wanted).
150 0 my %search_params = ( 'proper-email' => $author_email );
151 0 ▶ if ( $git->get_config( $CFG => 'match-mailmap-name' ) eq '1' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($git->get_config($CFG, "match-mailmap-name") eq 1) 0 0 0
152 0 $search_params{'proper-name'} = $author_name;
153 }
154 0 $log->debugf( __PACKAGE__ . q{::} . '_check_mailmap(): search_params=%s.', \%search_params );
155 0 $verified = $mailmap->verify(%search_params);
156 0 $log->debugf( __PACKAGE__ . q{::} . '_check_mailmap(): verified=%s.', $verified );
157  
158 # If was not found among proper-*, and user wants, search aliases.
159 0 ▶ if ( !$verified && $git->get_config( $CFG => 'allow-mailmap-aliases' ) eq '1' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $verified and $git->get_config($CFG, "allow-mailmap-aliases") eq 1) 0 0 0
Condition0%0/3
not $verified and $git->get_config($CFG, "allow-mailmap-aliases") eq 1
!ll&&!rl&&r
000
MC/DC0%0/2
not $verified and $git->get_config($CFG, "allow-mailmap-aliases") eq 1
not $verified $git->get_config($CFG, "allow-mailmap-aliases") eq 1
Truth table0%0/3
not(A) and B
A= not $verifiedB= $git->get_config($CFG, "allow-mailmap-aliases") eq 1
ABresult
0X0
100
111
160 0 my %c_search_params = ( 'commit-email' => $author_email );
161 0 ▶ if ( $git->get_config( $CFG => 'match-mailmap-name' ) eq '1' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($git->get_config($CFG, "match-mailmap-name") eq 1) 0 0 0
162 0 $c_search_params{'commit-name'} = $author_name;
163 }
164 0 $log->debugf( __PACKAGE__ . q{::} . '_check_mailmap(): c_search_params=%s.', \%c_search_params );
165 0 $verified = $mailmap->verify(%c_search_params);
166 }
167 0 ▶ if ( $verified == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verified == 0) 0 0 0
168 0 $git->fault( "Commit author '$author' has no match in mailmap file.", { prefix => $PKG, commit => $commit } );
169 0 ++$errors;
170 }
171  
172 0 return $errors == 0;
173 }
174  
175 0 ▶sub check_ref {
Subroutine check_ref: not called CC=4 SCAR=30.0
Pod undocumented
176 0 my ( $git, $ref ) = @_;
177  
178 0 ▶ return 1 unless $git->is_reference_enabled($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $git->is_reference_enabled($ref) 0 0 0
179  
180 0 my $errors = 0;
181 0 foreach my $commit ( $git->get_affected_ref_commits($ref) ) {
182 0 ▶ check_commit_at_server( $git, $commit )
Branch: 1 branch
BranchTrueFalseTotal
check_commit_at_server($git, $commit) or ++$errors 0 0 0
183 or ++$errors;
184 }
185  
186 0 return $errors == 0;
187 }
188  
189 # This routine can act both as an update or a pre-receive hook.
190 0 ▶sub check_affected_refs {
Subroutine check_affected_refs: not called CC=4 SCAR=30.0
Pod documented
191 0 my ($git) = @_;
192  
193 0 _setup_config($git);
194  
195 0 ▶ return 1 if $git->im_admin($git);
Branch: 1 branch
BranchTrueFalseTotal
if $git->im_admin($git) 0 0 0
196  
197 0 my $errors = 0;
198  
199 0 foreach my $ref ( $git->get_affected_refs() ) {
200 0 ▶ check_ref( $git, $ref )
Branch: 1 branch
BranchTrueFalseTotal
check_ref($git, $ref) or ++$errors 0 0 0
201 or ++$errors;
202 }
203  
204 0 return $errors == 0;
205 }
206  
207 0 ▶sub check_patchset {
Subroutine check_patchset: not called CC=3 SCAR=24.8
Pod documented
208 0 my ( $git, $opts ) = @_;
209  
210 0 _setup_config($git);
211  
212 0 my $sha1 = $opts->{'--commit'};
213 0 my $commit = $git->get_commit($sha1);
214  
215 0 my $branch = $opts->{'--branch'};
216 0 ▶ $branch = "refs/heads/$branch"
Branch: 1 branch
BranchTrueFalseTotal
unless $branch =~ m[^refs/]msx 0 0 0
217 unless $branch =~ m{^refs/}msx;
218 0 ▶ return 1 unless $git->is_reference_enabled($branch);
Branch: 1 branch
BranchTrueFalseTotal
unless $git->is_reference_enabled($branch) 0 0 0
219  
220 0 return check_commit_at_server( $git, $commit );
221 }
222  
223 # Install hooks
224 1 my $options = { config => \&_setup_config };
225  
226 1 GITHOOKS_CHECK_PRE_COMMIT( \&check_commit_at_client, $options );
227 1 GITHOOKS_CHECK_AFFECTED_REFS( \&check_affected_refs, $options );
228 1 GITHOOKS_CHECK_PATCHSET( \&check_patchset, $options );
229  
230 1 1;
231  
232 __END__
« blib/lib/Git/MoreHooks/CheckIndent.pm ↑ Summary blib/lib/Git/MoreHooks/CheckPerl.pm »