在Tortoise SVN中向用户显示预先提交的消息

时间:2011-01-17 13:14:24

标签: svn tortoisesvn commit

我们使用Tortoise SVN进行源代码管理,并且已经设置了提交消息模板。

我还希望在用户提交时向用户显示一些文本,这些文本不会包含在他们的提交消息中,按照“别忘了做X!”的方式。

这可能吗?

2 个答案:

答案 0 :(得分:3)

我使用Tortoise Docs设置了一个类似的环境,可以说:是的,它是!操作涉及一个Start-Commit Hook,用于填写用户应阅读的行和一个Pre-Commit Hook再次删除你的行:

Start-Commit Hook
这个钩子传递了三个参数:PATH MESSAGEFILE CWDMESSAGEFILE是将用于存储提交消息的临时文件的路径。您可以使用您的消息填写此临时文件不要忘记执行X!或者,在消息前面添加一些您将在提交消息中视为注释并被过滤掉的消息。由于Git在提交消息中使用#作为注释,因此我做了同样的事情:以#开头的每一行都从提交消息中过滤掉。因此我写了消息# Don't forget to do X!。 Perl中的示例实现(未经测试):

use strict;                         # what we always have
use warnings;                       # what we always have
use Fcntl ':flock';                 # lock files when writing
use Carp;                           # use croak instead of die
use English qw( -no_match_vars );   # words instad of cryptic variables

sub startcommit_hook{
  # open the logfile
  my $logfilename       = $ARGV[1];
  # write hint line about supported tags
  open my $handle, '>:utf8', $logfilename
    or croak "Opening $logfilename for writing failed\n";
  flock $handle, LOCK_EX;
    print {$handle} "# Don't forget to do X!\n";
  flock $handle, LOCK_UN;
  return close $handle or croak "unable to close $OS_ERROR";
}

startcommit_hook();

预先提交挂钩
这个钩子传递了四个参数:PATH DEPTH MESSAGEFILE CWD。预提交钩子的工作是过滤掉你在start-commit钩子中填入MESSAGEFILE in的消息(否则它将作为提交消息的一部分进入服务器,这可能不是你想要什么)。要么只是删除你的消息不要忘记做X!或 - 如果你使用上面写的评论方法 - 删除以#符号开头的每一行(或匹配模式^\s*#),因为它是我们世界的评论。

我们可以扩展start-commit钩子的文件来处理预提交的东西,因为参数的数量是不同的。决定调用哪个钩子由传递给脚本的参数计数(也是未经测试的)组成:

use strict;                         # what we always have
use warnings;                       # what we always have
use feature 'switch';               # for given-when construct
use Fcntl ':flock';                 # lock files when writing
use Carp;                           # use croak instead of die
use English qw( -no_match_vars );   # words instad of cryptic variables

sub startcommit_hook{
  # open the logfile
  my $logfilename       = $ARGV[1];
  # write hint line about supported tags
  open my $handle, '>:utf8', $logfilename
    or croak "Opening $logfilename for writing failed\n";
  flock $handle, LOCK_EX;
    print {$handle} "# Don't forget to do X!\n";
  flock $handle, LOCK_UN;
  return close $handle or croak "unable to close $OS_ERROR";
}

sub precommit_hook{
  my $logfilename       = $ARGV[2];
  # first, read the logfile
  open my $handle,'<:utf8',$logfilename or croak "Error reading file contents of $logfilename: $OS_ERROR\n";
  my @content = <$handle>;
  close $handle or croak "unable to close: $OS_ERROR";
  chomp @content;

  # now, write it, ignoring the comment lines
  open my $handle, '>:utf8', $logfilename
    or croak "Opening $logfilename for writing failed\n";
  flock $handle, LOCK_EX;

  foreach my $line(@content){
    if($line !~ /^\s*#/){   # line has no comment, print it.
      print {$handle} $line . "\n";
    }
  }

  flock $handle, LOCK_UN;
  close $handle or croak "unable to close $OS_ERROR";
  return;
}

given($#ARGV){
  when (3){startcommit_hook();}
  when (4)   {precommit_hook();}  # no user supplied -> auto lookup
  default  {croak "Invalid number of parameters";}
}

要激活挂钩,请打开TortoiseSVN的设置,转到hook scripts并将脚本添加一次作为start-commit挂钩,一次添加为预提交挂钩。要调用的命令行为perl /path/to/script。另外,请检查Wait for the script to finishHide script while running

注意
如果您需要传递给钩子的更多信息,您还可以在TortoiseSVN的设置中分配钩子时传递自定义参数。如果您指定自定义参数,这些参数将被传递到之前的默认参数(如docs中所述)传递。

答案 1 :(得分:0)

请参阅有关client side hook scripts的TortoiseSVN文档。