Skip to content

Instantly share code, notes, and snippets.

@jimgolfgti
Created May 9, 2017 09:41
Show Gist options
  • Save jimgolfgti/bd4319fbeff5d48395eda9ba8cafcc29 to your computer and use it in GitHub Desktop.
Save jimgolfgti/bd4319fbeff5d48395eda9ba8cafcc29 to your computer and use it in GitHub Desktop.
Prepend Jira ticket references to git commit message
#!/bin/sh
# If commit message does not refer to any JIRA issue number,
# this hook will parse the JIRA issue from the current branch and
# prepend it to the commit message.
# It will also ignore any brnahces that have no valid JIRA issue number
#
# Example:
# Branch: GG-645 or GG-645-some-text
# commit message: Updated README.md
# prepended commit message: CW-645 Updated README.md
#
# Example:
# Branch: foo-bar-baz
# commit message: Updated README.md
# prepended commit message: Updated README.md
#
# Install
# Copy this file into your projects .git/hooks/prepare-commit-msg file
#
parse_issue () {
local match
local re='\([[:alpha:]]\+-[[:digit:]]\+\).*'
match=$(echo $1 | sed -n "s/.*[^a-zA-Z]$re/\1/p")
[ -z "$match" ] && match=$(echo $1 | sed -n "s/^$re/\1/p")
match=$(echo $match | tr [a-z] [A-Z])
echo $match
}
source=$2
commit_msg=$(cat $1)
branch=$(git symbolic-ref --short HEAD)
commit_issue=$(parse_issue "$commit_msg")
branch_issue=$(parse_issue "$branch")
if [ "$source" = "message" -a -n "$branch_issue" -a -z "$commit_issue" ]
then
new_commit_msg="$branch_issue $commit_msg"
echo $new_commit_msg > $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment