Skip to content

Commit 3ae6dcc

Browse files
authored
Chore: Make installing the pre-commit hook "required" (#2295)
1 parent 45c8b24 commit 3ae6dcc

3 files changed

Lines changed: 79 additions & 12 deletions

File tree

.pre-commit-config.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ci:
99
autoupdate_branch: ''
1010
autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate'
1111
autoupdate_schedule: quarterly
12-
skip: [lychee]
12+
skip: [lychee, check-precommit-installed]
1313
submodules: false
1414

1515
# Please update the rev: SHAs below with this command:
@@ -26,6 +26,14 @@ repos:
2626

2727
- repo: local
2828
hooks:
29+
- id: check-precommit-installed
30+
name: Warn if the pre-commit git hook is not installed
31+
entry: python ./toolshed/check_precommit_installed.py
32+
language: python
33+
always_run: true
34+
pass_filenames: false
35+
verbose: true
36+
2937
- id: check-spdx
3038
name: Check SPDX Headers
3139
entry: python ./toolshed/check_spdx.py

CONTRIBUTING.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,18 @@ This project uses [pre-commit.ci](https://pre-commit.ci/) with GitHub Actions. A
6161
To set yourself up for running pre-commit checks locally and to catch issues before pushing your changes, follow these steps:
6262

6363
* Install pre-commit with: `pip install pre-commit`
64+
* Run this once per checkout: `pre-commit install`
6465
* You can manually check all files at any time by running: `pre-commit run --all-files`
6566

6667
This command runs all configured hooks (such as linters and formatters) across your repository, letting you review and address issues before committing.
6768

68-
**Optional: Enable automatic checks on every commit**
69-
If you want pre-commit hooks to run automatically each time you make a commit, install the git hook with:
70-
71-
`pre-commit install`
72-
73-
This sets up a git pre-commit hook so that all configured checks will run before each commit is accepted. If any hook fails, the commit will be blocked until the issues are resolved.
74-
75-
**Note on workflow flexibility**
76-
Some contributors prefer to commit intermediate or work-in-progress changes that may not pass all pre-commit checks, and only clean up their commits before pushing (for example, by squashing and running `pre-commit run --all-files` manually at the end). If this fits your workflow, you may choose not to run `pre-commit install` and instead rely on manual checks. This approach avoids disruption during iterative development, while still ensuring code quality before code is shared or merged.
77-
78-
Choose the setup that best fits your workflow and development style.
69+
Installing the hook is required, not optional. Some of the automated checks
70+
(the SPDX header updater and the `.pyi` stub generator for `cuda_core`) only
71+
keep the tree consistent if they run on *every* commit. Relying on manual
72+
`pre-commit run --all-files` invocations means these checks can be skipped
73+
between commits, leaving stale headers or out-of-date stubs in the history.
74+
If the hook isn't installed, `pre-commit run` (and CI) will print a visible
75+
warning reminding you to run `pre-commit install`.
7976

8077

8178
## Signing Your Work
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Warn (without failing) if the pre-commit git hook is not installed.
5+
6+
This repo relies on pre-commit running on *every* commit: the SPDX header
7+
updater and the .pyi stub generator only keep the tree consistent if they
8+
run automatically via the installed git hook. Contributors who only run
9+
`pre-commit run --all-files` manually (or never run pre-commit at all) can
10+
end up with stale headers or stubs. This check can't block the commit
11+
(if pre-commit isn't installed as a git hook, this script never runs
12+
during `git commit` in the first place) but it does fire during manual
13+
`pre-commit run` invocations and in CI, where it gives contributors a
14+
visible nudge to run `pre-commit install`.
15+
"""
16+
17+
import subprocess
18+
import sys
19+
20+
MARKER = b"File generated by pre-commit"
21+
22+
23+
def _git_hooks_dir() -> str:
24+
result = subprocess.run(
25+
["git", "rev-parse", "--git-path", "hooks"], # noqa: S607
26+
capture_output=True,
27+
check=True,
28+
text=True,
29+
)
30+
return result.stdout.strip()
31+
32+
33+
def main() -> int:
34+
hooks_dir = _git_hooks_dir()
35+
hook_path = f"{hooks_dir}/pre-commit"
36+
37+
try:
38+
with open(hook_path, "rb") as f:
39+
installed = MARKER in f.read()
40+
except FileNotFoundError:
41+
installed = False
42+
43+
if not installed:
44+
print(
45+
"\n"
46+
"==================== pre-commit WARNING ====================\n"
47+
"The pre-commit git hook is not installed in this checkout.\n"
48+
"Without it, checks only run when you invoke pre-commit\n"
49+
"manually, and will NOT run automatically on `git commit`.\n"
50+
"\n"
51+
"Fix this by running:\n"
52+
"\n"
53+
" pre-commit install\n"
54+
"==============================================================\n",
55+
file=sys.stderr,
56+
)
57+
58+
return 0
59+
60+
61+
if __name__ == "__main__":
62+
sys.exit(main())

0 commit comments

Comments
 (0)