这是针对任何类型的问题/票证编号提交消息的完整解决方案:
准备提交消息
#!/bin/bash
# Append issue number / bug tracking URL to commit.
#
# If the branch name contains the issue number, it will append it to the
# commit message. Example:
#
# BRANCH NAME LINE TO APPEND
# feature/GH-123-emoji GitHub: #123
# WRIKE-123-add-payment Wrike: https://www.wrike.com/open.htm?id=123
# UNKNOWN-123 Issue: #123
branchName=`git rev-parse --abbrev-ref HEAD`
IFS=- read issueTracker issueNumber <<< $(echo $branchName | sed -nr 's,([a-z-]+/)?([A-Z]+-[0-9]+)-.+,\2,p')
if [[ -z $issueNumber ]]; then
exit 0
fi
case "$issueTracker" in
WRIKE)
line="Wrike: https://www.wrike.com/open.htm?id=$issueNumber"
;;
GH)
line="GitHub: #$issueNumber"
;;
GL)
line="GitLab: #$issueNumber"
;;
*)
line="Issue: #$issueNumber"
;;
esac
# If the commit message already contains the line (`--amend`), then do
# not add it again.
if ! ( grep "$line" "$1" > /dev/null ); then
sed -i.bak -e "/# Please enter the commit message for your changes./ s,^,$line\n\n," $1
fi
将其放入存储库的目录以仅应用于存储库,或者在其中.git/hooks
设置core.hooksPath~/.gitconfig
并复制到该目录以应用于所有存储库。
除了其他有用的脚本外,请参阅我的配置文件存储库。