fix: enhance pre-commit hooks to auto-fix end-of-file and trailing whitespace issues, and re-stage modified files for review
This commit is contained in:
41
lefthook.yml
41
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"
|
||||
|
||||
Reference in New Issue
Block a user