Skip to content

Instantly share code, notes, and snippets.

@AdamStrojek
Last active July 11, 2019 09:00
Show Gist options
  • Save AdamStrojek/c3ad92916acec6963e0a198cc9d14946 to your computer and use it in GitHub Desktop.
Save AdamStrojek/c3ad92916acec6963e0a198cc9d14946 to your computer and use it in GitHub Desktop.
Prepend a Jira Issue ID to Git Commit Messages
#!/bin/sh
# To install it just enter in project:
# if [ -d .git/hooks ]; then HOOKF=.git/hooks/prepare-commit-msg && if [ ! -f $HOOKF ]; then curl -s https://gist.githubusercontent.com/AdamStrojek/c3ad92916acec6963e0a198cc9d14946/raw > $HOOKF && chmod 755 $HOOKF; else echo "Error: $HOOKF exists"; fi else echo "Error: Not a git repo"; fi
COMMIT_FILE=$1
COMMIT_MSG=$(cat $1)
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
JIRA_ID=$(echo "$CURRENT_BRANCH" | grep -Eo "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+")
if [ ! -z "$JIRA_ID" ]; then
echo "$JIRA_ID $COMMIT_MSG" > $COMMIT_FILE
echo "JIRA ID '$JIRA_ID', matched in current branch name, prepended to commit message. (Use --no-verify to skip)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment