| 1 | package Git::Hooks::PerlCritic; | ||||||||||
| 2 | 1 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | our $VERSION = '0.001000'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Module::Load 'load'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Git::Hooks; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | sub _changed { | ||||||||||
| 13 | 0 | ▶ | my $git = shift; | ||||||||
|
Subroutine
_changed: not called CC=1 SCAR=6.9
| |||||||||||
| 14 | |||||||||||
| 15 | my @changed | ||||||||||
| 16 | 0 | = grep { /\.(p[lm]|t)$/xms } | |||||||||
| 17 | $git->command( qw/diff --cached --name-only --diff-filter=AM/ ) | ||||||||||
| 18 | ; | ||||||||||
| 19 | |||||||||||
| 20 | 0 | return \@changed; | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | sub _set_critic { | ||||||||||
| 24 | 0 | ▶ | load 'Perl::Critic'; | ||||||||
|
Subroutine
_set_critic: not called CC=1 SCAR=6.9
| |||||||||||
| 25 | 0 | load 'Perl::Critic::Violation'; | |||||||||
| 26 | 0 | load 'Perl::Critic::Utils'; | |||||||||
| 27 | |||||||||||
| 28 | 0 | my $pc = Perl::Critic->new; | |||||||||
| 29 | 0 | my $verbosity = $pc->config->verbose; | |||||||||
| 30 | |||||||||||
| 31 | # set the format to be a comment | ||||||||||
| 32 | 0 | my $fmt = Perl::Critic::Utils::verbosity_to_format( $verbosity ); | |||||||||
| 33 | 0 | Perl::Critic::Violation::set_format( "# $fmt" ); | |||||||||
| 34 | |||||||||||
| 35 | 0 | return $pc; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | sub _check_violations { | ||||||||||
| 39 | 0 | ▶ | my $files = shift; | ||||||||
|
Subroutine
_check_violations: not called CC=2 SCAR=17.9
| |||||||||||
| 40 | |||||||||||
| 41 | 0 | my @violations; | |||||||||
| 42 | 0 | foreach my $file ( @$files ) { | |||||||||
| 43 | 0 | state $critic = _set_critic; | |||||||||
| 44 | |||||||||||
| 45 | 0 | @violations = $critic->critique( $file ); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 0 | return \@violations; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | PREPARE_COMMIT_MSG { | ||||||||||
| 52 | 0 | ▶ | my ( $git, $commit_msg_file ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 53 | |||||||||||
| 54 | 0 | my $changed = _changed( $git ); | |||||||||
| 55 | 0 | my $violations = _check_violations( $changed ); | |||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | if ( @$violations ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | my $pcf = 'Path::Class::File'; load $pcf; | |||||||||
| 59 | 0 | my $file = $pcf->new( $commit_msg_file ); | |||||||||
| 60 | 0 | my $contents = $file->slurp; | |||||||||
| 61 | |||||||||||
| 62 | # a space is being prepended, suspect internal join, remove it | ||||||||||
| 63 | 0 | ( $contents .= "@$violations" ) =~ s/^\ #//xmsg; | |||||||||
| 64 | |||||||||||
| 65 | 0 | $file->spew( $contents ); | |||||||||
| 66 | } | ||||||||||
| 67 | 1 | }; | |||||||||
| 68 | |||||||||||
| 69 | PRE_COMMIT { | ||||||||||
| 70 | 0 | ▶ | my $git = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 71 | |||||||||||
| 72 | 0 | my $changed = _changed( $git ); | |||||||||
| 73 | 0 | my $violations = _check_violations( $changed ); | |||||||||
| 74 | |||||||||||
| 75 | 0 | ▶ | if ( @$violations ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 0 | print @$violations; | |||||||||
| 77 | # . operator causes the array ref to give count, otherwise it would | ||||||||||
| 78 | # stringify | ||||||||||
| 79 | 0 | croak '# please fix ' . @$violations . ' perl critic errors before committing'; | |||||||||
| 80 | } | ||||||||||
| 81 | 1 | }; | |||||||||
| 82 | |||||||||||
| 83 | 1 | 1; | |||||||||
| 84 | |||||||||||
| 85 | # ABSTRACT: Perl Critic hooks for git | ||||||||||
| 86 | |||||||||||
| 87 | __END__ | ||||||||||
| 88 | |||||||||||
| 89 | =pod | ||||||||||
| 90 | |||||||||||
| 91 | =head1 NAME | ||||||||||
| 92 | |||||||||||
| 93 | Git::Hooks::PerlCritic - Perl Critic hooks for git | ||||||||||
| 94 | |||||||||||
| 95 | =head1 VERSION | ||||||||||
| 96 | |||||||||||
| 97 | version 0.001000 | ||||||||||
| 98 | |||||||||||
| 99 | =head1 DESCRIPTION | ||||||||||
| 100 | |||||||||||
| 101 | Allows you to utilize L<Perl::Critic> via | ||||||||||
| 102 | L<git hooks|http://www.kernel.org/pub/software/scm/git/docs/githooks.html> | ||||||||||
| 103 | using the L<Git::Hooks> framework. | ||||||||||
| 104 | |||||||||||
| 105 | First setup L<git-hooks.pl|Git::Hooks/"USAGE"> | ||||||||||
| 106 | |||||||||||
| 107 | Then you should choose to use only one of the available hooks. | ||||||||||
| 108 | |||||||||||
| 109 | =head1 HOOKS | ||||||||||
| 110 | |||||||||||
| 111 | =head2 pre-commit | ||||||||||
| 112 | |||||||||||
| 113 | ln -s git-hooks.pl pre-commit | ||||||||||
| 114 | git config --add githooks.pre-commit PerlCritic | ||||||||||
| 115 | |||||||||||
| 116 | This hook will prevent a commit that doesn't pass L<Perl::Critic> from being | ||||||||||
| 117 | committed. | ||||||||||
| 118 | |||||||||||
| 119 | =head2 prepare-commit-msg | ||||||||||
| 120 | |||||||||||
| 121 | ln -s git-hooks.pl prepare-commit-msg | ||||||||||
| 122 | git config --add githooks.preprare-commit-msg PerlCritic | ||||||||||
| 123 | |||||||||||
| 124 | This hook will simply append commented out critic warnings to the commit | ||||||||||
| 125 | message, so you may review them before committing. Note: requires | ||||||||||
| 126 | L<Path::Class::File>. | ||||||||||
| 127 | |||||||||||
| 128 | =head1 SEE ALSO | ||||||||||
| 129 | |||||||||||
| 130 | =over | ||||||||||
| 131 | |||||||||||
| 132 | =item L<Git::Hooks> | ||||||||||
| 133 | |||||||||||
| 134 | =item L<Perl::Critic> | ||||||||||
| 135 | |||||||||||
| 136 | =back | ||||||||||
| 137 | |||||||||||
| 138 | =head1 BUGS | ||||||||||
| 139 | |||||||||||
| 140 | Please report any bugs or feature requests on the bugtracker website | ||||||||||
| 141 | https://github.com/hostgator/git-hooks-perlcritic/issues or by email to | ||||||||||
| 142 | development@hostgator.com. | ||||||||||
| 143 | |||||||||||
| 144 | When submitting a bug or request, please include a test-file or a | ||||||||||
| 145 | patch to an existing test-file that illustrates the bug or desired | ||||||||||
| 146 | feature. | ||||||||||
| 147 | |||||||||||
| 148 | =head1 AUTHOR | ||||||||||
| 149 | |||||||||||
| 150 | Caleb Cushing <xenoterracide@gmail.com> | ||||||||||
| 151 | |||||||||||
| 152 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 153 | |||||||||||
| 154 | This software is Copyright (c) 2013 by L<HostGator.com|http://hostgator.com>. | ||||||||||
| 155 | |||||||||||
| 156 | This is free software, licensed under: | ||||||||||
| 157 | |||||||||||
| 158 | The Artistic License 2.0 (GPL Compatible) | ||||||||||
| 159 | |||||||||||
| 160 | =cut | ||||||||||