Skip to content

Instantly share code, notes, and snippets.

@jarrodparkes
Last active May 21, 2021 17:15
Show Gist options
  • Save jarrodparkes/46f4ae1eba7e8bb6ddb9bed32808e691 to your computer and use it in GitHub Desktop.
Save jarrodparkes/46f4ae1eba7e8bb6ddb9bed32808e691 to your computer and use it in GitHub Desktop.
Loosely enforces a [PROJECT]-[TICKET] naming structure for Git commits based on Git branch name.
#!/bin/bash
# SOURCE: https://git-scm.com/docs/githooks
# SOURCE: https://gist.github.com/bartoszmajsak/1396344
#==================================================================================================#
# Loosely enforces a [PROJECT]-[TICKET] naming structure for Git commits based on Git branch name. #
#==================================================================================================#
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
if [ "$BRANCH_NAME" == "master" ] || [ "$BRANCH_NAME" == "develop" ] || [ "$BRANCH_NAME" == "test" ] || [[ "$BRANCH_NAME" = release* ]]; then
# exit normally and don't modify the commit
exit 0
else
# otherwise, extract [PROJECT]-[TICKET] from the branch name
PROJECT_AND_TICKET=$(echo $BRANCH_NAME | grep -oE "[A-Z0-9]+-[0-9]+")
if [ -n "$PROJECT_AND_TICKET" ]; then
# prepend the commit message with "[PROJECT]-[TICKET]"
sed -i.bak -e "1s/^/$PROJECT_AND_TICKET: /" $1
else
echo "error: your branch name must contain \"[PROJECT]-[TICKET]\""
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment