diff --git a/contrib/pre-commit-hook/install_pre_commit_hook.sh b/contrib/pre-commit-hook/install_pre_commit_hook.sh new file mode 100755 index 0000000..bd35a2e --- /dev/null +++ b/contrib/pre-commit-hook/install_pre_commit_hook.sh @@ -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" \ No newline at end of file diff --git a/contrib/pre-commit-hook/pre-commit b/contrib/pre-commit-hook/pre-commit new file mode 100644 index 0000000..49f6c25 --- /dev/null +++ b/contrib/pre-commit-hook/pre-commit @@ -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"