向在Tortoise SVN中预先提交的用户显示消息 [英] Displaying a message to users pre-commit in Tortoise SVN

查看:176
本文介绍了向在Tortoise SVN中预先提交的用户显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We use Tortoise SVN for source control, and have already set up a commit message template.

我还希望在用户提交内容时向他们显示一些文本,则不会在提交消息中包含不要忘记做X!的字样。

I would also like to display some text to the user when they commit, that doesn't get included in their commit message, along the lines of "Don't forget to do X!".

这可能吗?

推荐答案

我已经使用草龟文档并可以说:是的!该操作涉及一个Start-Commit挂钩。会填充用户应阅读的行,并使用Pre-Commit Hook再次删除您的行:

I have set up a similar environment using the Tortoise Docs and can say: Yes, it is! Operation involves a Start-Commit Hook that fills in the lines that the user shall read and a Pre-Commit Hook that removes thee lines again:

Start-Commit Hook

该钩子传递了三个参数: PATH MESSAGEFILE CWD MESSAGEFILE 是用于存储提交消息的临时文件的路径。您可以在邮件中添加别忘了做X!这个临时文件,或者在邮件的前面加上一些内容,这些内容将在提交邮件中作为注释并被过滤掉。由于Git在提交消息中使用作为注释,所以我也做了同样的事情:以开头的每一行都被过滤了。提交消息之外。因此,我会写消息#不要忘记做X!。在Perl中的示例实现(未试用):

Start-Commit Hook
This hook gets passed three parameters: PATH MESSAGEFILE CWD. MESSAGEFILE is the path to a temporary file that will be used for storing the commit message. You can fill this temporary file with your message Don't forget to do X! Or, you prefix your message with something that you will treat as comment in the commit message and gets filtered out. Since Git uses # as comment in the commit message, I did the same: every line that starts with # gets filtered out of the commit message. And therefore I'd write the message # Don't forget to do X!. Sample implementation in Perl (untested):

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 *#匹配的行),因为它是在我们的世界中发表评论。

Pre-Commit Hook
This hook gets passed four parameters: PATH DEPTH MESSAGEFILE CWD. The job of the pre-commit hook is to filter out the message that you filled into MESSAGEFILE in in the start-commit hook (otherwise it will go as part of the commit message to the server and this probably isn't what you want). Either just delete your message Don't forget to do X! or – if you use the comment approach as I wrote above – delete every line that starts with a # sign (or that matches the pattern ^\s*#) since it's a comment in our world.

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

We could extend our file for the start-commit hook to handle also the pre-commit stuff since the number of parameters is different. The decision on which hook to call is made up of the parameter count passed to the script (also untested):

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的设置,转到挂钩脚本,然后将脚本添加为一次启动提交挂钩和一次预提交挂钩。调用的命令行为 perl / path / to / script 。还要检查等待脚本完成运行时隐藏脚本

To activate the hooks, open the settings of TortoiseSVN, go to hook scripts and add the script once as start-commit hook and once as pre-commit hook. The command line to call would be perl /path/to/script. And also check Wait for the script to finish and Hide script while running.

注意

如果需要将更多信息传递给挂钩,则在TortoiseSVN设置中分配挂钩时,也可以传递自定义参数。如果您分配自定义参数,则会在默认参数之前 传递给钩子(如文档)获得通过。

Note
If you need further information passed to the hooks, you could also pass custom parameters when you assign the hooks in the settings of TortoiseSVN. If you assign custom parameters, these get passed to the hook before the default parameters (as stated in the docs) get passed.

这篇关于向在Tortoise SVN中预先提交的用户显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆