我们使用Tortoise SVN进行源代码控制,已经设置了提交消息模板。
我还希望在用户提交时显示一些文本,这些文本不会包含在他们的提交消息中,例如“不要忘记做X!”
这是可能的吗?
我还希望在用户提交时显示一些文本,这些文本不会包含在他们的提交消息中,例如“不要忘记做X!”
这是可能的吗?
PATH MESSAGEFILE CWD
。MESSAGEFILE
是用于存储提交消息的临时文件的路径。您可以使用您的消息填充此临时文件 别忘了做X!或者,您可以在提交消息中添加前缀作为注释并被过滤掉。由于Git在提交消息中使用#
作为注释,所以我也这样做:每一行以#
开头的都会从提交消息中过滤掉。因此,我会写消息#别忘了做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
。提交前钩子的工作是过滤掉你在开始提交钩子中填写到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";}
}