| 1 | package Code::TidyAll::SVN::Precommit; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Capture::Tiny qw(capture_stdout capture_stderr); | ||
| 7 | use Code::TidyAll::Util qw(tempdir_simple); | ||
| 8 | use Code::TidyAll; | ||
| 9 | use Log::Any qw($log); | ||
| 10 | use Path::Tiny qw(path); | ||
| 11 | use SVN::Look; | ||
| 12 | use Try::Tiny; | ||
| 13 | |||
| 14 | use Moo; | ||
| 15 | |||
| 16 | our $VERSION = '0.85'; | ||
| 17 | |||
| 18 | # Public | ||
| 19 | has conf_name => ( is => 'ro' ); | ||
| 20 | has emergency_comment_prefix => ( is => 'ro', default => 'NO TIDYALL' ); | ||
| 21 | has extra_conf_files => ( is => 'ro', default => sub { [] } ); | ||
| 22 | has reject_on_error => ( is => 'ro' ); | ||
| 23 | has repos => ( is => 'ro', default => sub { $ARGV[0] } ); | ||
| 24 | has tidyall_class => ( is => 'ro', default => 'Code::TidyAll' ); | ||
| 25 | has tidyall_options => ( is => 'ro', default => sub { {} } ); | ||
| 26 | has txn => ( is => 'ro', default => sub { $ARGV[1] } ); | ||
| 27 | |||
| 28 | # Private | ||
| 29 | has cat_file_cache => ( init_arg => undef, is => 'ro', default => sub { {} } ); | ||
| 30 | has revlook => ( init_arg => undef, is => 'lazy' ); | ||
| 31 | |||
| 32 | sub _build_revlook { | ||
| 33 | my $self = shift; | ||
| 34 | return SVN::Look->new( $self->repos, '-t' => $self->txn ); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub check { | ||
| 38 | my ( $class, %params ) = @_; | ||
| 39 | |||
| 40 | my $fail_msg; | ||
| 41 | |||
| 42 | try { | ||
| 43 | my $self = $class->new(%params); | ||
| 44 | my $revlook = $self->revlook; | ||
| 45 | |||
| 46 | # Skip if emergency comment prefix is present | ||
| 47 | # | ||
| 48 | if ( my $prefix = $self->emergency_comment_prefix ) { | ||
| 49 | if ( index( $revlook->log_msg, $prefix ) == 0 ) { | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | my @files = ( $self->revlook->added(), $self->revlook->updated() ); | ||
| 55 | $log->info('----------------------------'); | ||
| 56 | $log->infof( | ||
| 57 | '%s [%s] repos = %s; txn = %s', | ||
| 58 | scalar(localtime), $$, scalar( getpwuid($<) ), | ||
| 59 | $self->repos, $self->txn | ||
| 60 | ); | ||
| 61 | $log->infof( 'looking at files: %s', join( ', ', @files ) ); | ||
| 62 | |||
| 63 | my %conf_files; | ||
| 64 | foreach my $file (@files) { | ||
| 65 | if ( my $conf_file = $self->find_conf_for_file($file) ) { | ||
| 66 | my $root = $conf_file->parent; | ||
| 67 | my $rel_file = substr( $file, length($root) + 1 ); | ||
| 68 | $conf_files{$conf_file}->{$rel_file}++; | ||
| 69 | } | ||
| 70 | else { | ||
| 71 | my $msg = sprintf( q{** could not find conf file upwards from '%s'}, $file ); | ||
| 72 | $log->error($msg); | ||
| 73 | die $msg if $self->reject_on_error; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | my @results; | ||
| 78 | for my $conf_file ( map { path($_) } keys %conf_files ) { | ||
| 79 | my $file_map = $conf_files{$conf_file}; | ||
| 80 | |||
| 81 | my $root = $conf_file->parent; | ||
| 82 | my $conf_name = $conf_file->basename; | ||
| 83 | $log->error("$root, $conf_file"); | ||
| 84 | my $tempdir = tempdir_simple(); | ||
| 85 | my @files = keys(%$file_map); | ||
| 86 | foreach my $rel_file ( $conf_name, @{ $self->extra_conf_files }, @files ) { | ||
| 87 | |||
| 88 | # TODO: what if cat fails | ||
| 89 | my $contents = $self->cat_file( $root->child($rel_file) ); | ||
| 90 | my $full_path = $tempdir->child($rel_file); | ||
| 91 | $full_path->parent->mkpath( { mode => 0755 } ); | ||
| 92 | $full_path->spew_raw($contents); | ||
| 93 | } | ||
| 94 | my $tidyall = $self->tidyall_class->new_from_conf_file( | ||
| 95 | $tempdir->child($conf_name), | ||
| 96 | no_cache => 1, | ||
| 97 | check_only => 1, | ||
| 98 | mode => 'commit', | ||
| 99 | %{ $self->tidyall_options }, | ||
| 100 | ); | ||
| 101 | my $stdout = capture_stdout { | ||
| 102 | push( @results, $tidyall->process_paths( map { $tempdir->child($_) } @files ) ); | ||
| 103 | }; | ||
| 104 | if ($stdout) { | ||
| 105 | chomp($stdout); | ||
| 106 | $log->info($stdout); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | if ( my @error_results = grep { $_->error } @results ) { | ||
| 111 | my $error_count = scalar(@error_results); | ||
| 112 | $fail_msg = join( | ||
| 113 | "\n", | ||
| 114 | sprintf( | ||
| 115 | '%d file%s did not pass tidyall check', | ||
| 116 | $error_count, $error_count > 1 ? 's' : q{} | ||
| 117 | ), | ||
| 118 | map { join( ': ', $_->path, $_->error ) } @error_results | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | catch { | ||
| 123 | my $error = $_; | ||
| 124 | $log->error($error); | ||
| 125 | die $error if $params{reject_on_error}; | ||
| 126 | }; | ||
| 127 | die $fail_msg if $fail_msg; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub find_conf_for_file { | ||
| 131 | my ( $self, $file ) = @_; | ||
| 132 | |||
| 133 | my @conf_names = $self->conf_name ? ( $self->conf_name ) : Code::TidyAll->default_conf_names; | ||
| 134 | my $search_dir = $file->parent; | ||
| 135 | my $cnt = 0; | ||
| 136 | while (1) { | ||
| 137 | foreach my $conf_name (@conf_names) { | ||
| 138 | my $conf_file = $search_dir->child($conf_name); | ||
| 139 | return $conf_file if ( $self->cat_file($conf_file) ); | ||
| 140 | } | ||
| 141 | if ( $search_dir eq '/' || $search_dir eq '' || $search_dir eq '.' ) { | ||
| 142 | return undef; | ||
| 143 | } | ||
| 144 | else { | ||
| 145 | $search_dir = $search_dir->parent; | ||
| 146 | } | ||
| 147 | die 'inf loop!' if ++$cnt > 100; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | sub cat_file { | ||
| 152 | my ( $self, $file ) = @_; | ||
| 153 | my $contents; | ||
| 154 | if ( exists( $self->cat_file_cache->{$file} ) ) { | ||
| 155 | $contents = $self->cat_file_cache->{$file}; | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | try { | ||
| 159 | capture_stderr { $contents = $self->revlook->cat($file) }; | ||
| 160 | } | ||
| 161 | catch { | ||
| 162 | $contents = ''; | ||
| 163 | }; | ||
| 164 | $self->cat_file_cache->{$file} = $contents; | ||
| 165 | } | ||
| 166 | return $contents; | ||
| 167 | } | ||
| 168 | |||
| 169 | 1; | ||
| 170 | |||
| 171 | # ABSTRACT: Subversion pre-commit hook that requires files to be tidyall'd | ||
| 172 | |||
| 173 | __END__ |