Skip to content

Instantly share code, notes, and snippets.

@wesruv wesruv/pre-commit forked from naxoc/pre-commit
Created Oct 28, 2014

Embed
What would you like to do?
#!/usr/bin/php
<?php
/**
* @file
* This is a Git pre-commit hook that informs you when you are
* about to commit whitespace or a debug function.
* From https://gist.github.com/naxoc/4150599
*/
$red = "\033[1;31m";
$red_end = "\033[0m";
$yellow = "\033[1;33m";
$yellow_end = "\033[0m";
/**
* An array of functions to check for.
*/
$check = array();
$check[] = ' dsm(';
$check[] = ' dpm(';
$check[] = ' dpr(';
$check[] = ' kpr(';
$check[] = ' kprint_r(';
$check[] = ' dd(';
$check[] = ' drupal_debug(';
$check[] = ' dpq(';
$check[] = 'wakka';
$rut_roh = false;
$return = 0;
$diff = array();
exec('git diff --staged', $diff, $return);
if ($return !== 0) {
fwrite(STDOUT, "git diff returned an error. Commit aborted.\n");
$rut_roh = true;
}
foreach ($diff as $lineno => $line) {
if (substr($line, 0, 1) != '+') {
// Skip the line if you aren't adding something that may contain a debug
// function call
continue;
}
foreach ($check as $lineno => $function) {
if (strstr(strtolower($line), $function)) {
fwrite(STDOUT, "{$red}Oh, noes! You were about to commit a $function?{$red_end}\n");
fwrite(STDOUT, $yellow . $line . $yellow_end);
fwrite(STDOUT, "\nCommit aborted.\n");
$rut_roh = true;
}
}
}
$whitespace = shell_exec('git diff --staged --check');
if (!empty($whitespace)) {
fwrite(STDOUT, "{$red}Commit aborted. Fix trailing whitespace.{$red_end}\n");
fwrite(STDOUT, $yellow . $whitespace . $yellow_end);
$rut_roh = true;
}
if ($rut_roh) {
exit(1);
}
// If this is still running, all is peachy. Let the developer commit.
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.