-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlint-gate.py
More file actions
executable file
·144 lines (116 loc) · 4.34 KB
/
lint-gate.py
File metadata and controls
executable file
·144 lines (116 loc) · 4.34 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
"""Stop hook lint gate for Claude Code.
Auto-fixes trivial issues (ruff --fix, ruff format) then runs the full
lint suite (ruff, mypy, basedpyright) on modified Python files before
Claude returns to the user. If unfixable errors remain, blocks the stop
with the error output so Claude can fix them first.
Install: copy to ~/.claude/hooks/lint-gate.py
Configure in ~/.claude/settings.json under hooks.Stop
"""
from __future__ import annotations
import json
import subprocess
import sys
from pathlib import Path
MAX_LINES_PER_TOOL = 50
TOOL_TIMEOUT = 120
# Auto-fix pass: runs first, silently fixes trivial issues
FIXERS: list[tuple[str, list[str]]] = [
("ruff", ["check", "--fix"]),
("ruff", ["format"]),
]
# Check pass: runs after fixers, reports remaining errors
CHECKERS: list[tuple[str, list[str]]] = [
("ruff", ["check"]),
("mypy", []),
("basedpyright", []),
]
def get_modified_python_files(cwd: Path) -> list[str]:
"""Find Python files with uncommitted changes (staged, unstaged, untracked)."""
commands = [
["git", "diff", "--name-only", "--diff-filter=d", "--", "*.py"],
["git", "diff", "--cached", "--name-only", "--diff-filter=d", "--", "*.py"],
["git", "ls-files", "--others", "--exclude-standard", "--", "*.py"],
]
modified: set[str] = set()
for cmd in commands:
try:
result = subprocess.run(
cmd, capture_output=True, text=True, cwd=cwd, timeout=10
)
for f in result.stdout.strip().splitlines():
if f and (cwd / f).is_file():
modified.add(f)
except (subprocess.SubprocessError, OSError):
pass
return sorted(modified)
def find_venv_activate(cwd: Path) -> str:
"""Return shell source command for venv activation, or empty string."""
for venv_dir in (".venv", "venv"):
activate = cwd / venv_dir / "bin" / "activate"
if activate.exists():
return f"source '{activate}' && "
return ""
def run_linter(
tool: str, args: list[str], files: list[str], cwd: Path, venv_prefix: str
) -> str | None:
"""Run a linter on files. Return error output, or None if clean/unavailable."""
file_args = " ".join(f"'{f}'" for f in files)
cmd = f"{venv_prefix}{tool} {' '.join(args)} {file_args}"
try:
result = subprocess.run(
["bash", "-c", cmd],
capture_output=True,
text=True,
cwd=cwd,
timeout=TOOL_TIMEOUT,
)
except subprocess.TimeoutExpired:
return f"(timed out after {TOOL_TIMEOUT}s)"
except OSError:
return None
if result.returncode == 0:
return None
output = result.stdout.strip() or result.stderr.strip()
if not output:
return None
# Skip "not found" errors — tool not installed, not a lint failure
if "command not found" in output or "No module named" in output:
return None
lines = output.splitlines()
if len(lines) > MAX_LINES_PER_TOOL:
output = "\n".join(lines[:MAX_LINES_PER_TOOL]) + (
f"\n... ({len(lines) - MAX_LINES_PER_TOOL} more lines)"
)
return output
def main() -> None:
input_data = json.load(sys.stdin)
# Prevent infinite loops: if already retrying after a block, allow stop
if input_data.get("stop_hook_active"):
json.dump({"decision": "approve"}, sys.stdout)
return
cwd = Path(input_data.get("cwd", ".")).resolve()
files = get_modified_python_files(cwd)
if not files:
json.dump({"decision": "approve"}, sys.stdout)
return
venv_prefix = find_venv_activate(cwd)
# Auto-fix pass: silently fix trivial issues (import sorting, formatting)
for tool, args in FIXERS:
run_linter(tool, args, files, cwd, venv_prefix)
# Check pass: report remaining unfixable errors
errors: list[str] = []
for tool, args in CHECKERS:
output = run_linter(tool, args, files, cwd, venv_prefix)
if output:
errors.append(f"=== {tool} ===\n{output}")
if errors:
reason = (
"Fix these errors in modified files before returning:\n\n"
+ "\n\n".join(errors)
)
json.dump({"decision": "block", "reason": reason}, sys.stdout)
else:
json.dump({"decision": "approve"}, sys.stdout)
if __name__ == "__main__":
main()