Skip to content

Instantly share code, notes, and snippets.

@ninjaparade
Created August 27, 2020 19:35
Show Gist options
  • Save ninjaparade/9bbfc61b6739cd14031058a21633b2eb to your computer and use it in GitHub Desktop.
Save ninjaparade/9bbfc61b6739cd14031058a21633b2eb to your computer and use it in GitHub Desktop.
Prefix Commit Messages with "[PROJECT_NAME_IN_CAPS]-[TICKET_NUMER]" from Branch Name
#!/bin/bash
# SOURCE: https://git-scm.com/docs/githooks
# SOURCE: https://gist.github.com/bartoszmajsak/1396344
# get the 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
# if "master" or "develop", then exit normally and don't modify the commit
exit 0
else
# otherwise, extract [PROJECT_NAME_IN_CAPS]-[TICKET_NUMBER] from the branch name
BRANCH_NAME=$(echo $BRANCH_NAME | grep -oE "[A-Z]+-[0-9]+")
# is the branch name in the source of the commit message?
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
# is the branch name non-empty?
# is this branch name in the source of the commit message?
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/$BRANCH_NAME: /" $1
else
echo "error: your branch name must contain \"[PROJECT_NAME_IN_CAPS]-[TICKET_NUMBER]\""
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment