Created
February 27, 2015 01:16
-
-
Save leocaseiro/3a899db45d8ed6ba15fe to your computer and use it in GitHub Desktop.
Syntax check your PHP before GIT Commit
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
#.git/hooks/pre-commit | |
#chmod +x pre-commit | |
#http://www.phil-barker.com/2013/07/syntax-check-your-php-before-git-commit/ | |
#!/usr/bin/php | |
<?php | |
// Grab all added, copied or modified files into $output array | |
exec('git diff --cached --name-status --diff-filter=ACM', $output); | |
foreach ($output as $file) { | |
$fileName = trim(substr($file, 1)); | |
// We only want to check PHP files | |
if (pathinfo($fileName,PATHINFO_EXTENSION) == "php") { | |
$lint_output = array(); | |
// Check syntax with PHP lint | |
exec("php -l " . escapeshellarg($fileName), $lint_output, $return); | |
if ($return == 0) { | |
continue; | |
} else { | |
echo implode("\n", $lint_output), "\n"; | |
exit(1); | |
} | |
} | |
} | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment