Skip to content

Instantly share code, notes, and snippets.

@robsquires
Last active September 14, 2017 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robsquires/b36fe685b2c9c02e34a4322a370f93dd to your computer and use it in GitHub Desktop.
Save robsquires/b36fe685b2c9c02e34a4322a370f93dd to your computer and use it in GitHub Desktop.
Pre populate the jira prefix on commit messages
# abort if .git-hooks is not present
# add .git-hooks to .git-ignore so people can opt-in
if [ ! -f .git-hooks ]; then
exit 0
fi
# some commit operations we dont want to prefix the message
if [[ "$2" == "merge" || "$2" == "commit" || "$2" == "template" || "$2" == "squash" ]]; then
echo "Not a regular commit so ignoring.."
exit 0
fi
# get initials from the dev's GIT config
# got to be a tidier regex for this
initials=$(git var GIT_AUTHOR_IDENT | sed 's/^\([A-Za-z]\{1\}\).* \([A-Za-z]\{1\}\).*$/\1\2/')
##
# override the prefix
##
# 1. default is just the dev's initials
prefix="[$initials][]"
# 2. dev is continuing a line of commits on the same ticket (i.e not after a merge or after someone elses line of commits)
# [RS DW][MR-312] updated... -> extract [RS DW][MR-312]
# [DW JR][MR-310] send.... -> ignore
# Merged branch... -> ignore
regex="^\[${initials}"
match=$(git log -1 --pretty=%B | grep -e "$regex")
if [ -n "$match" ]; then
prefix=$(git log -1 --pretty=%B | sed 's/^\(\[.*\]\).*$/\1/')
fi
# add prefix
echo "$prefix\n$(cat $1)" > "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment