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