#!/usr/bin/env sh
if [ -z "$1" ]; then
	echo "Missing argument (commit message). Did you try to run this manually?"
	exit 1
fi

commitTitle="$(cat $1 | head -n1)"

# ignore merge requests
if echo "$commitTitle" | grep -qE "Merge branch"; then
	echo "Commit hook: ignoring branch merge"
	exit 0
fi

# check semantic versioning scheme
if ! echo "$commitTitle" | grep -qE '^(feat|fix|docs|style|refactor|perf|test|chore|build|ci|revert)(\([a-z0-9\s\-\_\,]+\))?!?:\s(GH-([0-9]{4,10}))\s\w'; then
	echo "Your commit message did not follow semantic versioning: $commitTitle"
	echo ""
	echo "Format:   <type>(<optional-scope>)<optional-!-breaking-change>: GH-xxxx <subject>"
	echo "Example 1:                   feat(api): GH-2531 add endpoint"
	echo "Example 2 (Breaking change): feat!: GH-3120 change parameters in function formatDate"
	echo ""
	echo "Breaking changes: use ! after <type> and <scope> to express that the change is a breaking change"
	echo "Valid types: build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test"
        echo "    build: Changes that affect the build system or external dependencies (example scopes: gulp, npm)"
        echo "    ci: Changes to our CI configuration files and scripts (examples: GitLab, Jenkins)"
        echo "    docs: Documentation only changes"
        echo "    feat: A new feature"
        echo "    fix: A bug fix"
        echo "    perf: A code change that improves performance"
        echo "    refactor: A code change that neither fixes a bug nor adds a feature"
        echo "    test: Adding missing tests or correcting existing tests"
	echo ""
	echo "Please see"
	echo "- https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#commit-message-format"
	echo "- https://www.conventionalcommits.org/en/v1.0.0/#summary"
	exit 1
fi