Commitizen使用

一、工欲善其事,必先利其器。

    大量的代码提交,必然会产生大量的commit log,而每一次commit是阶段性的Ending,应记录着这一阶段所完成的事以及关注点,尽可能详细具体;且提供更多的历史信息,方便快速浏览;可以过滤某些commit(比如文档改动),便于快速查找信息;可以直接从commit生成Change log。所以log的格式就是关键所在,而[Commitizen](https://www.npmjs.com/package/commitizen)可以完美的解决这些问题。

二、Commitizen是什么?

    是一个格式化commit message的工具。它的安装需要[NPM](https://www.npmjs.com/package/npm)的支持,NPM是Node.js的包管理工具,所以首先安装[node.js](https://nodejs.org/en/download/),下载对应系统的包,安装即可。

三、Commitizen安装

npm install -g commitizen

安装changelog,是生成changelog的工具

npm install -g conventional-changelog
npm install -g conventional-changelog-cli

执行

npm ls -g -depth=0

检验上面两个工具是否安装成功,得到结果如下,表示成功:

/usr/local/lib
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

然后,运行下面命令,使其支持Angular的Commit message格式。

commitizen init cz-conventional-changelog --save --save-exact

但是注意,因为commitizen工具是基于Node.js的,而我们iOS项目工程目录下是没有package.json文件,所以会报错:

npm WARN saveError ENOENT: no such file or directory, open '/Users/Elite/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/Users/Elite/package.json'

对于此种错误,创建一个空的package.json文件,然后进入到项目目录,执行

npm init --yes

会生成项目对应项目的package.json,将项目目录下产生的package.json的内容写入到自己建的package.json(/User/Elite/package.json)中,如果有多个项目,将各项目生成的package.json内容写入到package.json中,下面是我的配置(/User/Elite/package.json):

[{
  "name": "salary",
  "version": "1.0.0",
  "description": "> v1.0 涵盖所有老师(非中教)的基本工资、奖励工资、惩罚工资。",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git@***.***.com:erp/salary.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "cz-conventional-changelog": "^2.1.0"
  },
  "config": {
    "commitizen": {
      "path": "./node_modules/cz-conventional-changelog"
    }
  },
  "dependencies": {}
},
{
  "name": "ats",
  "version": "1.0.0",
  "description": "composer.json composer配置 vendor 第三方类库",
  "main": "index.js",
  "directories": {
    "test": "tests"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git@***.***.com:ats/ats.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}]

然后进入到你要操作的项目目录,执行

conventional-changelog -p angular -i CHANGELOG.md -s

此时项目中多了CHANGELOG.md文件,表示生成 Change log成功了。以后,凡是用到git commit 命令的时候统一改为git cz,然后就会出现选项,生成符合格式的Commit Message。实例如下:

? Select the type of change that you're committing: (Use arrow keys)
❯ feat:     A new feature 
  fix:      A bug fix 
  docs:     Documentation only changes 
  style:    Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc) 
  refactor: A code change that neither fixes a bug nor adds a feature 
  perf:     A code change that improves performance 
  test:     Adding missing tests or correcting existing tests

然后按操作执行,即可产生change log。如果最后产生一个这样的错误:

Error: Could not resolve /Users/Elite/web/node_modules/cz-conventional-changelog. 
Cannot find module '/Users/Elite/web/node_modules/cz-conventional-changelog'

只需做个软连接即可:

ln -s /Users/Elite/node_modules /Users/Elite/web/node_modules

当然IDE工具下可以装个plugin: Git Commit Template

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章