Skip to content

Instantly share code, notes, and snippets.

@Konafets
Created August 21, 2020 16:56
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 Konafets/f159bfb9fe7ec803b9bc56c085bbc736 to your computer and use it in GitHub Desktop.
Save Konafets/f159bfb9fe7ec803b9bc56c085bbc736 to your computer and use it in GitHub Desktop.
This GIT hook populates the commit message with a prefix like [TASK] and suffix which contains the ticket number. It takes this information from the branch name. A branch name task/1234 will create a message: [TASK] (1234))
#!/bin/bash
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
# Only do something when we ARE NOT amending a commit!
if [ ! ${SHA1} ]; then
# We are only interesed in feature branches like feature/foo
if [ -z "$BRANCES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop)
fi
# Get the current branch name
BRANCH_NAME=$(git symbolic-ref --short HEAD)
# Get the first part of the branch name: foo/bar -> foo
# This is considered the type of the feature branch like bugfix, feature, task
FEATURE_BRANCH="${BRANCH_NAME%/*}"
FEATURE_BRANCH=$(tr '[:lower:]' '[:upper:]' <<< "$FEATURE_BRANCH")
# Get the last part of the branch name: foo/bar -> bar
# This is considered the JIRA issue number.
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_NAME=$(tr '[:lower:]' '[:upper:]' <<< "$BRANCH_NAME")
# When there are no digits in the second part or there is no second part, we do not have a ticket -> (-)
if [[ ! $BRANCH_NAME =~ [0-9] ]] || [ ! $BRANCH_NAME ]; then
BRANCH_NAME="-"
fi
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
if [ -n "$FEATURE_BRANCH" ]; then
sed -i.bak -e "1s/^/[$FEATURE_BRANCH] ($BRANCH_NAME)/" $1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment