# pre-commit not working at server side push

7 messages from 2011-07-20 to 2011-07-20. Participants: J. Bakshi, Ilari Liusvaara.
Thread: https://gitlist.dev/t/27862

## J. Bakshi, 2011-07-20 06:43

Subject: pre-commit not working at server side push
Message-ID: <20110720121356.4dc21f6b@shiva.selfip.org>
URL: https://gitlist.dev/e/20110720121356.4dc21f6b%40shiva.selfip.org

```
Hello list,

I have this bash script to check php syntax error and prevent the commit if there is any

ˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋ`
#!/bin/sh

##php_syntax_check

  for i in $(git diff-index --name-only --cached HEAD -- | grep -E '\.(php|engine|theme|install|inc>
        if [ -f $i ]; then
                output=$(/usr/bin/php5 -l $i)
                if [ "$output" == "No syntax errors detected in $i" ];
                then
                        echo "PHP syntax check for $i: OK"
                else
                        echo "=====================================================================>
                        echo "Pause $i for the commit due to PHP parse errors:"
                        echo "$output"
                        exit 1
                fi
        fi
    done

ˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋ`

Running fine when placed it at local copy.

I have placed the hook at the server under hooks directory and provided the execute permission also

$ chmod a+x hooks/pre-commit

But even with wrong php syntax I am able to push the file. Have I missed something ?

Thanks

```

## J. Bakshi, 2011-07-20 07:15

Subject: Re: pre-commit not working at server side push
Message-ID: <20110720124538.482628e3@shiva.selfip.org>
URL: https://gitlist.dev/e/20110720124538.482628e3%40shiva.selfip.org
In-Reply-To: <20110720121356.4dc21f6b@shiva.selfip.org>

```
On Wed, 20 Jul 2011 12:13:56 +0530
"J. Bakshi" <joydeep@infoservices.in> wrote:

> Hello list,
> 
> I have this bash script to check php syntax error and prevent the commit if there is any
> 
> ˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋ`
> #!/bin/sh
> 
> ##php_syntax_check
> 
>   for i in $(git diff-index --name-only --cached HEAD -- | grep -E '\.(php|engine|theme|install|inc>
>         if [ -f $i ]; then
>                 output=$(/usr/bin/php5 -l $i)
>                 if [ "$output" == "No syntax errors detected in $i" ];
>                 then
>                         echo "PHP syntax check for $i: OK"
>                 else
>                         echo "=====================================================================>
>                         echo "Pause $i for the commit due to PHP parse errors:"
>                         echo "$output"
>                         exit 1
>                 fi
>         fi
>     done
> 
> ˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋ`
> 
> Running fine when placed it at local copy.
> 
> I have placed the hook at the server under hooks directory and provided the execute permission also
> 
> $ chmod a+x hooks/pre-commit
> 
> But even with wrong php syntax I am able to push the file. Have I missed something ?
> 
> Thanks

seems it should be pre-receive hook at the server. So done accordingly but still not working 

```

## Ilari Liusvaara, 2011-07-20 07:18

Subject: Re: pre-commit not working at server side push
Message-ID: <20110720071807.GA10104@LK-Perkele-VI.localdomain>
URL: https://gitlist.dev/e/20110720071807.GA10104%40LK-Perkele-VI.localdomain
In-Reply-To: <20110720121356.4dc21f6b@shiva.selfip.org>

```
On Wed, Jul 20, 2011 at 12:13:56PM +0530, J. Bakshi wrote:
> Hello list,
> 
> I have this bash script to check php syntax error and prevent the commit if there is any
 
<snip>
 
> Running fine when placed it at local copy.
> 
> I have placed the hook at the server under hooks directory and provided the execute permission also
> 
> $ chmod a+x hooks/pre-commit
> 
> But even with wrong php syntax I am able to push the file. Have I missed something ?

Pre-commit runs on commit, not on push. And push is not commit.

-Ilari

```

## J. Bakshi, 2011-07-20 07:21

Subject: Re: pre-commit not working at server side push
Message-ID: <20110720125124.3c746a1a@shiva.selfip.org>
URL: https://gitlist.dev/e/20110720125124.3c746a1a%40shiva.selfip.org
In-Reply-To: <20110720071807.GA10104@LK-Perkele-VI.localdomain>

```
On Wed, 20 Jul 2011 10:18:07 +0300
Ilari Liusvaara <ilari.liusvaara@elisanet.fi> wrote:

> On Wed, Jul 20, 2011 at 12:13:56PM +0530, J. Bakshi wrote:
> > Hello list,
> > 
> > I have this bash script to check php syntax error and prevent the commit if there is any
>  
> <snip>
>  
> > Running fine when placed it at local copy.
> > 
> > I have placed the hook at the server under hooks directory and provided the execute permission also
> > 
> > $ chmod a+x hooks/pre-commit
> > 
> > But even with wrong php syntax I am able to push the file. Have I missed something ?
> 
> Pre-commit runs on commit, not on push. And push is not commit.
> 
> -Ilari

Thanks, already figured it out, it would be pre-receive on server; but still not working.

```

## Ilari Liusvaara, 2011-07-20 07:30

Subject: Re: pre-commit not working at server side push
Message-ID: <20110720073017.GA10465@LK-Perkele-VI.localdomain>
URL: https://gitlist.dev/e/20110720073017.GA10465%40LK-Perkele-VI.localdomain
In-Reply-To: <20110720124538.482628e3@shiva.selfip.org>

```
On Wed, Jul 20, 2011 at 12:45:38PM +0530, J. Bakshi wrote:
> On Wed, 20 Jul 2011 12:13:56 +0530
> "J. Bakshi" <joydeep@infoservices.in> wrote:
> 
> > Hello list,
> > 
> > I have this bash script to check php syntax error and prevent the commit if there is any
> > 
> > ˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋ`
> > #!/bin/sh
> > 
> > ##php_syntax_check
> > 
> >   for i in $(git diff-index --name-only --cached HEAD -- | grep -E '\.(php|engine|theme|install|inc>
> >         if [ -f $i ]; then
> >                 output=$(/usr/bin/php5 -l $i)
> >                 if [ "$output" == "No syntax errors detected in $i" ];
> >                 then
> >                         echo "PHP syntax check for $i: OK"
> >                 else
> >                         echo "=====================================================================>
> >                         echo "Pause $i for the commit due to PHP parse errors:"
> >                         echo "$output"
> >                         exit 1
> >                 fi
> >         fi
> >     done
> > 
> > ˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋ`

> seems it should be pre-receive hook at the server. So done accordingly but still not working 

Pre-receive runs after objects have been received but before branches are updated. So
it can inspect the newly arrived commits but the branches retain their old values.

- Firstly, the server does not have index, so diff-index does not do anything sane.
- Secondly, HEAD does not point to anything newly arrived (the hook gets list of update 
instructions via stdin).
- Thirdly, this hook must be prepared for multiple commits in multiple branches appearing at
once.

I think githooks(5) documents what these hooks receive and what they do.

-Ilari

```

## J. Bakshi, 2011-07-20 07:45

Subject: Re: pre-commit not working at server side push
Message-ID: <20110720131533.7bbba0c5@shiva.selfip.org>
URL: https://gitlist.dev/e/20110720131533.7bbba0c5%40shiva.selfip.org
In-Reply-To: <20110720073017.GA10465@LK-Perkele-VI.localdomain>

```
On Wed, 20 Jul 2011 10:30:17 +0300
Ilari Liusvaara <ilari.liusvaara@elisanet.fi> wrote:

> On Wed, Jul 20, 2011 at 12:45:38PM +0530, J. Bakshi wrote:
> > On Wed, 20 Jul 2011 12:13:56 +0530
> > "J. Bakshi" <joydeep@infoservices.in> wrote:
> > 
> > > Hello list,
> > > 
> > > I have this bash script to check php syntax error and prevent the commit if there is any
> > > 
> > > ˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋ`
> > > #!/bin/sh
> > > 
> > > ##php_syntax_check
> > > 
> > >   for i in $(git diff-index --name-only --cached HEAD -- | grep -E '\.(php|engine|theme|install|inc>
> > >         if [ -f $i ]; then
> > >                 output=$(/usr/bin/php5 -l $i)
> > >                 if [ "$output" == "No syntax errors detected in $i" ];
> > >                 then
> > >                         echo "PHP syntax check for $i: OK"
> > >                 else
> > >                         echo "=====================================================================>
> > >                         echo "Pause $i for the commit due to PHP parse errors:"
> > >                         echo "$output"
> > >                         exit 1
> > >                 fi
> > >         fi
> > >     done
> > > 
> > > ˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋ`
> 
> > seems it should be pre-receive hook at the server. So done accordingly but still not working 
> 
> Pre-receive runs after objects have been received but before branches are updated. So
> it can inspect the newly arrived commits but the branches retain their old values.
> 
> - Firstly, the server does not have index, so diff-index does not do anything sane.
> - Secondly, HEAD does not point to anything newly arrived (the hook gets list of update 
> instructions via stdin).
> - Thirdly, this hook must be prepared for multiple commits in multiple branches appearing at
> once.
> 
> I think githooks(5) documents what these hooks receive and what they do.
> 
> -Ilari

Thanks for your clarification. As a newbie I am interested to know any such pre-receive hook which can check php syntax before php.

```

## J. Bakshi, 2011-07-20 09:03

Subject: Re: pre-commit not working at server side push
Message-ID: <201107200903.p6K93q7A019900@dcnode-02.unlimitedmail.net>
URL: https://gitlist.dev/e/201107200903.p6K93q7A019900%40dcnode-02.unlimitedmail.net
In-Reply-To: <20110720131533.7bbba0c5@shiva.selfip.org>

```
On Wed, 20 Jul 2011 13:15:33 +0530
"J. Bakshi" <joydeep@infoservices.in> wrote:

> On Wed, 20 Jul 2011 10:30:17 +0300
> Ilari Liusvaara <ilari.liusvaara@elisanet.fi> wrote:
> 
> > On Wed, Jul 20, 2011 at 12:45:38PM +0530, J. Bakshi wrote:
> > > On Wed, 20 Jul 2011 12:13:56 +0530
> > > "J. Bakshi" <joydeep@infoservices.in> wrote:
> > > 
> > > > Hello list,
> > > > 
> > > > I have this bash script to check php syntax error and prevent the commit if there is any
> > > > 
> > > > ˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋ`
> > > > #!/bin/sh
> > > > 
> > > > ##php_syntax_check
> > > > 
> > > >   for i in $(git diff-index --name-only --cached HEAD -- | grep -E '\.(php|engine|theme|install|inc>
> > > >         if [ -f $i ]; then
> > > >                 output=$(/usr/bin/php5 -l $i)
> > > >                 if [ "$output" == "No syntax errors detected in $i" ];
> > > >                 then
> > > >                         echo "PHP syntax check for $i: OK"
> > > >                 else
> > > >                         echo "=====================================================================>
> > > >                         echo "Pause $i for the commit due to PHP parse errors:"
> > > >                         echo "$output"
> > > >                         exit 1
> > > >                 fi
> > > >         fi
> > > >     done
> > > > 
> > > > ˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋ`
> > 
> > > seems it should be pre-receive hook at the server. So done accordingly but still not working 
> > 
> > Pre-receive runs after objects have been received but before branches are updated. So
> > it can inspect the newly arrived commits but the branches retain their old values.
> > 
> > - Firstly, the server does not have index, so diff-index does not do anything sane.
> > - Secondly, HEAD does not point to anything newly arrived (the hook gets list of update 
> > instructions via stdin).
> > - Thirdly, this hook must be prepared for multiple commits in multiple branches appearing at
> > once.
> > 
> > I think githooks(5) documents what these hooks receive and what they do.
> > 
> > -Ilari
> 
> Thanks for your clarification. As a newbie I am interested to know any such pre-receive hook which can check php syntax before php.

The script given at the link below is working fine

http://git.661346.n2.nabble.com/Odd-results-writing-a-Git-pre-receive-hook-to-syntax-check-PHP-files-td5471120.html

But it doesn't show where the error is. The part actually checking the error is

ˋˋˋˋˋˋˋˋˋ`
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); 
                } 
        } 
} 
ˋˋˋˋˋˋˋˋˋ`

can we modify it so that it shows the error too ?

PS: I am not a php guy

```
