commit-message規範和實施

1.commit-message規範必要性

  1. 統一格式的提交記錄,更清晰和易讀
  2. 能夠經過提交記錄來了解本次提交的目的,更好的CR和重構
  3. 更容易瞭解變動,定位和發現問題
  4. 每一個提交描述都是通過思考的,改善提交質量
  5. 生成變動記錄

2.規範選型

通過commit-message規範評審,在業界經常使用的:atomeslintAngular等規範中, 能夠選擇最經常使用的Angular規範做爲咱們平常項目中的提交規範node

3.Angular的Commit Message規範簡介

每條提交記錄包含三個部分:header,body和footergit

<header> <BLANK LINE> <body> <BLANK LINE> <footer>

Commit Message Headergithub

<type>(<scope>): <short summary>
  │       │             │
  │       │             └─⫸ Summary in present tense. Not capitalized. No period at the end.
  │       │
  │       └─⫸ Commit Scope: animations|bazel|benchpress|common|compiler|compiler-cli|core|
  │                          elements|forms|http|language-service|localize|platform-browser|
  │                          platform-browser-dynamic|platform-server|router|service-worker|
  │                          upgrade|zone.js|packaging|changelog|dev-infra|docs-infra|migrations|
  │                          ngcc|ve
  │
  └─⫸ Commit Type: build|ci|docs|feat|fix|perf|refactor|test

其中<type>和<summary>是必要的 <scope>是可選的shell

Type 必須是如下的類型

  • feat: 新增頁面或功能
  • fix: bug修復
  • build: 影響構建系統或外部依賴項的更改
  • ci: 對 CI 配置文件和腳本的更改
  • docs: 文檔改動
  • perf: 性能提高改動
  • refactor: 不影響功能的代碼重構(既不修復錯誤也不添加功能)
  • test: 添加或修改測試用例

Summary用來提供更改的簡潔描述

4.規範實施

經過commitizen進行交互式提交,husky + commit-msg hook進行提交校驗,cz-customizable來自定義交互提交流程和文案

image-20210615173113038

1.使用commitizen工具,在commit時能夠交互的方式選擇type

安裝commitizennpm

npm i -D commitizen

package.json中添加對應的npm scriptjson

"commit":"cz"

改動添加到暫存區後執行commit提交api

npm run commit

2. 經過husky在git hooks中對不符合規範的提交進行攔截,攔截commitlint進行校驗

安裝husky , commitlint 和 符合angular提交規範的配置bash

npm i -D husky commitlint @commitlint/config-conventional

添加git hooks工具

npx husky install

package.json中添加prepare的npm hook, 在每次install自動執行(yarn v2+不支持prepare)性能

"prepare": "husky install"

執行添加commit-msg hook

若是使用husk v4.x版本(推薦升級到新版本),直接在package.json中或.huskyrc.json中新增commit-msg鉤子便可

package.json

"husky": {    
    "hooks": {      
        "commit-msg": "commitlint --edit $1"    
        }  
}

.huskyrc.huskyrc.json.huskyrc.jshusky.config.js

"hooks": {  
    "commit-msg": "commitlint --edit $1" 
}

若是使用husky v6+版本,須要添加對應的shell調用方式(husky v6對添加方式作了改動,因此沒法經過添加配置到package.json中運行)

npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'

添加commintlint配置(也能夠放到package.json中指定)

echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js

package.json中添加commitlint配置

"commitlint": {    
  "extends": [      
      "@commitlint/config-conventional"    
  ]
}

3. 擴展和自定義規範

commitizen提供的交互式默認是英文的,若是改爲中文或者對交互流程進行改動,可使用cz-customizable進行擴展和自定義

安裝cz-customizable

npm i -D cz-customizable

package.json中添加配置

"config": {    
    "commitizen": {      
        "path": "node_modules/cz-customizable"    
    },    
    "cz-customizable": {      
        "config": ".cz-config.js"    
    } 
}

.cz-config.js就是cz-customizable配置的具體文件了,能夠參考CZ-Config-Example並進行改動, 能夠把文案翻譯成中文,自定義修改提示等。

也能夠經過fork cz-customizable建立內封配置文件的npm包

npm i -D your-own-package
"config": {    
  "commitizen": {      
    "path": "node_modules/your-own-package"    
  } 
}

配置文件能夠自定義交互內容,好比能夠只保留type scope breakchange confirm

  • 選擇提交類型
  • 簡單描述本次改動
  • 是否有重大變動
  • 肯定提交

配置文件中設置skipQuestions: ['body','customScope','scope','footer'],便可忽略其餘選項

allowBreakingChanges: ['feat', 'fix'], 僅在feat和fix時提示 breakchange

4.其餘

經過npm script進行commit,若是eslint沒有經過(在pre-commit 鉤子中作了eslint檢測),可是又想提交能夠經過加'––'來向npm script傳參

npm run commit -- --no-verify # or npm run commit -- -n
相關文章
相關標籤/搜索