How to submit code in a civilized way

Time:2021-4-18

The most annoying things for programmers: writing tests, naming variables, and filling in code commit messages. Turn over a few open source projects, you can immediately aftertaste the youth of the composition.

In fact, the role of commit message is much more than that. After simple configuration, you can become a civilized citizen of code submission painlessly.

The role of commit message

At the very least, the project submission history is an important way for others (including future self) to understand the project. A good submission history can make it convenient for other people to participate in it, and it can also make it convenient for them to quickly locate problems.

In addition, the submission information can also be used to trigger CI construction, automatically generate changelog, and automatically upgrade the version semantics It only needs a little configuration to do so much. It’s really necessary for lazy people.

Choose a style

Like code style, commit message has various styles. If you don’t have a special hobby, it’s based on angular,
After independenceConventional CommitsStyle.
It is also basically the default configuration of each tool, so it doesn’t need to be tossed together.

Don’t remember the rules

Although there are not many rules, they may not be remembered at any time, especially for new people. They must be submitted in a friendly way.

Commitizen is a good choice. You can fill in the information by answering a few questions from the command line, which reduces the burden of memory.
It is a general tool that supports various styles through plug-ins. We choose Convention and need to install it
cz-conventional-changelog

npm install --save-dev commitizen cz-conventional-changelog

Then configurepackage.jsonYou can go through itnpm run commitSubmit.

{
  "scripts": {
    "commit": "git-cz"
  }
}

In addition, vscode users can also usevscode-commitizen
adoptctrl+shift+porcommand+shift+pSubmit.

Lint – lint is safe

Yes, the commit message also has linter, which can check the commit message and eliminate the typing hand and fishing in troubled waters.

Here, commitlint and husky are used to realize automatic detection.

Commitlint is also a common tool, which needs to install style configuration at the same time. Husky can easily use git hooks to trigger commit lint when commit.

npm install --save-dev @commitlint/cli @commitlint/config-conventional husky

Project root NEWcommitlint.config.js

module.exports = {
  extends: ['@commitlint/config-conventional']
}

to configurepackage.json

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

Auto update

Final installationstandard-versionAutomatic generation of changelog and automatic semantic upgrade of version are realized.

npm install --save-dev standard-version

to configurepackage.json

{
  "scripts": {
    "release": "standard-version"
  }
}

The first time you publish it, you can reset it with the following command

npm run release -- --first-release

Directly in the futurenpm run releaseThat’s it.

You can also specify the version manually:

# npm run script
npm run release -- --release-as minor
# Or
npm run release -- --release-as 1.1.0

Stick up the little red flowers

Adding a small badge to readme makes it easier for others to understand the style.

[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?maxAge=2592000)](http://commitizen.github.io/cz-cli/)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-brightgreen.svg?maxAge=2592000)](https://conventionalcommits.org)

Full configuration

install

npm install --save-dev commitizen cz-conventional-changelog @commitlint/cli @commitlint/config-conventional husky standard-version

to configurepackage.json

{
  "scripts": {
    "commit": "git-cz",
    "release": "standard-version"
  },
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  },
}

Project root NEWcommitlint.config.js

module.exports = {
  extends: ['@commitlint/config-conventional']
}

[End]

Recommended Today

Rust Study RoadMap

background In recent months, I began to learn rust, and began to write some code with rust. Now, I don’t have a deep understanding of rust, but it’s OK to write some code everyday, and I can learn the next step as needed. In the process of learning, the author also read a lot of […]