【问题标题】:Git commit hook - How to use the commit-msg hook to check for string in message?Git 提交钩子 - 如何使用 commit-msg 钩子检查消息中的字符串?
【发布时间】:2013-10-16 20:43:23
【问题描述】:

我需要制作一个 commit-msg 挂钩来检查提交消息是否在其中的任何部分包含“app.asana”。我搜索了一些参考资料和文档,我知道我需要为此使用 commit-msg。我必须使用 Perl 或 Bash 来实现。

有没有人知道这方面的线索,或者我可以在某个地方查看更多示例以了解如何做到这一点?

谢谢。

【问题讨论】:

  • This 可能会有所帮助。
  • 我已经访问过此页面,但谢谢。经过更多研究后,我做到了……实际上非常简单。

标签: git perl bash hook


【解决方案1】:

我找到了问题的答案。万一它可能对某人有帮助......

我刚刚在.git/hooks 中创建了一个commit-msg 文件,其中包含

#!/bin/sh

test -n "$(grep 'app.asana.com/' ${1})" || {
        echo >&2 "ERROR: Commit message is missing Asana's task link.\n\nPlease append the Asana's task link relative to this commit into the commit message."
        exit 1
}

每个用户都需要在.git/hooks 中有一个commit-msg 文件。然后,作为一种解决方案,我将commit-msg 添加到项目文件夹中(这样我就可以提取它了)以及另一个名为commit-msg-hook.sh 的文件,其中包含:

#!/bin/sh

cp commit-msg .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg
echo "\nThe commit-msg hook was added to git hooks"

我欢迎任何建议来改进我所做的事情。谢谢。

【讨论】:

  • 只是我还是第一个代码 sn-p 中的第二行不需要/不起作用。 chmod a+x .git/hooks/commit-msg 是使 commit-msg 可执行,以便它与 git 一起运行。但是,如果该文件(以及该命令)还没有 +x,则该文件(以及该命令)将永远无法执行,因此在实际文件中包含该命令是没有意义的。这篇文章是2013年的,如有错误请大家指正。肯定有人注意到了这一点?
  • @Russell 你是对的。我修好了,正在同行评审中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 2020-05-11
  • 2018-01-07
  • 2011-07-20
  • 1970-01-01
相关资源
最近更新 更多