husky实现git commit规范
本文首发于个人博客网址:https://www.kelen.cc/posts/husky-commit-rule
开发中如何统一git commit规范,对项目的开发和维护以及问题的回溯都很有效果,接下来看看如何实现commit信息规则的校验
commitlint校验commit信息
多人协作开发的时候,友好的commit信息对于项目开发人员来说非常重要,这时候commitlint就派上用场了,commitlint是来检测提交信息的格式是否符合规范
常用的commitlint模版
type(scope?): subject
body?
footer?
多个作用域
commitlint支持多个作用域,常用的分隔符选项有
"/"
"\"
","
commit规范的制定和使用
我们不仅可以使用社区的规范 @commitlint/config-conventional
,也可以自定义规范
常用的社区commit规范有
- @commitlint/config-angular
- @commitlint/config-conventional
- @commitlint/config-lerna-scopes
- @commitlint/config-patternplate
- conventional-changelog-lint-config-atom
- conventional-changelog-lint-config-canonical
- commitlint-config-jira
社区规范引入方法,在项目根目录下新建 commitlint.config.js
文件
// commitlint.config.js
module.exports = {extends: [[email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> *//config-conventional'], // => commitlint/config-conventional
};
也可以使用自定义规范,通过相对路径引入
// commitlint.config.js
module.exports = {extends: ['./config.js'], // => ./config.js
};
自定义commit规则的配置如下
// config.jsmodule.exports = {/** 继承自@commitlint/config-conventional@commitlint/config-conventional*/extends: ['@commitlint/config-conventional'],/** 使用@commitlint/format格式化*/formatter: '@commitlint/format',/** 重新自定义校验规则*/rules: {'type-enum': [2, 'always', ['foo']],},/** 排除校验情况*/ignores: [(commit) => commit === ''],/** commitlint默认排除规则*/defaultIgnores: true,/** 展示commit错误求助链接,配置无效* commitlint --help-url="https://www.baidu.com"*/helpUrl:'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',/** 提示输入*/prompt: {messages: {},questions: {type: {description: 'please input type:',},},},
};
相应的规则链接
灵活的自定义检测插件
由于官方的检测规则有点死板,有时需要更加灵活的限制,可以通过commitlint插件来解决,首先安装依赖 commitlint-plugin-function-rules
npm install --save-dev commitlint-plugin-function-rules
修改 commitlint.config.js
文件
module.exports = {// extends: [[email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> *//config-conventional'],plugins: ['commitlint-plugin-function-rules'],rules: {'header-max-length': [0],'function-rules/type-enum': [2,'always',(parsed) => {if (!['AA', 'BB'].includes(parsed.type)) {return [false, '必须以JIRA号(AA/BB)作为开头'];}return [true];},],},
};
自定义commit模板
官方自带的模版必须是以 :
规范来写commit信息的,如果我们想自定义commit模板,可以通过 parserPreset
配置来实现,例如以空格为分割符,可以这样实现
parserPreset: {parserOpts: {headerPattern: /^([feat|hotfix].*)\s(.*)/,headerCorrespondence: ['type', 'subject']}
},
husky使用
husky可以在git commit的钩子处理相关的操作,比如执行单元测试,代码lint,代码commit检测等。使用方法如下
npm install husky --save-dev
在根目录下执行
npm set-script prepare "husky install"
npm run prepare
比如新增一个 pre-commit
钩子,执行单元测试
npx husky add .husky/pre-commit "npm test"
git add .husky/pre-commit
比如新增一个 commit-msg
钩子,执行commit信息格式检测
npx husky add .husky/commit-msg "npx commitlint --edit $1"
至此,就可以实现在git commit时进行commit信息的格式检测
相关资料
- https://commitlint.js.org/#/
- https://github.com/vidavidorra/commitlint-plugin-function-rules
- https://www.npmjs.com/package/husky
本文为原创,未经授权,禁止任何媒体或个人自媒体转载
本文由博客一文多发平台 OpenWrite 发布!
husky实现git commit规范相关推荐
- git 工作流和git commit规范
目的 统一团队的Git工作流,包括分支使用.tag规范.issue等 统一团队的Git Commit日志标准,便于后续代码review,版本发布以及日志自动化生成 git工作流 git flow工作流 ...
- git commit规范 、CHANGELOG生成 和版本发布的标准自动化
长期以来,大家是不是受限于这种情况:团队中每位成员提交代码时填写的信息随意,没有一定的规范,在出问题后想要定位到某次提交记录时更是难上加难,或者是加上了 commitlint之类的规范,也没有添加ch ...
- git commit规范
如何规范git commit提交 github是我们用于协同开发的平台,方便开发人员协同开发,极大提高了开发效率,但是经过团队第一次协同开发后,我们发现了一个很大的问题,我们的git commit非常 ...
- git commit 规范指南
前言 Git 每次提交代码,都要写 Commit message(提交说明),否则就不允许提交.但是,一般来说,commit message 应该清晰明了,说明本次提交的目的. 不过话说回来,作为最具 ...
- git commit 规范校验配置和版本发布配置
一. 快速配置和版本发布流程 该章节主要是对下文内容的归纳方便往后的查阅,如果需要了解细节部分请从第二章节开始阅读 1.1 依赖包安装 # husky 包安装 npm install husky -- ...
- Git ~ commit 规范
commit 规范 保持每日下班前提交代码 commit 颗粒度细化, 某一个bug或者某一个功能点 作为一次单独的commit commit 格式 <type> <id> & ...
- React开发(144):Git Commit 规范
#### ``` feature: 开发新的功能 fix: 修复bug refactor: 代码重构 docs: 文档修改 style: 代码格式修改, 注意不是 css 修改 perf: 改善性能 ...
- git commit规范工具
npm install -g commitizen commitizen init cz-conventional-changelog --save --save-exact 以后,凡是用到git c ...
- git commit 规范及 changelog
使用插件standard-version和conventional-changelog生成 changelog 文档的方法. 具体步骤如下: 1. 安装插件 在 package.json 文件中补充添 ...
最新文章
- 不会三种编程语言的不算程序员 走近阿里云 MVP烁淼吐槽大佬
- 51nod1127(尺取法)
- .NET Core开发实战(第6课:作用域与对象释放行为)--学习笔记(下)
- elementUI的container布局设置全屏宽度
- nlp-tutorial代码注释3-3,双向RNN简介
- Java set的区别_Java Set集合详解及Set与List的区别
- 9,求整数的二进制中1的个数《剑指offer》
- ubuntu 桌面 终端 控制台 Bash Shell 命令行编辑 快捷键
- 金融衍生品 matlab,Matlab金融工程教程金融衍生品计算PPT精选文档
- 沪江日语频道 » 日语真题
- 6. update更新数据的4种方法
- 九月十月百度人搜,阿里巴巴,腾讯华为小米搜狗笔试面试八十题(2012年度笔试面试八十题)
- 【名师大讲坛】叶俊受《金刚经》的启发创造“名非论”-火锅智烩节目组根据视频文字整理
- 八皇后问题(回溯问题)
- Django 创建项目app
- 深入浅出内存马(一)
- 360 千亿级数据量的 Kafka 深度实践
- 安装es-header插件
- http请求属性 Accept enctype Content-Type
- matlab中种子填充算法