Skip to content

Commit

Permalink
ci: implement hooks git prepare-commit msg (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
luismayta committed Mar 17, 2021
1 parent eee30ec commit 32c201d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions provision/git/hooks/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
# -*- coding: utf-8 -*-

# this commit hook prepends the branch name to the commit message
# so that jira, github, gitlab can associate it with a ticket

if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(main master develop test)
fi

REGEX_VALIDATION="^[0-9]+$"
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"

BRANCH_EXCLUDED=$(printf "%s\\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\\[$BRANCH_NAME\]" "${1}")

if [[ -n "${BRANCH_NAME}" ]] && [[ "${BRANCH_NAME}" =~ ${REGEX_VALIDATION} ]] ; then
BRANCH_NAME="#${BRANCH_NAME}"
fi

if [ -n "${BRANCH_NAME}" ] && ! [[ ${BRANCH_EXCLUDED} -eq 1 ]] && ! [[ ${BRANCH_IN_COMMIT} -ge 1 ]]; then
sed -i.bak -e "1s/^/($BRANCH_NAME) /" "${1}"
fi

0 comments on commit 32c201d

Please sign in to comment.