• Git代码提交规范


    Git代码提交规范

    1.安装commitizen和cz-customizable

    npm install -g commitizen@4.2.4
    npm i cz-customizable@6.3.0 --save-dev
    
    • 1
    • 2

    2.在package.json中进行新增

    "config": {
      "commitizen": {
        "path": "node_modules/cz-customizable"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    {
      "name": "item_3",
      "version": "0.1.0",
      "private": true,
      "scripts": {
        "serve": "vue-cli-service serve",
        "build": "vue-cli-service build",
        "lint": "vue-cli-service lint",
        "prepare": "husky install"
      },
      "dependencies": {
        "axios": "^0.24.0",
        "core-js": "^3.6.5",
        "vue": "^3.2.8",
        "vue-i18n": "^9.2.0-beta.26",
        "vue-router": "^4.0.11",
        "vuex": "^4.0.2"
      },
      "devDependencies": {
        "@commitlint/cli": "^12.1.4",
        "@commitlint/config-conventional": "^12.1.4",
        "@vue/cli-plugin-babel": "~4.5.0",
        "@vue/cli-plugin-eslint": "~4.5.0",
        "@vue/cli-plugin-router": "~4.5.0",
        "@vue/cli-plugin-vuex": "~4.5.0",
        "@vue/cli-service": "~4.5.0",
        "@vue/compiler-sfc": "^3.0.0",
        "@vue/eslint-config-standard": "^5.1.2",
        "babel-eslint": "^10.1.0",
        "cz-customizable": "^6.3.0",
        "eslint": "^6.7.2",
        "eslint-plugin-import": "^2.20.2",
        "eslint-plugin-node": "^11.1.0",
        "eslint-plugin-promise": "^4.2.1",
        "eslint-plugin-standard": "^4.0.0",
        "eslint-plugin-vue": "^7.0.0",
        "husky": "^7.0.1",
        "sass": "^1.26.5",
        "sass-loader": "^8.0.2",
        "svg-sprite-loader": "^6.0.9",
        "unplugin-auto-import": "^0.5.11",
        "unplugin-vue-components": "^0.17.11"
      },
      "eslintConfig": {
        "root": true,
        "env": {
          "node": true
        },
        "extends": [
          "plugin:vue/vue3-essential",
          "@vue/standard"
        ],
        "parserOptions": {
          "parser": "@babel/eslint-parser"
        },
        "rules": {}
      },
      "browserslist": [
        "> 1%",
        "last 2 versions",
        "not dead",
        "not ie 11"
      ],
      "config": {
        "commitizen": {
          "path": "node_modules/cz-customizable"
        }
      },
      "lint-staged": {
        "src/**/*.{js,vue}": [
          "eslint --fix",
          "git add"
        ]
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    3.在根目录下新建.cz-config.js文件并写入配置 之后就可以用 git cz 来代替 git commit

    module.exports = {
      // 可选类型
      types: [
        { value: 'feat', name: 'feat:     新功能' },
        { value: 'fix', name: 'fix:      修复' },
        { value: 'docs', name: 'docs:     文档变更' },
        { value: 'style', name: 'style:    代码格式(不影响代码运行的变动)' },
        {
          value: 'refactor',
          name: 'refactor: 重构(既不是增加feature,也不是修复bug)'
        },
        { value: 'perf', name: 'perf:     性能优化' },
        { value: 'test', name: 'test:     增加测试' },
        { value: 'chore', name: 'chore:    构建过程或辅助工具的变动' },
        { value: 'revert', name: 'revert:   回退' },
        { value: 'build', name: 'build:    打包' }
      ],
      // 消息步骤
      messages: {
        type: '请选择提交类型:',
        customScope: '请输入修改范围(可选):',
        subject: '请简要描述提交(必填):',
        body: '请输入详细描述(可选):',
        footer: '请输入要关闭的issue(可选):',
        confirmCommit: '确认使用以上信息提交?(y/n/e/h)'
      },
      // 跳过问题
      skipQuestions: ['body', 'footer'],
      // subject文字长度默认是72
      subjectLimit: 72
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在这里插入图片描述

    Git强制commit

    1.使用husky进行强制git代码提交规范

    npm install --save-dev @commitlint/config-conventional@12.1.4 @commitlint/cli@12.1.4
    npm install husky@7.0.1 --save-dev
    npx husky install
    
    • 1
    • 2
    • 3

    多出一个.husky文件
    2.导入配置文件
    在这里插入图片描述

    
    ```javascript
    module.exports = {
      // 继承的规则
      extends: ['@commitlint/config-conventional'],
      // 定义规则类型
      rules: {
        // type 类型定义,表示 git 提交的 type 必须在以下类型范围内
        'type-enum': [
          2,
          'always',
          [
            'feat', // 新功能 feature
            'fix', // 修复 bug
            'docs', // 文档注释
            'style', // 代码格式(不影响代码运行的变动)
            'refactor', // 重构(既不增加新功能,也不是修复bug)
            'perf', // 性能优化
            'test', // 增加测试
            'chore', // 构建过程或辅助工具的变动
            'revert', // 回退
            'build' // 打包
          ]
        ],
        // subject 大小写不做校验
        'subject-case': [0]
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    3.在package.json中新增指令

    "prepare": "husky install"
    
    • 1

    在这里插入图片描述

    4.并执行

    npm run prepare
    
    • 1

    5.新增husky配置文件 并往里面写入

    npx husky add .husky/commit-msg
    
    • 1
    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    npx --no-install commitlint --edit
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    npx --no-install commitlint --edit
    
    • 1

    在这里插入图片描述

    效果
    在这里插入图片描述

    使用husky强制代码格式化 ,创建配置文件

    5.使用husky强制代码格式化 创建配置文件

    npx husky add .husky/pre-commit
    
    • 1

    6.往第六步生成的文件中写入

    npx lint-staged
    
    • 1

    8.把package.json文件的lint-staged修改为

    "lint-staged": {
       "src/**/*.{js,vue}": [      //src目录下所有的js和vue文件
         "eslint --fix",           // 自动修复
         "git add"                 // 自动提交时修复
       ]
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    MySQL-子查询(IN/ANY/ALL/EXIST/NOT EXIST)
    用Python中的马尔科夫链模拟文本
    hadoop大数据原理与应用-----初识hadoop习题集
    LabVIEW工业虚拟仪器的标准化实施
    探寻容器的本质
    Vue组件化开发步骤
    网络基础1
    SpringBoot SpringBoot 开发实用篇 5 整合第三方技术 5.9 jetcache 远程缓存方案 5.9.3 使用 jetcache
    酷开科技 | 酷开系统,带你寻觅最爱的影视之旅
    1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动
  • 原文地址:https://blog.csdn.net/qq_43547255/article/details/128162956