-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_commit_validate.py
More file actions
77 lines (65 loc) · 2 KB
/
pre_commit_validate.py
File metadata and controls
77 lines (65 loc) · 2 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
#!/usr/bin/env python3
# Copyright (c) 2026 Nardo (nardovibecoding). AGPL-3.0 — see LICENSE
"""PostToolUse hook: validate Python syntax after git commit on telegram-claude-bot."""
import json
import re
import subprocess
import sys
from pathlib import Path
def main():
try:
input_data = json.load(sys.stdin)
except (json.JSONDecodeError, EOFError):
print("{}")
return
tool_name = input_data.get("tool_name", "")
tool_input = input_data.get("tool_input", {})
if tool_name != "Bash":
print("{}")
return
cmd = tool_input.get("command", "")
if not re.search(r"git\s+commit", cmd):
print("{}")
return
# Only check telegram-claude-bot repo
cwd = input_data.get("cwd", "")
if "telegram-claude-bot" not in cwd:
print("{}")
return
# Get changed files from last commit
try:
result = subprocess.run(
["git", "-C", cwd, "diff", "--name-only", "HEAD~1", "HEAD"],
capture_output=True, text=True, timeout=10
)
changed = [f for f in result.stdout.strip().splitlines() if f.endswith(".py")]
except Exception:
print("{}")
return
if not changed:
print("{}")
return
# Validate syntax
errors = []
for f in changed:
full_path = Path(cwd) / f
if not full_path.exists():
continue
try:
result = subprocess.run(
["python3", "-m", "py_compile", str(full_path)],
capture_output=True, text=True, timeout=5
)
if result.returncode != 0:
errors.append(f"{f}: {result.stderr.strip()[:100]}")
except Exception:
pass
if errors:
msg = "⚠️ **Syntax errors in committed files:**\n"
msg += "\n".join(f" - {e}" for e in errors[:5])
msg += "\nFix before pushing."
print(json.dumps({"systemMessage": msg}))
else:
print("{}")
if __name__ == "__main__":
main()