Last active
August 29, 2015 14:08
-
-
Save josephj/9c43fae499c129fa1f67 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
$js_pattern = '/\.js$/'; | |
$return = 0; | |
$exit_status = 0; | |
$output = array(); | |
$matches = array('js' => array()); | |
// Get aganist git version | |
exec('git rev-parse --verify HEAD 2> /dev/null', $output, $return); | |
$against = ($return == 0) ? 'HEAD' : '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; | |
// Get diff index | |
$cmd = "git diff-index --cached --name-status $against"; | |
exec($cmd, $output, $return); | |
// Check Code Standard Error | |
foreach ($output as $item) { | |
$item = explode("\t", $item); | |
// Don't deal with commit id | |
if (count($item) !== 2) { | |
continue; | |
} | |
// Don't deal with deleted files | |
$file = $item[1]; | |
$status = $item[0]; | |
if ($status === 'D') { | |
continue; | |
} | |
// Find JavaScript files | |
if (preg_match($js_pattern, $file)) { | |
$matches["js"][] = escapeshellarg($file); | |
} | |
} | |
// Check JavaScript files with JSHint | |
$return = 0; | |
$message = array('jshint' => array()); | |
if (count($matches['js'])) { | |
foreach ($matches['js'] as $file) { | |
// Correct file path. | |
if (mb_substr($file, 0, 1) !== '/') { | |
$file = getcwd() . '/' . $file; | |
} | |
$cmd = "jshint $file"; | |
exec($cmd, $output, $return); | |
if ($return == 0) { | |
continue; | |
} | |
array_shift($output); | |
array_shift($output); | |
array_shift($output); | |
$message['jshint'][] = implode($output, "\n"); | |
$exit_status = 1; | |
} | |
} | |
if ($exit_status == 1) { | |
echo "\nCOMMIT FAILED!\n"; | |
if (count($message['jshint'])) { | |
echo "============================================================\n"; | |
echo "JSHint Error report \n"; | |
echo "============================================================\n"; | |
echo implode($message['jshint']) . "\n"; | |
echo "============================================================\n\n"; | |
exit(1); | |
} | |
} | |
exit($exit_status); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment