Skip to content

Instantly share code, notes, and snippets.

@Tethik
Created August 13, 2020 16:42
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 Tethik/1127a48c4521e0c396f45cb4ebd65b49 to your computer and use it in GitHub Desktop.
Save Tethik/1127a48c4521e0c396f45cb4ebd65b49 to your computer and use it in GitHub Desktop.
#!/bin/bash
TICKET_REGEX="^(([a-zA-Z]+)\-([0-9]+))"
BRANCH_REGEX="^(feature|chore)\/(([a-zA-Z]+)\-([0-9]+))\-.*"
branch=$(git branch --show-current)
if [[ $branch =~ $BRANCH_REGEX ]]; then
ticket="${BASH_REMATCH[2]}"
content=$(cat $1 | grep -v '^#')
if [[ -z "${content// }" ]]; then
exit 0 # empty message
fi
if [[ $content =~ $TICKET_REGEX ]]; then
if [[ ${BASH_REMATCH[1]} -ne $ticket ]]; then
echo "Warning: Different Ticket ID '${BASH_REMATCH[1]}' (vs '${ticket}') already prepended to commit message"
exit 1
else
exit 0 # already prepended
fi
fi
echo "${ticket} ${content}" > $1
echo "Ticket ID '${ticket}' prepended to commit message"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment