Skip to content

Instantly share code, notes, and snippets.

@brookemahoney
Last active August 29, 2015 14:06
Show Gist options
  • Save brookemahoney/7d62be6bd0ae0cb1eb5d to your computer and use it in GitHub Desktop.
Save brookemahoney/7d62be6bd0ae0cb1eb5d to your computer and use it in GitHub Desktop.
Git pre-commit hook for Drupal development
#!/usr/bin/php
<?php
/**
* @file
* If this file has an exit status other than 0, the commit will fail.
*/
// For each check, set to 1 to enable or 0 to disable.
$check_unwanted_strings = 1;
$check_grunt = 1;
//////
// There should be no reason to edit anything below this line.
//////
$diff = get_diff();
// Disallows commit if git diff returns an error.
if (diff_error($diff)) {
exit (link is external)(1);
}
// Disallows commit if the diff contains an unwanted string.
if ($check_unwanted_strings && contains_unwanted_string($diff)) {
exit (link is external)(1);
}
// Disallows commit if grunt is running.
if ($check_grunt && grunt_is_running()) {
exit (link is external)(1);
}
exit (link is external)(0);
/**
* Returns the git diff as an array of lines.
*/
function get_diff() {
$return = 0;
$diff = array (link is external)();
exec (link is external)('git diff && git diff --staged', $diff, $return);
if ($return !== 0) {
$diff = array (link is external)();
}
return $diff;
}
/**
* Checks for error in diff.
*/
function diff_error($diff) {
$return = 0;
if (empty (link is external)($diff)) {
fwrite (link is external)(STDOUT, "git diff returned an error. Commit aborted.\n");
$return = 1;
}
return $return;
}
/**
* Checks a diff for unwanted strings.
*/
function contains_unwanted_string($diff) {
$return = 0;
$checks = array (link is external)(
'dsm(',
'dpm(',
'sdpm(', // search_krumo
'dpr(',
'dprint_r(',
'db_queryd(',
'kpr(',
'kprint_r(',
'var_dump(',
'error_log(',
'drupal_debug(',
'console.dir',
'consolr.log',
'temppp',
);
foreach ($diff as $lineno => $line) {
// Skips the line if it's not an addition line.
if (substr (link is external)($line, 0, 1) != '+') {
continue;
}
// Checks each line in the diff for unwanted strings.
foreach ($checks as $check) {
if (strstr (link is external)($line, $check)) {
fwrite (link is external)(STDOUT, "You were about to commit a $check)? \n");
fwrite (link is external)(STDOUT, "Offending line in diff:\n");
fwrite (link is external)(STDOUT, "Line: $lineno $line\n");
fwrite (link is external)(STDOUT, "Commit aborted.\n");
$return = 1;
break 2;
}
}
}
return $return;
}
/**
* Checks if grunt is running.
*/
function grunt_is_running() {
$return = 0;
$output = array (link is external)();
exec (link is external)('killall -s "grunt" &> /dev/null && if [ $? -eq 0 ]; then echo 1; fi', $output);
if (!empty (link is external)($output)) {
fwrite (link is external)(STDOUT, "Commit aborted: Grunt is running. \n");
$return = 1;
}
return $return;
}
@brookemahoney
Copy link
Author

Disallows a commit if particular strings are in the diff or if Grunt is running.

@brookemahoney
Copy link
Author

Fixed a documentation error for one of the functions.

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