Created
September 7, 2012 20:06
-
-
Save oranj/3669205 to your computer and use it in GitHub Desktop.
Pre commit hook.
This file contains 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 | |
chdir (dirname(__FILE__).'/../../'); | |
$root = realpath(getcwd()).'/'; | |
define('ESC', "\033"); | |
function is_tty() { | |
static $is_tty = null; | |
if (is_null($is_tty)) { $is_tty = posix_isatty(STDOUT); } | |
return $is_tty; | |
} | |
function highlight($text) { | |
if (is_tty()) { | |
return ESC.'[0;36m'.$text.ESC."[0m"; | |
} else { | |
return $text; | |
} | |
} | |
// Maybe the best way of getting these files. | |
$git_text = `git status -s `; | |
if (! preg_match_all('/(A|M)(A|M|\?| ) (?P<file>.*)\n/', $git_text, $matches)) { | |
return; | |
} | |
$filenames = $matches['file']; | |
$file_data = array(); | |
foreach ($filenames as $filename) { | |
preg_match('/\.(?P<ext>.*?)$/', $filename, $matches); | |
$file_data [$filename] = array( | |
'ext' => isset($matches['ext']) ? isset($matches['ext']): '', | |
'fullpath' => $root.$filename, | |
'string' => (is_tty()?highlight($root):'').$filename | |
); | |
} | |
$found_bugs = array(); | |
// add any functions here. | |
$functions_to_look_for = array('see', 'drop'); | |
// Checking for the above functions. | |
foreach ($file_data as $filename => $data) { | |
if ($data['ext'] == 'php') { | |
$fh = fopen($root.$filename, 'r'); | |
$line_no = 1; | |
while ($line = fgets($fh)) { | |
if (preg_match($reg = '/\s*(?P<function_region>.*?)(?P<method>'.join('|', array_map('preg_quote', $functions_to_look_for)).')\s*\(/i', $line, $match) && ! preg_match('/function/i', $match['function_region']) && ! preg_match('/(#|\/\/)/', $match['function_region'])) { | |
$found_bugs ['debugging code'] []= array( | |
'data' => $data, | |
'line' => $line_no | |
); | |
} | |
$line_no++; | |
} | |
} | |
} | |
// checking all php files for error. | |
foreach ($file_data as $filename => $data) { | |
if ($data['ext'] == 'php') { | |
exec('php -l '.$data['fullpath'].' 2> /dev/null', $out); | |
if (preg_match('/.error .* on line (?P<line>[0-9]+)/si', join("\n",$out), $matches)) { | |
$found_bugs ['parsing'] []= array( | |
'data' => $data, | |
'line' => $matches['line'] | |
); | |
} | |
} | |
} | |
if ($found_bugs) { | |
foreach ($found_bugs as $type => $bug_list) { | |
echo "\nFound some errors related to ".highlight($type).":\n"; | |
foreach ($bug_list as $bug) { | |
echo "\t".highlight($bug['data']['string']).':'.$bug['line']."\n"; | |
} | |
} | |
echo "\nNo need to thank me, citizen\n\n"; | |
die(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
save this as .git/hooks/pre-commit in your project.