1

如何使用 husky 检查特定的 git 提交消息模式?每当git commit -m "message"调用命令时,我都想解析消息。我怎么能这样做?我怎样才能收到 git commit 消息并启动解析?

目前,我的 package.json 包含 husky:

"husky": {
    "hooks": {
        "commit-msg": "./shell-scripts/commit-msg-hook.sh && commitlint -E HUSKY_GIT_PARAMS"
    }
},

但我不确定在 shell 脚本文件中做什么。如何接收提交消息然后解析它?

这是我正在尝试的shell脚本。它与git 文档非常相似

#!/usr/bin/env ruby

message_file = ARGV[0]
message = File.read(message_file)

echo message

$regex = /([#) #([0-9])* ([A-Z])\w+/

if !$regex.match(message)
puts "Incorrect format"
exit 1
end
4

1 回答 1

0

From https://www.atlassian.com/git/tutorials/git-hooks

The only argument passed to this hook is the name of the file that contains > the message.

I am not a ruby expert but working on a shell script I ran into a similar problem where my $1 was not giving me the message as I would hope so but rather a path to a file. so doing commit_message="$(cat "$1")" gets me the actual message to then validate.

于 2022-01-01T16:01:48.207 回答