Skip to content

Instantly share code, notes, and snippets.

@eokoneyo
Last active July 21, 2022 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eokoneyo/beb835aba20c840e8055eb32e4602660 to your computer and use it in GitHub Desktop.
Save eokoneyo/beb835aba20c840e8055eb32e4602660 to your computer and use it in GitHub Desktop.
script to automatically include JIRA ticket in Git commit message
#!/bin/bash
# get current branch
branchName=$(git rev-parse --abbrev-ref HEAD)
branchName_uc=$(echo "${branchName}" | tr '[:lower:]' '[:upper:]')
# search jira issue id in a pattern such a "feature/ABC-123-description"
jiraId=$(echo "${branchName_uc}" | sed -nE 's/([^/]*\/)*([a-zA-Z]+-[0-9]+).*/\2/p')
# get git commit message file
commitMsgFile="$(git rev-parse --git-dir)/COMMIT_EDITMSG"
# only prepare commit message if pattern matched, jiraId was found and commit msg doesn't already have the ticket number
if [ -n "${jiraId}" ] && [ "${jiraId}" != "HEAD" ] && [ "$(grep -c "^\[${jiraId}\]" "${commitMsgFile}")" -eq 0 ]; then
sed -i.bak -e "1s/^/\[${jiraId}\] /" "${commitMsgFile}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment