git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
CPChris 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
Previous: Michael J GruberNext: Junio C Hamano
Message 17 of 20 in “Odd results writing a Git pre-receive hook to syntax check PHP files.”
  1. Chris PattiAug 27, 2010
  2. Michael J GruberAug 30, 2010
  3. Chris PattiAug 30, 2010
  4. Jonathan NiederAug 30, 2010
  5. Chris PattiAug 30, 2010
  6. Example git hooks (Re: Odd results writing a Git pre-receive hook to syntax check PHP files.)Jonathan Nieder, Aug 30, 2010
  7. Todd ZullingerAug 31, 2010
  8. Chris PattiAug 31, 2010
  9. Chris PattiAug 31, 2010
  10. Michael J GruberAug 30, 2010
  11. Chris PattiAug 30, 2010
  12. Chris PattiAug 30, 2010
  13. Sverre RabbelierAug 30, 2010
  14. Jonathan NiederAug 30, 2010
  15. Chris PattiAug 30, 2010
  16. Michael J GruberAug 30, 2010
  17. Chris PattiAug 31, 2010
  18. Junio C HamanoAug 31, 2010
  19. Chris PattiSep 1, 2010
  20. Chris PattiSep 2, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.