Skip to content

Instantly share code, notes, and snippets.

@parkan
Forked from sztanpet/pre-commit
Created November 11, 2010 20:10
Show Gist options
  • Save parkan/673091 to your computer and use it in GitHub Desktop.
Save parkan/673091 to your computer and use it in GitHub Desktop.
php pre-commit lint that doesn't break on deleted files
#!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