App-GitHooks-Plugin-ForceRegularUpdate
view release on metacpan or search on metacpan
lib/App/GitHooks/Plugin/ForceRegularUpdate.pm view on Meta::CPAN
=cut
sub run_pre_commit
{
my ( $class, %args ) = @_;
my $app = delete( $args{'app'} );
my $repository = $app->get_repository();
my $config = $app->get_config();
# Verify we have the max update age configured.
my $max_update_age = $config->get( 'ForceRegularUpdate', 'max_update_age' );
croak "'max_update_age' must be defined in the [ForceRegularUpdate] section of your .githooksrc file"
if !defined $max_update_age;
$max_update_age =~ s/^\s+//;
$max_update_age =~ s/\s*(?:#.*)$//;
croak "'max_update_age' in the [ForceRegularUpdate] section must be an integer expressing seconds"
if $max_update_age !~ /^\d+$/;
# Verify we have a description.
my $description = $config->get( 'ForceRegularUpdate', 'description' );
croak "'description' must be defined in the [ForceRegularUpdate] section of your .githooksrc file"
if !defined( $description ) || ( $description !~ /\w/ );
# Check if we have environment restrictions.
my $env_variable = $config->get( 'ForceRegularUpdate', 'env_variable' );
my $env_regex = $config->get_regex( 'ForceRegularUpdate', 'env_regex' );
if ( defined( $env_variable ) )
{
croak "You defined an environment variable to check against, but not a regex to use, in the [ForceRegularUpdate] section"
if !defined( $env_regex );
return $PLUGIN_RETURN_SKIPPED
if ( $ENV{ $env_variable } // '' ) !~ $env_regex;
}
# Retrieve the file that specifies the time of last update.
my $update_file = $config->get( 'ForceRegularUpdate', 'update_file' );
croak "'update_file' must be defined in the [ForceRegularUpdate] section of your .githooksrc file"
if !defined( $update_file );
$update_file =~ s/\$ENV\{'([^']+)'\}/$ENV{$1}/xeg;
$update_file =~ s/\$ENV\{"([^"]+)"\}/$ENV{$1}/xeg;
$update_file =~ s/\$ENV\{([^\}]+)\}/$ENV{$1}/xeg;
# Check if the update was ever performed.
my $failure_character = $app->get_failure_character();
if ( ! -e $update_file )
{
print $app->wrap(
$app->color( 'red', "$failure_character It appears that you have never performed $description on this machine - please do that before committing.\n" ),
"",
);
return $PLUGIN_RETURN_FAILED;
}
# Retrieve the value of the file and check whether it's within the bounds
# allowed.
my $last_update = File::Slurp::read_file( $update_file );
chomp( $last_update );
if ( !defined( $last_update ) # Invalid format.
|| ( $last_update !~ /^\d+$/ ) # Invalid format.
|| ( $last_update < time() - $max_update_age ) # Not updated in a long time.
|| ( $last_update > time() + 10 ) # Nice try setting the timestamp in the future to not have to update.
)
{
print $app->wrap(
$app->color(
'red',
"$failure_character It appears that you haven't performed $description on this machine for a long time. Please do that and try to commit again.\n"
),
"",
);
return $PLUGIN_RETURN_FAILED;
}
return $PLUGIN_RETURN_PASSED;
}
=head1 BUGS
Please report any bugs or feature requests through the web interface at
L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-ForceRegularUpdate/issues/new>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc App::GitHooks::Plugin::ForceRegularUpdate
You can also look for information at:
=over
=item * GitHub's request tracker
L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-ForceRegularUpdate/issues>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/app-githooks-plugin-forceregularupdate>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/app-githooks-plugin-forceregularupdate>
=item * MetaCPAN
L<https://metacpan.org/release/App-GitHooks-Plugin-ForceRegularUpdate>
=back
=head1 AUTHOR
L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
C<< <aubertg at cpan.org> >>.
( run in 1.292 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )