Last active
April 8, 2022 14:41
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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