#!/bin/bash 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 get_branch_name () { echo "$(git branch --show-current)" } # Function to extract the ticket number: # Ex: AVXSRE-12324-toto -> AVXSRE-1234 # AVXSRE-12134/titi -> AVXSRE-1234 function clean_branch_name () { echo "$1" | grep -E -o '[A-Z][A-Z0-9]+-[0-9]+' } function add_jira_ref () { # hook arguments COMMIT_MSG_FILE=$1 # Possible values are none (git commit), message (git commit -m ), template, merge, squash, or commit COMMIT_SOURCE=$2 SED="$(get_sed)" BRANCH="$(get_branch_name)" CLEAN_BRANCH="$(clean_branch_name $BRANCH)" # check branch name isn’t empty (typical e.g. during rebase) if not_include_ticket "${COMMIT_MSG_FILE}" && [ -n "${CLEAN_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: ${CLEAN_BRANCH}" >> "${COMMIT_MSG_FILE}" return 0 fi # check that this is a "commit" if [ -z "${COMMIT_SOURCE}" ]; then ${SED} -i "1s@^@\n\nJIRA: ${CLEAN_BRANCH}@" "${COMMIT_MSG_FILE}" return 0 fi # check that this is an "amend" if [ "${COMMIT_SOURCE}" == "commit" ]; then ${SED} -i "2s@^@\n\nJIRA: ${CLEAN_BRANCH}\n@" "${COMMIT_MSG_FILE}" return 0 fi fi }