We often need to manually maintain the CHANGELOG file before the release when we are working with multiple developers. However, if the development process does not standardize the commit, it is easy to miss some features during the maintenance of CHANGELOG, and manual maintenance of CHANGELOG files is tedious. Therefore, the COMMIT Message specification needs to be introduced to improve the maintenance efficiency of code and how to automatically generate CHANGELOG files.

This article introduces the Angular specification, which is currently the most widely used, reasonable, systematic, and equipped with tools.

Commit Message specification format

< type > (< scope >) : < subject > / / short line < body > / / short line < footer >Copy the code

Header

Type (required)

# main typeFeat. Fix a bug# special typeStyle: changes that do not affect the meaning of the code, such as removing whitespace, changing indentation, adding or deleting semicolons. Build: changes that are made to the builder or external dependencies, such as webpack, NPM refactor: changes that use revert when refactoring. Do git revert to print the message# do not use type
test: Adds tests or modifies existing tests PERF: Changes to improve performance CI: Changes to chore related to CI: does not modify SRC ortestRemaining modifications, such as changes to the build process or accessibility toolsCopy the code

Scope (Optional)

Scope is used to specify the scope of the commit impact, such as the data layer, the control layer, the view layer, and so on, depending on actual usage

Subject (required)

Subject A short description of the commit purpose, no more than 50 characters long

Body

The body is used to describe the detailed description of the commit. The content can be divided into multiple lines, mainly describing the situation before the change and the motivation for the change. Small changes are not required, but major requirements and updates should be added as much as possible to explain the body

Footer

break changes

Break changes Indicates whether disruptive changes (incompatible changes) have occurred. This parameter must be specified for changes involving break changes, such as version upgrade, interface parameter reduction, interface deletion, and interface migration.

affect issues

Affect Issues Indicates whether an issue is affected.

Git xcommitizen + cz-Xcomm-Changelog: Git xcomm-Changelog: Git xcomm-Changelog: Git xcomm-Changelog: Git xcomm-Changelog: Git xcomm-Changelog: Git xcomm-Changelog: Git xcomm-Changelog: Git xcomm-Changelog

Commitizen (Specification commit command line tool)

The installation

npm install -g commitizen
Copy the code

cz-conventional-changelog

Use mode (choose one of the two)

  • Global Installation use
npm install -g cz-conventional-changelog
Copy the code

In global mode, the ~/.czrc configuration file is required to specify Adapter for Commitizen.

echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
Copy the code
  • Used in the project

Run the following command to support Angular’s Commit Message format

commitizen init cz-conventional-changelog --save --save-exact
Copy the code

Commit to submit

Git Cz allows you to select a commit message as prompted by the build automation

Git Cz Enter interactive mode and fill in the blanks as prompted

1. Select the type of change that you're research type (
      
       ) 2. What is the scope of this change (e.g. component or file name)? 3. Write a short, imperative tense description of the change: 4. Provide a longer description of the change: (Press enter to skip) Write a long description of the changes (). Is (y/ N) a destructive modification? Default n (
       
) 6. Does this change affect any openreve issues? What problem did the (y/n) change fix? The default n (< footer >)
Copy the code

Commitlint & Husky (Commitlint message validation)

  • commitlint: used to check submitted information;
  • huskyhookTool, operating on phigit-commitgit-pushphase

Depend on the installation

npm i husky @commitlint/config-conventional @commitlint/cli -D
Copy the code

Configuration information

Commitlint configuration

Create a new commitlint.config.js configuration file in the root directory, or in package.json (either)

Commitlint. Config. Js file

rulesYou can also use the default without addingrulesconfiguration

More rules configuration information

module.exports = {
  extends: ['@commitlint/config-conventional'].rules: {
    'body-leading-blank': [2.'always'].// Body begins on a blank line
    'header-max-length': [2.'always'.72].// Header contains a maximum of 72 characters
    'subject-full-stop': [0.'never'].// subject does not end with '.'
    'type-empty': [2.'never'].// type is not empty
    'type-enum': [2.'always'['feat'.// New features, requirements
      'fix'./ / bug fixes
      'docs'.// Document content changed
      'style'.// Changes that do not affect the meaning of the code, such as removing Spaces, changing indentation, adding and removing semicolons
      'refactor'.// Code refactoring
      'test'.// Add or modify tests
      'chore'.// Do not modify other changes to SRC or test, such as changes to the build process or accessibility tools
      'revert'.// Execute git revert printed message]],}};Copy the code

Package. The json file

{  
  "commitlint": {
    "extends": ["@commitlint/config-conventional"]}}Copy the code

Husky configuration

The oldhuskyConfiguration, I tried it and it seems to have no effect, you can use the new configuration below

Package. The json file

{
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"}}}Copy the code

The newhuskyconfiguration

Active Hooks

Executing the following command generates a.husky folder

npx husky install
Copy the code

Add Hooks

Add content to the.husky file

npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'
Copy the code

Conventional- Changelog – CLI (Automatically generating Changelog file information)

The installation

npm install -g conventional-changelog-cli
Copy the code

Commands are configured and executed

Configure the commands into the package.json file script

{
  "scripts": {
    "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s"}}Copy the code

Run NPM run Changelog to automatically generate the Changelog. md file

npm run changelog
Copy the code

Note: Before running the NPM run Changelog command to automatically generate Changelog. md, you need to update the version according to SemVer specifications. Otherwise, the incremental CHANGELOG always has the previous COMMIT record.

Reference article: The Commitlint Git Guide Commit Message specification regulates your Commit messages and automatically generates them according to the Commit CHANGELOG.md