Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git Perl——提交消息钩子不';退出1时无法停止提交_Git_Perl_Githooks_Git Commit - Fatal编程技术网

Git Perl——提交消息钩子不';退出1时无法停止提交

Git Perl——提交消息钩子不';退出1时无法停止提交,git,perl,githooks,git-commit,Git,Perl,Githooks,Git Commit,上下文: 我使用一些git钩子在Perl脚本上自动应用一些格式化选项。在预提交钩子上,我使用perltidy清理并重新格式化脚本。我想检查用户在提交消息上放了什么,如果它是空的或等同于“abort”,我们想阻止提交并撤消格式修改 问题: 我删除了git钩子的.sample扩展,并使用chmod u+x.git/hooks/commit msg使其可执行,但是当我的脚本exit 1时,提交并没有像它应该的那样停止 提交消息 这个钩子由git提交[1]和git合并[1]调用,可以使用--no-ver

上下文:

我使用一些git钩子在Perl脚本上自动应用一些格式化选项。在预提交钩子上,我使用perltidy清理并重新格式化脚本。我想检查用户在提交消息上放了什么,如果它是空的或等同于“abort”,我们想阻止提交并撤消格式修改

问题:

我删除了git钩子的.sample扩展,并使用
chmod u+x.git/hooks/commit msg使其可执行,但是当我的脚本
exit 1
时,提交并没有像它应该的那样停止

提交消息

这个钩子由git提交[1]和git合并[1]调用,可以使用--no-verify选项绕过。它只接受一个参数,即保存建议的提交日志消息的文件名。以非零状态退出会导致命令中止

资料来源:

#/usr/bin/perl-w
严格使用;
使用警告;
#获取包含提交消息的文件的路径
我的$commit_文件=$ARGV[0];
#读取文件并提取提交消息(不以#开头的行)
我的@commit_msg;

打开(我的$fh,"当我第一次安装你的钩子时,我无法让它提交任何东西,但那是因为钩子有太多的负面影响。你的语言老师关于避免双重负面影响的警告也会在编写软件时帮助你。钩子试图通过测试消极意义上的一行是否好看来寻找有效条件,如果是,则将
$boolean
设置为1,但仅当
$boolean
为0时,才将
退出0
(表示成功)

非描述性名称
$boolean
可能是部分原因。在设置该名称和要生成的退出状态之间,您可能失去了预期含义。此外,只要提交消息的最后一行有效,逻辑背后的意图就会失败

下面的代码以git 2.17.1所需的方式运行

!/usr/bin/perl-w
严格使用;
使用警告;
die“用法:$0提交日志消息\n”,除非@ARGV==1;#(1)
#获取包含提交消息的文件的路径
我的$commit_file=shift;#(2)
#读取文件并提取提交消息(不以#开头的行)
我的$commit_msg=“”;

打开我的$fh,“我认为脚本中的逻辑是相反的,即
abort
这个词不会导致退出代码1

我认为以下措施应该有效:

!/usr/bin/perl
严格使用;
使用警告;
使用自动模具;
我的($commit\u msg\u文件)=@ARGV
或死“用法:$0\n”;

打开(my$fh,'你确定选项
--no verify
未启用吗?100%确定。我只需调用:
git commit
,然后输入我的提交消息。你确定它提交了任何内容吗?对我来说,它总是中止提交,因为每一行至少包含最后一个换行符,所以它从不“中止”或“.Why
git status
git log-p
怎么说?逻辑不是颠倒了吗?“abort”不会导致提交被阻止…您好!谢谢您的回答!我想了解更多信息:1.为什么在这里使用shift比ARGV更可取?(2)@Antoine它可以说是更为惯用或优雅的Perl,我们倾向于对使用
$array[$i]
的单个数组索引或使用
substr
的字符串索引进行索引有一点偏见。编写它的另一种方法是
my($commit\u file)=@ARGV;
。请注意,
我的
列表周围的括号很重要,因为它们传递列表上下文,这样,
$commit\u file
将获取
@ARGV
中第一个元素的值。
#!/usr/bin/perl -w

use strict;
use warnings;

# Get the path to the files in which we have the commit message
my $commit_file = $ARGV[0];

# Read the file and extract the commit message (lines which don't start with #) 
my @commit_msg;
open(my $fh, "<", "$commit_file");
while (my $line = <$fh>) {
    if (substr($line, 0, 1) ne "#") {
        push(@commit_msg, $line);
    }
}

# Check the message isn't empty or we don't have a "abort" line
my $boolean = 0;
foreach my $line (@commit_msg) {
    if ($line ne "abort" && $line ne "") {
        $boolean = 1;
    }
}

if ($boolean == 0) {
    print "We should commit the modifications\n";
    exit 0; # Don't prevent commit
}
else {
    print "We shouldn't commit the modifications\n";
    exit 1; # Prevent commit
}
my $valid_commit_msg = $commit_msg ne "" && $commit_msg !~ /^abort$/m;
if ($valid_commit_msg) { ... }