Skip to content

Commit c2d602a

Browse files
author
Abhinay Kukkadapu
committed
Add lintrunner pre-commit hook
Summary: Run lintrunner automatically on every commit via .githooks/pre-commit. Hooks are enabled by default through install_executorch.sh. Test Plan: Verified lint violations block commits and clean files pass.
1 parent 1e9bd73 commit c2d602a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

.githooks/pre-commit

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,25 @@ if git diff --cached --name-only | grep -q "^torch_pin.py$"; then
2727
fi
2828
fi
2929

30-
exit 0
30+
# --- lintrunner ---
31+
32+
if ! command -v lintrunner >/dev/null 2>&1; then
33+
echo "Warning: lintrunner not found. Skipping lint checks."
34+
echo "Install with: pip install lintrunner lintrunner-adapters && lintrunner init"
35+
exit 0
36+
fi
37+
38+
staged_files=$(git diff --cached --name-only --diff-filter=ACMR)
39+
40+
lintrunner -a --revision 'HEAD^' --skip MYPY
41+
lint_status=$?
42+
43+
# Re-stage only paths that were already staged before lintrunner ran.
44+
while IFS= read -r path; do
45+
[ -z "${path}" ] && continue
46+
if [ -f "${path}" ]; then
47+
git add -- "${path}" || true
48+
fi
49+
done <<< "${staged_files}"
50+
51+
exit $lint_status

install_executorch.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@
1010
# so we avoid repeated symlink segments in downstream CMake paths.
1111
cd -- "$( realpath "$( dirname -- "${BASH_SOURCE[0]}" )" )" &> /dev/null || /bin/true
1212
./run_python_script.sh ./install_executorch.py "$@"
13+
14+
# Install git hooks if inside a git repo
15+
if git rev-parse --git-dir > /dev/null 2>&1; then
16+
git config core.hooksPath .githooks
17+
fi

0 commit comments

Comments
 (0)