12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #!/usr/bin/env bash
- # Description: Git hook to check the JIRA reference in the commit message
- # Usage: Copy this file on your hooksPath with the name "commit-msg"
- # Ref: https://aviatrix.atlassian.net/l/c/ARC3NMy1#Git-client-hook-for-automatic-Jira-ref-insertion
- set -e
- [ -z "${GITHOOK_DEBUG}" ] || set -x
- # hook arguments
- COMMIT_MSG_FILE=$1
- # Possible values are none (git commit), message (git commit -m <msg>), template, merge, squash, or commit
- COMMIT_SOURCE=$2
- BRANCH=$(git branch --show-current)
- function get_sed () {
- OS=$(uname -s)
- if [ "${OS}" == "Linux" ]; then
- SED=$(command -v sed)
- else
- SED=$(command -v gsed)
- fi
- if [ -z "${SED}" ]; then
- echo ">> GNU 'sed' was not found on the system."
- echo ">> Please install it"
- exit 1
- fi
- echo "${SED}"
- }
- function not_include_ticket () {
- set -o pipefail
- if $(cat "$*" | grep -q "^JIRA: "); then
- return 1
- fi
- return 0
- }
- # Function to extract the ticket number:
- # Ex: AVXSRE-12324-toto -> AVXSRE-1234
- # AVXSRE-12134/titi -> AVXSRE-1234
- function get_branch_name () {
- echo "$*" | ${SED} -e 's@\([[:alpha:]]\+\).*-\([[:digit:]]\+\).*@\1-\2@g'
- }
- SED="$(get_sed)"
- BRANCH="$(get_branch_name $(git branch --show-current))"
- # check branch name isn’t empty (typical e.g. during rebase)
- if not_include_ticket "${COMMIT_MSG_FILE}" && [ -n "${BRANCH}" ]; then
- # check that this is a "message": if a -m or -F option was given
- if [ "${COMMIT_SOURCE}" == "message" ]; then
- echo >> ${COMMIT_MSG_FILE}
- echo "JIRA: ${BRANCH}" >> ${COMMIT_MSG_FILE}
- exit 0
- fi
- # check that this is a "commit"
- if [ -z "${COMMIT_SOURCE}" ]; then
- ${SED} -i "1s@^@\n\nJIRA: ${BRANCH}@" ${COMMIT_MSG_FILE}
- exit 0
- fi
- # check that this is an "amend"
- if [ "${COMMIT_SOURCE}" == "commit" ]; then
- ${SED} -i "2s@^@\n\nJIRA: ${BRANCH}\n@" ${COMMIT_MSG_FILE}
- exit 0
- fi
- fi
|