Created
June 13, 2013 01:01
-
-
Save josephj/5770443 to your computer and use it in GitHub Desktop.
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/php | |
<?php | |
// Set constants. | |
$js_pattern = "/\.js$/"; | |
// Set default variables. | |
$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); | |
// Get branch name | |
$branch = trim(`git rev-parse --abbrev-ref HEAD`); | |
// 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; | |
} | |
// Match JavaScript files. | |
if (preg_match($js_pattern, $file)) | |
{ | |
$matches["js"][] = escapeshellarg($file); | |
} | |
} | |
$message = array("jslint" => array()); | |
$return = 0; | |
// Check JavaScript files. | |
if (count($matches["js"])) | |
{ | |
foreach ($matches["js"] as $file) | |
{ | |
// Correct file path. | |
if (mb_substr($file, 0, 1) !== "/") | |
{ | |
$file = getcwd() . "/" . $file; | |
} | |
$cmd = "jslint --continue --nomen --regexp --sloppy $file"; | |
exec($cmd, $output, $return); | |
if ($return == 0) | |
{ | |
continue; | |
} | |
array_shift($output); | |
array_shift($output); | |
array_shift($output); | |
$message["jslint"][] = implode($output, "\n"); | |
$exit_status = 1; | |
} | |
} | |
if ($exit_status == 1) | |
{ | |
echo "\nCOMMIT FAILED!\n"; | |
if (count($message["jslint"])) | |
{ | |
echo "============================================================\n"; | |
echo "JSLint Error report \n"; | |
echo "============================================================"; | |
echo implode($message["jslint"]) . "\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