Skip to content

Commit 7dafc55

Browse files
Lotramryneeverett
authored andcommitted
Add prek pre-commit hooks for ruff and ty
1 parent 4849d59 commit 7dafc55

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

.pre-commit-config.yaml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
ci: {}
2-
repos: []
2+
repos:
3+
- repo: local
4+
hooks:
5+
- id: ruff-format
6+
name: ruff format
7+
entry: scripts/run.sh ruff format --force-exclude --check
8+
language: system
9+
types_or: [python, pyi]
10+
11+
- id: ruff-lint
12+
name: ruff lint
13+
entry: scripts/run.sh ruff check --force-exclude
14+
language: system
15+
types_or: [python, pyi]
16+
17+
- id: ty
18+
name: ty check
19+
entry: scripts/run.sh ty check
20+
language: system
21+
types_or: [python, pyi]
22+
pass_filenames: false

bugwarrior/docs/contributing.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ separately, but you may find it convenient to run ``ruff check``, ``ruff
7171
format``, and ``ty check``, or integrate `ruff <https://docs.astral.sh/ruff/editors/setup/>`_
7272
and `ty <https://docs.astral.sh/ty/editors/>`_ with your editor.
7373

74+
Optionally, you can install the provided hooks to have ruff and ty run
75+
automatically before each commit. The hooks use `prek <https://github.com/j178/prek>`_
76+
(or `pre-commit <https://pre-commit.com/>`_ as an alternative):
77+
78+
.. tab:: pip
79+
80+
.. code-block:: bash
81+
82+
$ pip install prek
83+
$ prek install
84+
85+
.. tab:: uv
86+
87+
.. code-block:: bash
88+
89+
$ uv tool install prek
90+
$ prek install
91+
92+
The hooks use ``scripts/run.sh`` to invoke ruff and ty from the project's
93+
virtualenv (via ``uv run`` for uv users, or ``.venv`` for pip users), so they
94+
always run the same versions as the test suite.
95+
7496
Making a pull request
7597
---------------------
7698

scripts/run.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
# Run a command via `uv run` if uv is available, otherwise run it directly.
3+
# If a .venv directory exists, prepend it to PATH so the venv's tools are
4+
# used without requiring the user to have manually activated it.
5+
if command -v uv >/dev/null 2>&1 && [ -f "uv.lock" ]; then
6+
exec uv run "$@"
7+
else
8+
if [ -d ".venv" ]; then
9+
export PATH=".venv/bin:$PATH"
10+
fi
11+
if ! command -v "$1" >/dev/null 2>&1; then
12+
echo "error: '$1' not found."
13+
echo "Make sure your virtualenv is set up (see CONTRIBUTING for instructions)."
14+
echo "To skip this hook: git commit --no-verify (or -n)"
15+
echo "To skip only this hook: SKIP=$1 git commit"
16+
exit 1
17+
fi
18+
exec "$@"
19+
fi

0 commit comments

Comments
 (0)