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

只谈情不闲聊 提交于 2019-12-07 05:04:31

问题


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.


回答1:


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

    }
  }
}



回答2:


It sounds like you've got the foundation figured out already:

  • get list of all files being committed
  • search each one of them in turn for particular text
  • if text is found, reject commit

You'll find some information on writing pre-commit hooks in the manual.




回答3:


It should not be too difficult to modify this example in Python to do what you want. See also the hooks subdirectory of your repository for some templates and hook scripts and contributed hook scripts.



来源:https://stackoverflow.com/questions/1689052/how-can-i-access-the-commited-file-from-a-subversion-pre-commit-hook-in-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!