Skip to content

Instantly share code, notes, and snippets.

@spoonerWeb
Created August 11, 2016 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spoonerWeb/9374953fc9a610f4762a477163c60f76 to your computer and use it in GitHub Desktop.
Save spoonerWeb/9374953fc9a610f4762a477163c60f76 to your computer and use it in GitHub Desktop.
Pre-Receive Hook (php-lint)
#!/usr/bin/php
<?php
$ref = trim($argv[2]);
$diff = array();
$return = 0;
exec("git diff --name-only $argv[1] $ref 2> /dev/null", $diff, $return);
if ($return > 0) {
echo "Could not run git diff\n\n";
exit(1);
}
$filename_pattern = '/\.php$/';
foreach ($diff as $file) {
if (!preg_match($filename_pattern, $file)) {
continue;
}
$tree = array();
$return = 0;
exec("git ls-tree $ref $file 2> /dev/null", $tree, $return);
if ($return > 0 || empty($tree)) {
echo "Deleted file '$file'! skipping test...\n\n";
continue;
}
echo "\nRunning php linter on '$file'...\n";
$tree = preg_split('/\s/', $tree[0]);
$fileContents = array();
exec("git cat-file $tree[1] $tree[2] 2> /dev/null", $fileContents, $return);
if ($return > 0) {
echo "Could not run git cat-file\n\n";
exit(1);
}
$fileContents = implode("\n", $fileContents);
$pipes = array();
$proc = proc_open('php -l',
array(0 => array('pipe', 'r'),
1 => array('pipe', 'w')),
$pipes);
if (!is_resource($proc)) {
echo "Could not creater php linter process\n\n";
exit(1);
}
fwrite($pipes[0], $fileContents);
fclose($pipes[0]);
fclose($pipes[1]);
$resultCode = proc_close($proc);
if ($resultCode != 0) {
echo "Error parsing file '$file'\n\n";
exit($resultCode);
}
print "'$file' PASSED linter test..\n\n";
}
echo "No errors detected\n\n";
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment