如何在 commit-msg 钩子中捕获 git commit 消息?

mar*_*aro 5 git bash githooks

我写了一个脚本

#!/bin/bash
commit_msg=$1
echo "Your commit message: $commit_msg"
Run Code Online (Sandbox Code Playgroud)

hooks/commit-msg该验证中提交消息

git commit -m "fixed a bug"
Run Code Online (Sandbox Code Playgroud)

但是当我运行钩子时,我有:

Your commit message: .git/COMMIT_EDITMSG
Run Code Online (Sandbox Code Playgroud)

代替

Your commit message: fixed a bug
Run Code Online (Sandbox Code Playgroud)

如何将提交消息捕获到变量中?

我已经阅读了如何捕获 git commit 消息并运行一个操作, 但它对我没有帮助,因为那个钩子是为了post-receive我需要它,commit-msg所以我没有我的提交消息

git log -1 HEAD --pretty=format:%s
Run Code Online (Sandbox Code Playgroud)

因为我的钩子阻止了提交。

bis*_*hop 8

文档

commit-msg 钩子接受一个参数,它也是一个包含开发人员编写的提交消息的临时文件的路径

因此,您需要读取给定文件的内容以提供消息:

commit_msg=$(cat "${1:?Missing commit message file}")
Run Code Online (Sandbox Code Playgroud)