12
头图

TL;DR:

👉 commitizen use

👉 commitlint uses

Configuration that can be copied directly

👉 The principle of git cz

Reference code library

The meaning of standard commit msg

Standardized and formatted commit messages can allow us to better track the evolution of requirements, quickly find submissions when rolling back, and make our warehouse more professional at the last time.

How does the team standardize commit msg, relying on preaching and documentation? Of course it has to be generated and constrained by tools. With so many wheels on the front end, this tool is not a problem.

  • commitizen question and answer generation commit msg format
  • commitlint check card control commit msg standardization

commitizen

commitizen: simple commit conventions for internet citizens.

commitizen/cz-cli uses the git cz command it provides to replace the git commit command to generate a commit message that meets the specifications.

The default submission specification of commitizen is strongly stipulated by the angular team. If we want to customize it, we need to cooperate with the Adapter. Here we use cz-customizable . Let us directly introduce the project-level configuration.

Commitizen configuration

Execute the npm install -D commitizen , npm install -D cz-customizable commands

Then configure the scripts and config fields in the package.json file

{
  "scripts": {
    "commit": "cz"
  },
  "config": {
    "commitizen": {
      "path": "./node_modules/cz-customizable"
    },
    "cz-customizable": {
      "config": ".cz-configrc.js"
    }
  }
}

Add the .cz-configrc.js file, refer to cz-demo

Try npm run commit in the root directory of the project

Subsequent t npm run commit replaces git commit

commitlint

How to ensure that all members in the group use the npm run commit command? Of course it is with tools.

Here we use commitlint, which is similar to eslint. Use git hooks to intercept commit msg that does not conform to the specification.

Installation dependencies

npm install --save-dev @commitlint/{config-conventional,cli}

npm install yorkie --save-dev

Add the .commitlint.config.js file

Expand the open source configuration, and then add some personalized rules
Reference configuration

If you are as confused as me about Applicable always|never: never inverts the rule. link

Configure git hooks

In order to intercept irregular commit msg, you need to use commit-msg of git hooks to automatically execute commitlint

"gitHooks": {
  "commit-msg": "commitlint -e $GIT_PARAMS"
}

Try entering a commit msg randomly, and found it very magical. Card control is in effect

Follow the above steps to standardize the commit msg of your team.

in conclusion:

step 1: install dependencies

npm install -D commitizen cz-customizable
npm install -D @commitlint/{config-conventional,cli}
npm install -D yorkie 

step 2: Add configuration file

Commitizen configuration file .cz-configrc.js in custom format, and standard .commitlint.config.js file for verification

step 2: Configure package.json

{
  "scripts": {
    "commit": "cz"
  },
  "config": {
    "commitizen": {
      "path": "./node_modules/cz-customizable"
    },
    "cz-customizable": {
      "config": ".cz-configrc.js"
    }
  },
  "gitHooks": {
    "commit-msg": "commitlint -e $GIT_PARAMS"
  }
}

The principle of git cz

If you are a global commitizen, you can use the git cz command instead of npm run commit .

git cz is 06187c6df32219? The git command is also the commitizen command. 2 silly face???

By consulting information, git cz is a custom git command generated by commitizen using git file naming conventions. cz-cli/issues

Git follows the naming convention git-<subcmd> automatically parse the subcommands in the executable file in the PATH. These subcommands can be executed git <subcmd>

Let's look at the bin field of the commitizen warehouse package.json. It's really fragrant, it's a custom command of git

 "bin": {
    "cz": "./bin/git-cz",
    "git-cz": "./bin/git-cz",
    "commitizen": "./bin/commitizen"
  },

finally

Refer to this demo warehouse to arm your code base

Original address


亖混子
4.7k 声望2.4k 粉丝

自信、自洽、自在