You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@subversion.apache.org by Marcos Chaves <ma...@gmail.com> on 2006/09/14 18:32:54 UTC

Pre-commit hook to verify Perl syntax

Hi. I couldn't find this in the archives.

I'm trying to create a pre-commit hook that only allows the commit of
Perl codes that pass the "perl -c" check. I have a initial version
(see below) that does exactly what I want IF the Perl code does not
use any customized module (.pm), because Perl can't find it when run
from the hook script.

Does anybody know how this could be done? I'm afraid that the hook
script would need to perform more tasks, like checking out the current
version of the path where the modules are located and then trying to
run "perl -c". Any ideas?

Thanks in advance,

-Marcos

- - - - - 8< - - - - -

#!/usr/bin/env perl -w

#
# pre-commit hook for Subversion
# deny commit of .pl and .pm Perl scripts if they don't pass the syntax check.
#
# under Windows, the contents of 'pre-commit.bat' should be:
#   ...(repo path)...\hooks\pre-commit.pl %1 %2
#

$repos = shift;
$txn   = shift;

# list files changed on this transaction
foreach $line (`svnlook changed -t $txn \"$repos\"`)
{
  chomp($line);
  if ($line !~ /^([AUD]).\s\s(.+)$/)
  {
    print STDERR "Can't parse [$line].\n";
    exit(-1);
  }
  else
  {
    $action = $1;
    $file   = $2;

    # check syntax of new or modified .pl and .pm Perl files with 'perl -c'
    if (($file =~ /\.p[lm]$/) && ($action =~ /[AU]/))
    {
      system("svnlook cat -t $txn \"$repos\" \"$file\" | perl -c");
      if ($? != 0)
      {
        print STDERR "\n$file does not pass the syntax check.";
        exit(-1);
      }
    }
  }
}

exit(0);

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Pre-commit hook to verify Perl syntax + build test

Posted by Erik Huelsmann <eh...@gmail.com>.
On 9/18/06, Peter Werner <l....@vasas.no-ip.org> wrote:
> > after checking out the code, you either allow the commit or disallow
> > it.
>
> No, not in the post-commit.  You can do it in the pre-commit.
>
>
> BTW, is there anybody here who made a pre-commit check that tries to
> run a build (on trunk if it was changed) and reject the commit if build
> breaks?  (I'm just about to write one.)

No, generally, commits are accepted and a continuous integration
process tests validity. If the build gets broken, the admin operates a
flocking device on the dev who committed that revision.

Seriously: why be so strict as to validate every commit a prio? You
can always roll back to the last working revision: you're using a VCS
:-)

> The code "checkout" (svnlook cat)  part is the similar to perl syntax
> checking.

bye,

Erik.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Pre-commit hook to verify Perl syntax + build test

Posted by allan <al...@muly.dk>.
Peter Werner wrote:
>> after checking out the code, you either allow the commit or disallow
>> it.
> 
> No, not in the post-commit.  You can do it in the pre-commit.

just to clarify. i was speaking in a pre-commit context.

> 
> BTW, is there anybody here who made a pre-commit check that tries to
> run a build (on trunk if it was changed) and reject the commit if build
> breaks?  (I'm just about to write one.)

we tried at one moment, but decided it wan not practical, so decided on 
a post-commit check instead. when people get a email notification with 
the exact error message they are generally quick to fix it themselves.

you could set up a build server that would build on every relevant 
commit/changeset and spit out some status. i guess mileage will wary

./a

> The code "checkout" (svnlook cat)  part is the similar to perl syntax
> checking.
> 
>   WP
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
> For additional commands, e-mail: users-help@subversion.tigris.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Pre-commit hook to verify Perl syntax + build test

Posted by Peter Werner <l....@vasas.no-ip.org>.
> after checking out the code, you either allow the commit or disallow
> it.

No, not in the post-commit.  You can do it in the pre-commit.


BTW, is there anybody here who made a pre-commit check that tries to
run a build (on trunk if it was changed) and reject the commit if build
breaks?  (I'm just about to write one.)

The code "checkout" (svnlook cat)  part is the similar to perl syntax
checking.

  WP

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Pre-commit hook to verify Perl syntax

Posted by allan juul <al...@muly.dk>.
Marcos Chaves wrote:
> Hi Allan.
> 
>>> I'm trying to create a pre-commit hook that only allows the commit of
> 
>> we do it in a post-commit.
> 
> But what do you do if the check that you perform fails? A transaction
> that is invalid by your criteria was just commited.
> 
> I think that you decided for the post-commit because then you can
> easily extract the new directory structure just commited to perform
> a check that can handle the file dependencies. I'll try to do
> something similar on the pre-commit phase, because I want to block
> the commit if the transaction is invalid based on my criteria.
> 
> Thanks all for the ideas.
> 
> -Marcos
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
> For additional commands, e-mail: users-help@subversion.tigris.org
> 
> 

you *can* export/checkout/extract the code you are trying to comit and 
then do whatever checking you want, right?

after checking out the code, you either allow the commit or disallow it.

the only difference in your and our case is that you check out in the 
transaction phase rather than the revision phase.

./allan

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Pre-commit hook to verify Perl syntax

Posted by Marcos Chaves <ma...@gmail.com>.
Hi Allan.

>> I'm trying to create a pre-commit hook that only allows the commit of

> we do it in a post-commit.

But what do you do if the check that you perform fails? A transaction
that is invalid by your criteria was just commited.

I think that you decided for the post-commit because then you can
easily extract the new directory structure just commited to perform
a check that can handle the file dependencies. I'll try to do
something similar on the pre-commit phase, because I want to block
the commit if the transaction is invalid based on my criteria.

Thanks all for the ideas.

-Marcos

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Pre-commit hook to verify Perl syntax

Posted by allan <al...@muly.dk>.
Marcos Chaves wrote:
> Hi. I couldn't find this in the archives.
> 
> I'm trying to create a pre-commit hook that only allows the commit of
> Perl codes that pass the "perl -c" check. I have a initial version
> (see below) that does exactly what I want IF the Perl code does not
> use any customized module (.pm), because Perl can't find it when run
> from the hook script.
> 
> Does anybody know how this could be done? I'm afraid that the hook
> script would need to perform more tasks, like checking out the current
> version of the path where the modules are located and then trying to
> run "perl -c". Any ideas?
> 
> Thanks in advance,
> 
> -Marcos

we do it in a post-commit.
basically it first checks the paths being comitted and if meeting the 
criteria for doing a compile check at all, it will export the files from 
a logic directory base (see example below).
so if you were commiting perlproject1/folder1/file1.pl, then the hook 
would export everything below perlproject1 and then run "perl -c 
perlproject1/main.pl"

perlproject1/main.pl
perlproject1/folder1/file1.pl
perlproject1/folder1/file2.pl
perlproject1/folder1/file1.pm
perlproject2/folder1/file1.pl

hth
./allan



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Pre-commit hook to verify Perl syntax

Posted by Peter Werner <l....@vasas.no-ip.org>.
> > Filter between svnlook cat and perl -c that removes module
> > references?
> 
> But that would break the code too. I think it would be tricky to not
> breat it, unless there's something I didn't see. Did you think about
> something special to do it without breaking the code?

It breaks the code but it's not a problem.  The question is if it breaks
the syntax.  Sometimes it does and sometimes it's not so easy to filter
out modules.  However you can make a rule how to use modules and perhaps
your code allows this easy way.  E.g. the following example passes
syntax check, just cause runtime error:

#filtered#use Math::Complex;
my $z = Math::Complex->make(4, 2);

Modules are problems not only for your syntax checks so a more complex
solution would be a policy, that every new module must be listed in a
file and installed on the svn server before it can be used.  That
external dependency list could be very helpful in the future.

  WP

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Pre-commit hook to verify Perl syntax

Posted by Marcos Chaves <ma...@gmail.com>.
Hi Peter. Thanks for the quick reply.

> Filter between svnlook cat and perl -c that removes module
references?

But that would break the code too. I think it would be tricky to not
breat it, unless there's something I didn't see. Did you think about
something special to do it without breaking the code?

I wonder if somebody already tried that before.

Thanks again,

-Marcos

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org

Re: Pre-commit hook to verify Perl syntax

Posted by Peter Werner <l....@vasas.no-ip.org>.
> run "perl -c". Any ideas?

Filter between svnlook cat and perl -c that removes module references?

  WP

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@subversion.tigris.org
For additional commands, e-mail: users-help@subversion.tigris.org