Created
September 17, 2014 19:52
-
-
Save heddn/9175734d8a6c2b034567 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/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 = '../../'; | |
| // 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', | |
| ); | |
| $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); | |
| print "\nPrecommit: PHP Lint, phpcs and jshint\n\n"; | |
| // The number of ignored items. | |
| $ignored_items_count = count($ignore); | |
| 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; | |
| } | |
| } | |
| } | |
| $ext = pathinfo($file, PATHINFO_EXTENSION); | |
| if ($ext == 'js') { | |
| var_dump($file); | |
| $return = 0; | |
| $jshint_cmd = 'cd ' . __DIR__ . '/' . $return_back . ' && jshint ' . $file; | |
| exec($jshint_cmd, $jshint_output, $return); | |
| echo "\njshinting - $jshint_cmd\n"; | |
| if ($return !== 0) { | |
| // Format error messages and set exit code. | |
| echo implode("\n", $jshint_output), "\n"; | |
| $exit_code = 1; | |
| } | |
| } | |
| 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; | |
| $extensions = implode(',', $file_exts); | |
| $phpcs_cmd = 'cd ' . __DIR__ . '/' . $return_back . $extra . ' && phpcs --standard=DrupalSecure --extensions=' . $extensions . ' ' . $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