diff --git a/.githooks/pre-commit b/.githooks/pre-commit index f29342da67e..14453843815 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -27,4 +27,57 @@ if git diff --cached --name-only | grep -q "^torch_pin.py$"; then fi fi -exit 0 +# --- lintrunner --- + +if ! command -v lintrunner >/dev/null 2>&1; then + echo "Warning: lintrunner not found. Skipping lint checks." + echo "Install with: pip install lintrunner lintrunner-adapters && lintrunner init" + exit 0 +fi + +if [ ! -f .lintrunner.toml ]; then + echo "Warning: .lintrunner.toml not found. Skipping lint checks." + exit 0 +fi + +git_dir=$(git rev-parse --git-dir) + +# Portable hash: sha256sum (Linux) or shasum (macOS) +if command -v sha256sum >/dev/null 2>&1; then + toml_hash=$(sha256sum .lintrunner.toml | cut -d' ' -f1) +else + toml_hash=$(shasum -a 256 .lintrunner.toml | cut -d' ' -f1) +fi +stored_hash="" +[ -f "${git_dir}/.lintrunner_init_hash" ] && stored_hash=$(cat "${git_dir}/.lintrunner_init_hash") + +if [ "${toml_hash}" != "${stored_hash}" ]; then + echo "Running lintrunner init..." + if lintrunner init; then + echo "${toml_hash}" > "${git_dir}/.lintrunner_init_hash" + else + echo "lintrunner init failed. Run 'lintrunner init' manually." + exit 1 + fi +fi + +staged_files=$(git diff --cached --name-only --diff-filter=ACMR) + +# Use HEAD^ if it exists (skip on initial commit) +revision_flag="--revision HEAD^" +if ! git rev-parse HEAD^ >/dev/null 2>&1; then + revision_flag="" +fi + +lintrunner -a $revision_flag --skip MYPY +lint_status=$? + +# Re-stage only paths that were already staged before lintrunner ran. +while IFS= read -r path; do + [ -z "${path}" ] && continue + if [ -f "${path}" ]; then + git add -- "${path}" || true + fi +done <<< "${staged_files}" + +exit $lint_status diff --git a/install_executorch.sh b/install_executorch.sh index 931728296e1..3289fc7c5b0 100755 --- a/install_executorch.sh +++ b/install_executorch.sh @@ -10,3 +10,8 @@ # so we avoid repeated symlink segments in downstream CMake paths. cd -- "$( realpath "$( dirname -- "${BASH_SOURCE[0]}" )" )" &> /dev/null || /bin/true ./run_python_script.sh ./install_executorch.py "$@" + +# Install git hooks if inside a git repo +if git rev-parse --git-dir > /dev/null 2>&1; then + git config core.hooksPath .githooks +fi