#!/bin/sh
# JIRA Commit Message Hook
# @daryltucker
# chmod +x .git/hooks/commit-msg

# Skip Rebases
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
if [[ $BRANCH_NAME =~ ^\(no\ branch* ]]; then
    exit 0
fi

IDENTIFIER=$(git branch | grep '*'  | sed 's/* //' | sed -E 's/^feature[s]*/([A-Z a-z]+-[0-9]+).*/\1/g')
MESSAGE="$(cat "$1" | sed -e 's/#.*$//')"

if [ -n "$MESSAGE" ] && [ -n "$IDENTIFIER" ]; then
    echo "$IDENTIFIER"': '"$(echo "$MESSAGE")"  | iconv -t UTF-8 > "$1"
else
    exit 0
fi