Skip to content

Instantly share code, notes, and snippets.

@nfreear
Last active October 15, 2015 04:05
Show Gist options
  • Save nfreear/2563338 to your computer and use it in GitHub Desktop.
Save nfreear/2563338 to your computer and use it in GitHub Desktop.
Git pre-commit hook to run PHP lint, check for [CR] Mac-only line endings..
#!/C/xampp/php/php
<?php
#!/usr/bin/env php
/**
* Git pre-commit hook to run php lint.
*
* Git hooks on Windows: Ensure that the path to PHP is added to the %PATH% variable, and run `git commit` via the Bash shell.
*
* @copyright 2012-04-30 N.D.Freear.
* @license MIT
* @link http://progit.org/book/ch7-3.html
*/
#echo '>> '.$argv[0] .' | exit(1)' .PHP_EOL; exit(1);
define('GIT_COMMAND', 'git ls-files --modified');
$handle = popen(escapeshellcmd(GIT_COMMAND), 'r'); //2>&1
$result = fread($handle, 2096);
pclose($handle);
$file_list = explode("\n", $result);
#$file_list[] = __FILE__;
foreach ($file_list as $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
switch ($ext) {
case '':
break;
#case 'htaccess':
# system('httpd -t');
break;
case 'php': # Fall through.
case 'inc':
if (! php_lint($file, $message)) {
echo '> Hook aborting commit: php-lint '.PHP_EOL. $message.PHP_EOL;
exit (1);
}
break;
default:
echo "> Hook untested file: ";
break;
}
echo '> OK: php-lint '. $file.PHP_EOL;
}
echo '> Hook OK: pre-commit'.PHP_EOL;
exit (0);
function php_lint($file, &$message = null) {
$handle = popen(escapeshellcmd('php -l '.$file.' 2>&1'), 'r'); //2>&1
$php_res = fread($handle, 2096);
pclose($handle);
$message = explode("\n", $php_res);
$message = $message[1];
return FALSE === stripos($php_res, 'parse error');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment