App-GitHooks
view release on metacpan or search on metacpan
lib/App/GitHooks/StagedChanges.pm view on Meta::CPAN
our $VERSION = '1.9.0';
=head1 METHODS
=head2 new()
Instantiate a new C<App::GitHooks::StagedChanges> object.
my $staged_changes = App::GitHooks::StagedChanges->new(
app => $app,
);
Arguments:
=over 4
=item * app I<(mandatory)>
An C<App::GitHook> instance.
=back
=cut
sub new
{
my ( $class, %args ) = @_;
my $app = delete( $args{'app'} );
# Check arguments.
croak 'An "app" argument is mandatory'
if !Data::Validate::Type::is_instance( $app, class => 'App::GitHooks' );
return bless(
{
app => $app,
},
$class,
);
}
=head2 get_app()
Return the parent C<App::GitHooks> object.
my $app = $staged_changes->get_app();
=cut
sub get_app
{
my ( $self ) = @_;
return $self->{'app'};
}
=head2 verify()
Verify the changes that are being committed.
This method returns an array composed of:
=over 4
=item * A boolean to indicate whether the checks passed or failed.
=item * A boolean to indicate whether any warnings were displayed.
=back
( $allow_commit, $has_warnings ) = $staged_changes->verify();
=cut
sub verify
{
my ( $self, %args ) = @_;
croak 'Invalid argument(s): ' . join( ', ', keys %args )
if scalar( keys %args ) != 0;
$self->analyze_changes();
# Check the changed files.
return $self->check_changed_files();
}
=head2 check_changed_files()
Verify that the files changed pass various rules.
This method returns an array composed of:
=over 4
=item * A boolean to indicate whether the files passed the checks.
=item * A boolean to indicate whether any warnings were displayed.
=back
my ( $all_files_pass, $has_warnings ) = check_changed_files();
=cut
sub check_changed_files
{
my ( $self ) = @_;
my $app = $self->get_app();
my $repository = $app->get_repository();
# Get a list of changes from Git.
my @changes = $repository->run( 'diff', '--cached', '--name-status', '--', '.' );
# Parse changes.
my $files = {};
foreach my $change ( @changes )
{
my ( $git_action, $file ) = ( $change =~ /^(\w+)\s+(.*)$/x );
if ( !defined( $file ) )
{
print $app->wrap( "Could not parse git diff output:\n$change\n" );
return 0;
}
$files->{ $file } = $git_action;
}
# Check each file.
my $allow_commit = 1;
my $has_warnings = 0;
my $total = scalar( keys %$files );
my $count = 1;
foreach my $file ( sort keys %$files )
{
( run in 0.489 second using v1.01-cache-2.11-cpan-99c4e6809bf )