Intro
At work, we prepend every commit message with its issue number, which, later, makes it easier for us
to link a code change to its issue.
As this is only a convention, every now and then, it occurs that a commit message does not include an issue number.
While this is not a problem on a local branch, it is on a plublic branch.
Because using git push --force
is frowned upon and is not recommended on public branches, I decided to invest 15 minutes of my coffee break and write a small git hook that kindly reminds me to add the issue number to my commit messages.
Given
Every commit message should start with five digits (e.g., 66911
), except for merge commits, which should start with Merge
instead.
Since we start the name of each feature branch with the very same issue number, the issue number of the commit message should match the issue number of the branch name.
When
I hereby present the kind git-hook script.
#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
# To enable this hook, store this file to ".git/hooks/commit-msg" and
# remove the ".git/hooks/commit-msg.sample" file.
# extracts the issue number from the branch name, which consists of the first five digits, in this case
current_branch_number="$(git rev-parse --abbrev-ref HEAD | grep -oE '^[0-9]{5}')"
# regex to validate commit-msg starts with five digits followed by a colon and space
normal_commit_regex='^[0-9]{5}: '
merge_commit_regex='^Merge'
error_msg="That's something I cannot allow to happen. Your commit message is missing '$current_branch_number: ' or 'Merge'."
# regex to validate commit-msg issue number matches the branch number
commit_issue_number="$(grep -oE '^[0-9]{5}' $1)"
wrong_number_msg="Where did you learn to type? Wrong issue number. Use $current_branch_number instead of $commit_issue_number."
if grep -qE "$normal_commit_regex" "$1"; then
# validate commit-msg issue number is equal to branch number
if [[ $commit_issue_number != $current_branch_number ]]; then
echo "$wrong_number_msg"
exit 1
fi
elif grep -qE "$merge_commit_regex" "$1"; then
# nothing to do here
:
else
echo "$error_msg" >&2
exit 1
fi
The complete script can be downloaded from Github Gist.
Then
Running the following git command yields the following error message:
$ git commit -m "Does not start with five digits nor the word 'Merge'"
That's something I cannot allow to happen. Your commit message is missing '66911: ' or 'Merge'.
Yes, I borrowed some lines from the sudo insults.
Executing the following git command on branch with the name 12345-fix-something
yields the following error message:
$ git commit -m "67890: Contains the wrong issue number"
Where did you learn to type? Wrong issue number. Use 12345 instead of 67890.
In case no branch exists or the commit should have a different issue number than the branch, add the
--no-verify
option to thegit commit
command to skip the execution of the script.
Conclusion
Works like a charm.
I thought about using the prepare-commit-msg
git hook to directly aid composing the commit message but I don't know if it's worth the effort.