.husky/commit-msg
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
PATTERN_COMMITS='(^IMO-[0-9]+\s#time\s([0-9]+([.][0-9]+)?[wdhm])([ ][0-9]+([.][0-9]+)?[wdhm])*\s\|\s[[:alnum:]_].+|Merge|Revert|Apply)'
msg=$(cat $1 | sed '/^#.*/d')
if ! [[ "$msg" =~ $PATTERN_COMMITS ]]; then
echo -e "\x1b[31mError commit message:\x1b[0m \x1b[33m"$msg"\x1b[0m does not follow conventional commit standard, e.g \x1b[33m 'IMO-1234 #time 1w1d1h1m | This is my commit' \x1b[33m"
exit 1;
fi
我正在使用的 Git commit 命令
git commit -m "IMO-1209 #time 1m | testing husky 11"
Hooks 应该通过正则表达式并且提交应该工作。 但是我得到如下错误
Error commit message: IMO-1209 #time 1m | testing husky 11 does not follow conventional commit standard, e.g 'IMO-1234 #time 1w1d1h1m | This is my commit'
bash 中的正则表达式不支持
\s
。使用[[:space:]]
代替。