Re: Odd results writing a Git pre-receive hook to syntax check PHP files.
- From
- Chris Patti <cpatti@gmail.com>
- Date
- Aug 31, 2010, 21:06 UTC
- Message-ID
- <AANLkTimiSBjifxtDoFXAEAEpYM8bJ18SwJ5Fj8zqh_G6@mail.gmail.com>
- In-Reply-To
- <4C7C020C.6090907@drmicha.warpmail.net>
On Mon, Aug 30, 2010 at 3:10 PM, Michael J Gruber <git@drmicha.warpmail.net> wrote:
Show 7 quoted lines
> > git diff-tree --root $newrev > > will take care of this. > > Michael >
Thank you sir! You are a scholar and a gentleman, and I sincerely appreciate the help.
Here is the completed hook for anyone who is curious, this turned into a more complex project than I thought it would be, but I learned a heck of a lot along the way so that's a good thing!
-Chris ---- #!/home/php/bin/php <?php
function parseHookInput() {
$fpstdin = fopen("php://stdin","r");$line = fgets($fpstdin);
list($old_sha1,$new_sha1,$refname)=explode(" ",$line); #echo "ns: $new_sha1\n";
#echo "os: $old_sha1\n";
#echo "refname: $refname\n";
return array($old_sha1,$new_sha1);
}function detectNewBranch($old_sha1,$new_sha1) {
if ($old_sha1 == "0000000000000000000000000000000000000000") {
return true;
}
else {
return false;
}
}function parseDiff($old_sha1,$new_sha1,$diffcmd,$regex) {
$diff = array(); exec($diffcmd,$diff,$diffrcval);
if ($diffrcval != 0) {
echo "Syntax checker hook is malfunctioning. Can't
execute git ls-tree. Failing gracefully and allowing this push.\n";
exit(0);
}
# If we can execute the git diff I'm assuming we have access
to a working git. foreach ($diff as &$diffline) {
preg_match($regex,$diffline,$matches);
$blob = $matches[1];
$filename = $matches[2];
#echo "Blob: $blob Filename: $filename\n";
SyntaxCheckFile($blob,$filename);
}
}function syntaxCheckFile($blob,$filename) {
$needle = '/(\.php|\.module|\.install)$/';
if (preg_match($needle,$filename)) {
#echo "Checking $filename\n";
$dummy = array();
exec("git show $blob|/home/php/bin/php -l",$dummy,$checkrcval);
if ($checkrcval != 0) {
echo "There was a syntax error in '$filename'.
Rejecting this attempted merge!\n";
exit(1);
}
}
}function parseCommit($old_sha1,$new_sha1) {
# if this is the first commit on a new branch, $old_sha1 will
be a bunch of zeroes, and so
# git diff --raw will fail, since there's no old ref to
compare against. So, we parse the
# results of git diff-tree -root=$new_sha1 instead to get the
blob and filename we'll need.
if (detectNewBranch($old_sha1,$new_sha1)) {
$diffcmd="git diff-tree --root $new_sha1";
$regex="/\:\w+ \w+ \w+ (\w+) \w (.+)/";
}
else {
$diffcmd="git diff --raw $old_sha1 $new_sha1";
$regex="/\:\d+ \d+ \w+... (\w+)... \w\t(.+)/";
}
parseDiff($old_sha1,$new_sha1,$diffcmd,$regex);
}# End function definitions. Main code body starts here.
# This pre-receive git hook gets passed the ref before the push, and the ref that would be # created if the push succeeds. list($old_sha1,$new_sha1) = parseHookInput();
parseCommit($old_sha1,$new_sha1);
exit(0);
-- Christopher Patti - Geek At Large | GTalk: cpatti@gmail.com | AIM: chrisfeohpatti | P: (260) 54PATTI "Technology challenges art, art inspires technology." - John Lasseter, Pixar