Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Created June 10, 2013 19:02
Show Gist options
  • Save sycobuny/5751292 to your computer and use it in GitHub Desktop.
Save sycobuny/5751292 to your computer and use it in GitHub Desktop.
A (relatively) easy-to-extend pre-commit hook for linting multiple languages in git.
#!/usr/bin/env perl
use warnings;
use strict;
my ($fail) = 0;
my ($linters) = {
php => [qr/\.(php|inc)$/i, 'php -l', '%s --'],
perl => [qr/\.(pl|pm|t)$/i, 'perl -cw', '%s'],
};
foreach my $file (split("\n", `git diff --cached --name-only`)) {
foreach my $lang (keys %$linters) {
my ($pat, $linter, $linter_command) = @{ $linters->{$lang} };
if ($file =~ $pat) {
$linter_command = sprintf($linter_command, $linter);
`git show :$file | $linter_command 2>&1 1>/dev/null`;
if ($?) {
print "Syntax error in $file, check with: $linter $file\n";
$fail = 1;
}
}
}
}
print "Refusing to commit\n" if $fail;
exit($fail);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment