Abort commit if todo 到代码中,除非在提交消息中找到 "work in progress"

Abort commit if todo into the code except when "work in progress" found in the commit message

我需要 git 中止包含待办事项的提交,除非在提交消息中指定了 "work in progress" 文本。

鉴于我总是通过 -m 选项提供它,因此没有办法将提交消息访问到预提交挂钩中,这意味着它应该可用于所有可用的 git 钩子?

如果没有,我该怎么办?

, you will need to use the commit-msg hook.

I need to get git to abort commits containing todo except when in the commit message "work in progress" text is specified.

你还没有在这里定义 "containing todo",但我们还是试一试吧:

#! /bin/sh
# commit-msg hook: if "work in progress" is included, allow anything,
# otherwise require that no line adds the word "todo" anywhere.
#
# Note that this will forbid a commit that includes a text file
# or comment saying that you should not include the word "todo"
# in your commits!
grep "work in progress" "" >/dev/null && exit 0 # allow
git diff --cached | grep '^+.*todo' >/dev/null && {
    echo "commit aborted: found \"todo\" without \"work in progress\"" 1>&2
    exit 1 # forbid
}
exit 0 # test passed, allow

注意:这完全未经测试。