Skip to content

Instantly share code, notes, and snippets.

@drobinson
Created December 12, 2022 18:50
Show Gist options
  • Save drobinson/5ab1b6e21295129f093bdf8e771a3326 to your computer and use it in GitHub Desktop.
Save drobinson/5ab1b6e21295129f093bdf8e771a3326 to your computer and use it in GitHub Desktop.
Prepend commit message with Jira ticket number from the current branch
#!/bin/sh
# Put this file in .git/hooks/ to prepend all commit messages with the
# Jira ticket number found in the current branch name
# modified from https://community.atlassian.com/t5/Bitbucket-questions/automatically-append-JIRA-issue-ID-into-commit-message/qaq-p/605991#M24736
# 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 "s/^/\[$jiraId\] /" $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment