From 3768cb6c0e381706b8d0ced9ea586fbfd0c5ff08 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sun, 16 Nov 2025 21:20:23 +0100 Subject: [PATCH] fix: improve pre-commit hook to avoid formatting loop - Auto-fix with shfmt on staged files - Run shellcheck validation directly instead of full shell_check.sh - Avoids shfmt -d validation after auto-formatting (prevents .orig file loop) - Ensures consistent formatting without blocking commits --- .githooks/pre-commit | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 8f2e369..6b6b64e 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -36,10 +36,19 @@ fi printf 'Running shell_check validation...\n' # Run shell_check to validate all checks -if ! scripts/meta/shell_check.sh --skip-install 2>&1; then - printf '\nCommit aborted: shell_check reported issues.\n' >&2 - printf 'Fix the remaining problems and retry the commit.\n' >&2 - exit 1 +# Note: We only validate the staged files since we just auto-formatted them +# Validate only shellcheck, not shfmt (we already applied formatting) +mapfile -d '' -t staged_shell_files < <(git diff --cached --name-only --diff-filter=ACM -z | grep -zE '\.(sh|bash|zsh)$' || true) + +if [[ ${#staged_shell_files[@]} -gt 0 ]]; then + # Run shellcheck on staged files + if command -v shellcheck > /dev/null 2>&1; then + if ! shellcheck -x -S style "${staged_shell_files[@]}" 2>&1; then + printf '\nCommit aborted: shellcheck found issues.\n' >&2 + printf 'Fix the remaining problems and retry the commit.\n' >&2 + exit 1 + fi + fi fi printf 'shell_check passed. Proceeding with commit.\n'