|
| 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