Re: Odd results writing a Git pre-receive hook to syntax check PHP files.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Aug 30, 2010 at 3:10 PM, Michael J Gruber
<git@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> 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@xxxxxxxxx | AIM:
chrisfeohpatti | P: (260) 54PATTI
"Technology challenges art, art inspires technology." - John Lasseter, Pixar
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]