有些时候公司会要求git commit 强制附加jira链接,确定本次提交属于某个具体任务,提供下解决思路。
调研后决定使用husky实现,需要的插件
# 安装husky
npm install husky -D
# 设置运行脚本并运行
npm set-script prepare "husky install"
npm run prepare
# 安装commitlint 根据操作系统选一种
# Install and configure if needed
npm install --save-dev @commitlint/{cli,config-conventional}
# For Windows:
npm install --save-dev @commitlint/config-conventional @commitlint/cli
# 添加hooks及赋权
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
chmod a+x .husky/commit-msg
# 生成commitlint.config.js
# Configure commitlint to use conventional config
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
附:我的.husky/commit-mgs
msg=`awk '{printf("%s",$0)}' $1`
result=$(echo $msg | grep "127.0.0.1")
if [[ "$result" != "" ]];
then
echo -e "提交成功!"
else
echo -e "提交失败:提交信息中必须包含jira的url"
exit 1
fi
npx --no -- commitlint --edit "$1"
报错信息
git commit -m ‘test’ 错误信息
-e 提交失败:提交信息中必须包含jira的url
也可以通过commitlint.config.js 进行自定义配置
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'hello-world-rule': [2, 'always'],
},
plugins: [
{
rules: {
'hello-world-rule': ({subject}) => {
const HELLO_WORLD = 'Hello World';
return [
subject.includes(HELLO_WORLD),
`Your subject should contain ${HELLO_WORLD} message`,
];
},
},
},
],
};
报错如下
⧗ input: test ✖ Your subject should contain Hello World message
[hello-world-rule]✖ found 1 problems, 0 warnings ⓘ Get help:
https://github.com/conventional-changelog/commitlint/#what-is-commitlint