Skip to content

Instantly share code, notes, and snippets.

@mzedeler
Created May 17, 2010 13:50
Show Gist options
  • Save mzedeler/403773 to your computer and use it in GitHub Desktop.
Save mzedeler/403773 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# 20100517 Michael Zedeler
# This pre-commit hook script runs perlcritic against any file
# recognized as being perl (using file name and file content heuristics).
# Files resulting in critic violations at the level returned by
# git config perlcritic.level are rejected.
#
# Use --no-verify to override.
use strict;
use warnings;
sub is_perl {
my $filename = shift;
my $is_perl = $filename =~ /\.p[ml]$/;
if(not $is_perl) {
open my $TYPE, "file $filename|";
$is_perl = <$TYPE> =~ /perl/i;
}
return $is_perl;
}
sub critic_level {
open my $LH, 'git config perlcritic.level|';
my($level) = <$LH> =~ /(\S+)/;
$level ||= 5;
return $level;
}
open my $FILES, 'git diff --name-status|';
while(<$FILES>) {
next unless my($filename) = /^M\t(.+)\s+/;
next unless is_perl($filename);
system(sprintf('perlcritic -%d %s', critic_level, $filename));
die "Won't commit!" unless $? == 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment