Skip to content

Commit 82fe1de

Browse files
authored
Merge pull request #3315 from boutproject/pre-commit-hooks
Consistent developer tooling; refactor formatting CI
2 parents e97bb62 + c37f698 commit 82fe1de

File tree

11 files changed

+900
-43
lines changed

11 files changed

+900
-43
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ PenaltyBreakString: 1000
9393
PenaltyExcessCharacter: 1000000
9494
PenaltyReturnTypeOnItsOwnLine: 60
9595
PointerAlignment: Left
96+
QualifierAlignment: Left
9697
ReflowComments: false
9798
SortIncludes: true
9899
# SortUsingDeclarations: true

.clang-tidy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Checks: 'clang-diagnostic-*,clang-analyzer-*,performance-*,readability-*,bugprone-*,clang-analyzer-*,cppcoreguidelines-*,mpi-*,misc-*,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-misc-non-private-member-variables-in-classes,-clang-analyzer-optin.mpi*,-bugprone-exception-escape,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-readability-function-cognitive-complexity,-misc-no-recursion,-bugprone-easily-swappable-parameters,-readability-identifier-length'
2+
Checks: 'clang-diagnostic-*,clang-analyzer-*,performance-*,readability-*,bugprone-*,clang-analyzer-*,cppcoreguidelines-*,mpi-*,misc-*,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-misc-non-private-member-variables-in-classes,-clang-analyzer-optin.mpi*,-bugprone-exception-escape,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-readability-function-cognitive-complexity,-misc-no-recursion,-bugprone-easily-swappable-parameters,-readability-identifier-length,-cppcoreguidelines-pro-bounds-avoid-unchecked-container-access'
33
WarningsAsErrors: ''
44
HeaderFilterRegex: ''
55
FormatStyle: file
@@ -29,6 +29,7 @@ These are all basically unavoidable in HPC numeric code:
2929
-readability-magic-numbers
3030
-cppcoreguidelines-avoid-magic-numbers
3131
-cppcoreguidelines-pro-bounds-pointer-arithmetic
32+
-cppcoreguidelines-pro-bounds-avoid-unchecked-container-access
3233
-readability-function-cognitive-complexity
3334
-bugprone-easily-swappable-parameters
3435
-readability-identifier-length

.clangd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Diagnostics:
2+
MissingIncludes: Strict
3+
ClangTidy:
4+
FastCheckFilter: None
5+
Includes:
6+
IgnoreHeader: ["adios2/.*", "bits/.*", "cpptrace/.*", "petsc.*"]

.github/workflows/auto-formatting.yml

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,19 @@ jobs:
1515
ref: ${{ github.head_ref }}
1616
fetch-depth: 0
1717

18-
# This also installs git-clang-format
19-
- name: Install formatters
20-
run: |
21-
sudo apt install clang-format python3-pip python3-setuptools python3-wheel
22-
pip3 install ruff cmake-format
18+
- name: "Set up Python"
19+
uses: actions/setup-python@v6
2320

24-
- name: Version
25-
run: |
26-
python3 --version
27-
$HOME/.local/bin/ruff --version
28-
clang-format --version
29-
$HOME/.local/bin/cmake-format --version
30-
31-
- name: Run clang-format
32-
id: format
33-
run:
34-
while ! git clang-format origin/${{ github.base_ref }} ; do git add . ; done
35-
36-
- name: Run ruff
37-
run: |
38-
pwd
39-
ls
40-
$HOME/.local/bin/ruff format .
41-
42-
- name: Run cmake-format
43-
run: |
44-
pwd
45-
ls
46-
$HOME/.local/bin/cmake-format -i $(find -name CMakeLists.txt) cmake/*.cmake
47-
48-
- name: Commit formatting
49-
uses: stefanzweifel/git-auto-commit-action@v7
21+
- name: Install uv
22+
uses: astral-sh/setup-uv@v7
5023
with:
51-
commit_message: "[bot] Apply format changes"
52-
skip_push: true
24+
enable-cache: true
5325

54-
- name: Ignore formatting
55-
run: |
56-
msg=$(git rev-list --format=%s --max-count=1 HEAD|tail -n -1)
57-
test "$msg" = "[bot] Apply format changes" && git rev-parse HEAD >> .git-blame-ignore-revs || :
26+
- name: Install dev tools
27+
run: uv sync --only-dev
5828

59-
- name: Commit formatting
60-
uses: stefanzweifel/git-auto-commit-action@v7
61-
with:
62-
commit_message: "[bot] Add last format changes commit to ignore file"
29+
- name: Check prek versions are up-to-date
30+
run: uv tool run sync-with-uv --check --diff
31+
32+
- name: Run everything
33+
run: uv tool run prek run --show-diff-on-failure --from-ref origin/${{ github.base_ref }}

.github/workflows/clang-tidy-review.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
uses: ZedThree/clang-tidy-review@v0.23.1
2525
id: review
2626
with:
27+
annotations: true
2728
build_dir: build
2829
apt_packages: "libfftw3-dev,libnetcdf-c++4-dev,libopenmpi-dev,petsc-dev,slepc-dev,liblapack-dev,libparpack2-dev,libsundials-dev,uuid-dev"
2930
config_file: ".clang-tidy"

.pre-commit-config.yaml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# This file requires prek rather than pre-commit
2+
exclude:
3+
glob:
4+
- "tools/**/*.pro"
5+
- "tools/**/*.m"
6+
- "tools/**/*.[ch]"
7+
8+
# See https://pre-commit.com for more information
9+
# See https://pre-commit.com/hooks.html for more hooks
10+
repos:
11+
- repo: builtin
12+
hooks:
13+
- id: trailing-whitespace
14+
- id: end-of-file-fixer
15+
- id: check-added-large-files
16+
17+
# This would be nice to use, but it is a significant slowdown:
18+
19+
# # Sync versions of hook tools with `uv.lock`
20+
# - repo: https://github.com/tsvikas/sync-with-uv
21+
# rev: v0.5.0
22+
# hooks:
23+
# - id: sync-with-uv
24+
25+
# C++ formatting
26+
- repo: https://github.com/pre-commit/mirrors-clang-format
27+
rev: v22.1.0
28+
hooks:
29+
- id: clang-format
30+
types_or: [c++, c, cuda]
31+
32+
# Python linting and formatting
33+
- repo: https://github.com/astral-sh/ruff-pre-commit
34+
rev: v0.15.5
35+
hooks:
36+
# Run the linter.
37+
- id: ruff-check
38+
# Run the formatter.
39+
- id: ruff-format
40+
41+
# CMake formatting
42+
- repo: https://github.com/cheshirekow/cmake-format-precommit
43+
rev: v0.6.13
44+
hooks:
45+
- id: cmake-format
46+
additional_dependencies: [pyyaml>=5.1]
47+
48+
# Various config files
49+
- repo: https://github.com/python-jsonschema/check-jsonschema
50+
rev: 0.37.0
51+
hooks:
52+
- id: check-github-workflows
53+
- id: check-readthedocs
54+
- id: check-citation-file-format
55+
- id: check-dependabot
56+
57+
# Docs linting
58+
- repo: https://github.com/sphinx-contrib/sphinx-lint
59+
rev: v1.0.2
60+
hooks:
61+
- id: sphinx-lint
62+
files: 'manual/sphinx/.*'

manual/sphinx/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def __getattr__(cls, name):
192192
# List of patterns, relative to source directory, that match files and
193193
# directories to ignore when looking for source files.
194194
# This patterns also effect to html_static_path and html_extra_path
195-
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
195+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", ".venv", "venv"]
196196

197197
# The name of the Pygments (syntax highlighting) style to use.
198198
pygments_style = "sphinx"

manual/sphinx/developer_docs/contributing.rst

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,78 @@ give you comments to improve the code. You can make additional changes
173173
and push them to the same feature branch and they will be automatically
174174
added to the pull request.
175175

176+
Running Tests
177+
~~~~~~~~~~~~~
178+
179+
We run many tests and checks automatically on GitHub (collectively called "CI")
180+
for a variety of build configurations. See :ref:`sec-runtestsuite` for how to
181+
run them locally. Running the full test suite can take some time, but it's a
182+
good idea to run at least the unit tests locally when you're making changes:
183+
184+
.. code-block:: console
185+
186+
cmake --build build --target check-unit-tests
187+
188+
Along with the automated tests, we also run things like formatters and static
189+
analysis in CI, which may provide further feedback (see :ref:`sec-coding-style`
190+
for more details). If your PR fails the format check on CI, please install the
191+
formatters and run them according to the next section. Usually this is just:
192+
193+
.. code-block:: console
194+
195+
git clang-format next
196+
197+
which will format just the parts of C++ files that have changed since ``next``.
198+
199+
Formatting and Linters
200+
~~~~~~~~~~~~~~~~~~~~~~
201+
202+
For frequent developers, we strongly recommend installing the formatting and
203+
linting tools locally and building them into your regular workflow. You can
204+
install the majority of our developer tools at once using `uv
205+
<https://docs.astral.sh/uv/>`_ from the top of your BOUT++ directory:
206+
207+
.. code-block:: console
208+
209+
uv sync --only-dev --inexact
210+
211+
This will install all the developer tools into a virtual environment (creating
212+
one if necessary, typically under ``.venv``). You can then activate the virtual
213+
environment and use the tools manually, or via integrations in your editor.
214+
215+
We *strongly* recommend using ``uv`` to install the developer tools, as this
216+
will ensure that you get the *exact* versions used in CI and by other
217+
developers, but you could also use ``pip install --group dev`` if you wish.
218+
219+
The quickest way to run all the formatters on a PR at once is typically:
220+
221+
.. code-block:: console
222+
223+
uv tool run prek run --from-ref next
224+
225+
This will run all our formatters on files that have changed since ``next``.
226+
227+
Install Pre-Commit Hooks (Optional but recommended)
228+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
229+
230+
We also have a `prek <https://prek.j178.dev>`_ config that can run some of
231+
these automatically using pre-commit hooks -- that is, when you run ``git
232+
commit``, these tools will run and abort the commit if they change any
233+
files. You then just have to stage the new changes and try again.
234+
235+
Install our developer tools with ``uv`` as above, and then install the
236+
pre-commit hook:
237+
238+
.. code-block:: console
239+
240+
prek install
241+
242+
That's it! ``prek`` will install the tools into their own separate virtual
243+
environment so they won't interfere with any you may have, and ``git`` will take
244+
care of running them automatically.
245+
246+
.. _sec-coding-style:
247+
176248
Coding Style
177249
------------
178250

@@ -189,6 +261,25 @@ These guidelines are intended to make code easier to read and therefore
189261
easier to understand. Being consistent in coding style also helps
190262
comprehension by reducing cognitive load.
191263

264+
We use various tools to enforce code style and quality:
265+
266+
- `clang-format <https://clang.llvm.org/docs/ClangFormat.html>`_ for formatting
267+
of C++ code
268+
- `clang-tidy <https://clang.llvm.org/extra/clang-tidy/index.html>`_ for linting
269+
and static analysis of C++ code
270+
- `ruff <https://astral.sh/ruff>`_ for formatting and linting of Python code
271+
- `cmake-format <https://github.com/cheshirekow/cmake_format>`_ for formatting
272+
CMake files
273+
- `sphinx-lint <https://github.com/sphinx-contrib/sphinx-lint>`_ for linting
274+
documentation pages
275+
276+
You can install all of these tools using:
277+
278+
.. code-block:: console
279+
280+
pip install -r requirements_dev.txt
281+
282+
192283
Comments
193284
~~~~~~~~
194285

pyproject.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,23 @@ requires = []
44
# Defined by PEP 517:
55
build-backend = "backend"
66
backend-path = ["tools/pylib/_boutpp_build/"]
7+
8+
[dependency-groups]
9+
dev = [
10+
"cmake~=4.2",
11+
"clang-format~=22.0",
12+
"clang-tidy~=22.0",
13+
"clangd~=22.0",
14+
"clangd-tidy~=1.1",
15+
"cmakelang~=0.6",
16+
"prek>=0.3.5",
17+
"pyyaml~=6.0",
18+
"ruff~=0.15",
19+
"sphinx-lint~=1.0",
20+
"sync-with-uv~=0.5.0",
21+
]
22+
maint = [
23+
"pygithub~=2.8",
24+
"ruamel-yaml~=0.19",
25+
"Unidecode~=1.3",
26+
]

requirements_dev.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
clang-format~=22.0
2+
clang-tidy~=22.0
3+
cmakelang~=0.6
4+
pre-commit>=4.5.1
5+
ruff
6+
sphinx-lint~=1.0

0 commit comments

Comments
 (0)