#!/bin/sh
# Copyright 2026 Petri Koistinen. Licensed under the Apache License, Version 2.0.
set -eu

REPO_ROOT="$(git rev-parse --show-toplevel)"
cd "$REPO_ROOT"

# Only an entirely prose-only staged change takes the documentation lane.
if python3 "$REPO_ROOT/script/documentation-gate.py" commit; then
    exit 0
else
    status=$?
    [ "$status" -eq 1 ] || exit "$status"
fi

# RefineID's own crates are never pinned: every commit builds the head of
# refineid-core main. Without the network the lock file keeps its core
# revision, and the daily track-core-main job moves it later. This comes
# first, so a moved core counts as a code-level change below.
if ./script/update-core.sh; then
    git add Cargo.lock
else
    echo "pre-commit: core main not reachable; the lock file keeps its core revision" >&2
fi

# The workspace version names the UTC day of the last code-level change.
# A commit that touches only documentation, workflows or hooks keeps it.
if git diff --cached --name-only | grep -q -v -E '^(\.github/|script/githook/|doc/|docs/)|\.md$|^LICENSE|^\.gitignore$|^\.editorconfig$'; then
    stamped=$(./script/version-stamp.sh)
    if [ -n "$stamped" ]; then
        echo "$stamped"
        git add Cargo.toml Cargo.lock
    fi
fi

exec "$REPO_ROOT/script/verify-commit.sh"
