Skip to content

Instantly share code, notes, and snippets.

@sztanpet
Forked from hugowetterberg/cleanup.sed
Last active September 4, 2015 21:04
Show Gist options
  • Save sztanpet/383768 to your computer and use it in GitHub Desktop.
Save sztanpet/383768 to your computer and use it in GitHub Desktop.
#!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 );
@parkan
Copy link

parkan commented Nov 11, 2010

you want --diff-filter=ACMRTUXB, otherwise deleted/moved files will break this

@sztanpet
Copy link
Author

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment