How to validate commit message convention using Commitlint and Husky
Tags: javascript, node, git
Published: 2020-04-24 20:19:58

Commit messages are so important when you’re working with a team, Make other members understand what did you done. So although the team has agreed to a convention, You may found some mistakes sometimes :eyes:

Here I’ll show How to validate a commit message before doing a commit and make the convention rules.

Lets to understand our dependencies.

#### **:boom:GitHooks with Husky**

Git Hooks is an efficient feature from git to help us execute scripts or programs before or after some events like: Commit, Merge, Push, And others.

So we need to use commit-msg hook to can validate the message.

Why need to use Husky?

You need to set up these hooks inside your project to can ensure other members uses that convention. So Husky comes for rescue
It gives us a configuration to setup it in our package.json.

To install Husky

:point_right: `npm install husky --save-dev`

Setup configuration

```
// package.json
{
  "husky": {
    "hooks": {
      "commit-msg": "excute validation script here"
    }
  }
}
```

#### **:boom:CommitLint && Commit CLI**

commitlint lint messages based on commons converntions.

By default, it uses [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) and this is the repo [Conventional Commits Repo](https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional#type-enum)
Also, you can use another conventions like [Angular Commits Convention](https://github.com/conventional-changelog/commitlint/tree/master/@commitlint/config-angular) Or Use Conventions made by contributors like [Jira Convention](https://github.com/Gherciu/commitlint-jira)

**Install commitlint cli and conventional config**
:point_right: `npm install --save-dev @commitlint/{config-conventional,cli}`

**For Windows:**
:point_right: `npm install --save-dev @commitlint/config-conventional @commitlint/cli`

**Configure commitlint to use conventional config**

```
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
```

that will create configuration file called `commitlint.config.js` to setup what convention want to use.

**Note:** Configuration is picked up from `commitlint.config.js` or `commitlintrc.js` or `.commitlintrc.json` or `.commitlintrc.yml` file or a `commitlint field` in `package.json`

**Now You Need To Tell Husky to use Commitlint when commit-msg executes By Edit Husky Object in Package.json**

```
// package.json
{
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  }
}
```

Now All things are setuped if you run with wrong commit message will give you an error as below

```
git commit -m "foo: this will fail"
husky > commit-msg (node v10.1.0)
No staged files match any of provided globs.
⧗   input: foo: this will fail
✖   type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum]

✖   found 1 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

husky > commit-msg hook failed (add --no-verify to bypass)
```

**For more detailed setup Instruction**

1. [Local Setup](https://conventional-changelog.github.io/commitlint/#/guides-local-setup)
2. [CI Setup](https://conventional-changelog.github.io/commitlint/#/guides-ci-setup)

**Note: All Dependencies installed as Development Dependencies you Don't need to take it to production**

**See [DEMO]**
[DEMO]:https://github.com/omarfesal/commitLintDemo