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

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