| 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 | our $VERSION = '0.019'; # VERSION: generated by DZP::OurPkgVersion | ||
| 13 | |||
| 14 | |||
| 15 | 1 | ▶ | use Git::Hooks 3.000000; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | use Path::Tiny; | ||
| 17 | use Try::Tiny; | ||
| 18 | use Log::Any qw{$log}; | ||
| 19 | use Module::Load qw{ load }; | ||
| 20 | |||
| 21 | my $PKG = __PACKAGE__; | ||
| 22 | my ($CFG) = __PACKAGE__ =~ /::([^:]+)$/msx; | ||
| 23 | $CFG = 'githooks.' . $CFG; | ||
| 24 | |||
| 25 | ############# | ||
| 26 | # Grok hook configuration, check it and set defaults. | ||
| 27 | |||
| 28 | sub _setup_config { | ||
| 29 | my ($git) = @_; | ||
| 30 | $log->debugf( __PACKAGE__ . '::_setup_config(%s):', '$git' ); | ||
| 31 | |||
| 32 | my $config = $git->get_config(); | ||
| 33 | $log->tracef( __PACKAGE__ . '::_setup_config(): Current Git config:\n%s.', $config ); | ||
| 34 | |||
| 35 | $config->{ lc $CFG } //= {}; | ||
| 36 | |||
| 37 | my $default = $config->{ lc $CFG }; | ||
| 38 | $default->{'match-mailmap-name'} //= ['1']; | ||
| 39 | $default->{'allow-mailmap-aliases'} //= ['1']; | ||
| 40 | |||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | ########## | ||
| 45 | |||
| 46 | sub check_commit_at_client { | ||
| 47 | my ($git) = @_; | ||
| 48 | |||
| 49 | my $current_branch = $git->get_current_branch(); | ||
| 50 | return 1 unless $git->is_reference_enabled($current_branch); | ||
| 51 | |||
| 52 | my $author_name = $ENV{'GIT_AUTHOR_NAME'}; | ||
| 53 | my $author_email = '<' . $ENV{'GIT_AUTHOR_EMAIL'} . '>'; | ||
| 54 | |||
| 55 | my $commit = ':0'; | ||
| 56 | return _check_author( $git, $commit, $author_name, $author_email ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub check_commit_at_server { | ||
| 60 | my ( $git, $commit ) = @_; | ||
| 61 | |||
| 62 | my $author_name = $commit->{'author_name'}; | ||
| 63 | my $author_email = '<' . $commit->{'author_email'} . '>'; | ||
| 64 | |||
| 65 | return _check_author( $git, $commit, $author_name, $author_email ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _check_author { | ||
| 69 | my ( $git, $commit, $author_name, $author_email ) = @_; | ||
| 70 | |||
| 71 | _setup_config($git); | ||
| 72 | |||
| 73 | return 1 if $git->im_admin(); | ||
| 74 | |||
| 75 | my $errors = 0; | ||
| 76 | _check_mailmap( $git, $commit, $author_name, $author_email ) or ++$errors; | ||
| 77 | |||
| 78 | return $errors == 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _check_mailmap { | ||
| 82 | my ( $git, $commit, $author_name, $author_email ) = @_; | ||
| 83 | |||
| 84 | my $errors = 0; | ||
| 85 | eval { | ||
| 86 | load 'Git::Mailmap'; | ||
| 87 | 1; # To cover the fact that operation correctly returns a false value. | ||
| 88 | } or do { | ||
| 89 | $log->errorf( __PACKAGE__ . q{::} | ||
| 90 | . '_set_critic():Cannot load Git::Mailmap' | ||
| 91 | ); | ||
| 92 | $git->fault( 'Cannot load Git::Mailmap', { prefix => $PKG, commit => $commit } ); | ||
| 93 | ++$errors; | ||
| 94 | return $errors; | ||
| 95 | }; | ||
| 96 | |||
| 97 | # TODO Move config checking to _setup_config() or elsewhere so it can break early on. | ||
| 98 | my $author = $author_name . q{ } . $author_email; | ||
| 99 | my $mailmap = Git::Mailmap->new(); | ||
| 100 | my $mailmap_as_string; | ||
| 101 | try { | ||
| 102 | $mailmap_as_string = $git->run( 'cat-file', '-p', 'HEAD:.mailmap' ); | ||
| 103 | }; | ||
| 104 | if ( defined $mailmap_as_string ) { | ||
| 105 | $mailmap->from_string( 'mailmap' => $mailmap_as_string ); | ||
| 106 | $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 | my $mapfile_location = $git->get_config( 'mailmap' => 'file' ); | ||
| 112 | if ( defined $mapfile_location ) { | ||
| 113 | if ( -e $mapfile_location ) { | ||
| 114 | my $file_as_str = path($mapfile_location)->slurp_utf8; | ||
| 115 | $mailmap->from_string( 'mailmap' => $file_as_str ); | ||
| 116 | $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 | $git->fault( (sprintf 'Config variable \'mailmap.file\' (value: %s) does not point to a file.', $mapfile_location), | ||
| 121 | { prefix => $PKG, commit => $commit } | ||
| 122 | ); | ||
| 123 | ++$errors; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | # 3) Config variable mailmap.blob | ||
| 128 | my $mapfile_blob = $git->get_config( 'mailmap' => 'blob' ); | ||
| 129 | if ( defined $mapfile_blob ) { | ||
| 130 | my $blob_as_str; | ||
| 131 | try { | ||
| 132 | $blob_as_str = $git->run( 'cat-file', '-p', $mapfile_blob ); | ||
| 133 | }; | ||
| 134 | if ( defined $blob_as_str ) { | ||
| 135 | $mailmap->from_string( 'mailmap' => $blob_as_str ); | ||
| 136 | $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 | $git->fault( (sprintf 'Config variable \'mailmap.blob\' (value: %s) does not point to a file.', $mapfile_blob), | ||
| 141 | { prefix => $PKG, commit => $commit } | ||
| 142 | ); | ||
| 143 | ++$errors; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | my $verified = 0; | ||
| 148 | |||
| 149 | # Always search (first) among proper emails (and names if wanted). | ||
| 150 | my %search_params = ( 'proper-email' => $author_email ); | ||
| 151 | if ( $git->get_config( $CFG => 'match-mailmap-name' ) eq '1' ) { | ||
| 152 | $search_params{'proper-name'} = $author_name; | ||
| 153 | } | ||
| 154 | $log->debugf( __PACKAGE__ . q{::} . '_check_mailmap(): search_params=%s.', \%search_params ); | ||
| 155 | $verified = $mailmap->verify(%search_params); | ||
| 156 | $log->debugf( __PACKAGE__ . q{::} . '_check_mailmap(): verified=%s.', $verified ); | ||
| 157 | |||
| 158 | # If was not found among proper-*, and user wants, search aliases. | ||
| 159 | if ( !$verified && $git->get_config( $CFG => 'allow-mailmap-aliases' ) eq '1' ) { | ||
| 160 | my %c_search_params = ( 'commit-email' => $author_email ); | ||
| 161 | if ( $git->get_config( $CFG => 'match-mailmap-name' ) eq '1' ) { | ||
| 162 | $c_search_params{'commit-name'} = $author_name; | ||
| 163 | } | ||
| 164 | $log->debugf( __PACKAGE__ . q{::} . '_check_mailmap(): c_search_params=%s.', \%c_search_params ); | ||
| 165 | $verified = $mailmap->verify(%c_search_params); | ||
| 166 | } | ||
| 167 | if ( $verified == 0 ) { | ||
| 168 | $git->fault( "Commit author '$author' has no match in mailmap file.", { prefix => $PKG, commit => $commit } ); | ||
| 169 | ++$errors; | ||
| 170 | } | ||
| 171 | |||
| 172 | return $errors == 0; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub check_ref { | ||
| 176 | my ( $git, $ref ) = @_; | ||
| 177 | |||
| 178 | return 1 unless $git->is_reference_enabled($ref); | ||
| 179 | |||
| 180 | my $errors = 0; | ||
| 181 | foreach my $commit ( $git->get_affected_ref_commits($ref) ) { | ||
| 182 | check_commit_at_server( $git, $commit ) | ||
| 183 | or ++$errors; | ||
| 184 | } | ||
| 185 | |||
| 186 | return $errors == 0; | ||
| 187 | } | ||
| 188 | |||
| 189 | # This routine can act both as an update or a pre-receive hook. | ||
| 190 | sub check_affected_refs { | ||
| 191 | my ($git) = @_; | ||
| 192 | |||
| 193 | _setup_config($git); | ||
| 194 | |||
| 195 | return 1 if $git->im_admin($git); | ||
| 196 | |||
| 197 | my $errors = 0; | ||
| 198 | |||
| 199 | foreach my $ref ( $git->get_affected_refs() ) { | ||
| 200 | check_ref( $git, $ref ) | ||
| 201 | or ++$errors; | ||
| 202 | } | ||
| 203 | |||
| 204 | return $errors == 0; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub check_patchset { | ||
| 208 | my ( $git, $opts ) = @_; | ||
| 209 | |||
| 210 | _setup_config($git); | ||
| 211 | |||
| 212 | my $sha1 = $opts->{'--commit'}; | ||
| 213 | my $commit = $git->get_commit($sha1); | ||
| 214 | |||
| 215 | my $branch = $opts->{'--branch'}; | ||
| 216 | $branch = "refs/heads/$branch" | ||
| 217 | unless $branch =~ m{^refs/}msx; | ||
| 218 | return 1 unless $git->is_reference_enabled($branch); | ||
| 219 | |||
| 220 | return check_commit_at_server( $git, $commit ); | ||
| 221 | } | ||
| 222 | |||
| 223 | # Install hooks | ||
| 224 | my $options = { config => \&_setup_config }; | ||
| 225 | |||
| 226 | GITHOOKS_CHECK_PRE_COMMIT( \&check_commit_at_client, $options ); | ||
| 227 | GITHOOKS_CHECK_AFFECTED_REFS( \&check_affected_refs, $options ); | ||
| 228 | GITHOOKS_CHECK_PATCHSET( \&check_patchset, $options ); | ||
| 229 | |||
| 230 | 1; | ||
| 231 | |||
| 232 | __END__ | ||