Javascript is required
·
6 min read

How To Automatically Generate A Helpful Changelog From Your Git Commit Messages

How To Automatically Generate A Helpful Changelog From Your Git Commit Messages Image

Creating a changelog is a usual task if a new software version is going to be released. It contains all the changes which were made since the last release and is helpful to remember what has changed in the code and to be able to inform the users of our code.

In many projects, creating the changelog is a manual process that is often undesired, error-prone, and time-consuming. This article describes some tools that can help to automate the changelog creation based on the Git history.

Let's start with some basics.

Semantic Versioning

Semantic Versioning (SemVer) is a de facto standard for code versioning. It specifies that a version number always contains these three parts:

SemVer

  • MAJOR: is incremented when you add breaking changes, e.g. an incompatible API change
  • MINOR: is incremented when you add backward compatible functionality
  • PATCH: is incremented when you add backward compatible bug fixes

Conventional Commits

The Conventional Commits specification proposes introducing a standardized lightweight convention on top of commit messages. This convention dovetails with SemVer, asking software developers to describe in commit messages, features, fixes, and breaking changes that they make.

Developers tend to write commit messages that serve no purpose. Usually, the message does not describe where changes were made, what was changed, and what was the motivation for making the changes.

So I recommend writing commit messages using the Conventional Commits specification:

1<type>[optional scope]: <description>
2
3[optional body]
4
5[optional footer]

An example of such a message:

1fix(ABC-123): Caught Promise exception
2
3We did not catch the promise exception thrown by the API call
4and therefore we could not show the error message to the user

The commit type <type> can take one of these value:

  • fix: a commit of this type patches a bug in your codebase and correlates with the patch version in semantic versioning
  • feat: a commit of this type introduces a new feature to the codebase and correlates with a minor version in semantic versioning
  • BREAKING CHANGE: a commit that has the text BREAKING CHANGE: at the beginning of its optional body or footer section introduces a breaking API change and correlates with a major version in semantic versioning. A breaking change can be part of commits of any type. e.g., a fix:, feat: & chore: types would all be valid, in addition to any other type.

Other types like chore:, docs:, style:, refactor:, perf:, test: are recommended by the Angular convention. These types have no implicit effect on semantic versioning and are not part of the conventional commit specification.

I also recommend reading How to Write Good Commit Messages: A Practical Git Guide.

Auto-Generate Changelog

Now we can start to automate the changelog creation.

  1. Follow the Conventional Commits Specification in your repository. We will use @commitlint/config-conventional to enforce this via Git hooks.
  2. Use standard-version, a utility for versioning using SemVer and changelog generation powered by Conventional Commits.

I will demonstrate the usage based on this demo project which was initialized running npm init and git init.

The next step is to install husky, which sets up your Git hooks:

npx husky-init && npm install

Then install commitlint with a config, which will be used to lint your commit message:

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

As we are using config-conventional we are automatically following the Angular commit convention.

Now we need to tell Husky to run commitlint during the Git commit hook. Therefore, we need to add a commit-msg file to the .husky folder:

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

Finally, we create a .commitlintrc.json file which extends the rules from config-conventional:

json
1{
2  "extends": ["@commitlint/config-conventional"]
3}

Running git commit with an invalid message will now cause an error:

1▶ git commit -m "this commit message is invalid"
2husky > commit-msg (node v14.8.0)
3⧗   input: this commit message is invalid
4✖   subject may not be empty [subject-empty]
5✖   type may not be empty [type-empty]
6
7✖   found 2 problems, 0 warnings
8ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
9
10husky > commit-msg hook failed (add --no-verify to bypass)

and valid commits will work:

1▶ git commit -m "feat: initial feature commit"
2[master (root-commit) a87f2ea] feat: initial feature commit
3 5 files changed, 1228 insertions(+)
4 create mode 100644 .commitlintrc.json
5 create mode 100644 .gitignore
6 create mode 100644 index.js
7 create mode 100644 package-lock.json
8 create mode 100644 package.json

Now we are safe and can guarantee that only valid commit messages are in our repository.

Generate Changelog

Finally, we can create our changelog from our Git history. First step is to install standard-version:

npm i --save-dev standard-version

Now we can create some npm scripts in our package.json:

json
1"scripts": {
2    "release": "standard-version",
3    "release:minor": "standard-version --release-as minor",
4    "release:patch": "standard-version --release-as patch",
5    "release:major": "standard-version --release-as major"
6  },

The changelog generation can be configured via a .versionrc.json file or placing a standard-version stanza in your package.json.

In our demo we use a .versionrc.json file based on the Conventional Changelog Configuration Spec:

json
1{
2  "types": [
3    { "type": "feat", "section": "Features" },
4    { "type": "fix", "section": "Bug Fixes" },
5    { "type": "chore", "hidden": true },
6    { "type": "docs", "hidden": true },
7    { "type": "style", "hidden": true },
8    { "type": "refactor", "hidden": true },
9    { "type": "perf", "hidden": true },
10    { "type": "test", "hidden": true }
11  ],
12  "commitUrlFormat": "https://github.com/mokkapps/changelog-generator-demo/commits/{{hash}}",
13  "compareUrlFormat": "https://github.com/mokkapps/changelog-generator-demo/compare/{{previousTag}}...{{currentTag}}"
14}

An array of type objects represents the explicitly supported commit message types, and whether they should show up in the generated changelog file. commitUrlFormat is an URL representing a specific commit at a hash and compareUrlFormat is an URL representing the comparison between two git shas.

The first release can be created by running npm run release -- --first-release in the terminal:

1▶ npm run release -- --first-release
2
3> changelog-generator-demo@0.0.0 release /Users/mhoffman/workspace/changelog-generator-demo
4> standard-version "--first-release"
5
6✖ skip version bump on first release
7✔ created CHANGELOG.md
8✔ outputting changes to CHANGELOG.md
9✔ committing CHANGELOG.md
10✔ tagging release v0.0.0
11ℹ Run `git push --follow-tags origin master` to publish

An exemplary CHANGELOG.md could look similar to this one:

CHANGELOG First Release

What I like is that the changelog is divided by the type of commit, it contains links to the specific commits and link to the diff of the version.

Of course, you can always edit the auto-generated changelog to make it more readable though. The generated changelog Markdown text can be pasted into GitHub releases so that it shows up next to each release tag. There are a lot more options in the tools to customize linting commits or the changelog generation.

Conclusion

For lazy developers like me, an automatic changelog generation is a nice tool that saves me a lot of time. Additionally, we have better commit messages in our code repository as they follow an established specification.

It needs some time to get used to the commit convention. You could encounter some discussions in your team as all code contributors need to follow the convention. The Git hook solution should catch the wrong messages as early as possible but you could also add a guard in your CI/CD pipeline.

In my opinion, it is worth the effort to introduce the Git commit convention and the changelog generation in projects. We as developers do not need to invest much time & brain capacity for the changelog generation and have a helpful document where we can look up what has changed between our software releases. Additionally, we can easily share this with the users of our software so that they also see what they can expect from each new release.

I will never share any of your personal data. You can unsubscribe at any time.

If you found this article helpful.You will love these ones as well.
Chrome Recorder: Record, Replay and Measure User Flows Image

Chrome Recorder: Record, Replay and Measure User Flows

How I Increased My Productivity With Visual Studio Code Image

How I Increased My Productivity With Visual Studio Code

Use Nitro as Mock Server Image

Use Nitro as Mock Server

Use Shiki to Style Code Blocks in HTML Emails Image

Use Shiki to Style Code Blocks in HTML Emails