This script is meant to be used with Husky as a prepare-commit-msg Git hook. Each time you commit, it extracts the issue identifier or user-story identifier from the current branch name and prefixes your commit message with the extracted identifier.
It supports identifiers of the form ABCD-1234
and 1234
, and will look for such identifiers right after the /
in the name of the current branch.
If you're on the branch feature/JIRA-874-cannot-login-on-macos
, for example, this hook will prefix each of your commit messages with [ JIRA-874 ]
.
There are simpler shell scripts that achieve the same, but this solution works on Windows too.
First, install Husky:
npm install husky --save-dev
Then, install this hook:
npm install prefix-commit-message --save-dev
Finally, add the following to package.json
:
"husky": {
"hooks": {
"prepare-commit-msg": "prefix-commit-message"
}
}
Pass JS snippet (which will be evaluated during the runtime) in single quotes as configuration param to override default prefix structure:
For example:
"prepare-commit-msg": "prefix-commit-message '${identifier}
'"
Will produce commit message: JIRA-874 cannot-login-on-macos