Skip to content

Instantly share code, notes, and snippets.

@inverse
Last active July 9, 2020 06:48
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 inverse/d0de698a88bfc7e392daec12b0d028f5 to your computer and use it in GitHub Desktop.
Save inverse/d0de698a88bfc7e392daec12b0d028f5 to your computer and use it in GitHub Desktop.
Git hook for adding branch name to commit message if it conforms to ticket naming convention
#!/bin/bash
# Prepend branch name to commit message in [] if matching regex. e.g. DEN-123
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
if [ -n "$BRANCH_NAME" ] && [[ $BRANCH_NAME =~ ^([A-Z]+-[0-9]+)(\-.+)?$ ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/[${BASH_REMATCH[1]}] /" $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment