#!/bin/sh
# script/precommit: run by the git pre-commit hook; fails the commit if
# checks fail. Our own extension to scripts-to-rule-them-all. The Go
# extra: go.mod and go.sum must already be tidy, so a commit never
# carries a module file that go mod tidy would rewrite.
set -eu

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"

main() {
    cd "$ROOT"
    go mod tidy
    if ! git diff --quiet --exit-code -- go.mod go.sum; then
        echo "precommit: go mod tidy changed go.mod or go.sum;" \
            "stage them and retry" >&2
        exit 1
    fi
    "$SCRIPT_DIR/check"
}

main "$@"
