Project: pygitweb

2a506ff import json
import subprocess
import sys
from pathlib import Path
from sys import stderr
from typing import Any
import pygit2
5e51915 from pygittools.hooks import HookResult, PreCommit
2a506ff
"""
Pre-commit hook: run Ruff lint and format checks with machine-readable JSON on stdout.
Uses ``ruff check --output-format=json`` and
``ruff format --check --preview --output-format=json`` (preview is required for JSON on ``format``).
Parses JSON and prints a short summary to stderr on failure.
"""
def _ruff_base() -> list[str]:
73c9c30 return [sys.executable, "-m", "ruff"]
2a506ff
def _parse_diagnostic_list(raw: str) -> list[dict[str, Any]]:
73c9c30 s = raw.strip()
if not s:
return []
data = json.loads(s)
if isinstance(data, list):
return [x for x in data if isinstance(x, dict)]
return []
2a506ff
def _format_location(loc: object) -> str:
73c9c30 if not isinstance(loc, dict):
return "?:?"
row = loc.get("row", "?")
col = loc.get("column", "?")
return f"{row}:{col}"
2a506ff
def _summarize_diagnostics(label: str, rows: list[dict[str, Any]]) -> str:
73c9c30 lines = [f"{label}:"]
for row in rows:
fn = row.get("filename", "?")
msg = row.get("message", row.get("code", "?"))
loc = row.get("location")
lines.append(f" {fn}:{_format_location(loc)}: {msg}")
return "\n".join(lines)
2a506ff
def _run_ruff_json(
73c9c30 args: list[str],
cwd: Path,
2a506ff ) -> tuple[int, list[dict[str, Any]], str]:
73c9c30 proc = subprocess.run(
_ruff_base() + args,
cwd=cwd,
capture_output=True,
text=True,
)
stderr_extra = proc.stderr or ""
try:
rows = _parse_diagnostic_list(proc.stdout)
except json.JSONDecodeError:
return (
1,
[],
f"invalid JSON from ruff (stdout):\n{proc.stdout!r}\nstderr:\n{stderr_extra}",
)
return proc.returncode, rows, stderr_extra
2a506ff
class PreCommitRuff(PreCommit):
73c9c30 def __init__(self, repo: pygit2.Repository, paths: list[str] | None = None):
super().__init__(repo)
self.paths = paths if paths is not None else ["."]
def run(self) -> HookResult:
workdir = self.repo.workdir
if workdir is None:
stderr.write("PreCommitRuff: bare repository has no working tree; skipping ruff.\n")
return HookResult.SUCCESS
root = Path(workdir)
rc, rows, extra = _run_ruff_json(["check", "--output-format=json", *self.paths], root)
if rc != 0:
stderr.write(_summarize_diagnostics("ruff check", rows) + "\n")
if extra.strip():
stderr.write(extra)
return HookResult.FAILURE
rc, rows, extra = _run_ruff_json(
["format", "--check", "--preview", "--output-format=json", *self.paths],
root,
)
if rc != 0:
stderr.write(_summarize_diagnostics("ruff format", rows) + "\n")
if extra.strip():
stderr.write(extra)
return HookResult.FAILURE
return HookResult.SUCCESS