在 Tortoise SVN 中向用户预提交显示一条消息

发布于 2024-10-12 03:01:28 字数 121 浏览 1 评论 0原文

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

我还想在用户提交时向他们显示一些文本,这些文本不会包含在他们的提交消息中,类似于“不要忘记做 X!”。

这可能吗?

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

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!".

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩一世无双 2024-10-19 03:01:28

我使用 设置了类似的环境Tortoise Docs 可以说:是的,是的! 操作涉及一个 Start-Commit Hook(填充用户应读取的行)和一个 Pre-Commit Hook(再次删除这些行) :

启动提交挂钩
该钩子传递三个参数:PATH MESSAGEFILE CWDMESSAGEFILE 是将用于存储提交消息的临时文件的路径。您可以使用您的消息填充此临时文件不要忘记执行 X! 或者,您可以在消息前面添加一些您将在提交消息中视为注释并被过滤掉的内容。由于 Git 在提交消息中使用 # 作为注释,我也做了同样的事情:以 # 开头的每一行都会从提交消息中过滤掉。因此我会写下消息# Don't忘记做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();

Pre-Commit 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";}
}

要激活钩子,请打开 TortoiseSVN 的设置,转到钩子脚本并添加脚本一次作为开始提交挂钩和一次作为预提交挂钩。要调用的命令行是perl /path/to/script。还要选中等待脚本完成运行时隐藏脚本

注意
如果您需要向挂钩传递更多信息,您还可以在 TortoiseSVN 设置中分配挂钩时传递自定义参数。如果您分配自定义参数,这些参数将默认参数之前传递到挂钩(如 docs)获得通过。

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
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();

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.

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";}
}

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.

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.

不一样的天空 2024-10-19 03:01:28

See the TortoiseSVN documentation about client side hook scripts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文