1. 程式人生 > >commitlint+husky規範commit 日誌

commitlint+husky規範commit 日誌

為了方便開發團隊協作開發,commit -m 中的資訊需要有個規範,方便直觀的看出每次commit 目的
主要說下安裝步驟和commit 規範標準,有問題歡迎探討指出~~

##############################安裝步驟#############################
一.安裝node,從Node.js官網下載對應平臺的安裝程式
安裝成功後,終端執行命令 node -v 和 npm -v 可以檢視安裝的node,npm的版本號

$ node -v
v10.13.0

$ npm -v
6.4.1

二:cd到專案目錄,建立package.json檔案
1.通過下面命令列可以直接建立預設package.json檔案
npm init -y
2.通過下面命令列一步步輸入建立自定義的package檔案內容 (注意package name:不可以以大寫字母開頭,下面有提示)
npm init

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (runloop) Runloop
Sorry, name can no longer contain capital letters.
package name: (runloop) runloop
version: (1.0.0) 
description: 測試commitlint
entry point: (index.js) 
test command: 
git repository: (https://github.com/wei3715/RunLoop.git) 
keywords: 
author: zww
license: (ISC) 
About to write to /Users/jolly/Desktop/up/self/RunLoop/package.json:

{
  "name": "runloop",
  "version": "1.0.0",
  "description": "測試commitlint",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/wei3715/RunLoop.git"
  },
  "author": "zww",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/wei3715/RunLoop/issues"
  },
  "homepage": "https://github.com/wei3715/RunLoop#readme"
}


Is this OK? (yes) yes

執行完以下命令可以在專案根目錄看到package-lock.json,package.json檔案

三:安裝套件@commitlint/config-angular @commitlint/cli、husky

//1. 安裝套件@commitlint/config-angular @commitlint/cli
$ npm install --save-dev @commitlint/config-angular @commitlint/cli
npm notice created a lockfile as package-lock.json. You should commit this file.
+ @commitlint/
[email protected]
+ @commitlint/[email protected] added 149 packages from 62 contributors and audited 388 packages in 28.666s found 0 vulnerabilities //2.安裝套件husky $ npm install --save-dev husky > [email protected] install /Users/jolly/Desktop/up/self/RunLoop/node_modules/husky > node husky install husky > setting up git hooks husky > done + [email protected] added 41 packages from 13 contributors and audited 466 packages in 10.866s found 0 vulnerabilities

安裝完成後package.json檔案的內容:

{
  "name": "runloop",
  "version": "1.0.0",
  "description": "測試commitlint",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/wei3715/RunLoop.git"
  },
  "author": "zww",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/wei3715/RunLoop/issues"
  },
  "homepage": "https://github.com/wei3715/RunLoop#readme",
  "devDependencies": {
    "@commitlint/cli": "^7.2.1",
    "@commitlint/config-angular": "^7.1.2",
    "husky": "^1.1.4"
  }
}

四:將husky hook加入到到package.json檔案中

{
  "name": "runloop",
  "version": "1.0.0",
  "description": "測試commitlint",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/wei3715/RunLoop.git"
  },
  "author": "zww",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/wei3715/RunLoop/issues"
  },
  "homepage": "https://github.com/wei3715/RunLoop#readme",
  "devDependencies": {
    "@commitlint/cli": "^7.2.1",
    "@commitlint/config-angular": "^7.1.2",
    "husky": "^1.1.4"
  },
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  }
}

五:建立commitlint.config.js檔案到專案根目錄,設定規範

$ vim commitlint.config.js

編輯輸入以下一行內容即可,這個檔案代表以後commit -m 後面的提交應遵循的規範

module.exports = {extends: ['@commitlint/config-angular']};

好的,大功告成~~完成之後專案根目錄會多出下面這幾個檔案
在這裡插入圖片描述

測試效果
下面執行commit命令測試下效果:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	commitlint.config.js
	node_modules/
	package-lock.json
	package.json

nothing added to commit but untracked files present (use "git add" to track)
$ git add .
$ git commit -m "測試 commitlint"
husky > commit-msg (node v10.13.0)

⧗   input: 
測試 commitlint

✖   message may not be empty [subject-empty]
✖   type may not be empty [type-empty]
✖   found 2 problems, 0 warningshusky > commit-msg hook failed (add --no-verify to bypass)

可以看到如果不按照規範書寫commit的日誌會提示報錯

下面輸入正確的commit 日誌資訊:注意冒號後面要留空格,下面有介紹具體的編輯規範資訊

$ git commit -m "feat(): 新增commitlint"
husky > commit-msg (node v10.13.0)

⧗   input: feat(): 新增commitlint
✔   found 0 problems, 0 warnings[master 7a5bc00] feat(): 新增commitlint
 4097 files changed, 219349 insertions(+)
 create mode 100644 commitlint.config.js
 create mode 120000 node_modules/.bin/JSONStream
 create mode 120000 node_modules/.bin/commitlint
 create mode 120000 node_modules/.bin/conventional-commits-parser
 ........(這裡第一次會有很多crete  mode資訊)

//最後執行git push把修改推送到遠端
$ git push
Counting objects: 3519, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3351/3351), done.
Writing objects: 100% (3519/3519), 2.21 MiB | 653.00 KiB/s, done.
Total 3519 (delta 741), reused 0 (delta 0)
remote: Resolving deltas: 100% (741/741), done.
To github.com:wei3715/RunLoop.git
   9d89ab6..7a5bc00  master -> master

測試通過後,將下面檔案新增到忽略檔案.gitignore中

## commitlint
node_modules/
package-lock.json

##############################下面是commit規範##########################
commit message的格式

// Header
<type>(scope): <subject>
// 空一行
Body
// 空一行
Footer

不管是哪一個部分,任何一行都不得超過72個字元(或100個字元),這是為了避免自動換行影響美觀(我的測試是不能超過72字元)

$ git add .
$ git commit -m "test(): good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&"
husky > commit-msg (node v10.13.0)

⧗   input: 
test(): good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&good&

✖   header must not be longer than 72 characters [header-max-length]
✖   found 1 problems, 0 warningshusky > commit-msg hook failed (add --no-verify to bypass)

Header:是必需的
(1) 注意冒號後面有空格,不能以大寫字母開頭
無空格時報以下錯誤:

$ git commit -m "test():good"
husky > commit-msg (node v10.13.0)

⧗   input: 
test():good

✖   message may not be empty [subject-empty]
✖   type may not be empty [type-empty]
✖   found 2 problems, 0 warningshusky > commit-msg hook failed (add --no-verify to bypass)

大寫字母開頭報以下錯誤:

$ git commit -m "test(): Good"
husky > commit-msg (node v10.13.0)

⧗   input: 
test(): Good

✖   subject must not be sentence-case, start-case, pascal-case, upper-case [subject-case]
✖   found 1 problems, 0 warningshusky > commit-msg hook failed (add --no-verify to bypass)

(2) 包括三個欄位:type(必需)、scope(可選)和subject(必需)

  • type:用於說明 commit 的型別,被指定在 commitlint.config.js 的 type-enum
    feat:新功能(feature)
    fix:修補bug
    docs:文件
    style: 格式(不影響程式碼執行的變動)
    refactor:重構(即不是新增功能,也不是修改bug的程式碼變動)
    test:增加測試
    chore:構建過程或輔助工具的變動
    revert: 回滾到上一個版本

有一種比較特殊的情況: revert

如果當前 commit 用於撤銷以前的 commit,則必須以revert:開頭,後面跟著被撤銷 Commit 的 Header。

revert: feat(pencil): add 'graphiteWidth' option
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.
  • scope: 可以省略;用於說明 commit 的影響範圍,比如資料層、控制層、檢視層等等,視專案不同而不同
  • subject:subject 是 commit 目的的簡短描述,不超過50個字元
    以動詞開頭,使用第一人稱現在時,比如change,而不是changed或changes
    第一個字母小寫
    結尾不加句號(.)

Body:可省略; body 部分是對本次 commit 的描述,可以分成多行,例如下面這樣

More detailed explanatory text, if necessary.  Wrap it to 
about 72 characters or so. Further paragraphs come after blank lines.- Bullet points are okay, too- Use a hanging indent

有兩個注意點。

(1)使用第一人稱現在時,比如使用change而不是changed或changes。

(2)應該說明程式碼變動的動機,以及與以前行為的對比。

Footer:可省略; footer 用於不相容變動和關閉ISSUE
(1)不相容變動

如果當前程式碼與上一個版本不相容,則 Footer 部分以BREAKING CHANGE開頭,後面是對變動的描述、以及變動理由和遷移方法。例如:


BREAKING CHANGE: isolate scope bindings definition has changed.
 
    To migrate the code follow the example below:
 
    Before:
 
    scope: {
      myAttr: 'attribute',
    }
 
    After:
 
    scope: {
      myAttr: '@',
    }
 
    The removed `inject` wasn't generaly useful for directives so there should be no code using it.

(2)關閉 Issue

如果當前 commit 針對某個issue,那麼可以在 Footer 部分關閉這個 issue 。

Closes #234

也可以一次關閉多個 issue

Closes #123, #245, #992

測試完整commit message

//使用git commit 方便輸入多行的commit message
$ git commit

//輸入完整的commit message: header + body +footer

test(): 測試完成commit message
    
    I just change string to test
    完整的commit message
    添加了一個 測試字串
    
    Closes #111

//輸入完成後
husky > commit-msg (node v10.13.0)

⧗   input: test(): 測試完成commit message
✔   found 0 problems, 0 warnings[master 7ac378a] test(): 測試完成commit message
 1 file changed, 1 insertion(+), 1 deletion(-)


//檢視最近一個提交記錄
git log -n 1
commit 7ac378ad532f16cb84035214908b0833bbf3f6c8 (HEAD -> master)
Author: jolly <[email protected]>
Date:   Wed Nov 14 18:02:53 2018 +0800

    test(): 測試完成commit message
    
    I just change string to test
    完整的commit message
    添加了一個 測試字串
    
    Closes #111 

另外對commitlint.config.js 檔案的內容再具體說下,我上次建立的commitlint.config.js 檔案只輸入了一行內容:

module.exports = {extends: ['@commitlint/config-angular']};

這樣就會遵循預設的commit規範,也可以輸入更多內容來自定義自己團隊的規範:

module.exports = {
    extends: ['@commitlint/config-conventional'],
    rules: {
        'subject-case': [0, 'never'],
        'type-enum': [
            2,                  //代表必須輸入
            'always', 
            [
                "docs",     // Adds or alters documentation. 僅僅修改了文件,比如README, CHANGELOG, CONTRIBUTE等等
                "chore",    // Other changes that don't modify src or test files. 改變構建流程、或者增加依賴庫、工具等
                "feat",     // Adds a new feature. 新增feature
                "fix",      // Solves a bug. 修復bug
                "merge",    // Merge branch ? of ?.
                "perf",     // Improves performance. 優化相關,比如提升效能、體驗
                "refactor", // Rewrites code without feature, performance or bug changes. 程式碼重構,沒有加新功能或者修復bug
                "revert",    // Reverts a previous commit. 回滾到上一個版本                
                "style",    // Improves formatting, white-space. 僅僅修改了空格、格式縮排、都好等等,不改變程式碼邏輯                
                "test"     // Adds or modifies tests. 測試用例,包括單元測試、整合測試等                
            ]
        ]
    }
};

相關推薦

commitlint+husky規範commit 日誌

為了方便開發團隊協作開發,commit -m 中的資訊需要有個規範,方便直觀的看出每次commit 目的 主要說下安裝步驟和commit 規範標準,有問題歡迎探討指出~~ ##############################安裝步驟###########

回顧我這一年多的 Git commit 日誌

一眨眼已到 2018 年底,我入職喜馬也一年多了,這一年裡成長了不少,但對外輸出少了很多,主要原因還是太懶。 今天趁懶癌沒發作,跟著 git 提交日誌,回顧一下這一年多寫的程式碼。 剛入職一個多月的提交 可以看到,我的提交日誌還是比較清晰的,當次提交做了什麼,基本可以一

回顧一年多的 Git commit 日誌有感

一眨眼已到 2018 年底,我入職喜馬也一年多了,這一年裡成長了不少,但對外輸出少了很多,主要原因還是太懶。 今天趁懶癌沒發作,跟著 git 提交日誌,回顧一下這一年多寫的程式碼。 剛入職一個多月的提交 可以看到,我的提交日誌還是比較清晰的,當次提交做了什麼,

Android開發規範日誌Log

文章目錄 1. 現有的系統Log 2. 改進的Log工具 3. 從日誌模組談擴充套件性 Log對於開發者定位問題來說是一個必不可少的工具。開發人員需要通過Log提供的資訊,比如Crash異常,能夠定位異常型別以及異常的

git 修改某次 commit 日誌和內容

1、將當前分支無關的工作狀態進行暫存 git stash 2、將 HEAD 移動到需要修改的 commit 的前一個上 commit d87dbd5c076 commit1 commit a37c03214ad commit2 commit a37c034543

commit日誌歷史不一致的Git倉庫合併

首先我在現有的專案資料夾新建了一個本地倉庫,然後在Coding.net新建了一個私有專案,準備把本地專案推到Coding。 首先先git pull [SSH] 到本地的倉庫,但是會出現錯誤 refusing to merge unrelated histor

使用 Commitizen 撰寫 Angular 規範commit message

sin for bug semi fix doc -c existing new 本文為原創文章,轉載請標明出處 目錄 安裝及配置 使用 1. 安裝及配置 npm install -g commitizen npm install -g cz-conventional-

軟件開發的目錄規範/定制程序的入口/引用配置文件/引用自定義模塊/logging模塊/日誌繼承與propagate屬性/通過字典導入配置/日誌模塊在項目中的使用

clas common 邏輯 導入 字典 pro gpo 項目 日誌模塊 02.軟件開發的目錄規範單獨的文件放單獨的東西py文件src 程序的核心有關setting 配置有關common 常用功能的集合體文件夾lib        庫log      日誌conf 配

mysql 常用命令,語句規範,及日誌儲存

1.mysql常用命令 SELECT VERSION(); 顯示當前版本 SELECT NOW();顯示當前日期時間 SELECT USER(); 顯示當前使用者 2.mysql語句規範 1.關鍵字與函式名全部大寫 2.資料庫名稱,表名稱,欄位名稱等全部小寫 3

PHP規範PSR3(日誌介面)介紹

本文件描述了用於記錄庫的通用介面。 主要目標是允許庫以簡單和通用的方式接收Psr \ Log \ LoggerInterface物件並將日誌寫入其中。具有自定義需求的框架和CMS可以擴充套件介面以用於它們自己的目的,但是應該保持與該文件的相容性。這可確保應用程式使用的第三方庫可以寫入集中式應用程

git pre-commit hook failed 解決辦法 解除安裝husky

husky > npm run -s precommit (node v8.11.3) 'lint-staged' �����ڲ����ⲿ���Ҳ���ǿ����еij��� ���������ļ��� husky

git commit 規範工具

為什麼使用 commit 規範? 首先看一下國際知名專案 angularjs 提交歷史 我的提交歷史: 納尼???我都寫了什麼???  你有沒有中槍 --> 因此,我們的 git commit 規範提上日程 1.commitizen 

一個維護版本日誌整潔的Git提交規範

1 關於提交日誌規範 良好的Commit Message有利於程式碼審查,能更快速查詢變更記錄,並且可以直接生成Change log。 Commit Message的寫法規範:conventional-changelog 為了規範程式碼提交的說明,這裡我們使用angular的規範寫法: <type

自家公司關於git commit規範

程式碼提交的commit info提個建議,fix的issue是哪個issue?都要有明確的連結。推薦方式:1.建立issue,說明問題的背景和原因。http://git.startdt.net/payment/paycenter/issues2.提交程式碼的時候commit info引入該issue3.測試

Logger日誌列印規範

首先來看一下比較常用的Logger日誌級別(部分未列出): error - 執行期錯誤日誌記錄,應該有專門的error日誌檔案。; warn - 警告資訊,如程式呼叫了一個即將作廢的介面,介面的不當使用,執行狀態不是期望的但仍可繼續處理等; info - 有必要的事件資訊記錄。 debug - 除錯資訊,業

學習日誌1---運用匈牙利命名法的命名規範,以及註釋規範

記錄下來方便查閱,用於C++程式設計 命名規範 1.變數命名 字首 表示型別 例子 a 陣列 aScore[50] b 波爾變數 bFlag c 字元變數 cSex i或n 整形變數 iNum,

Commitizen —— git commit 規範工具

1.commitizen     拉取線上程式碼庫,執行 sudo cnpm install -g commitizen 生成 package.json 檔案  npm init --yes 然後,執行下面命令,使其支援Angular的Commit message格

我的編碼習慣 —— 日誌規範

開發中日誌這個問題,每個公司都強調,也制定了一大堆規範,但根據實際情況看,效果不是很明顯,主要是這個東西不好測試和考核,沒有日誌功能一樣跑啊。 但程式設計活久見,開發久了,總會遇到“這個問題生產環境上能重現,但是沒有日誌,業務很複雜,不知道哪一步出錯了?” 這個時候,怎麼辦? 還能怎麼辦,發個版

git分支管理及git commit message規範

分支管理 如圖所示: master分支只用於存放線上版本 線上緊急bug,使用hot-fix分支 開發在dev分支上,小的測試bug也可在dev分支修改。線上緊急修復bug也需合併到dev分支 開發複雜的新功能可新建分支dev-${devName} Git Commit message

規範Git Commit Message

讓Commitlint支援Angular規範 一、安裝依賴: 1、自動檢測依賴安裝 npm install --save-dev @commitlint/config-angular @commitlint/cli 或者用yarn yarn add @commitl