-
-
Save sztanpet/383768 to your computer and use it in GitHub Desktop.
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 --name-only --diff-filter=ACMRTUXB 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 ); |
you want --diff-filter=ACMRTUXB, otherwise deleted/moved files will break this
thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
needs \n line endings (not the windows \r\n ones), and the php and git executables to be on the path (mostly here to remind windows users of this)