#!/usr/bin/env bash

function check_git_conflicts {
    if git rev-parse --verify HEAD >/dev/null 2>&1
    then
        against=HEAD
    else
        # Initial commit: diff against an empty tree object
        against="$(git hash-object -t tree /dev/null)"
    fi

    # If there are whitespace errors or conflict errors, print the offending file names and fail.
    git diff-index --check --cached "${against}" -- || exit 1
}

if [[ ! -x "$(command -v "quickstyle")" ]]; then
    echo >&2 "quickstyle not found, visit https://github.com/stackrox/workflow for installation instructions"
    exit 1
fi

echo >&2 "Checking style, if you want to skip pre-commit hooks use --no-verify"
quickstyle || exit 1

check_git_conflicts

if git status -s | grep -q ".groovy"; then
    echo >&2 "Checking style for groovy"
    make -C qa-tests-backend style || exit 1
fi
