【发布时间】:2019-07-19 01:46:00
【问题描述】:
上下文:
我正在使用一些 git-hooks 在 Perl 脚本上自动应用一些格式化选项。在预提交挂钩上,我使用 perltidy 清理并重新格式化我的脚本。我想检查用户在提交消息上放了什么,如果它是空的或相当于“中止”,我们想阻止提交并撤消格式修改。
问题:
我删除了 git 钩子的 .sample 扩展名,并使用 chmod u+x .git/hooks/commit-msg 使其可执行,但是当我的脚本 exit 1 时,提交并没有像应有的那样停止。
提交消息
这个钩子由 git-commit[1] 和 git-merge[1] 调用,可以通过 --no-verify 选项绕过。它采用单个参数,即保存建议的提交日志消息的文件的名称。以非零状态退出会导致命令中止。
来源:https://git-scm.com/docs/githooks#_commit_msg
#!/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
}
该脚本是可执行的并且有效!如果我在提交某些内容时输入“中止”,它会打印“我们不应该提交修改”,但提交它们会丢弃退出 1...
希望有人能帮忙!我是 git-hook 的新手,找不到解决方案。也许我错过了 Stackoverflow 上的某些内容,但我没有找到回答这个问题的帖子。
最好的,
安东尼
编辑:
我不承诺使用:--no-verify
【问题讨论】:
-
您确定没有启用选项
--no-verify? -
100% 确定。我只是打电话:
git commit然后我输入我的提交信息。 -
你确定它提交了什么吗?对我来说,它总是中止提交,因为每一行至少包含最后一个换行符,所以它永远不会“中止”或“”。
-
为什么是
git status?git log -p说什么? -
逻辑是不是反过来了? “中止”不会导致阻止提交...
标签: git perl githooks git-commit