feat(hooks): add git hooks management and auto-fix on pre-commit
This commit is contained in:
7 files changed
+201
-20
No files matched your search
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Multi-ecosystem formatter runner.
|
||||
|
||||
Best-effort: formats sources if tools exist. Safe to run repeatedly.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def has_file(*names: str) -> bool:
|
||||
return any((Path.cwd() / name).exists() for name in names)
|
||||
|
||||
|
||||
def run(cmd: list[str]) -> int:
|
||||
try:
|
||||
return subprocess.run(cmd, check=False).returncode
|
||||
except Exception:
|
||||
return 0
|
||||
|
||||
|
||||
def main() -> int:
|
||||
# Node/TS: prettier write
|
||||
if has_file("package.json"):
|
||||
if shutil.which("npm"):
|
||||
run(["npx", "--yes", "prettier", "--write", "."]) # type: ignore
|
||||
elif shutil.which("pnpm"):
|
||||
run(["pnpm", "dlx", "prettier", "--write", "."]) # type: ignore
|
||||
elif shutil.which("yarn"):
|
||||
run(["npx", "--yes", "prettier", "--write", "."]) # type: ignore
|
||||
|
||||
# Python: ruff format or black
|
||||
if has_file("pyproject.toml", "requirements.txt"):
|
||||
if shutil.which("ruff"):
|
||||
run(["ruff", "format", "."]) # type: ignore
|
||||
elif shutil.which("black"):
|
||||
run(["black", "."]) # type: ignore
|
||||
|
||||
# Rust: cargo fmt
|
||||
if has_file("Cargo.toml") and shutil.which("cargo"):
|
||||
run(["cargo", "fmt", "--all"]) # type: ignore
|
||||
|
||||
# Go: gofmt -w
|
||||
if has_file("go.mod") and shutil.which("go"):
|
||||
# Format in-place; gofmt returns 0 even if files changed
|
||||
run(["gofmt", "-w", "."]) # type: ignore
|
||||
|
||||
# Java: Gradle/Maven format plugins are not standard; skip
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import shutil
|
||||
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Multi-ecosystem lint fixer.
|
||||
|
||||
Best-effort: applies automatic fixes if tools exist. Safe to run repeatedly.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def has_file(*names: str) -> bool:
|
||||
return any((Path.cwd() / name).exists() for name in names)
|
||||
|
||||
|
||||
def run(cmd: list[str]) -> int:
|
||||
try:
|
||||
return subprocess.run(cmd, check=False).returncode
|
||||
except Exception:
|
||||
return 0
|
||||
|
||||
|
||||
def main() -> int:
|
||||
import shutil
|
||||
|
||||
# Node/TS: eslint --fix and prettier --write
|
||||
if has_file("package.json"):
|
||||
if shutil.which("npx"):
|
||||
run(["npx", "--yes", "eslint", "--fix", "."]) # type: ignore
|
||||
run(["npx", "--yes", "prettier", "--write", "."]) # type: ignore
|
||||
elif shutil.which("pnpm"):
|
||||
run(["pnpm", "dlx", "eslint", "--fix", "."]) # type: ignore
|
||||
run(["pnpm", "dlx", "prettier", "--write", "."]) # type: ignore
|
||||
|
||||
# Python: ruff --fix or autoflake + isort + black
|
||||
if has_file("pyproject.toml", "requirements.txt"):
|
||||
if shutil.which("ruff"):
|
||||
run(["ruff", "check", "--fix", "."]) # type: ignore
|
||||
else:
|
||||
if shutil.which("autoflake"):
|
||||
run(["autoflake", "-r", "--in-place", "--remove-all-unused-imports", "."]) # type: ignore
|
||||
if shutil.which("isort"):
|
||||
run(["isort", "."]) # type: ignore
|
||||
if shutil.which("black"):
|
||||
run(["black", "."]) # type: ignore
|
||||
|
||||
# Go: go fmt and go fix
|
||||
if has_file("go.mod") and shutil.which("go"):
|
||||
run(["go", "fmt", "./..."]) # type: ignore
|
||||
run(["go", "fix", "./..."]) # type: ignore
|
||||
|
||||
# Rust: cargo clippy --fix (requires nightly for some fixes; allow failure)
|
||||
if has_file("Cargo.toml") and shutil.which("cargo"):
|
||||
run(["cargo", "clippy", "--fix", "-Z", "unstable-options", "--allow-dirty", "--allow-staged"]) # type: ignore
|
||||
|
||||
# Java: no standard auto-fix; skip
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user