Skip to content

Commit 447795b

Browse files
authored
Add pre-commit for automated pre-push checks (#4660)
## Description / motivation Versioned hook configuration and a single documented install path reduce friction and drift between machines compared to copy-pasted shell hooks. ## How to test 1. `$ uv sync --group dev` 2. `$ uv run pre-commit install --hook-type pre-push` 3. Make any change, commit it, and push it <img width="908" height="253" alt="image" src="https://github.com/user-attachments/assets/d492e2a8-9f33-4adf-866e-04c54cbf57d9" /> ### Details - Add `.pre-commit-config.yaml` with a **pre-push** hook that runs `./ninja check` - Add `pre-commit` to the `dev` dependency group and refresh `uv.lock`. - Update `docs/contributing.md`: contributors install hooks via `uv run pre-commit` / `python -m pre_commit`, and drop the old suggestion of manual `.git/hooks` scripts. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1 parent 64386ca commit 447795b

File tree

5 files changed

+234
-9
lines changed

5 files changed

+234
-9
lines changed

.pre-commit-config.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
default_install_hook_types: [pre-push]
2+
3+
repos:
4+
- repo: local
5+
hooks:
6+
- id: ninja-check
7+
name: ninja check
8+
entry: bash -c 'if [ "$OS" = "Windows_NT" ]; then ./tools/ninja.bat check; else ./ninja check; fi'
9+
language: system
10+
pass_filenames: false
11+
always_run: true
12+
stages: [pre-push]

docs/contributing.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,30 @@ For information on adding new translatable strings to Anki, please see
102102
## Tests Must Pass
103103

104104
Please make sure 'ninja check' completes successfully before submitting code.
105-
You can do this automatically by adding the following into
106-
.git/hooks/pre-commit or .git/hooks/pre-push and making it executable.
107105

108-
```sh
109-
#!/bin/bash
110-
./ninja check
106+
[pre-commit](https://pre-commit.com/) is used to run that check from a Git hook.
107+
It is configured in `.pre-commit-config.yaml` at the repository root. After
108+
installing the dev dependencies (for example `uv sync --group dev`), run one of:
109+
110+
```
111+
uv run pre-commit install --hook-type pre-push
112+
```
113+
111114
```
115+
python3 -m pre_commit install --hook-type pre-push
116+
```
117+
118+
(`pre-commit` alone only works if that executable is on your `PATH`, for example
119+
after `pip install --user pre-commit` or with your virtual environment activated.)
120+
121+
The bundled hook runs `./ninja check` on **pre-push** (not on every commit),
122+
because the full check suite can take a long time. You can still run
123+
`./ninja check` manually at any time.
112124

113-
You may want to explicitly set PATH to your normal shell PATH in that script,
114-
as pre-commit does not use a login shell, and if your path differs Bazel will
115-
end up recompiling things unnecessarily.
125+
You may need to ensure your usual shell `PATH` is visible to the hook (for
126+
example if tools such as `ninja` are installed outside standard locations),
127+
because pre-commit does not use a login shell and a different path can cause
128+
extra rebuilds.
116129

117130
If your change is non-trivial and not covered by the existing unit tests, please
118131
consider adding a unit test at the same time.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ classifiers = ["Private :: Do Not Upload"]
77

88
[dependency-groups]
99
dev = [
10+
"pre-commit",
1011
"mypy",
1112
"mypy-protobuf",
1213
"ruff",

tools/minilints/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const IGNORED_FOLDERS: &[&str] = &[
3636
".mypy_cache",
3737
"./extra",
3838
"./ts/.svelte-kit",
39+
"./.venv",
3940
];
4041

4142
fn main() -> Result<()> {

0 commit comments

Comments
 (0)