-
-
Save parkan/673091 to your computer and use it in GitHub Desktop.
php pre-commit lint that doesn't break on deleted files
This file contains 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
#!php | |
<?php | |
$files = array(); | |
exec('git diff-index --cached --diff-filter=ACMRTUXB --name-only HEAD', $files ); | |
$exitcode = 0; | |
// dont redirect stderr to stdin, we will get the errors twice, redirect it to dev/null | |
if ( PHP_OS == 'WINNT' ) | |
$redirect = ' 2> NUL'; | |
else | |
$redirect = ' 2> /dev/null'; | |
foreach( $files as $file ) { | |
if ( !preg_match('/\.php$/i', $file ) or !is_file( $file ) ) | |
continue; | |
exec('php -l ' . escapeshellarg( $file ) . $redirect, $output, $return ); | |
if ( !$return ) // php -l gives a 0 error code if everything went well | |
continue; | |
$exitcode = 1; // abort the commit | |
array_shift( $output ); // first line is always blank | |
array_pop( $output ); // the last message is always "Errors parsing httpdocs/test.php" | |
echo implode("\n", $output ), "\n"; // an extra newline for it to look good | |
} | |
exit( $exitcode ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment