【问题标题】:Using husky to parse git commit message使用 husky 解析 git commit 消息
【发布时间】:2020-05-11 08:35:20
【问题描述】:

如何使用 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 documentation 的非常相似

#!/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

【问题讨论】:

  • 为什么要投反对票?请告诉我,如果我错过了什么
  • “解析消息”是什么意思?在接收提交消息方面,看看commitlint在做什么,看看npmjs.com/package/husky#access-git-params-and-stdin
  • @jonrsharpe 我想解析消息并检查提交消息中是否存在#
  • 您对此有什么具体问题吗?你的脚本里有什么?
  • @jonrsharpe 添加了我尝试过的脚本

标签: git githooks husky git-husky


【解决方案1】:

来自https://www.atlassian.com/git/tutorials/git-hooks

传递给此挂钩的唯一参数是包含 > 消息的文件的名称。

我不是 ruby​​ 专家,但在编写 shell 脚本时遇到了类似的问题,我的 $1 没有像我希望的那样给我消息,而是给我一个文件的路径。这样做 commit_message="$(cat "$1")" 让我得到实际消息,然后进行验证。

【讨论】:

    猜你喜欢
    • 2017-06-06
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2022-08-10
    • 2012-12-07
    • 1970-01-01
    相关资源
    最近更新 更多