Streamline Your Workflow: Automate JIRA Issue ID Prefixes for Commits
Enhancing Git History Tracking
When I joined KICKGOING, they had a coding convention. We had to give Issue Number(Ticket ID) to the commit message like ‘RIDER-100 …’
Why it is required? The commit message give us information what we did in the commit. But we can’t why did it and who ordered.
If the source control is linked to the issue tracking system, we can jump to the task by tapping the prefix.
I sometimes missed or mistyped it. So I began to found a way to automate the job.
Modifying Commit Message
First thing what I researched was how to modify exist commit message by script without rebase command.
Git provides hook as plugin. Maybe you can see hooks directory under .git of your project. If no, you should create it.
Let’t see what hooks Git provides.
One of this hooks can do what we want. It’s prepare-commit-msg and it means a script for Commit Message Preparing Stage.
Initially, the file would be empty.
So how can we modify the message with it?
The script takes parameters and first parameter has a path for the file containing the commit message.
We can overwrite it like following script.
echo "new commit message" > $1
Extracting Issue Number
Each branch has name contaning issue number like ‘RIDER-100 add xxx feature’.
I took the name by command.
git rev-parse — abbrev-ref HEAD
However I needed only an issue id, not full name.
It is a little bit difficult to understand, but I created shell script to extract only the project id and issue number. I won’t tell details.
echo ${branch_name} | cut -d ‘-’ -f1–2 | cut -d ‘-’ -f1–2 | tr ‘[:lower:]’ ‘[:upper:]’
This extracts only RIDER-100 from RIDER-100 add xxx feature.
Attaching Issue Number
To attach the issue id to current commit message, I merged together.
COMMIT_MSG_FILE=$1
branch_name=`git rev-parse --abbrev-ref HEAD`
issue_number=`echo ${branch_name} | cut -d '-' -f1-2 | cut -d '-' -f1-2 | tr '[:lower:]' '[:upper:]'`
commit_msg=`head ${COMMIT_MSG_FILE}`
and overwrite the commit message file with it.
echo "${issue_number} ${commit_msg}" > ${COMMIT_MSG_FILE}
Permission
I expected the script run when I committed with this error.
hint: The ‘.git/hooks/prepare-commit-msg’ hook was ignored because it’s not set as executable.
hint: You can disable this warning with `git config advice.ignoredHook false`.
What is wrong? prepare-commit-msg doesn’t have a permission to run.
We can give it by execute this shell. I won’t going to tell more.
chmod ug+x
You probably don’t know shell command like that.
So I created script to turn or/off the hook.
config-git-commit-prefix.sh
#!/bin/sh
#script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
script_dir=$(dirname "$0")
script_path=$script_dir/../.git/hooks/prepare-commit-msg
if [ $1 = "1" ]; then
chmod ug+x $script_path
echo Enable prepare-commit-msg hook
else
chmod ug-x $script_path
echo Disable prepare-commit-msg hook
fi
You need just to run this command to turn off or on the hook.
config-git-commit-prefix.sh 0
Sharing
Git ignores .git directory, so we can’t push it to repository to share with our colleagues.
So I decided to make a script to create the hook and turn on it.
setup-git-hooks.sh
#!/bin/sh
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
hook_path=.git/hooks/prepare-commit-msg
hook_full_path=$script_dir/../$hook_path
echo "#!/bin/sh\n\nsh Scripts/add-commit-prefix.sh \$@" > $hook_full_path
echo "# Git hook created #"
echo path: $hook_full_path
sh $script_dir/config-git-commit-prefix.sh 1
This will make a prepare-commit-msg to run add-commit-prefix.sh.
and add-commit-prefix.sh has this content.
#!/usr/bin/env bash
COMMIT_MSG_FILE=$1
branch_name=`git rev-parse --abbrev-ref HEAD`
issue_number=`echo ${branch_name} | cut -d '-' -f1-2 | cut -d '-' -f1-2 | tr '[:lower:]' '[:upper:]'`
commit_msg=`head ${COMMIT_MSG_FILE}`
echo "${issue_number} ${commit_msg}" > ${COMMIT_MSG_FILE}
Others just need to run setup-git-hooks.sh after pulling to use this automation.
Rebase
While rebasing, the branch can’t be detached. So we should not to run this script during the task(rename or editing, etc).
if [[ ${branch_name} == "HEAD" ]]; then
exit
fi
echo "${issue_number} ${commit_msg}" > ${COMMIT_MSG_FILE}
Duplication
Maybe you may not remember this automation and just put the issue id also. Then the commit message will be ‘MSWG-100 MSWG-100 …’
So we should check if the commit message already contains it.
if [[ ${commit_msg} == *"${issue_number}"* ]]; then
exit
fi
echo "${issue_number} ${commit_msg}" > ${COMMIT_MSG_FILE}
Xcode
You might want to see any message for this hook. However you saw the error.
But this is not an error, visual studio code doesn’t emit the error. And we can see the new commit is created.
If you found this post helpful, please give it a round of applause 👏. Explore more Git-related content in my other posts.