From f3566821a678fca214653350d06738f1a5342590 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 14 May 2026 17:48:43 +0200 Subject: [PATCH] afegir git hooks per format, tidy i cppcheck --- .githooks/pre-commit | 92 ++++++++++++++++++++++++++++++++++++++++++++ Makefile | 10 ++++- 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..685e87c --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +# Pre-commit hook: aplica clang-format als fitxers C++ staged abans del commit. +# - Només toca fitxers staged dins source/ (exclou source/external/). +# - Avorta el commit si hi ha canvis NO staged en aquests fitxers (per no incloure'ls sense voler). + +set -euo pipefail + +if ! command -v clang-format >/dev/null 2>&1; then + echo "pre-commit: clang-format no trobat — saltant format check" >&2 + exit 0 +fi + +mapfile -t STAGED < <(git diff --cached --name-only --diff-filter=ACMR \ + | grep -E '^source/.*\.(cpp|hpp|h)$' \ + | grep -vE '^source/external/' || true) + +if [ ${#STAGED[@]} -eq 0 ]; then + exit 0 +fi + +UNSTAGED_DIRTY=() +for f in "${STAGED[@]}"; do + if ! git diff --quiet -- "$f"; then + UNSTAGED_DIRTY+=("$f") + fi +done + +if [ ${#UNSTAGED_DIRTY[@]} -gt 0 ]; then + echo "pre-commit: aquests fitxers tenen canvis NO staged i estan al commit." >&2 + echo " Fes 'git add' o 'git stash' abans de continuar:" >&2 + printf ' %s\n' "${UNSTAGED_DIRTY[@]}" >&2 + exit 1 +fi + +clang-format -i "${STAGED[@]}" +git add -- "${STAGED[@]}" + +# --- clang-tidy només sobre els fitxers staged --- +if ! command -v clang-tidy >/dev/null 2>&1; then + echo "pre-commit: clang-tidy no trobat — saltant tidy" >&2 + exit 0 +fi + +REPO_ROOT="$(git rev-parse --show-toplevel)" +BUILD_DIR="$REPO_ROOT/build" + +if [ ! -f "$BUILD_DIR/compile_commands.json" ]; then + echo "pre-commit: generant compile_commands.json (build dir buit)..." >&2 + cmake -S "$REPO_ROOT" -B "$BUILD_DIR" >/dev/null +fi + +echo "pre-commit: clang-tidy sobre ${#STAGED[@]} fitxer(s)..." >&2 +if ! clang-tidy -p "$BUILD_DIR" --quiet "${STAGED[@]}"; then + echo "pre-commit: clang-tidy ha trobat errors — commit avortat" >&2 + exit 1 +fi + +# --- cppcheck només sobre els .cpp staged --- +if ! command -v cppcheck >/dev/null 2>&1; then + echo "pre-commit: cppcheck no trobat — saltant cppcheck" >&2 + exit 0 +fi + +CPP_STAGED=() +for f in "${STAGED[@]}"; do + [[ "$f" == *.cpp ]] && CPP_STAGED+=("$f") +done + +if [ ${#CPP_STAGED[@]} -eq 0 ]; then + exit 0 +fi + +echo "pre-commit: cppcheck sobre ${#CPP_STAGED[@]} fitxer(s)..." >&2 +if ! cppcheck \ + --enable=warning,style,performance,portability \ + --std=c++20 \ + --language=c++ \ + --inline-suppr \ + --suppress=missingIncludeSystem \ + --suppress=toomanyconfigs \ + --suppress='*:*source/external/*' \ + --suppress='*:*source/core/rendering/sdl3gpu/spv/*' \ + --suppress=normalCheckLevelMaxBranches \ + -D_DEBUG \ + -DLINUX_BUILD \ + --quiet \ + --error-exitcode=1 \ + -I "$REPO_ROOT/source" \ + "${CPP_STAGED[@]}"; then + echo "pre-commit: cppcheck ha trobat errors — commit avortat" >&2 + exit 1 +fi diff --git a/Makefile b/Makefile index e748e49..448128c 100644 --- a/Makefile +++ b/Makefile @@ -432,6 +432,13 @@ cppcheck: @cmake $(CMAKE_GEN) -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) @cmake --build build --target cppcheck +# ============================================================================== +# GIT HOOKS +# ============================================================================== +hooks-install: + @git config core.hooksPath .githooks + @echo "Git hooks activats: $(shell pwd)/.githooks" + # DESCARGA DE GAMECONTROLLERDB # ============================================================================== controllerdb: @@ -481,6 +488,7 @@ help: @echo " make clean - Borrar carpeta $(BUILDDIR)/" @echo " make rebuild - clean + all" @echo " make show-version - Mostrar version actual ($(VERSION))" + @echo " make hooks-install - Activar git hooks del proyecto" @echo " make help - Mostrar esta ayuda" -.PHONY: all debug run run-debug clean rebuild release _windows-release _macos-release _linux-release wasm wasm-debug compile-shaders pack controllerdb format format-check tidy tidy-fix cppcheck show-version help +.PHONY: all debug run run-debug clean rebuild release _windows-release _macos-release _linux-release wasm wasm-debug compile-shaders pack controllerdb format format-check tidy tidy-fix cppcheck hooks-install show-version help