Skip to content

Instantly share code, notes, and snippets.

@eryno
Forked from omnicolor/svn-lint.php
Last active August 29, 2015 14:17
Show Gist options
  • Save eryno/f52b2c32554d7708737d to your computer and use it in GitHub Desktop.
Save eryno/f52b2c32554d7708737d to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
/**
* CLI utility to lint all changed files in a local SVN repo.
* Modified from a pre-commit Subversion script by Omni Adams.
*
* @author Omni Adams <[email protected]>
* @author Eryn O'Neil <[email protected]>
*/
/**
* 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/bin/php');
/**
* Path to svnlook binary.
*/
define('SVN', '/usr/bin/svn');
/**
* 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 = SVN . ' status '
. '| ' . GREP . ' "^[UAM?]" '
. '| ' . GREP . ' "\\.php$" '
. '| ' . AWK . ' \'{print $2}\'';
exec($changedCommand, $changedOutput);
foreach ($changedOutput as $file) {
$lint = array();
// Send stderr to /dev/null, or it will appear prematurely
// in the terminal. This defeats the purpose.
$lintCommand = PHP . " -l $file 2> /dev/null";
exec($lintCommand, $lint);
if ("No syntax errors detected in $file" == $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 $file" == $line) {
continue;
}
$message .= "\t" . $line . PHP_EOL;
}
file_put_contents('php://stderr', $message . PHP_EOL);
$errors = true;
}
if ( ! $errors ) {
echo 'No errors found.' . PHP_EOL;
}
return $errors;
}
if (runPhpLint($transaction, $repository)) {
exit(1);
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment