#!/usr/bin/env bash
#
# Git commit-msg hook to enforce coding standards
#
# @author      Steve Talbot
# @copyright   Copyright (c) Solviq Ltd 2016-7
# @license     MIT
# @package     solviq/skeleton-webapp

CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASEDIR="${CWD%/*/*/*}"
violation=0

# Check for ticket number in commit message (except for merges)
# using a variant on Atlassian's regular expression
# @link https://confluence.atlassian.com/stashkb/integrating-with-custom-jira-issue-key-313460921.html
if [ -f "$BASEDIR/.git/MERGE_MSG" ]; then
    echo "This appears to be a merge commit"
elif ! grep -E "(^|[^A-Za-z0-9_-])([A-Z][A-Z0-9_]+-[0-9]+)([^A-Za-z0-9_-]|$)" $1; then
    echo "Your commit message does not appear to contain a ticket number"
    ((violation++))
fi

# Allow commit?
if (( violation > 0 )); then
    echo "Fix violation(s) before commit!"
    exit 1
fi
exit 0
