#!/usr/bin/env bash

# This hook helps to enforce a commit message format convention
#
# It can be very frustrating when many commits are squashed for a final PR
# and the latter gets rejected due to incorrect commit message formatting
#
# Another reason for this enforcement is to provide valid input data for the
# automatic changelog generator which relies on the keywords found in the regex below

function log {
        # shellcheck disable=2317
        printf "[$(tput setaf 1)$(tput bold)error$(tput sgr0)] %s\n" "${@}"
}

message_file="${1}"
message="$(head -n1 "${message_file}")"
regex="^(bugfix|doc|docs|chore|feat|feature|fix|hotfix|perf|refactor|remove|security|style|test)(\([a-zA-Z0-9_-]+\))?!?:[[:space:]]+[[:graph:]]+([[:graph:]]|[[:space:]])*$"

if ! [[ "${message}" =~ ${regex} ]]; then
  log "Invalid commit message format please consult CONTRIBUTING for more details on how to write appropriate commit messages"
  exit 1
fi
