#!/bin/sh

echo "verifying commit contents..."


if [ $(git rev-parse --abbrev-ref HEAD) = "main" ]
then
	echo >&2 "do *not* commit to main"
	echo >&2 ""
	echo >&2 "rejecting"
	exit 1
fi

# based off of: https://deaddabe.fr

set -eu

if ! cargo fmt -- --check
then
	echo >&2 "your formatting is wrong"
	echo >&2 "in exceptional cases you can suppress this message"
	echo >&2 "with git commit --no-verify"
	exit 1
fi

if ! cargo clippy --all-targets -- -D warnings
then
	echo >&2 "there are some clippy issues"
	echo >&2 ""
	echo >&2 "rejecting"
	exit 1
fi

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

# Redirect output to stderr.
exec 1>&2

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --
