Skip to content

Instantly share code, notes, and snippets.

@oleksiilevzhynskyi
Created July 15, 2018 08:28
Show Gist options
  • Save oleksiilevzhynskyi/dd2fe14efa833738a15d50d3297bbaff to your computer and use it in GitHub Desktop.
Save oleksiilevzhynskyi/dd2fe14efa833738a15d50d3297bbaff to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Automatically adds ticket name to every commit message
# when branch name starts with "XX-00"
#
# Don't change message when it already contains any ticket name.#
#
# Don't do anything when in merge - https://git-scm.com/docs/githooks#_prepare_commit_msg
[ -f $GIT_DIR/MERGE_MSG ] && exit 0
# Get ticket name
SHORT_NAME=$(git symbolic-ref --short HEAD | sed -e "s|^\([A-Z]\{1,\}-[0-9]\{1,\}\).*|\1|")
echo "Checking commit message..."
# Append ticket name only when it is exist
if [[ "$SHORT_NAME" =~ ([A-Z]+-[0-9]+) ]]; then
if [[ `cat $1` =~ ([A-Z]+-[0-9]+) ]]; then
echo "Message is ok, nothing to add."
else
echo "Appending: $SHORT_NAME"
sed -i.bak -e "1s/^/$SHORT_NAME /" $1
fi
else
echo "Message is ok, nothing to add."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment