diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 63b30cf..a454fdc 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -4,9 +4,12 @@ # Enable per-clone with: # git config core.hooksPath .githooks # -# Re-runs the script audit generator when any nomarchy-* script in the three -# script directories is added, modified, or deleted in this commit, then -# stages the refreshed docs/SCRIPTS.md so it lands together with the change. +# Two responsibilities: +# 1. Lint changed nomarchy-* scripts (bash -n + shellcheck if available) +# so syntax errors and unquoted-var bugs don't ship. +# 2. Regenerate docs/SCRIPTS.md when any nomarchy-* script under the three +# script directories is added, modified, or deleted in this commit, and +# stage the refreshed file so it lands with the change. set -e @@ -15,6 +18,31 @@ cd "$repo_root" script_dirs_re='^(features/scripts/utils|core/system/scripts|themes/engine/scripts)/nomarchy-' +# 1. Lint changed scripts. bash -n catches syntax errors (always fatal). +# shellcheck catches unquoted-var, use-before-define, missing-shebang, etc. +# We only fail on severity=error so the long tail of pre-existing warnings +# (info / style / warning) doesn't block commits — those can be cleaned up +# incrementally without a flag day. +changed_scripts=$(git diff --cached --name-only --diff-filter=ACMR \ + | grep -E "$script_dirs_re" || true) +if [[ -n "$changed_scripts" ]]; then + while IFS= read -r script; do + [[ -f "$script" ]] || continue + if ! bash -n "$script"; then + echo "pre-commit: bash syntax error in $script — aborting commit." >&2 + exit 1 + fi + if command -v shellcheck >/dev/null 2>&1; then + if ! shellcheck --severity=error --shell=bash "$script"; then + echo "pre-commit: shellcheck found error-level issues in $script — aborting commit." >&2 + echo "pre-commit: fix the reported issues, or rerun with --no-verify after a deliberate decision to ship." >&2 + exit 1 + fi + fi + done <<< "$changed_scripts" +fi + +# 2. Regenerate the script audit doc. if git diff --cached --name-only --diff-filter=ACMRD | grep -qE "$script_dirs_re"; then echo "pre-commit: regenerating docs/SCRIPTS.md (script change detected)…" ./bin/utils/nomarchy-docs-scripts --out docs/SCRIPTS.md