ML-187: restructure pre-commit checks to run conditionally

Gate each check behind staged file pattern matching so a docs
or backlog change no longer triggers the full test suite. Add
presto pytest and Docker image validation guards. Fall back to
running all checks when invoked directly (not via git commit).
This commit is contained in:
Claudio Ortolina
2026-05-18 16:09:34 +01:00
parent 795e511fc1
commit 974ef45c29
4 changed files with 121 additions and 30 deletions
+66 -19
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
#MISE description="Run checks before a commit"
#MISE description="Run checks before a commit (conditional on staged file types)"
set -e
@@ -8,27 +8,74 @@ source "$(git rev-parse --show-toplevel)/scripts/_helpers.sh"
ensure_working_directory!
debug_msg "Running shellcheck..."
fd . 'scripts/' --exclude '*.hurl' -t file --exec shellcheck --color
# Convert space-separated STAGED list to newline-delimited for grep.
# $STAGED is exported by .git/hooks/pre-commit via git diff-index.
staged_lines() {
echo "$STAGED" | tr ' ' '\n'
}
debug_msg "Running credo..."
mix credo --strict
# Early exit if no staged files (e.g., --allow-empty)
if [ -z "$STAGED" ]; then
debug_msg "No staged files found, skipping pre-commit checks."
exit 0
fi
debug_msg "Running sobelow..."
mix sobelow --compact --exit
# --- Shell scripts ---
if staged_lines | grep -qE '^scripts/|^\.shellcheckrc'; then
debug_msg "Running shellcheck..."
fd . 'scripts/' --exclude '*.hurl' -t file --exec shellcheck --color
fi
debug_msg "Checking translations..."
mix gettext.extract --check-up-to-date
# --- Elixir ---
if staged_lines | grep -qE '^(lib/|test/|config/|mix\.exs|mix\.lock|priv/repo/migrations/|priv/gettext/|\.credo\.exs|\.formatter\.exs|\.sobelow-conf)'; then
debug_msg "Running credo..."
mix credo --strict
debug_msg "Checking formatting..."
mix format --check-formatted
prettier --check '.pi/extensions/**/*.{ts,js,json}'
prettier --check 'assets/css/**/*.css' 'assets/js/**/*.js'
prettier --check 'docs/**/*.md' 'docs/**/*.livemd' 'README.md'
prettier --check 'backlog/archive/**/*.md' 'backlog/completed/**/*.md' 'backlog/tasks/**/*.md' 'backlog/docs/**/*.md'
debug_msg "Running sobelow..."
mix sobelow --compact --exit
debug_msg "Checking unused deps..."
mix deps.unlock --unused
debug_msg "Checking translations..."
mix gettext.extract --check-up-to-date
debug_msg "Running tests..."
mix test
debug_msg "Checking formatting..."
mix format --check-formatted
debug_msg "Running tests..."
mix test
fi
# deps.unlock sub-gate: only when mix.exs or mix.lock changed
if staged_lines | grep -qE '^mix\.exs$|^mix\.lock$'; then
debug_msg "Checking unused deps..."
mix deps.unlock --unused
fi
# --- Assets (JS/TS/CSS) ---
if staged_lines | grep -qE '^assets/|^\.pi/extensions/.*\.(ts|js|json)$'; then
debug_msg "Checking assets formatting..."
prettier --check 'assets/css/**/*.css' 'assets/js/**/*.js' '.pi/extensions/**/*.{ts,js,json}' '!.pi/extensions/**/node_modules/**'
fi
# --- Documentation ---
if staged_lines | grep -qE '^docs/|^README\.md$|^AGENTS\.md$'; then
debug_msg "Checking docs formatting..."
prettier --check 'docs/**/*.md' 'docs/**/*.livemd' 'README.md' 'AGENTS.md'
fi
# --- Backlog ---
if staged_lines | grep -qE '^backlog/'; then
debug_msg "Checking backlog formatting..."
prettier --check 'backlog/archive/**/*.md' 'backlog/completed/**/*.md' 'backlog/tasks/**/*.md' 'backlog/docs/**/*.md'
fi
# --- Presto ---
if staged_lines | grep -qE '^presto/'; then
debug_msg "Checking presto..."
(cd presto && mise run test)
fi
# --- Docker ---
if staged_lines | grep -qE '^Dockerfile$|^\.dockerignore$|^compose\.yaml$'; then
debug_msg "Validating Docker image..."
mise run dev:validate-docker-image
fi