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

附录

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