#!/usr/bin/env sh
echo "Running commit message checks..."

. "$(dirname -- "$0")/../../.gitea/conventional_commits/hooks/text-styles.sh"


# Get the commit message
commit="$(cat .git/COMMIT_EDITMSG)"
# Define the conventional commit regex
regex='^((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.+\))?(!?):\s([a-zA-Z0-9-_!\&\.\%\(\)\=\w\s]+)\s?(,?\s?)((ref(s?):?\s?)(([A-Z0-9]+\-[0-9]+)|(NOISSUE))))|(release: .*)$'

# Check if the commit message matches the conventional commit format
if ! echo "$commit" | grep -Pq "$regex"
then
    echo
    colorPrint red "❌ Failed to create commit. Your commit message does not follow the conventional commit format."
    colorPrint red "Please use the following format: $(colorPrint brightRed 'type(scope)?: description')"
    colorPrint red "Available types are listed below. Scope is optional. Use ! after type to indicate breaking change."
    echo
    colorPrint brightWhite "Quick examples:"
    echo "feat: add email notifications on new direct messages refs ABC-1213"
    echo "feat(shopping cart): add the amazing button ref: DEFG-23"
    echo "feat!: remove ticket list endpoint ref DADA-109"
    echo "fix(api): handle empty message in request body refs: MINE-82"
    echo "chore(deps): bump some-package-name to version 2.0.0 refs ASDF-12"
    echo
    colorPrint brightWhite "Commit types:"
    colorPrint brightCyan "build: $(colorPrint white "Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)" -n)"
    colorPrint brightCyan "ci: $(colorPrint white "Changes to CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)" -n)"
    colorPrint brightCyan "chore: $(colorPrint white "Changes which doesn't change source code or tests e.g. changes to the build process, auxiliary tools, libraries" -n)"
    colorPrint brightCyan "docs: $(colorPrint white "Documentation only changes" -n)"
    colorPrint brightCyan "feat: $(colorPrint white "A new feature" -n)"
    colorPrint brightCyan "fix: $(colorPrint white "A bug fix" -n)"
    colorPrint brightCyan "perf: $(colorPrint white "A code change that improves performance" -n)"
    colorPrint brightCyan "refactor: $(colorPrint white "A code change that neither fixes a bug nor adds a feature" -n)"
    colorPrint brightCyan "revert: $(colorPrint white "Revert a change previously introduced" -n)"
    colorPrint brightCyan "test: $(colorPrint white "Adding missing tests or correcting existing tests" -n)"
    echo

    colorPrint brightWhite "Reminders"
    echo "Put newline before extended commit body"
    echo "More details at $(underline "http://www.conventionalcommits.org")"
    echo
    echo "The commit message you attempted was: $commit"
    echo
    exit 1
fi