#!/bin/sh
# Fast checks only. Measured on a warm tree: fmt 0.06s, clippy 0.07s,
# `cargo test --lib` 0.44s, so this hook is about six tenths of a second.
#
# The full `cargo test` is 4.0s — the integration tests run Monte Carlo ensembles
# in a debug build — and `cargo doc` wants the whole dependency graph, so both are
# in pre-push instead.
#
# Install: git config core.hooksPath .githooks
set -e

echo "pre-commit: cargo fmt --check"
cargo fmt --check

echo "pre-commit: cargo clippy"
cargo clippy --all-targets -- -D warnings

echo "pre-commit: cargo test --lib"
cargo test --quiet --lib

echo "pre-commit: US spelling"
staged=$(git diff --cached --name-only --diff-filter=ACM -- '*.rs' '*.md' '*.toml')
if [ -n "$staged" ]; then
    # shellcheck disable=SC2086
    hits=$(git grep -wniE -e 'metre|metres|kilometre|kilometres' \
        -e 'licence|defence|programme|catalogue|grey' \
        -e 'manoeuvre|manoeuvres|manoeuvred|manoeuvring' \
        -e 'normalise|normalised|normalises|normalising|normalisation' \
        -e 'stabilise|stabilised|stabilises|stabilising|stabilisation' \
        -e 'recognise|recognised|recognises|recognising' \
        -e 'monomorphise|monomorphised|monomorphises|monomorphising' \
        -e 'serialise|serialised|serialises|serialising|serialisation' \
        -e 'behaviour|behaviours|colour|colours|centre|centres' \
        -e 'analyse|analysed|analyses|analysing' \
        -e 'organise|organised|organisation|optimise|optimised|optimisation' \
        -e 'initialise|initialised|initialisation|utilise|utilised' \
        -e 'modelling|modelled|unmodelled|mismodelling|labelled|cancelled|travelled' \
        -e 'favour|favours|honour|honours|neighbour|neighbours' \
        --cached -- $staged || true)
    if [ -n "$hits" ]; then
        echo "$hits"
        echo
        echo "pre-commit: British spellings found above. This project uses US English."
        exit 1
    fi
fi

echo "pre-commit: ok"
