diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100644 index 0000000..e4caf43 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +set -euo pipefail + +echo "[pre-commit] Running cargo fmt --all" >&2 + +if ! command -v cargo >/dev/null 2>&1; then + echo "[pre-commit] cargo not found in PATH" >&2 + exit 1 +fi + +cargo fmt --all + +# Stage any Rust files that were reformatted +changed=$(git diff --name-only --diff-filter=M | grep -E '\\.rs$' || true) +if [ -n "$changed" ]; then + echo "$changed" | xargs git add + echo "[pre-commit] Added formatted files" >&2 +fi + +# Fail if further diffs remain (shouldn't happen normally) +unfmt=$(git diff --name-only --diff-filter=M | grep -E '\\.rs$' || true) +if [ -n "$unfmt" ]; then + echo "[pre-commit] Some Rust files still differ after formatting:" >&2 + echo "$unfmt" >&2 + exit 1 +fi + +exit 0 diff --git a/README.md b/README.md index 7c78d4f..60877b0 100644 --- a/README.md +++ b/README.md @@ -460,6 +460,18 @@ cargo run -p socktop -- ws://127.0.0.1:3000/ws cargo run -p socktop_agent -- --enableSSL --port 8443 ``` +### Auto-format on commit + +A sample pre-commit hook that runs `cargo fmt --all` is provided in `.githooks/pre-commit`. +Enable it (one-time): + +```bash +git config core.hooksPath .githooks +chmod +x .githooks/pre-commit +``` + +Every commit will then format Rust sources and restage them automatically. + --- ## Roadmap