Created
June 6, 2011 21:53
-
-
Save carbonphyber/1011197 to your computer and use it in GitHub Desktop.
Git pre-commit hook for PHP+JS+CSS (Gaia version)
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
#!/usr/bin/env php | |
<?php | |
$output = array(); | |
$errorOutput = array(); | |
$return = 0; | |
$pathToErrorFile = 'temp/minify_errors.txt'; | |
file_put_contents($pathToErrorFile, ''); | |
exec('git rev-parse --verify HEAD 2> /dev/null', $output, $return); | |
$against = $return == 0 ? 'HEAD' : '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; | |
exec("git diff-index --cached --name-only {$against}", $output); | |
$exit_status = 0; | |
ob_start(); | |
foreach ($output as $file) { | |
if (!is_readable($file)) { | |
continue; // if the file isn't readable,.. we can't syncheck it. SKIP to the next file to check. | |
} | |
$returnVal = 0; | |
$resultLines = array(); | |
$phpsyn_failed = false; | |
$extension = strrchr($file, '.'); | |
if (0 === strcmp($extension, '.php')) { | |
// validation of PHP files | |
$lint_output = array(); | |
exec("phpsyn " . escapeshellarg($file), $resultLines, $returnVal); | |
if ($returnVal === 0) { | |
continue; | |
} | |
$exit_status = 1; | |
$phpsyn_failed = true; | |
} else if (0 === strcmp($extension, '.js')) { | |
// validation of JS files | |
// minification of JS files | |
exec('dev/minify/minify ' . escapeshellarg($file) . " -f 2>&1", $resultLines, $returnVal); | |
} else if (0 === strcmp($extension, '.css')) { | |
// validation of CSS files | |
// minification of CSS files | |
exec('dev/minify/minify ' . escapeshellarg($file) . " -f 2>&1", $resultLines, $returnVal); | |
} | |
// if there was an error, give the devloper some useful information and set the Exit status to be non-zero | |
if (0 !== $returnVal) { | |
$systemCommandResults = array(); | |
$exit_status = $returnVal; | |
for($i = 0; $i < count($resultLines); $i++) { | |
if(0 === strcmp($extension, '.php')) { | |
// only echo the lines with 'error: ' in the text (phpsyn error format) | |
if(FALSE !== strpos($resultLines[$i], 'error: ')) { | |
$errorOutput[] = $file . ': ' . trim($resultLines[$i]); | |
} | |
} else { | |
// only echo the lines with '[ERROR]' in the text (JSLint error format) | |
if(FALSE !== strpos($resultLines[$i], '[ERROR]')) { | |
$errorOutput[] = $file . ': ' . trim($resultLines[$i]); | |
} | |
} | |
} | |
if ($phpsyn_failed) { | |
echo implode("\n", $resultLines) . "\nPHP SYNTAX CHECK FAILED. SEE NOTES ABOVE.\n"; | |
} | |
} | |
} | |
ob_end_clean(); | |
if (0 !== $exit_status) { // display a summary message at the bottom of the output | |
echo "\n" . implode("\n", $errorOutput); | |
echo "\n\n" . 'There was an error during pre-commit. See above.' . "\n"; | |
} | |
exit($exit_status); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment