git 配置commit-msg

本文介绍了如何通过husky和commitlint来确保git commit信息中包含Jira链接,以符合公司规范。首先安装husky和commitlint,然后配置 husky 的commit-msg钩子,并编写检查脚本来验证提交信息是否包含Jira链接。当提交信息不满足条件时,脚本将返回错误信息提示缺少Jira链接。
摘要由CSDN通过智能技术生成

有些时候公司会要求git commit 强制附加jira链接,确定本次提交属于某个具体任务,提供下解决思路。
调研后决定使用husky实现,需要的插件

  • 安装husky
  • 安装commitlint
# 安装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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值