Skip to content

Commit 53f681b

Browse files
Copilotpancetta
andauthored
Add pre-commit hooks mirroring CI linting pipeline (#611)
* Initial plan * Add pre-commit hooks configuration matching CI pipeline Co-authored-by: pancetta <7158893+pancetta@users.noreply.github.com> * Fix ruff pre-commit configuration to remove redundant check argument Co-authored-by: pancetta <7158893+pancetta@users.noreply.github.com> * Test pre-commit hooks with valid code * Remove test file --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: pancetta <7158893+pancetta@users.noreply.github.com>
1 parent 24b5830 commit 53f681b

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

.pre-commit-config.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
# This pre-commit configuration matches the CI pipeline linting checks exactly.
4+
# The CI runs: black pySDC --check --diff --color and ruff check pySDC
5+
# Therefore, we only check files in the pySDC directory here.
6+
repos:
7+
- repo: https://github.com/psf/black
8+
rev: 24.10.0
9+
hooks:
10+
- id: black
11+
args: [--check, --diff, --color]
12+
language_version: python3
13+
files: ^pySDC/
14+
- repo: https://github.com/astral-sh/ruff-pre-commit
15+
rev: v0.8.4
16+
hooks:
17+
- id: ruff
18+
files: ^pySDC/

docs/contrib/02_continuous_integration.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,27 @@ fi
122122
```
123123
Don't forget to assign execution rights.
124124

125+
### Using pre-commit hooks (Recommended)
126+
127+
For a more robust and automated approach, you can use the [pre-commit](https://pre-commit.com/) framework, which manages git hooks for you. The repository includes a `.pre-commit-config.yaml` file that is configured to run the exact same linting checks as the CI pipeline.
128+
129+
To set up pre-commit hooks:
130+
131+
```bash
132+
# Install pre-commit (can also be done with conda/mamba or as part of pip install -e .[dev])
133+
pip install pre-commit
134+
135+
# Install the git hooks
136+
pre-commit install
137+
138+
# (Optional) Run against all files to check current state
139+
pre-commit run --all-files
140+
```
141+
142+
Once installed, the hooks will automatically run `black` and `ruff` on staged files before each commit. The hooks are configured to check only files in the `pySDC` directory, matching exactly what the CI pipeline checks.
143+
144+
> :bell: **Note:** The pre-commit hooks run in `--check` mode by default (same as CI), meaning they will prevent commits if formatting issues are found. You can fix formatting issues by running `black pySDC` directly, then staging and committing again.
145+
125146
As a final note, make sure to regularly update linting related packages, as they constantly introduce checking of more PEP8 guidelines.
126147
This might cause the linting to fail in the GitHub action, which uses the most up to date versions available on the conda-forge channel, even though it passed locally.
127148

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ dev = [
5151
'ruff',
5252
'pytest',
5353
'pytest-cov',
54-
'sphinx'
54+
'sphinx',
55+
'pre-commit'
5556
]
5657

5758
[tool.pytest.ini_options]

0 commit comments

Comments
 (0)