前端之路:用Angular规范来约束团队git提交
栏目: JavaScript · 发布时间: 5年前
内容简介:团队协作开发,这种方法的好处是,任何人只要想往本项目提交 commit,就必须按照规范,否则不然提交。这样就做到了每次提交都会检查 commit 信息了,当不符合规范时,会给出提示。 例如:
团队协作开发, git
作为一个开源的分布式版本控制系统,俨然成为当下最受欢的项目代码版本管理工具, 即是团队,就要有一定的规矩,规范,这样才能更好的发挥团队效率。熟悉 git
的小伙伴都知道,每次提交代码, 都要写 Commit message(提交说明),否则就不允许提交,至于提交的信息 git
并没有进行约束,以至于, 提交上来的说明是五花八门,由此社区出现了多种写法规范,其中 Angular 规范 是目前最为广泛的写法,比较合理和系统化。 本文就来讲一讲两种使用 Angular 规范的方法。
法一,基于项目通过 hooks 触发(我司团队用的此方法)
这种方法的好处是,任何人只要想往本项目提交 commit,就必须按照规范,否则不然提交。
- 安装所需依赖,
npm i --save-dev husky chalk
; - 在项目的根目录新建一个文件夹
.github
(名字随意),这个文件夹专门用来存放 github 相关的东西; - 在上一步的文件夹中新建
node
脚本文件verifyCommitMsg
(名字随意); - 在上一步的脚本文件中写入一下代码:
#!/bin/env node const chalk = require('chalk'); const msgPath = process.env.HUSKY_GIT_PARAMS; if (!msgPath) { console.error(chalk.red(`process.env.HUSKY_GIT_PARAMS can't be ${msgPath},please check`)); process.exit(1); } console.log('HUSKY_GIT_PARAMS: ', msgPath); const msg = require('fs') .readFileSync(msgPath, 'utf-8') .trim(); console.log(msg); const commitRE = /^(revert: )?(feat|fix|docs|style|refactor|perf|test|workflow|ci|chore|types)(\(.+\))?: .{1,50}/; if (!commitRE.test(msg)) { console.log(); console.error( ` ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` + chalk.red( ` Proper commit message format is required for automated changelog generation. Examples:\n\n` ) + ` ${chalk.green(`feat(compiler): add 'comments' option`)}\n` + ` ${chalk.green(`fix(v-model): handle events on blur (close #28)`)}\n\n` + chalk.red(` See .github/COMMIT_CONVENTION.md for more details.\n`) // chalk.red(` You can also use ${chalk.cyan(`npm run commit`)} to interactively generate a commit message.\n`) ); process.exit(1); } 复制代码
- 在
package.json
增加/修改 hooks,如下:
... "husky": { "hooks": { "commit-msg": "node .github/verifyCommitMsg" } }, ... 复制代码
这样就做到了每次提交都会检查 commit 信息了,当不符合规范时,会给出提示。 例如:
法二,基于 npm 依赖插件
这种方法的好处是,提交比较人性化。
- 全局安装依赖,
npm i -g commitizen
- 初始化适配器,
commitizen init cz-conventional-changelog --save-dev --save-exact
- 用
git cz
命令 代替git commit
例如:
=== 文中不足,欢迎指正 ===
以上所述就是小编给大家介绍的《前端之路:用Angular规范来约束团队git提交》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Designing for Emotion
Aarron Walter / Happy Cog / 2011-10-18 / USD 18.00
Make your users fall in love with your site via the precepts packed into this brief, charming book by MailChimp user experience design lead Aarron Walter. From classic psychology to case studies, high......一起来看看 《Designing for Emotion》 这本书的介绍吧!