git hook 工具

2020-04-22  本文已影响0人  sorry510

配置commit信息提交规范,不做代码规范处理husky
cnpm install --save-dev husky 一般使用的框架都含有,无需再单独安装
cnpm install --save-dev @commitlint/config-conventional @commitlint/cli
package.json


 "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"  
    }
  },

commitlint.config.js

module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'type-enum': [
      2, 
      'always', 
      [
        "feat", // 新功能
        "fix", // 修复问题
        "docs", // 文档记录
        "style", // 修复代码格式
        "refactor", // 重构代码
        "test", // 测试相关
        "chore", // 构建过程或辅助工具的变动
        "revert", // 回滚
        "merge", // 合并代码
        "depend" // 添加第三方依赖
      ]
    ],
    'subject-full-stop': [0, 'never'],
    'subject-case': [0, 'never']
  }
}

正确提交格式示例
type(?scope): subject

git commit -m'feat(blog): 新增微博同步功能'
git commit -m'feat: 新增微博同步功能'
git commit -m'fix: 修复bug #123'

错误提交格式示例

git commit -m'feat(blog) 新增微博同步功能'
git commit -m'feat 新增微博同步功能'
git commit -m'feat:新增微博同步功能'
git commit -m'foo: 修复bug'
  1. captainhook
    安装composer require --dev captainhook/captainhook
    创建配置文件 ./vendor/bin/captainhook configure,在项目跟目录生成captainhook.json文件
    .git目录生成hook文件,./vendor/bin/captainhook install,此处应该写成自动化的,让成员自动安装,可以安装composer-plugin命令如下composer require --dev captainhook/plugin-composer
    或者修改composer.json,让使用者在composer install 或 update时自动执行脚本
 "scripts": {
        "post-autoload-dump": [
            "vendor\\bin\\captainhook install -f -s"
        ]
    }

配置captainhook.json文件,此处只配置了commit-msg,其余根据需求按照文档自行配置

{
    "commit-msg": {
        "enabled": true,
        "actions": [
            {
                "action": "\\CaptainHook\\App\\Hook\\Message\\Action\\Regex",
                "options": {
                    "regex": "#^(feat|fix|docs|style|refactor|test|chore|revert|merge|depend)(\\(.*\\))?:.+#",
                    "error": "No match for: %s",
                    "success": "Found match: %s"
                }
            }
        ]
    },
    "pre-push": {
        "enabled": false,
        "actions": []
    },
    "pre-commit": {
        "enabled": true,
        "actions": []
    },
    "prepare-commit-msg": {
        "enabled": false,
        "actions": []
    },
    "post-commit": {
        "enabled": false,
        "actions": []
    },
    "post-merge": {
        "enabled": false,
        "actions": []
    },
    "post-checkout": {
        "enabled": false,
        "actions": []
    }
}

正确提交格式

`type(?scope): subject`
  1. bruli/php-git-hooks
  2. https://github.com/BrainMaestro/composer-git-hooks
上一篇下一篇

猜你喜欢

热点阅读