Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the commited file from a Subversion pre-commit hook in Perl?

I need to do the following:

  1. Write pre-commit hook in Perl

  2. Hook should check all files being committed for presence of some text, and fail if that text is not found

Basically, I need an example of Perl hook that reads files being committed.

I am really looking for some elegant solution with the least amount of code.

Notes: Hook should use svnlook or other better way to find files.

like image 752
Chicago Avatar asked Nov 06 '09 17:11

Chicago


1 Answers

pre-commit hook:

my $repos = shift;
my $txn = shift;

foreach my $line (`$svnlook changed -t $txn "$repos"`)
{
  chomp($line);
  if ($line !~ /^([AUD_]).\s\s(.+)$/)
  {
    print STDERR "Can't parse [$line].\n";
    exit(1);
  }
  else
  {
    my $action = $1;
    my $file = $2;
    chomp($file);
    #If path has trailing slash, then it is a folder and we want to skip folders
    if($file =~ /\/$/)
    {
    next;
    }
    my $fileContent = `$svnlook cat -t $txn "$repos" "$file"`;
    if ($action =~ /[AU]/)
    {
       my @lines = split(/\n/, $fileContent );
       #Check for whatever you need in this file's content

    }
  }
}
like image 68
Chicago Avatar answered Nov 18 '22 17:11

Chicago