Files
landingpage-haus-schleusingen/scripts/pre-commit-checks.sh
Hermes 85cf4f3b03 chore(hooks): add shared pre-commit-checks script
Extrahiert die Pre-Commit-Checks (lint-staged + PHPUnit) in ein
gemeinsames Script, das sowohl vom Husky-Hook (.husky/pre-commit)
als auch von scripts/safe-commit.sh aufgerufen wird.

Logik:
- lint-staged laeuft immer (HTML/CSS/JS/JSON/MD/PHP-Syntax)
- PHPUnit laeuft nur, wenn PHP-relevante Dateien gestaged sind
  (*.php, phpunit.xml, composer.json, composer.lock)
- Safety-Check: alle gestaged PHP-Dateien muessen auf Disk existieren
- Bei Test-Fehler wird der Commit mit Exit-Code != 0 abgebrochen
- Composer-Deps werden nur bei Bedarf installiert (Cache-Hit)
2026-06-03 23:42:22 +00:00

78 lines
3.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# pre-commit-checks.sh Pre-Commit Checks (Lint + PHPUnit)
# Wird vom Husky-Hook (.husky/pre-commit) und von scripts/safe-commit.sh aufgerufen.
#
# Abbruch mit Exit-Code != 0 bei Fehler.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$REPO_ROOT"
# ─────────────────────────────────────────────────────────────
# 1) lint-staged (HTML, CSS, JS, JSON, MD, PHP-Syntax)
# ─────────────────────────────────────────────────────────────
if command -v npx >/dev/null 2>&1 && [ -f "node_modules/.bin/lint-staged" ]; then
echo "🔍 Pre-Commit: lint-staged laeuft..."
if ! npx lint-staged; then
echo ""
echo "❌ Pre-Commit: lint-staged fehlgeschlagen. Commit abgebrochen."
exit 1
fi
echo "✅ Pre-Commit: lint-staged OK"
fi
# ─────────────────────────────────────────────────────────────
# 2) PHPUnit nur, wenn PHP-relevante Dateien gestaged sind
# - *.php Quellcode & Tests
# - phpunit.xml Test-Konfiguration
# - composer.json PHP-Abhaengigkeiten
# - composer.lock Lock-File
# ─────────────────────────────────────────────────────────────
PHP_TOUCHED=$(git diff --cached --name-only --diff-filter=ACMR \
| grep -E '\.(php)$|^phpunit\.xml$|^composer\.(json|lock)$' || true)
if [ -n "$PHP_TOUCHED" ]; then
echo ""
echo "==> PHP-Dateien geaendert -> PHPUnit wird ausgefuehrt"
echo " Betroffene Dateien:"
printf ' - %s\n' $PHP_TOUCHED
# Safety-Check: alle gestaged PHP-Dateien muessen auf der Disk existieren.
# Sonst wuerde PHPUnit eine inkonsistente Codebasis testen (Staged != Working Tree).
MISSING=""
for f in $PHP_TOUCHED; do
[ -f "$f" ] || MISSING="$MISSING $f"
done
if [ -n "$MISSING" ]; then
echo ""
echo "FEHLER: Gestaged PHP-Dateien existieren nicht auf der Disk:"
printf ' - %s\n' $MISSING
echo " Loesung: 'git restore --staged <datei>' oder Working-Tree synchronisieren."
exit 1
fi
# Composer-Deps nur installieren, falls noetig (Cache-Hit fuer wiederholte Commits)
if [ ! -f "vendor/bin/phpunit" ]; then
echo ""
echo "==> vendor/ fehlt -> composer install laeuft"
if ! command -v composer >/dev/null 2>&1; then
echo "FEHLER: 'composer' ist nicht installiert."
echo " Installation: https://getcomposer.org/download/"
exit 1
fi
composer install --no-interaction --prefer-dist --no-progress
fi
echo ""
echo "==> PHPUnit laeuft..."
if ! vendor/bin/phpunit; then
echo ""
echo "FEHLER: PHPUnit-Tests fehlgeschlagen. Commit abgebrochen."
echo " Behebe die Fehler und versuche es erneut."
exit 1
fi
echo ""
echo "==> PHPUnit OK"
fi