Skip to content

Instantly share code, notes, and snippets.

@digitalist
Created June 3, 2016 21:56
Show Gist options
  • Save digitalist/ae33e199a69acd3d017ddd81b6b19908 to your computer and use it in GitHub Desktop.
Save digitalist/ae33e199a69acd3d017ddd81b6b19908 to your computer and use it in GitHub Desktop.
pre-commit git-hook to check for debug messages
#!/usr/bin/php
<?php
#for debug/scripting
#use --no-verify - like this:
#git commit -m "init" --no-verify
echo "============= RUNNING PRE-COMMIT HOOK\n\n";
$forbidden=array(
'var_dump',
'print_r',
'echo',
'print',
'debug_'
);
//run git, it can be done without /tmp/, but i'm too lazy to do it today
exec('git diff master --cached --no-color > /tmp/stage.diff');
exec('git diff master --no-color >> /tmp/stage.diff');
$data=preg_split("/[\r\n]+/", file_get_contents('/tmp/stage.diff'));
$filenames=array();
$errors = array();
foreach ($data as $k=>$v) {
if (preg_match_all("/\+\+\+ b\/(.*)/", $v, $filenameMatches)) {
if(isset($filenameMatches[1][0])){
$filename = $filenameMatches[1][0];
}
}
foreach ($forbidden as $fk=>$fv) {
$pattern = '/^\+.*'.$fv.'\s*/';
if (preg_match($pattern, $v)){
$errors[]="\n>>>$fv in [".$filename."]\n>>> $v \n";
}
}
}
$errors = array_unique($errors);
if (count($errors)){
foreach ($errors as $k=>$v){
echo $v;
}
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment