Skip to content

Instantly share code, notes, and snippets.

@grafikchaos
Last active December 18, 2015 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grafikchaos/5829412 to your computer and use it in GitHub Desktop.
Save grafikchaos/5829412 to your computer and use it in GitHub Desktop.
example pre-commit hook to PHP lint the PHP files for Drupal projects
#!/usr/bin/env php
<?php
$output = array();
$return = 0;
exec('git rev-parse --verify HEAD 2> /dev/null', $output, $return);
$against = $return == 0 ? 'HEAD' : '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
exec("git diff-index --cached --name-only {$against}", $output);
#$filename_pattern = '/\.php$/';
$filename_pattern = '/\.(php|engine|theme|install|inc|module|test)$/';
$exit_status = 0;
foreach ($output as $file) {
if (!preg_match($filename_pattern, $file)) {
// don't check files that aren't PHP
continue;
}
$lint_output = array();
exec("php -l " . escapeshellarg($file), $lint_output, $return);
if ($return == 0) {
continue;
}
echo implode("\n", $lint_output), "\n";
$exit_status = 1;
}
exit($exit_status);
@grafikchaos
Copy link
Author

I typically store this in my Dropbox folder and symlink it down to my project's .git/hooks/pre-commit file like so:

chmod +x ~/Dropbox/dotfiles/phplint_drupal_pre-commit
ln -s ~/Dropbox/dotfiles/phplint_drupal_pre-commit .git/hooks/pre-commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment