我正在为团队实施 git 模板提交消息。我.git/hooks/prepare-commit-msg通过添加一行来通过钩子完成它:
cat ".gitmessage" >> "$1"
我还配置了以下内容:
git config --local commit.template .gitmessage
好吧,模板功能工作正常,但只有在没有 -m标志的情况下调用 git commit 时
。
不幸的是,所有团队成员的工作流程是:
git add ...
git commit -m "Some message"
Run Code Online (Sandbox Code Playgroud)
问题:如何强制 git 始终打开编辑器来编辑消息,即使在被git commit -m ...命令调用时也是如此?
您可以修改commit-msgpre-hook 来实现此目的。例如,您可以检查提交消息中的行数(检查下面的示例);使用一些正则表达式来检查模板是否受到尊重;ETC。
#!/bin/bash
# Check if the number of lines is greater than 3
commit_msg_file="${1}"
minimum_lines=3
num_lines=$(wc -l "${commit_msg_file}" | cut -f1 -d' ')
if (( $num_lines < $minimum_lines )); then
echo >&2 "Error!"
exit 1
fi
Run Code Online (Sandbox Code Playgroud)