From 3d2ad5f292e88fdea4df43ce9171212f2420d081 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sun, 23 Nov 2025 12:25:08 +0100 Subject: [PATCH] afegit pre-hooks --- .claude/settings.local.json | 7 +++- CLAUDE.md | 10 +++++ Makefile | 11 ++++- tools/hooks/pre-commit | 80 +++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100755 tools/hooks/pre-commit diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 0becd5a..25f2e95 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -2,7 +2,12 @@ "permissions": { "allow": [ "Bash(cat:*)", - "Bash(cmake --build:*)" + "Bash(cmake --build:*)", + "Bash(chmod:*)", + "Bash(.git/hooks/pre-commit)", + "Bash(brew --prefix:*)", + "Bash(make setup_hooks:*)", + "Bash(tools/hooks/pre-commit)" ], "deny": [], "ask": [] diff --git a/CLAUDE.md b/CLAUDE.md index 7193544..c973cc6 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -107,3 +107,13 @@ tools/linter/run_cppcheck.sh -u # unused functions - OpenGL - C++20 compiler (clang++ on macOS, g++ on Linux/Windows) - CMake 3.10+ + +## Git Hooks + +Pre-commit hooks are stored in `tools/hooks/` and must be configured after cloning: + +```bash +make setup_hooks +``` + +This configures git to use `tools/hooks/` as the hooks directory. The pre-commit hook runs `clang-format` to verify code formatting before each commit. diff --git a/Makefile b/Makefile index 8011abb..1b4ab67 100644 --- a/Makefile +++ b/Makefile @@ -385,6 +385,14 @@ linux_release: # Elimina la carpeta temporal $(RMDIR) "$(RELEASE_FOLDER)" +# ============================================================================== +# SETUP +# ============================================================================== +setup_hooks: + @echo "Configurando git hooks..." + git config core.hooksPath tools/hooks + @echo "✓ Git hooks configurados desde tools/hooks/" + # ============================================================================== # REGLAS ESPECIALES # ============================================================================== @@ -407,9 +415,10 @@ help: @echo " macos_release - Crear release completo para macOS" @echo " pack_tool - Compilar herramienta de empaquetado" @echo " resources.pack - Generar pack de recursos desde data/" + @echo " setup_hooks - Configurar git hooks (pre-commit)" @echo " show_version - Mostrar version actual ($(VERSION))" @echo " help - Mostrar esta ayuda" FORCE: -.PHONY: windows windows_debug windows_release macos macos_debug macos_release linux linux_debug linux_release pack_tool resources.pack show_version help +.PHONY: windows windows_debug windows_release macos macos_debug macos_release linux linux_debug linux_release pack_tool resources.pack setup_hooks show_version help diff --git a/tools/hooks/pre-commit b/tools/hooks/pre-commit new file mode 100755 index 0000000..346d3f1 --- /dev/null +++ b/tools/hooks/pre-commit @@ -0,0 +1,80 @@ +#!/bin/bash +# +# Pre-commit hook: Verifica el formato de archivos C++ con clang-format +# Los archivos en source/external/ son ignorados automáticamente +# + +# Colores para output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +NC='\033[0m' # No Color + +# Buscar clang-format en ubicaciones comunes +CLANG_FORMAT=$(command -v clang-format) +if [ -z "$CLANG_FORMAT" ]; then + # Buscar en ubicaciones comunes de macOS/Linux + for path in \ + "/opt/homebrew/opt/llvm/bin/clang-format" \ + "/opt/homebrew/bin/clang-format" \ + "/usr/local/opt/llvm/bin/clang-format" \ + "/usr/local/bin/clang-format" \ + "/usr/bin/clang-format"; do + if [ -x "$path" ]; then + CLANG_FORMAT="$path" + break + fi + done +fi + +if [ -z "$CLANG_FORMAT" ]; then + echo -e "${YELLOW}Warning: clang-format not found, skipping format check${NC}" + exit 0 +fi + +# Obtener archivos staged (solo .cpp, .hpp, .h) +STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|hpp|h)$') + +# Si no hay archivos C++ staged, salir +if [ -z "$STAGED_FILES" ]; then + exit 0 +fi + +# Filtrar archivos en external/ +FILES_TO_CHECK="" +for file in $STAGED_FILES; do + # Excluir source/external/ y archivos generados + if [[ ! "$file" =~ ^source/external/ ]] && [[ ! "$file" =~ project\.h$ ]]; then + if [ -f "$file" ]; then + FILES_TO_CHECK="$FILES_TO_CHECK $file" + fi + fi +done + +# Si no quedan archivos después del filtrado, salir +if [ -z "$FILES_TO_CHECK" ]; then + exit 0 +fi + +# Verificar formato +FAILED_FILES="" +for file in $FILES_TO_CHECK; do + if ! $CLANG_FORMAT --dry-run --Werror "$file" 2>/dev/null; then + FAILED_FILES="$FAILED_FILES\n - $file" + fi +done + +# Reportar resultados +if [ -n "$FAILED_FILES" ]; then + echo -e "${RED}Formatting errors found in:${NC}" + echo -e "$FAILED_FILES" + echo "" + echo -e "Run ${YELLOW}clang-format -i ${NC} to fix, or use:" + echo -e " ${YELLOW}cmake --build build --target format${NC}" + echo "" + echo -e "To bypass this check, use: ${YELLOW}git commit --no-verify${NC}" + exit 1 +fi + +echo -e "${GREEN}Format check passed${NC}" +exit 0