We are using SourceTree on Windows and Jira, and our rule is that the commit message always has to include the ticket ID from Jira, like HAD-4234
.
It feels like this is something that can be automated. Also, sometimes people are forgetting this.
I have now tried and failed to come up with a way to do this. The main thing I tried was setting up a prepare-commit-msg
hook in .git\hooks
of the project, but that one didn't seem to have any effect at all (aka, it looks like SourceTree simply ignored it). I have tried a number of variations such as the following:
#!C:/Programs/Git/bin/sh.exe
#
# Inspects branch name and checks if it contains a Jira ticket number (i.e. ABC-123).
# If yes, commit message will be automatically prepended with [ABC-123].
#
# Useful for looking through git history and relating a commit or group of commits
# back to a user story.
#
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
# Ensure BRANCH_NAME is not empty and is not in a detached HEAD state (i.e. rebase).
# SKIP_PREPARE_COMMIT_MSG may be used as an escape hatch to disable this hook,
# while still allowing other githooks to run.
if [ ! -z "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "HEAD" ] && [ "$SKIP_PREPARE_COMMIT_MSG" != 1 ]; then
PREFIX_PATTERN='[A-Z]{2,5}-[0-9]{1,4}'
[[ $BRANCH_NAME =~ $PREFIX_PATTERN ]]
PREFIX=${BASH_REMATCH[0]}
PREFIX_IN_COMMIT=$(grep -c "\[$PREFIX\]" $1)
# Ensure PREFIX exists in BRANCH_NAME and is not already present in the commit message
if [[ -n "$PREFIX" ]] && ! [[ $PREFIX_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s~^~[$PREFIX] ~" $1
fi
fi
In the end, I am still left with a blank commit message box. I did manage to configure SourceTree to automatically link the commit ID to the Jira ticket when it is typed in, but that is not what I want.
What I want is that every developer on my team, when he opens the commit dialogue, is already provided with the ticket ID in the commit message box, like "HAD-4234: "
I have read about something called commit message templates, but my latest status is that those are not implemented yet for SourceTree on Windows.
So, is there a way to achieve this on SourceTree for Windows? Or is this simply impossible as of yet?