Skip to content

Instantly share code, notes, and snippets.

@cbporch
Last active April 8, 2022 14:41
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 cbporch/7fedb6f456dd9b5c60e03d21c472ff99 to your computer and use it in GitHub Desktop.
Save cbporch/7fedb6f456dd9b5c60e03d21c472ff99 to your computer and use it in GitHub Desktop.
commit-msg: Prepend Jira ID to a commit in a git hook that Pycharm will execute
#!/bin/sh
#
# Automatically prepends branch name to every commit message
# if the branch name has a prefix with a Jira ticket id, i.e. FOO-123/my-branch.
#
# Get and clean up the present branch name, then grep it for a format similar to Jira ticket IDs
NAME=$(git branch | grep '\* ' | sed 's/* \((no branch, rebasing \)\{0,1\}//' | sed 's/\/.*//' \
| tr "[:lower:]" "[:upper:]" \
| grep -E "[A-Z]{2,10}-\d{1,6}")
# assert name exists & branch name is not already present in the commit, i.e. when rewording/amending
if [ -n "$NAME" ] && [ "" == "$(grep '^'"$NAME"':' $1)" ]
then
echo "branch not in commit msg"
echo "$NAME"': '"$(cat "$1")" > "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment