testsAndMisc/linux_configuration/.githooks/pre-commit
Krzysztof Rudnicki 2b3c1a69f7 fix(hooks): make git hooks and their toolchain mandatory and fail-closed
A completely absent `prettier` binary went unnoticed long enough for
unformatted markdown to reach main. Two independent causes, both fixed here.

No pre-push hook existed. The three hooks declaring `stages: [pre-push]` in
.pre-commit-config.yaml — prettier, ci-mirror, pytest-coverage — therefore
never ran locally at all. Adds one.

The pre-commit hook failed open. shfmt and shellcheck were wrapped in
`if command -v`, and the entire pre-commit framework was skipped with a
warning when its binary was missing. A silently skipped gate is
indistinguishable from a passing one, so a missing tool now aborts.

- lib/common.sh: require_tool tries `sudo -n pacman -S` and aborts with the
  exact command when a tool cannot be installed non-interactively;
  verify_toolchain applies it to all eight binaries the hooks invoke.
- The pre-push hook resolves git's stdin refs into --from-ref/--to-ref.
  Without a range, `pre-commit run --hook-stage pre-push` checks *staged*
  files, and nothing is staged at push time, so prettier would have received
  zero files and passed vacuously.
- ensure_hooks_path repairs core.hooksPath drift on every hook run.
- meta/scripts/install_hooks.sh covers the one case the hooks cannot
  self-heal — a fresh clone where the config was never set, so nothing runs.
  `--check` reports state and exits non-zero when incomplete.

Note `pre-commit install` must never be used here: it writes into
.git/hooks/, which git ignores entirely while core.hooksPath is set.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 11:09:11 +02:00

113 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
repo_root=$(git rev-parse --show-toplevel)
cd "$repo_root"
# shellcheck source=linux_configuration/.githooks/lib/common.sh
source "${repo_root}/linux_configuration/.githooks/lib/common.sh"
printf 'Verifying hook wiring and toolchain before committing...\n'
ensure_hooks_path
verify_toolchain
printf ' ✓ toolchain complete\n'
printf 'Running auto-fixers and shell_check before committing...\n'
# Get list of staged shell files
mapfile -t staged_files < <(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(sh|bash|zsh)$' || true)
if [[ ${#staged_files[@]} -gt 0 ]]; then
printf 'Auto-fixing %d shell file(s)...\n' "${#staged_files[@]}"
# Auto-fix with shfmt (guaranteed present by verify_toolchain)
printf ' → Running shfmt...\n'
for file in "${staged_files[@]}"; do
if [[ -f $file ]]; then
shfmt -w "$file" 2> /dev/null || true
fi
done
# Re-stage the auto-fixed files
for file in "${staged_files[@]}"; do
if [[ -f $file ]]; then
git add "$file"
fi
done
printf ' ✓ Auto-fixes applied and staged\n'
fi
printf 'Running shell_check validation...\n'
# Run shell_check to validate all checks
# 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 (guaranteed present by verify_toolchain)
# -x: follow source directives
# -S warning: only fail on warning or higher (not info-level SC1091)
if ! shellcheck -x -S warning "${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
printf 'shell_check passed.\n'
# Run duplicate code detection
printf 'Running duplicate code detection...\n'
JSCPD_BIN="${HOME}/.local/node_modules/.bin/jscpd"
# Install jscpd if not present
if [[ ! -x $JSCPD_BIN ]]; then
printf ' → jscpd not found, installing...\n'
if ! npm install --prefix ~/.local jscpd 2>&1; then
printf '\nCommit aborted: failed to install jscpd.\n' >&2
printf 'Please install manually: npm install --prefix ~/.local jscpd\n' >&2
exit 1
fi
printf ' ✓ jscpd installed\n'
fi
# Run jscpd and capture output
# --min-lines 14: minimum 14 lines to consider a clone (ignores small boilerplate)
# --min-tokens 25: minimum 25 tokens to consider a clone
# --threshold 2: fail if more than 2% duplication
jscpd_output=$("$JSCPD_BIN" \
--pattern "**/*.sh" \
--min-lines 14 \
--min-tokens 25 \
--threshold 2 \
--reporters "console" \
--ignore "**/node_modules/**,**/.git/**,**/misc/testsAndMisc-bash/**,**/*.txt" \
. 2>&1) || jscpd_exit=$?
if [[ ${jscpd_exit:-0} -ne 0 ]]; then
printf '\n%s\n' "$jscpd_output"
printf '\nCommit aborted: duplicate code exceeds 2%% threshold.\n' >&2
printf 'Consider extracting common code to scripts/lib/common.sh\n' >&2
printf 'To see all duplicates: %s --pattern "**/*.sh" --min-lines 5 .\n' "$JSCPD_BIN" >&2
exit 1
fi
printf ' ✓ Duplication check passed (under 2%% threshold)\n'
# Run pre-commit framework hooks (.pre-commit-config.yaml)
# This covers: Python (ruff, mypy, pylint, bandit, flake8, autoflake),
# C/C++ (clang-format, cppcheck, flawfinder), TypeScript (eslint),
# shell (shellcheck), and general checks (trailing-whitespace, etc.)
printf '\nRunning pre-commit framework hooks...\n'
if ! pre-commit run --hook-stage pre-commit; then
printf '\nCommit aborted: pre-commit hooks failed.\n' >&2
printf 'Fix the issues above and retry the commit.\n' >&2
exit 1
fi
printf ' ✓ pre-commit framework hooks passed\n'
printf 'All checks passed. Proceeding with commit.\n'