Git Commit Hook

前言

剛好寫Shell工程,需要規範代碼提交,就簡單記錄下。╮(╯▽╰)╭

流程

安裝插件

# 安裝npm環境
brew install npm

# 安裝插件
npm install -g @commitlint/cli @commitlint/config-conventional
# 安裝後,如果項目下面沒有package.json
[npm | cnpm] init -y

結果爲:(提前將最終的配置結果貼出來,😁)

{
  "name": "xxxx",
  "version": "0.0.1",
  "author": "notzonotdied",
  "description": "xxxx",
  "scripts": {
  },
  "license": "ISC",
  "devDependencies": {
    "@commitlint/cli": "^8.2.0",
    "@commitlint/config-conventional": "^8.2.0",
    "@commitlint/prompt": "^8.2.0",
    "commitizen": "^4.0.3",
    "conventional-changelog-cli": "^2.0.25",
    "husky": "^4.0.0-beta.5"
  },
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  },
  "config": {
    "commitizen": {
      "path": "@commitlint/prompt"
    }
  },
  "dependencies": {
    "lint": "^0.7.0"
  }
}

配置commit規範

# 在項目根目錄下執行如下指令
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js

commitlint.config.js

# 默認
"module.exports = {extends: ['@commitlint/config-conventional']}"

/**
 * add:         新增文件,新增功能
 * update:      在原有的功能上新增
 * revert:
 * chore:       構建過程或輔助工具的變動
 * test:        增加測試
 * refactor:    重構(即不是新增功能,也不是修改bug的代碼變動)
 * style:       格式(不影響代碼運行的變動)
 * docs:        文檔(documentation)
 * fix:          修bug
 * feature:
 * */
module.exports = {
    extends: ['@commitlint/config-conventional'],
    rules: {
        'type-enum': [2, 'always', [
            "feature", "fix", "docs", "style", "refactor", "test", "chore", "revert", "update", "add"
        ]],
        'subject-full-stop': [2, 'never', '.'],
        'subject-case': [2, 'always', ['upper-case']]
    }
}

上面我們就完成了commitlint的安裝與提交規範的制定。檢驗commit message的最佳方式是結合git hook,所以需要配合Husky

配置Git Hook:Husky

husky繼承了Git下所有的鉤子,在觸發鉤子的時候,husky可以阻止不合法的commit,push等等。注意使用husky之前,必須先將代碼放到git 倉庫中,否則本地沒有.git文件,就沒有地方去繼承鉤子了。

# 使用如下命令安裝
npm install husky --save-dev

安裝成功後需要在項目下的package.json中配置:

# 下面是^4.0.0-beta.5版本的配置
"husky": {
  "hooks": {
    "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
  }
},

最後我們可以正常的git操作git add .,但是我們的git commit會被hook,只有符合規範的纔會被提交。

生效

當我們配置完畢package.json後,就可以使用在根目錄下執行yarn install即可。

{
  "name": "xxxx",
  "version": "0.0.1",
  "author": "notzonotdied",
  "description": "xxxx",
  "scripts": {
  },
  "license": "ISC",
  "devDependencies": {
    "@commitlint/cli": "^8.2.0",
    "@commitlint/config-conventional": "^8.2.0",
    "@commitlint/prompt": "^8.2.0",
    "commitizen": "^4.0.3",
    "conventional-changelog-cli": "^2.0.25",
    "husky": "^4.0.0-beta.5"
  },
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  },
  "config": {
    "commitizen": {
      "path": "@commitlint/prompt"
    }
  },
  "dependencies": {
    "lint": "^0.7.0"
  }
}
  • 執行yarn install
➜  xxx git:(master) yarn install
yarn install v1.22.4
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
✨  Done in 5.09s.

執行完畢後會在項目根目錄下生成package-lock.json

附錄

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