From d74ea47e2c55a463b26b2d30c2909a4be5c778f3 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Sat, 7 Mar 2026 02:26:30 +0000 Subject: [PATCH] fix: enhance pre-commit hooks to auto-fix end-of-file and trailing whitespace issues, and re-stage modified files for review --- lefthook.yml | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/lefthook.yml b/lefthook.yml index a4150c27..528eb0ed 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -24,18 +24,32 @@ pre-commit: glob: "*.{go,ts,tsx,js,jsx,yaml,yml,sh,md}" exclude: "frontend/(coverage|dist|node_modules|\\.vite)/|.*\\.tsbuildinfo$" run: | + modified=0 for file in {staged_files}; do [ -f "$file" ] && [ -s "$file" ] && \ - [ -n "$(tail -c1 "$file")" ] && echo >> "$file" + [ -n "$(tail -c1 "$file")" ] && echo >> "$file" && modified=1 done - git add {staged_files} + if [ "$modified" -eq 1 ]; then + git add {staged_files} + echo "end-of-file-fixer: files modified and re-staged — review changes and commit again." + exit 1 + fi trailing-whitespace: glob: "*.{go,ts,tsx,js,jsx,yaml,yml,sh,md}" exclude: "frontend/(coverage|dist|node_modules|\\.vite)/|.*\\.tsbuildinfo$" run: | - sed -i 's/[[:space:]]*$//' {staged_files} - git add {staged_files} + modified=0 + for file in {staged_files}; do + if grep -qP '\s+$' "$file" 2>/dev/null; then + sed -i 's/[[:space:]]*$//' "$file" && modified=1 + fi + done + if [ "$modified" -eq 1 ]; then + git add {staged_files} + echo "trailing-whitespace: trailing spaces removed and re-staged — review changes and commit again." + exit 1 + fi check-yaml: glob: "*.{yaml,yml}" @@ -87,7 +101,15 @@ pre-commit: frontend-lint: glob: "frontend/**/*.{ts,tsx,js,jsx}" - run: cd frontend && npm run lint -- --fix + run: | + cd frontend && npm run lint -- --fix; lint_exit=$? + cd .. + if ! git diff --quiet -- {staged_files}; then + git add {staged_files} + echo "ESLint: auto-fixed files and re-staged — review changes and commit again." + exit 1 + fi + exit $lint_exit # ============================================================ @@ -177,5 +199,12 @@ lint-full: run: docker run --rm -i hadolint/hadolint < Dockerfile markdownlint: - run: markdownlint --fix . + run: | + markdownlint --fix .; md_exit=$? + if ! git diff --quiet; then + git add -A + echo "markdownlint: auto-fixed files and re-staged — review changes and commit again." + exit 1 + fi + exit $md_exit exclude: "node_modules|\\.venv|test-results|codeql-db|codeql-agent-results"