#!/bin/sh
# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 IT Beratung Hermann GmbH
#
# Auto-format staged Rust before every commit, so unformatted code never reaches
# a commit, and therefore never reaches main. rustfmt is not run for you
# anywhere else, so this hook is the gate.
#
# Enable once per clone:  mise run setup-hooks   (sets core.hooksPath=.githooks)
set -eu

staged_rs=$(git diff --cached --name-only --diff-filter=ACM -- '*.rs' || true)
[ -z "$staged_rs" ] && exit 0

# rustfmt needs the crate/workspace context, so format the workspace, then
# re-stage only the files that were already staged. A staged file that also has
# unstaged edits gets its formatting folded into the commit too — inspect with
# `git diff` if you keep partial stages.
mise exec -- cargo fmt --all

printf '%s\n' "$staged_rs" | tr '\n' '\0' | xargs -0 git add --
