| 1 | package Code::TidyAll::Git::Precommit; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use Capture::Tiny qw(capture_stdout capture_stderr); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Code::TidyAll::Git::Util qw(git_files_to_commit); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Code::TidyAll; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use IPC::System::Simple qw(capturex run); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 15 | ▶ | use Path::Tiny qw(path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use Scope::Guard qw(guard); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 15 | ▶ | use Specio::Library::Builtins; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 15 | ▶ | use Specio::Library::String; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 15 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 15 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 15 | our $VERSION = '0.85'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 15 | has conf_name => ( | |||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||
| 23 | isa => t('NonEmptyStr'), | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 15 | has git_path => ( | |||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||
| 28 | isa => t('NonEmptyStr'), | ||||||||||||||||||||||||||||
| 29 | default => 'git' | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 15 | has no_stash => ( | |||||||||||||||||||||||||||
| 33 | is => 'ro', | ||||||||||||||||||||||||||||
| 34 | isa => t('Bool'), | ||||||||||||||||||||||||||||
| 35 | default => 0, | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 15 | has tidyall_class => ( | |||||||||||||||||||||||||||
| 39 | is => 'ro', | ||||||||||||||||||||||||||||
| 40 | isa => t('ClassName'), | ||||||||||||||||||||||||||||
| 41 | default => 'Code::TidyAll' | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | has tidyall_options => ( | ||||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||
| 46 | isa => t('HashRef'), | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | default => sub { {} } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 15 | ); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 15 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 15 | my ( $class, %params ) = @_; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 15 | my $fail_msg; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 15 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||
| 56 | 15 | my $self = $class->new(%params); | |||||||||||||||||||||||||||
| 57 | 15 | my $tidyall_class = $self->tidyall_class; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Find conf file at git root | ||||||||||||||||||||||||||||
| 60 | 15 | my $root_dir = capturex( $self->git_path, qw( rev-parse --show-toplevel ) ); | |||||||||||||||||||||||||||
| 61 | 15 | chomp($root_dir); | |||||||||||||||||||||||||||
| 62 | 15 | $root_dir = path($root_dir); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | my @conf_names | ||||||||||||||||||||||||||||
| 65 | 15 | ▶ | = $self->conf_name ? ( $self->conf_name ) : Code::TidyAll->default_conf_names; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 15 | ▶ | my ($conf_file) = grep { $_->is_file } map { $root_dir->child($_) } @conf_names | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | or die sprintf( 'could not find conf file %s', join( ' or ', @conf_names ) ); | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 15 | my $guard; | |||||||||||||||||||||||||||
| 70 | 15 | ▶ | unless ( $self->no_stash || $root_dir->child( '.git', 'MERGE_HEAD' )->exists ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->no_stash or $root_dir->child(".git", "MERGE_HEAD")->exists
MC/DC50%1/2
$self->no_stash or $root_dir->child(".git", "MERGE_HEAD")->exists
$self->no_stash
$root_dir->child(".git", "MERGE_HEAD")->exists
Truth table66%2/3
A or B
A= $self->no_stashB= $root_dir->child(".git", "MERGE_HEAD")->exists
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # We stash things to make sure that we only attempt to run tidyall | ||||||||||||||||||||||||||||
| 73 | # on changes in the index while ensuring that after the hook runs | ||||||||||||||||||||||||||||
| 74 | # the working directory is in the same state it was before the | ||||||||||||||||||||||||||||
| 75 | # commit. | ||||||||||||||||||||||||||||
| 76 | # | ||||||||||||||||||||||||||||
| 77 | # If there's nothing to stash there's no stash entry, in which | ||||||||||||||||||||||||||||
| 78 | # case popping would be very bad. | ||||||||||||||||||||||||||||
| 79 | 14 | my $pre_stash_state | |||||||||||||||||||||||||||
| 80 | = capturex( [ 0, 1 ], $self->git_path, qw( rev-parse -q --verify refs/stash ) ); | ||||||||||||||||||||||||||||
| 81 | 14 | run( | |||||||||||||||||||||||||||
| 82 | $self->git_path, qw( stash save --keep-index --include-untracked ), | ||||||||||||||||||||||||||||
| 83 | 'TidyAll pre-commit guard' | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | 14 | my $post_stash_state | |||||||||||||||||||||||||||
| 86 | = capturex( [ 0, 1 ], $self->git_path, qw( rev-parse -q --verify refs/stash ) ); | ||||||||||||||||||||||||||||
| 87 | 14 | ▶ | unless ( $pre_stash_state eq $post_stash_state ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 13 | ▶ | $guard = guard { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 89 | 13 | ▶ | my ($version) = capturex(qw( git version )) =~ /([0-9]+\.[0-9]+\.[0-9]+)/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | or die 'Cannot determine version number from git version output!'; | ||||||||||||||||||||||||||||
| 91 | 13 | my $minor = ( split /\./, $version )[1]; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # When pop is run quietly in 2.24.x it deletes files! See | ||||||||||||||||||||||||||||
| 94 | # https://public-inbox.org/git/CAMcnqp22tEFva4vYHYLzY83JqDHGzDbDGoUod21Dhtnvv=h_Pg@mail.gmail.com/ | ||||||||||||||||||||||||||||
| 95 | # for the initial bug report. This was fixed in 2.25. | ||||||||||||||||||||||||||||
| 96 | 13 | ▶ | my @args = $minor == 24 ? () : ('-q'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 13 | run( $self->git_path, 'stash', 'pop', @args ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 13 | } | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # Gather file paths to be committed | ||||||||||||||||||||||||||||
| 103 | 15 | my @files = git_files_to_commit($root_dir); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | my $tidyall = $tidyall_class->new_from_conf_file( | ||||||||||||||||||||||||||||
| 106 | $conf_file, | ||||||||||||||||||||||||||||
| 107 | no_cache => 1, | ||||||||||||||||||||||||||||
| 108 | check_only => 1, | ||||||||||||||||||||||||||||
| 109 | mode => 'commit', | ||||||||||||||||||||||||||||
| 110 | 15 | %{ $self->tidyall_options }, | |||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||
| 112 | 15 | my @results = $tidyall->process_paths(@files); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 15 | ▶ | if ( my @error_results = grep { $_->error } @results ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 3 | my $error_count = scalar(@error_results); | |||||||||||||||||||||||||||
| 116 | 3 | ▶ | $fail_msg = sprintf( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | "%d file%s did not pass tidyall check\n", | ||||||||||||||||||||||||||||
| 118 | $error_count, $error_count > 1 ? 's' : q{} | ||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 123 | 0 | my $error = $_; | |||||||||||||||||||||||||||
| 124 | 0 | die "Error during pre-commit hook (use --no-verify to skip hook):\n$error"; | |||||||||||||||||||||||||||
| 125 | 15 | }; | |||||||||||||||||||||||||||
| 126 | 15 | ▶ | die "$fail_msg\n" if $fail_msg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 15 | 1; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # ABSTRACT: Git pre-commit hook that requires files to be tidyall'd | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||