Skip to content

Instantly share code, notes, and snippets.

@paolomainardi
Last active August 29, 2015 14:06
Show Gist options
  • Save paolomainardi/0c81e58c66d0cee27394 to your computer and use it in GitHub Desktop.
Save paolomainardi/0c81e58c66d0cee27394 to your computer and use it in GitHub Desktop.
pre-commit_dcq
#!/usr/bin/php
<?php
/**
* @file
* A Git pre-commit hook script to check files for PHP syntax errors and Drupal
* coding standards violations. Requires phpcs and Coder Sniffer:
*
* @see https://drupal.org/node/1419988
*/
$return_back = '../../';
$php_cs_bin = realpath(getcwd() . '/bin/phpcs');
// Building an array of ignored files and folders.
$file_path = __DIR__ . '/' . $return_back . '.dcq_ignore';
$ignore = array();
if (file_exists($file_path)) {
$file = fopen($file_path, "r");
if ($file) {
while (!feof($file)) {
$ignore[] = fgets($file);
}
}
}
// Extensions of files to test.
$file_exts = array(
'engine',
'php',
'module',
'inc',
'install',
'profile',
'test',
'theme',
'txt',
);
$exit_code = 0;
$files = array();
$return = 0;
// Determine whether this is the first commit or not. If it is, set $against to
// the hash of a magical, empty commit to compare to.
exec('git rev-parse --verify HEAD 2> /dev/null', $files, $return);
$against = ($return == 0) ? 'HEAD' : '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
// Identify changed files.
exec("git diff-index --cached --name-only $against", $files);
// The number of ignored items.
$ignored_items_count = count($ignore);
echo "\033[1;33mDrupal code quality checker - pre-commit hook\033[0m\n\n";
foreach ($files as $file) {
if (file_exists($file) && !is_dir($file)) {
// Check files to ignore.
if ($ignored_items_count > 0) {
foreach ($ignore as $key => $value) {
if (substr($file, 0, strlen(trim($value))) == trim($value) && trim($value) != '') {
echo "\033[0;31mIgnored by DCQ - $file\n\033[0m";
continue 2;
}
}
}
echo "\033[0;32mChecking file {$file}\033[0m\n\n";
$ext = pathinfo($file, PATHINFO_EXTENSION);
if (!in_array($ext, $file_exts)) {
continue;
}
$phpcs_output = array();
$file = escapeshellarg($file);
// Add extra path to get warranty for run drush.
$dir = str_replace('\'', '', dirname($file));
$extra = '';
if ($dir !== '.') {
$extra = $dir . '/';
}
// Perform PHP syntax check (lint).
$return = 0;
$lint_cmd = "php -l {$file}";
$lint_output = array();
exec($lint_cmd, $lint_output, $return);
if ($return !== 0) {
// Format error messages and set exit code.
$exit_code = 1;
}
// Get file name for checking.
$filename = str_replace('\'', '', basename($file));
// Perform phpcs test.
$return = 0;
$phpcs_cmd = 'cd ' . __DIR__ . '/' . $return_back . $extra . ' && ' . $php_cs_bin . ' --standard=Drupal --extensions=php,module,inc,install,test,profile,theme ' . $filename;
exec($phpcs_cmd, $phpcs_output, $return);
if ($return !== 0) {
// Format error messages and set exit code.
echo implode("\n", $phpcs_output), "\n";
$exit_code = 1;
}
}
}
exit($exit_code);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment