|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""GateFlow PreToolUse hook guard for Bash commands. |
| 3 | +
|
| 4 | +This is intentionally deterministic (no LLM call) to avoid noisy hook failures. |
| 5 | +It asks for confirmation when a Bash command appears likely to delete/overwrite |
| 6 | +SystemVerilog/Verilog source files. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import json |
| 12 | +import os |
| 13 | +import re |
| 14 | +import sys |
| 15 | +from typing import Any, Dict |
| 16 | + |
| 17 | + |
| 18 | +SV_EXT_RE = re.compile(r"\.(?:svh?|vh?|v)(?=$|[^A-Za-z0-9_])", re.IGNORECASE) |
| 19 | + |
| 20 | + |
| 21 | +def _looks_like_sv_project(cwd: str) -> bool: |
| 22 | + # Keep this cheap: check only a couple common directories. |
| 23 | + candidates = [cwd, os.path.join(cwd, "rtl"), os.path.join(cwd, "tb")] |
| 24 | + for d in candidates: |
| 25 | + try: |
| 26 | + for name in os.listdir(d): |
| 27 | + lower = name.lower() |
| 28 | + if lower.endswith((".sv", ".svh", ".v", ".vh")): |
| 29 | + return True |
| 30 | + except Exception: |
| 31 | + continue |
| 32 | + return False |
| 33 | + |
| 34 | + |
| 35 | +def _has_sv_path(cmd: str) -> bool: |
| 36 | + return SV_EXT_RE.search(cmd) is not None |
| 37 | + |
| 38 | + |
| 39 | +def _is_destructive_git(cmd_l: str, sv_project: bool, mentions_sv: bool) -> bool: |
| 40 | + if "git " not in cmd_l: |
| 41 | + return False |
| 42 | + |
| 43 | + # These are destructive for the working tree; we only trigger if we can |
| 44 | + # reasonably believe it's an SV project, or the command explicitly mentions |
| 45 | + # SV/V file paths. |
| 46 | + if (sv_project or mentions_sv) and "git reset" in cmd_l and "--hard" in cmd_l: |
| 47 | + return True |
| 48 | + |
| 49 | + if (sv_project or mentions_sv) and "git clean" in cmd_l and ( |
| 50 | + " -f" in cmd_l or "--force" in cmd_l |
| 51 | + ): |
| 52 | + return True |
| 53 | + |
| 54 | + if "git checkout" in cmd_l and (sv_project or mentions_sv): |
| 55 | + # `git checkout -- <path>` or forced checkout are most likely to discard changes. |
| 56 | + if " -- " in cmd_l or " checkout --" in cmd_l or " -f" in cmd_l or "--force" in cmd_l: |
| 57 | + return True |
| 58 | + |
| 59 | + if "git restore" in cmd_l: |
| 60 | + # Restore is inherently a revert; only ask when it targets SV/V or in an SV project |
| 61 | + # with forceful flags. |
| 62 | + if mentions_sv: |
| 63 | + return True |
| 64 | + if sv_project and ("--staged" in cmd_l or "--source" in cmd_l or "--worktree" in cmd_l): |
| 65 | + return True |
| 66 | + |
| 67 | + return False |
| 68 | + |
| 69 | + |
| 70 | +def _overwrites_via_redirection(cmd: str, mentions_sv: bool) -> bool: |
| 71 | + if not mentions_sv: |
| 72 | + return False |
| 73 | + # Catch: `> file.sv`, `>> file.sv`, `>| file.sv`, and common `tee file.sv`. |
| 74 | + if re.search(r"(?:^|[^0-9])>>?\s*[^\\n]*\.(?:svh?|vh?|v)(?=$|[^A-Za-z0-9_])", cmd, re.IGNORECASE): |
| 75 | + return True |
| 76 | + if re.search(r"\btee\b[^\n]*\.(?:svh?|vh?|v)(?=$|[^A-Za-z0-9_])", cmd, re.IGNORECASE): |
| 77 | + return True |
| 78 | + return False |
| 79 | + |
| 80 | + |
| 81 | +def _deletes_sv(cmd_l: str, sv_project: bool, mentions_sv: bool) -> bool: |
| 82 | + if "rm" not in cmd_l: |
| 83 | + return False |
| 84 | + |
| 85 | + # Directly references SV/V files. |
| 86 | + if mentions_sv and re.search(r"\brm\b", cmd_l): |
| 87 | + return True |
| 88 | + |
| 89 | + # Recursively deleting common SV directories. |
| 90 | + if sv_project and re.search(r"\brm\b[^\n]*(?:-rf|-fr|--recursive)[^\n]*(?:\brtl\b|\btb\b|/rtl/|/tb/)", cmd_l): |
| 91 | + return True |
| 92 | + |
| 93 | + return False |
| 94 | + |
| 95 | + |
| 96 | +def _should_ask(cmd: str, cwd: str) -> bool: |
| 97 | + cmd_l = cmd.lower() |
| 98 | + mentions_sv = _has_sv_path(cmd) |
| 99 | + sv_project = _looks_like_sv_project(cwd) if cwd else False |
| 100 | + |
| 101 | + return ( |
| 102 | + _overwrites_via_redirection(cmd, mentions_sv) |
| 103 | + or _deletes_sv(cmd_l, sv_project, mentions_sv) |
| 104 | + or _is_destructive_git(cmd_l, sv_project, mentions_sv) |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +def main() -> int: |
| 109 | + try: |
| 110 | + payload: Dict[str, Any] = json.load(sys.stdin) |
| 111 | + except Exception: |
| 112 | + # Malformed input: do nothing (never block tool usage due to hook failure). |
| 113 | + sys.stdout.write("{}") |
| 114 | + return 0 |
| 115 | + |
| 116 | + tool_name = payload.get("tool_name", "") |
| 117 | + tool_input = payload.get("tool_input", {}) or {} |
| 118 | + cmd = tool_input.get("command", "") if isinstance(tool_input, dict) else "" |
| 119 | + cwd = payload.get("cwd", "") or "" |
| 120 | + |
| 121 | + if tool_name != "Bash" or not isinstance(cmd, str) or not cmd.strip(): |
| 122 | + sys.stdout.write("{}") |
| 123 | + return 0 |
| 124 | + |
| 125 | + if _should_ask(cmd, cwd): |
| 126 | + out: Dict[str, Any] = { |
| 127 | + "hookSpecificOutput": {"permissionDecision": "ask"}, |
| 128 | + "systemMessage": ( |
| 129 | + "This command looks like it may delete/overwrite .sv/.svh/.v/.vh files. " |
| 130 | + "Please confirm before I run it." |
| 131 | + ), |
| 132 | + } |
| 133 | + sys.stdout.write(json.dumps(out)) |
| 134 | + return 0 |
| 135 | + |
| 136 | + sys.stdout.write("{}") |
| 137 | + return 0 |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + raise SystemExit(main()) |
| 142 | + |
0 commit comments