Files
coder/scripts/githooks/pre-commit
T
Mathias Fredriksson 23542cb6af feat: smart file-based target selection for scripts/githooks (#23358)
Pre-commit classifies staged files and runs make pre-commit-light
when no Go, TypeScript, or Makefile changes are present. This
skips gen, lint/go, lint/ts, fmt/go, fmt/ts, and the binary
build. A markdown-only commit takes seconds instead of minutes.

Pre-push uses the same heuristic: if only light files changed
(docs, shell, terraform, etc.), tests are skipped entirely.
Falls back to the full make targets when Go/TS/Makefile changes
are detected, CODER_HOOK_RUN_ALL=1 is set, or the diff range
can't be determined.

Also adds test-storybook to make pre-push (vitest with the
storybook project in Playwright browser mode).
2026-03-20 17:05:44 +02:00

41 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Pre-commit hook that runs CI-equivalent checks locally.
# Classifies staged files by type and only runs relevant make
# targets. Falls back to the full `make pre-commit` when the
# Makefile changed or CODER_HOOK_RUN_ALL=1 is set.
#
# Installation (worktree-compatible):
#
# git config core.hooksPath scripts/githooks
#
# Bypass: git commit --no-verify
set -euo pipefail
cd "$(git rev-parse --show-toplevel)"
unset GIT_DIR
# In linked worktrees, set worktree-scoped hooksPath to override shared config.
if [[ "$(git rev-parse --git-dir)" != "$(git rev-parse --git-common-dir)" ]]; then
git config --worktree core.hooksPath scripts/githooks
fi
if [[ ${CODER_HOOK_RUN_ALL:-} == 1 ]]; then
exec make pre-commit
fi
staged=$(git diff --cached --name-only --diff-filter=d)
if [[ -z $staged ]]; then
echo "pre-commit: no staged changes, skipping"
exit 0
fi
# If Go, TS, or build-system files changed, run the full
# pre-commit. Otherwise run the lightweight target that
# covers everything except gen, Go/TS fmt+lint, and binary build.
if echo "$staged" | grep -qE '\.(go|ts|tsx|sql|proto)$|^go\.(mod|sum)$|^site/|^Makefile$'; then
exec make pre-commit
fi
exec make pre-commit-light