Skip to content

Instantly share code, notes, and snippets.

@hSATAC
Created December 16, 2011 08:35
Show Gist options
  • Select an option

  • Save hSATAC/1485150 to your computer and use it in GitHub Desktop.

Select an option

Save hSATAC/1485150 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
// Set constants.
$php_pattern = "/\.(php|css)$/";
$img_pattern = "/[^(_24bit)]\.(jpg|jpeg|gif|png)$/";
$js_pattern = "/\.js$/";
// Set default variables.
$return = 0;
$exit_status = 0;
$output = array();
$matches = array("php" => array(), "service" => array(), "js" => array(), "img" => 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 PHP files.
if (preg_match($php_pattern, $file))
{
if (strpos($file, "index/modules/service/controllers/") !== FALSE)
{
$matches["service"][] = escapeshellarg($file);
}
else
{
$matches["php"][] = escapeshellarg($file);
}
}
// Match JavaScript files.
if (preg_match($js_pattern, $file))
{
$matches["js"][] = escapeshellarg($file);
}
// Match image files.
if (preg_match($img_pattern, $file))
{
$matches["img"][] = escapeshellarg($file);
}
}
$message = array("phpcs" => array(), "lang" => array(), "jslint" => array(), "optimg" => array(), "other" => array());
$return = 0;
// Check general PHP files.
$list = implode(" ", $matches["php"]);
if (count($matches["php"]))
{
exec("phpcs -n --report=summary $list", $message["phpcs"], $return);
if ($return == 1)
{
$exit_status = 1;
$message["phpcs"] = array_slice($message["phpcs"], 2);
array_splice($message["phpcs"], count($message["phpcs"]) - 4, 3);
}
exec("php /home/m/bin/lang_label_check.php $list", $message["lang"], $return);
if ($return == 1)
{
$exit_status = 1;
}
}
// Check service PHP files.
$list = implode(" ", $matches["service"]);
$output = array();
if (count($matches["service"]))
{
exec("phpcs -n --report=summary --standard=MiiiService $list", $output, $return);
if ($return == 1)
{
$exit_status = 1;
$output = array_slice($output, 5);
array_splice($output, count($output) - 4, 3);
$message["phpcs"] = array_merge($message["phpcs"], $output);
}
}
// Check JavaScript files.
if (count($matches["js"]))
{
foreach ($matches["js"] as $file)
{
exec("node /home/m/lib/jslint.run.js $file", $output, $return);
if ($return == 0)
{
continue;
}
$message["jslint"][] = "jslint -f " . $file;
$exit_status = 1;
}
}
// Check image files.
if (count($matches["img"]))
{
$saved = 0;
foreach ($matches["img"] as $file)
{
$cmd = "/home/m/bin/optimg --png8 --check-only $file";
exec($cmd, $output, $return);
foreach ($output as $item)
{
$saved = explode("\t", $item);
$saved = (isset($saved[1])) ? $saved[1] : 0;
if ($saved == 0)
{
continue;
}
$message["img"][] = "/home/m/bin/optimg --png8 " . $file . " (Save $saved bytes)";
$exit_status = 1;
}
}
}
// Check if you're on branch master, staging or qa
if (in_array($branch, array('master','qa','staging'))) {
$message["other"][] = "You should not commit [$branch] directly!";
$exit_status = 1;
}
if ($exit_status == 1)
{
echo "\nCOMMIT FAILED!\n";
if (count($message["other"]))
{
echo "======================================================================================\n";
echo "Other Error report \n";
echo "======================================================================================\n";
echo implode("\n", $message["other"]) . "\n";
echo "======================================================================================\n\n";
exit(1);
}
if (count($message["jslint"]))
{
echo "======================================================================================\n";
echo "JSLint Error report \n";
echo "======================================================================================\n";
echo implode("\n", $message["jslint"]) . "\n";
echo "======================================================================================\n\n";
exit(1);
}
if (count($message["phpcs"]))
{
echo "======================================================================================\n";
echo "PHP_CodeSniffer Error Report \n";
echo "======================================================================================\n";
echo implode("\n", $message["phpcs"]);
echo "======================================================================================\n\n";
exit(1);
}
if (count($message["lang"]))
{
echo "======================================================================================\n";
echo "Language Label Error Report \n";
echo "======================================================================================\n";
echo implode("\n", $message["lang"]) . "\n";
echo "======================================================================================\n\n";
exit(1);
}
if (count($message["img"]))
{
echo "======================================================================================\n";
echo "Image Optimization Warning \n";
echo "======================================================================================\n";
echo "You have the following choices: \n";
echo "1. If you are sure your image has been optimzied, add --no-verify option please.\n";
echo "2. If you need use PNG 24, please use the pattern of *_24bit.png.\n";
echo "3. If you would like using optimg command-line tool, execute the following commands.\n";
echo "======================================================================================\n";
echo implode("\n", $message["img"]) . "\n";
echo "======================================================================================\n\n";
exit(1);
}
echo "Please fix your code and commit again.\n\n";
}
exit($exit_status);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment