annotate-git-commit
adds ticket identifier parsed from branch name to Git commit messages.
If you include ticket identifier in a regularly parseable format in your ticket names, you can add a call to annotate-git-commit
in your prepare-commit-msg
git hook and it will add the parsed ticket identifier to your commit messages.
Here's an example of a .git/hooks/prepare-commit-msg
script that parses ClubHouse identifiers:
annotate-git-commit add-ticket '\b(ch\d+)\b' "$1"
When you type git commit
in the repository and your branch name is feature/ch1234/foo
, the commit message will end in the line Ticket: ch1234
.
annotate-git-commit
is built in Swift 4.2. It has been tested on a Mac (running macOS 10.14.1), but it should work fine on Linux too.
To install it, clone the repository and in the repository root run:
swift build -c release -Xswiftc -static-stdlib
cp ./.build/x86_64-apple-macosx10.10/release/annotate-git-commit /usr/local/bin
You can also install it with mint:
mint install juri/annotate-git-commit
Running annotate-git-commit --help
outputs a list of subcommands. Run annotate-git-commit COMMAND --help
to get help on a command.
annotate-git-commit
uses ICU regular expressions. To test a regexp against a branch name, use annotate-git-commit test-regexp
.
Once you have a good regexp for your repository, add the following as .git/hooks/prepare-commit-msg
:
annotate-git-commit add-ticket 'REGEXP' "$1"
with REGEXP
replaced with your regular expression.
annotate-git-commit
will exit with a status code in case of an IO error or failed regular expression parsing. You can choose how it behaves in case of failed ticket name parsing (i.e. invalid branch name or failed git execution.)
The default is to omit the ticket information from the commit but to exit successfully so as to not cause problems just because you didn't have a ticket id in your branch name. You can specify other behaviors:
- Adding the flag
--abort
will make it abort on error with an error message and error status. --placeholder PLACEHOLDER
will use PLACEHOLDER instead of the ticket id in the commit message.