Skip to content

Instantly share code, notes, and snippets.

@PoisonousJohn
Last active September 23, 2019 15:56
Show Gist options
  • Save PoisonousJohn/7840f2436675dca530f08d9d3723e196 to your computer and use it in GitHub Desktop.
Save PoisonousJohn/7840f2436675dca530f08d9d3723e196 to your computer and use it in GitHub Desktop.
Prepend commit message with ticket number in case your branch is of format 'branch#123` where 123 is a number.
# based on https://medium.com/@nicklee1/prepending-your-git-commit-messages-with-user-story-ids-3bfea00eab5a
# Include any branches for which you wish to disable this script
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop staging test)
fi
# Get the current branch name and check if it is excluded
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
TRIMMED=$(echo $BRANCH_NAME | sed -n -r -e "s/.+(\#.+)$/\1/p")
# If it isn't excluded, preprend the trimmed branch identifier to the given message
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
sed -i.bak -e "1s/^/$TRIMMED /" $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment