Code-TidyAll

 view release on metacpan or  search on metacpan

lib/Code/TidyAll/Git/Precommit.pm  view on Meta::CPAN

package Code::TidyAll::Git::Precommit;

use strict;
use warnings;

use Capture::Tiny            qw(capture_stdout capture_stderr);
use Code::TidyAll::Git::Util qw(git_files_to_commit);
use Code::TidyAll;
use IPC::System::Simple qw(capturex run);
use Log::Any            qw($log);
use Path::Tiny          qw(path);
use Scope::Guard        qw(guard);
use Specio::Library::Builtins;
use Specio::Library::String;
use Try::Tiny;

use Moo;

our $VERSION = '0.85';

has conf_name => (
    is  => 'ro',
    isa => t('NonEmptyStr'),
);

has git_path => (
    is      => 'ro',
    isa     => t('NonEmptyStr'),
    default => 'git'
);

has no_stash => (
    is      => 'ro',
    isa     => t('Bool'),
    default => 0,
);

has tidyall_class => (
    is      => 'ro',
    isa     => t('ClassName'),
    default => 'Code::TidyAll'
);

has tidyall_options => (
    is      => 'ro',
    isa     => t('HashRef'),
    default => sub { {} }
);

sub check {
    my ( $class, %params ) = @_;

    my $fail_msg;

    try {
        my $self          = $class->new(%params);
        my $tidyall_class = $self->tidyall_class;

        # Find conf file at git root
        my $root_dir = capturex( $self->git_path, qw( rev-parse --show-toplevel ) );
        chomp($root_dir);
        $root_dir = path($root_dir);

        my @conf_names
            = $self->conf_name ? ( $self->conf_name ) : Code::TidyAll->default_conf_names;
        my ($conf_file) = grep { $_->is_file } map { $root_dir->child($_) } @conf_names
            or die sprintf( 'could not find conf file %s', join( ' or ', @conf_names ) );

        my $guard;
        unless ( $self->no_stash || $root_dir->child( '.git', 'MERGE_HEAD' )->exists ) {

            # We stash things to make sure that we only attempt to run tidyall
            # on changes in the index while ensuring that after the hook runs
            # the working directory is in the same state it was before the
            # commit.



( run in 0.687 second using v1.01-cache-2.11-cpan-39bf76dae61 )