I wonder whether it is possibel to automatically append the JIRA issue ID to every commit message automatically?
All our branch names include the issue id with the issue type name.
Is there a way to automatically append it to a commit message when commiting?
We are using script runner, maybe there is a way to do it with it?
By the time the commit is created with the original commit message, it gets its hash. If you changed the commit message afterward (even programatically), that'd result in a new hash which could eventually break things.
So, I'd suggest you'd rather require committers to enter the issue key in the commit message (a'priori).
You can use the git prepare-commit-msg hook. It's a script that get executed before you write the commit message. I've written such a script. It works for branches named such as "feature/ABC-123-description" and will add "ABC-123" to the commit message. And the developer has all the liability to modify the commit message afterwards. You can set up such a hook at repository level or globally on a machine. That means you must set up the script on every developer machine though.
# get current branch
branchName=`git rev-parse --abbrev-ref HEAD`
# search jira issue id in a pattern such a "feature/ABC-123-description"
jiraId=$(echo $branchName | sed -nr 's,[a-z]+/([A-Z]+-[0-9]+)-.+,\1,p')
# only prepare commit message if pattern matched and jiraId was found
if [[ ! -z $jiraId ]]; then
# $1 is the name of the file containing the commit message
sed -i.bak -e "1s/^/\n\n$jiraId\n/" $1
I'm getting an error in source tree when using this pre-commit hook.
the file SED tries to open IS present and the comment is there.
git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks commit -q -F C:\Users\Ralph\AppData\Local\Temp\ppjj2kg3.xax
sed: no input files
on Windows10 , GIT2.30.2 Sourcetree 3.4.7