Skip to content

Commit 488cbdc

Browse files
authored
Merge pull request #510 from xcp-ng/ohu/implement-pre-commit
pre-commit: add config for checks on commit
2 parents 36bdee3 + 5d907ca commit 488cbdc

5 files changed

Lines changed: 108 additions & 0 deletions

File tree

.pre-commit-config.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: "ruff"
5+
name: "ruff"
6+
language: "system"
7+
require_serial: true
8+
entry: "uv run ruff check"
9+
types: ["python"]
10+
- id: "flake8"
11+
name: "flake8"
12+
language: "system"
13+
require_serial: true
14+
entry: "uv run flake8"
15+
types: ["python"]
16+
- id: "mypy"
17+
name: "mypy"
18+
language: "system"
19+
require_serial: true
20+
entry: "uv run mypy --install-types --non-interactive ."
21+
pass_filenames: false
22+
types: ["python"]
23+
- id: "pyright"
24+
name: "pyright"
25+
language: "system"
26+
require_serial: true
27+
entry: "uv run pyright ."
28+
pass_filenames: false
29+
types: ["python"]

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,57 @@ servers (LSP).
382382
These configurations are provided as optional suggestions for convenience and do not imply official support or a
383383
requirement to use specific tools.
384384

385+
### Pre-commit hook
386+
387+
#### General
388+
389+
The repository has a [prek](https://prek.j178.dev/) configuration, which allows for making sure that each commit created is compliant with the [code checking implemented in the CI](./.github/workflows/code-checkers.yml). This feature is **opt-in** and can be run manually.
390+
391+
This tool is configured via the [`.pre-commit-config.yaml`](./.pre-commit-config.yaml) file.
392+
393+
In order to run all the checks that the CI performs, you can simply run:
394+
```shell
395+
$ uv run prek -a
396+
ruff.....................................................................Passed
397+
flake8...................................................................Passed
398+
mypy.....................................................................Passed
399+
pyright..................................................................Passed
400+
```
401+
402+
#### Install git hooks
403+
404+
[prek](https://prek.j178.dev/) really shines once you start to run it automatically before each commit.
405+
For this, we need to tell [prek](https://prek.j178.dev/) to install a [git hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) in our local repository:
406+
407+
```bash
408+
$ uv run prek install
409+
```
410+
411+
Note that you can easily remove this hook by running:
412+
413+
```bash
414+
$ uv run prek uninstall
415+
```
416+
417+
Once the hooks are in place, `git commit` will automatically:
418+
- stash the changes that are not staged for this commit
419+
- perform the `ruff`, `mypy`, `flake8` and `pyright` checks (unless the staged changes do not affect any python files, in which case the checks are skipped)
420+
- re-apply the changes that were stashed
421+
422+
If the checks happen to fail, the commit is cancelled.
423+
424+
If for some reason, you are fine with a specific check failing, you can simply skip it with the `SKIP` environment variable.
425+
You can also bypass all the hooks by adding the git option `--no-verify`, or the environment variable `GIT_NO_HOOKS=1`:
426+
427+
```bash
428+
# Only skip the `flake8` hook
429+
$ SKIP=flake8 git commit -m "my message"
430+
# Do not run any hook before commit
431+
$ git commit -m "my message" --no-verify
432+
# Same thing, using an environment variable instead
433+
$ GIT_NO_HOOKS=1 git commit -m "my message"
434+
```
435+
385436
### [VSCodium](https://vscodium.com/)
386437

387438
A few plugins are recommended to properly report the diagnostics during the development:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dev = [
3232
"types-colorama",
3333
"types-pexpect",
3434
"zizmor",
35+
"prek>=0.3.11",
3536
]
3637

3738
[tool.pyright]

requirements/dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ types-pygments
1313
types-colorama
1414
types-pexpect
1515
zizmor
16+
prek>=0.3.11
1617
-r base.txt

uv.lock

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)