background

Commitlint Git commit format verification tool.

The installation

  • Install the NPM package
npm install -g @commitlint/cli @commitlint/config-conventional
Copy the code
  • Creating a Configuration File
touch commitlint.config.js
// commitlint.config.js
module.exports = {
  extends: ['@commitlint/config-conventional']
}
Copy the code

The configuration file

By default, the commitlint.config.js configuration file is read from the root directory

Specifying a configuration file

commitlint --config <file path>
Copy the code

Configuration items

  • Extends extends extends from external configuration files or NPM configuration packages
{
  extends: [
    // Use the locally shared configuration file
    './commitlint.local.js'.// Use third-party NPM packages
    '@commitlint/config-conventional'.// A third-party package in '
      
       /commitlint-config' format, providing shorthand mode
      
    '@coolcompany'.// commitlint-config-*
    'lerna']},Copy the code
  • ParserPreset parser path (required)
{
   parserPreset: 'conventional-changelog-atom',}Copy the code
  • Formatter formatting tool (required)
{
  formatter: '@commitlint/format',}Copy the code
  • Rules A local custom rule, which has a higher priority than extends
{
  'type-enum': [2.'always'['foo']]}Copy the code
  • Ignores the function and helps ignore some unwanted submissions
{
  ignores: [(commit) = > commit === ' '],}Copy the code
  • DefaultIgnores Whether to ignore the configuration by default
{
  defaultIgnores: true
}
Copy the code
  • HelpUrl Help address

‘github.com/conventiona… ‘,

  • Prompt Customizes prompt information
{
  prompt: {
    messages: {},
    questions: {
      type: {
        description: 'please input type:',},},},}Copy the code

User-defined verification rules

Rules of the format

[rule name]:[level, applicable, value]

  • Level Verification level
    • 0 to disable
    • 1 warning
    • 2 error
  • applicable
    • always
    • nerver
  • The value parameter values

Acceptable parameters for the rule:

  1. Array of rules
  2. The function () that returns an array of rules => arry
  3. Array of Promise rules
{
  "rules": {"header-max-length": [0."always".72]."header-max-length": () = > [0."always".72]."header-max-length": async() = > [0."always".72].}}Copy the code

Rules of the item

  • Header-case Word format, for example, upper-case all uppercase
  • End of the header – full – stop
  • Header-max-length Indicates the maximum length of the header
  • Header-min-length Specifies the minimum length of the header
  • references-empty
  • scope
    • Scope-enum scope Optional value, e.g. [‘components’, ‘utils’, ‘cli’]
    • Scope-case Scope word format
    • Scope-empty Specifies whether the scope is empty
    • Scope-max-length scope Indicates the maximum content length
    • Scope-min-length Indicates the minimum content length of scope
  • subject
    • Subject-case subject Word format
    • Subject-empty Specifies whether the subject is empty
    • Subject-full-stop subject Terminates the subject
    • Subject-max-length subject Indicates the maximum content length
    • Subject-min-length Indicates the minimum content length of the subject
    • The subject — the exclamation mark separator
  • type
    • Type-enum Type Optional value Example: [‘feat’, ‘fix’]
    • Type-case type Indicates the word format
    • Type-empty Specifies whether the type is empty
    • Type-max-length type Indicates the maximum content length
    • Type-min-length Indicates the minimum length of the type
  • signed-off-by
  • trailer-exists

  • Body-full-stop Body terminator
  • Body-leading-blank body starts with an empty line
  • Body-empty Specifies whether the body is empty
  • Body-max-length Indicates the maximum length of the body
  • Body-max-line-length Specifies the maximum number of body lines
  • Body-min-length Specifies the minimum body length
  • Body-case word format, for example, upper-case all uppercase

  • Footer-leading-blank footer starts with an empty line
  • Footer -empty Specifies whether the footer is empty
  • Footer-max-length Indicates the maximum length of the footer
  • Footer-max-line-length Specifies the maximum number of footer lines
  • Footer-min-length Specifies the minimum footer length

Rules of the plugin

The rules plug-in provides us with the ability to customize rule validators.

Local plug-in

{
  rules: {
    // Add rules
    'header-size-10': [2.'always']}plugins: [
     // Define the plug-in
    {
      rules:{
        // Define rules
        'header-size-10': ({header}) = > {
           const pass = header.length > 10
           const message = 'Header contains a maximum of 10 characters
           return [pass, message]
        }
      }  
    }
  ]
Copy the code

Other plug-ins on the official website

Prompt reminders

A prompt similar to Commitizen provides a commit prompt tool.

The installation

  • Package installation
npm i -D  @commitlint/cli  @commitlint/config-conventional @commitlint/prompt-cli
Copy the code
  • The configuration file
// commitlint.config.js
module.exports = {extends: ['@commitlint/config-conventional']}
Copy the code
  • Package. The json Settings
"scripts": {
  "commit": "commit"
}
Copy the code
  • use

git add *

npm run commit
Copy the code

Commitizen adapter

Commitizen is officially provided with an adapter @commitLint/CZ-COMMITLint as an alternative to Prompt

The adapter consists of two main parts:

  • Messages: prompt message
  • Questions: Interactive configuration

Modifying adapters

Customize type prompts through the commitlint.config.json configuration file

Configure Chinese prompts for commands

@ commitline/cz – commitlint details