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

Fio*_*ite 5 svn tortoisesvn commit

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

我还想在用户提交时向用户显示一些文本,这些文本没有包含在他们的提交消息中,沿着"别忘了做X!"的方式.

这可能吗?

eck*_*kes 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();
Run Code Online (Sandbox Code Playgroud)

预提交挂钩
该挂钩传递四个参数:PATH DEPTH MESSAGEFILE CWD。预提交挂钩的工作是过滤掉您MESSAGEFILE在开始提交挂钩中填写的消息(否则它将作为提交消息的一部分发送到服务器,这可能不是您想要的)。要么删除你的消息,别忘了做X!或者 - 如果您使用我上面写的注释方法 - 删除以符号开头#(或与模式匹配^\s*#)的每一行,因为它是我们世界中的注释。

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

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";}
}
Run Code Online (Sandbox Code Playgroud)

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

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