-
Notifications
You must be signed in to change notification settings - Fork 928
Add lintrunner pre-commit hook #18689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,57 +1,63 @@ | ||
| # Git Hooks | ||
|
|
||
| This directory contains Git hooks for the ExecuTorch repository. | ||
| This directory contains Git hooks for the ExecuTorch repository. It is used as | ||
| `core.hooksPath`, so git looks here instead of `.git/hooks/`. | ||
|
|
||
| ## Pre-commit Hook | ||
| ## Hooks | ||
|
|
||
| The pre-commit hook automatically updates the PyTorch commit pin in `.ci/docker/ci_commit_pins/pytorch.txt` whenever `torch_pin.py` is modified. | ||
| ### pre-commit | ||
|
|
||
| ### How It Works | ||
| Runs on every commit: | ||
|
|
||
| 1. When you commit changes to `torch_pin.py`, the hook detects the change | ||
| 2. It parses the `NIGHTLY_VERSION` field (e.g., `dev20251004`) | ||
| 3. Converts it to a date string (e.g., `2025-10-04`) | ||
| 4. Fetches the corresponding commit hash from the PyTorch nightly branch at https://github.com/pytorch/pytorch/tree/nightly | ||
| 5. Updates `.ci/docker/ci_commit_pins/pytorch.txt` with the new commit hash | ||
| 6. Automatically stages the updated file for commit | ||
| 1. **torch_pin sync** — when `torch_pin.py` is staged, updates the PyTorch commit | ||
| pin in `.ci/docker/ci_commit_pins/pytorch.txt` and syncs grafted c10 files. | ||
| 2. **lintrunner** — runs `lintrunner -a --revision HEAD^ --skip MYPY` on changed | ||
| files. Auto-fixes formatting and blocks on lint errors. Soft-fails if lintrunner | ||
| is not installed. Runs `lintrunner init` automatically when `.lintrunner.toml` | ||
| changes. | ||
|
|
||
| ### Installation | ||
| ### pre-push | ||
|
|
||
| To install the Git hooks, run: | ||
| Delegates to `.git/hooks/pre-push` if one exists. This allows backend-specific | ||
| pre-push hooks (e.g., ARM's license and commit message checks) to work alongside | ||
| the repo-wide hooks. | ||
|
|
||
| ## Installation | ||
|
|
||
| Hooks are installed automatically by `./install_executorch.sh`. | ||
|
|
||
| To install manually: | ||
|
|
||
| ```bash | ||
| .githooks/install.sh | ||
| git config core.hooksPath .githooks | ||
| ``` | ||
|
|
||
| This will copy the pre-commit hook to `.git/hooks/` and make it executable. | ||
| ### ARM backend pre-push | ||
|
|
||
| ### Manual Usage | ||
|
|
||
| You can also run the update script manually at any time: | ||
| ARM contributors should additionally install the ARM-specific pre-push hook: | ||
|
|
||
| ```bash | ||
| python .github/scripts/update_pytorch_pin.py | ||
| cp backends/arm/scripts/pre-push .git/hooks/ | ||
| ``` | ||
|
|
||
| ### Uninstalling | ||
| ## Bypassing | ||
|
|
||
| To remove the pre-commit hook: | ||
| To skip hooks for a single commit or push: | ||
|
|
||
| ```bash | ||
| rm .git/hooks/pre-commit | ||
| git commit --no-verify | ||
| git push --no-verify | ||
| ``` | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| If the hook fails during a commit: | ||
| If the torch_pin hook fails: | ||
|
|
||
| 1. Check that Python 3 is available in your PATH | ||
| 2. Ensure you have internet connectivity to fetch commits from GitHub | ||
| 3. Verify that the `NIGHTLY_VERSION` in `torch_pin.py` is in the correct format (`devYYYYMMDD`) | ||
| 4. Make sure the corresponding nightly release exists in the PyTorch nightly branch | ||
|
|
||
| You can run the script manually to see detailed error messages: | ||
| If lintrunner fails: | ||
|
|
||
| ```bash | ||
| python .github/scripts/update_pytorch_pin.py | ||
| ``` | ||
| 1. Run `lintrunner init` to install linter tools | ||
| 2. Check that your virtual environment is activated |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Delegate to local pre-push hook if present (e.g., ARM backend). | ||
| # .githooks/ is set as core.hooksPath, so git won't look in .git/hooks/ | ||
| # automatically. This passthrough ensures local hooks still run. | ||
| local_hook="$(git rev-parse --git-dir)/hooks/pre-push" | ||
| if [ -x "$local_hook" ]; then | ||
| "$local_hook" "$@" | ||
| fi |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if someone else (IIRC Arm) installs their own pre-commit hooks, would it collide with that? Also Arm has more scripts here for push etc - https://github.com/pytorch/executorch/tree/19bbeac41ab4ba21aa95a44d464e72b8da571302/backends/arm/scripts We should consolidate.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @digantdesai good point, it seems their pre-commit hook is a subset of what we are doing and pre-push hook is very arm specific, i think we can consolidate the pre-commit but pre-push we should leave it as-is, because it has some sign-off checks, commit format etc which might surprise others. What i will do is we can let the installation via install_executorch take precedence per hook, and honor the backend's own hooks via thin wrapper and call them if installed.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also CC arm POCs if needed as fyi. |
||
| if git rev-parse --git-dir > /dev/null 2>&1; then | ||
| git config core.hooksPath .githooks | ||
| fi | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remind me why skip MYPY?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is taking too much time and every time if the developer amends the commit, it might annoy them.