Created
June 10, 2013 19:02
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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