# 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())
