【发布时间】:2011-06-10 10:57:27
【问题描述】:
我们使用 Tortoise SVN 进行源代码控制,并且已经设置了提交消息模板。
我还想在用户提交时向他们显示一些文本,这些文本不会包含在他们的提交消息中,类似于“不要忘记做 X!”。
这可能吗?
【问题讨论】:
标签: svn tortoisesvn commit
我们使用 Tortoise SVN 进行源代码控制,并且已经设置了提交消息模板。
我还想在用户提交时向他们显示一些文本,这些文本不会包含在他们的提交消息中,类似于“不要忘记做 X!”。
这可能吗?
【问题讨论】:
标签: svn tortoisesvn commit
我已经使用Tortoise Docs 设置了一个类似的环境,并且可以说:是的,它是! 操作涉及一个 Start-Commit Hook,它填充用户应该阅读的行和一个再次删除行的 Pre-Commit Hook:
开始提交挂钩
这个钩子传递了三个参数:PATH MESSAGEFILE CWD。 MESSAGEFILE 是用于存储提交消息的临时文件的路径。你可以用你的消息填充这个临时文件别忘了做 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。 pre-commit 钩子的工作是过滤掉你在 start-commit 钩子中填写到MESSAGEFILE 的消息(否则它将作为提交消息的一部分发送到服务器,这可能不是你的想)。要么只删除你的消息别忘了做 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 钩子和一次作为 pre-commit 钩子。要调用的命令行是perl /path/to/script。还要检查Wait for the script to finish 和Hide script while running。
注意
如果您需要传递给钩子的更多信息,您还可以在 TortoiseSVN 的设置中分配钩子时传递自定义参数。如果您分配自定义参数,这些参数会在传递默认参数(如docs 中所述)之前传递给钩子。
【讨论】:
请参阅关于client side hook scripts 的 TortoiseSVN 文档。
【讨论】: