Added pre-commit hook

This commit is contained in:
Wouter Bokslag
2026-02-22 19:48:58 +01:00
parent ea91232234
commit 9a971b9202
2 changed files with 36 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
#!/usr/bin/env bash
# This script installs the pre-commit hook, that checks whether staged files
# are formatted according to rustfmt.toml
set -euo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
ROOT="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null)" || {
echo "install.sh: not inside a git repository" >&2
exit 1
}
SRC="$SCRIPT_DIR/pre-commit"
DST="$ROOT/.git/hooks/pre-commit"
cp -f "$SRC" "$DST"
chmod +x "$DST"
echo "Installed: $DST"

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env bash
# This script has some limitations. Most notably, it checks the files on-disk, which is a problem
# if a file was edited, added and then re-edited. The added (and thus to be committed) file is
# different from the on-disk file. Furthermore, small differences between cargo ftm and rustfmt may exist.
# As this is just a helpful tool, we can accept those limitations.
set -euo pipefail
ROOT="$(git rev-parse --show-toplevel)"
CFG="$ROOT/rustfmt.toml"
git diff --cached --name-only -z --diff-filter=ACMR -- '*.rs' \
| xargs -0 -r rustfmt --config-path "$CFG" --check
echo "rustfmt check successful"