我想为我的 git 存储库创建自定义 prepare-commit-msg
Hook 。
首先,我阅读了有关参数的内容,因此我创建了类似 test 的内容来查看这些参数的值。
#!/usr/bin/env bash
readonly file_with_message=$1
readonly source_of_message=$2
readonly commit_sha=$3
echo "File: \"${file_with_message}\""
echo ""
echo "Source: \"${source_of_message}\""
echo ""
echo "SHA: \"${commit_sha}\""
echo ""
我得到了以下日志:
File: ""
Source: ""
SHA: ""
commit-msg File: ""
[1111-pre-commit-msg-test 4f347d4] add .idea
5 files changed, 189 insertions(+)
create mode 100644 .idea/fast.iml
create mode 100644 .idea/misc.xml
create mode 100644 .idea/modules.xml
create mode 100644 .idea/vcs.xml
create mode 100644 .idea/workspace.xml
基本上,我可以看到,我的钩子(Hook)正在提交期间执行,但没有参数。至少应该有第一个。
$ git --version
git version 2.17.2 (Apple Git-113)
有谁知道为什么会这样吗? 谢谢:)
PS:我可以看到 commit-msg
Hook 具有相同的行为。 (其中还有一条日志消息)
最佳答案
原因是,我们有自定义的钩子(Hook)执行系统:
每个<hook>
试图执行<hook>.local
和<hook>.enectiva
(enectiva 是我们的产品,这就是扩展的原因)
所以在这种情况下:
文件prepare-commit-msg
钩子(Hook)包含:
#!/usr/bin/env bash
hook_name="prepare-commit-msg"
git_path=".git/hooks"
files="./${git_path}/${hook_name}.local ./${git_path}/${hook_name}.enectiva"
for f in ${files}; do
if [[ -f ${f} ]]; then
${f} || exit 1
fi
done
该文件也获得了正确的参数,但没有传递它们。
我之前的日志代码来自prepare-commit-msg.enectiva
,所以有点误导。和prepare-commit-msg.enectiva
没有收到任何参数。
所以解决方案是使用${f} $1 $2 $3
这是更改钩子(Hook)后的结果:
File: ".git/COMMIT_EDITMSG"
Source: "message"
SHA: ""
编辑:
${f} "${@}" || exit 1
关于bash - Git prepare-commit-msg 钩子(Hook)没有得到任何参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53771489/