Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pre-commit SVN hook that runs lint
#!/usr/local/bin/php
<?php
/**
* Pre-commit Subversion script.
*
* @author Omni Adams <omni@digitaldarkness.com>
*/
/**
* Path to the awk binary.
*/
define('AWK', '/usr/bin/awk');
/**
* Path to the grep binary.
*/
define('GREP', '/bin/grep');
/**
* Path to the php binary.
*/
define('PHP', '/usr/local/bin/php');
/**
* Path to svnlook binary.
*/
define('SVNLOOK', '/usr/bin/svnlook');
/**
* Divider to inject into error messages to pretty them up.
*/
define('DIVIDER',
'********************************************************************************');
/**
* Run PHP lint on every PHP file that changed.
*
* If any of the PHP files in the current transaction have lint errors, writes
* an error message to standard error and returns true.
* @param string $transaction SVN transaction ID.
* @param string $repository Full path to the repository.
* @return boolean True if there was an error.
*/
function runPhpLint($transaction, $repository) {
$errors = false;
$changedCommand = SVNLOOK . ' changed -t "'
. $transaction . '" "'
. $repository . '" '
. '| ' . GREP . ' "^[UA]" '
. '| ' . GREP . ' "\\.php$" '
. '| ' . AWK . ' \'{print $2}\'';
exec($changedCommand, $changedOutput);
foreach ($changedOutput as $file) {
$lint = array();
$lintCommand = SVNLOOK . ' cat -t "'
. $transaction . '" "'
. $repository . '" "' . $file . '" '
. '| ' . PHP . ' -l';
exec($lintCommand, $lint);
if ('No syntax errors detected in -' == $lint[0]) {
// Lint returns text on good files,
// don't write that to standard
// error or consider it an error.
continue;
}
$message = PHP_EOL . DIVIDER . PHP_EOL . PHP_EOL
. $file . ' contains PHP error(s):' . PHP_EOL;
foreach ($lint as $line) {
// PHP lint includes some blank lines in its output.
if ('' == $line) {
continue;
}
// PHP lint tells us where the error is,
// which because we pass it in by piping it
// to standard in, doesn't tell us anything.
if ('Errors parsing -' == $line) {
continue;
}
$message .= "\t" . $line . PHP_EOL;
}
file_put_contents('php://stderr', $message . PHP_EOL);
$errors = true;
}
return $errors;
}
if (runPhpLint($transaction, $repository)) {
exit(1);
}
exit(0);
@eryno

This comment has been minimized.

Copy link

@eryno eryno commented Mar 23, 2015

I just googled for a script like this, and ended up on your github. The world is small, and my friends are smart. Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.