Update (working)

This commit is contained in:
Lucas Peter 2025-03-05 15:23:04 +01:00
parent ba998dda99
commit 94db7760e2
No known key found for this signature in database
2 changed files with 135 additions and 43 deletions

View file

@ -1,54 +1,131 @@
#!/bin/bash #!/bin/bash
set -eo pipefail
# Configuration # Configuration
ROOT_DIR=$(git rev-parse --show-toplevel) ROOT_DIR=$(git rev-parse --show-toplevel)
CONFIG_FILE="$ROOT_DIR/.tools/.clang-format" CLANG_FORMAT_BIN="${ROOT_DIR}/.tools/clang-format"
CLANG_CONFIG="${ROOT_DIR}/.tools/.clang-format"
TARGET_DIRS=("Source" "Private" "Public" "Classes") TARGET_DIRS=("Source" "Private" "Public" "Classes")
FILE_EXTS=("*.h" "*.cpp" "*.hpp" "*.c") FILE_EXTS=("*.h" "*.cpp" "*.hpp" "*.c")
# Options
DRY_RUN=false DRY_RUN=false
VERBOSE=false VERBOSE=false
CHECK_MODIFIED=false
# Help text
show_help() {
cat <<EOF
Usage: $0 [OPTIONS]
Options:
--dry-run Check formatting without modifying files
--verbose Show detailed processing information
--modified-only Check only modified files
--help Show this help message
Exit codes:
0 - Everything formatted correctly
1 - Formatting errors detected
2 - Configuration error
EOF
}
# Parse arguments # Parse arguments
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
--dry-run) DRY_RUN=true; shift ;; --dry-run) DRY_RUN=true; shift ;;
--verbose) VERBOSE=true; shift ;; --verbose) VERBOSE=true; shift ;;
--config) CONFIG_FILE="$2"; shift 2 ;; --modified-only) CHECK_MODIFIED=true; shift ;;
*) echo "Unknown option: $1"; exit 1 ;; --help) show_help; exit 0 ;;
*) echo "Unknown option: $1"; exit 2 ;;
esac esac
done done
# Find all source files # Verify dependencies
FILES=() check_dependencies() {
if [[ ! -x "${CLANG_FORMAT_BIN}" ]]; then
echo "❌ clang-format non trouvé ou non exécutable dans .tools/"
echo " Vérifiez le chemin: ${CLANG_FORMAT_BIN}"
exit 2
fi
if [[ ! -f "${CLANG_CONFIG}" ]]; then
echo "❌ Fichier de configuration .clang-format manquant"
echo " Vérifiez le chemin: ${CLANG_CONFIG}"
exit 2
fi
}
# File discovery
find_source_files() {
local files=()
if [[ "${CHECK_MODIFIED}" == true ]]; then
# Fichiers modifiés uniquement
mapfile -t files < <(git diff --name-only --diff-filter=ACMR HEAD -- $(printf "%s " "${TARGET_DIRS[@]}") | grep -E '\.(h|cpp|hpp|c)$')
else
# Tous les fichiers
for dir in "${TARGET_DIRS[@]}"; do for dir in "${TARGET_DIRS[@]}"; do
full_dir="$ROOT_DIR/$dir" local full_dir="${ROOT_DIR}/${dir}"
if [ -d "$full_dir" ]; then if [[ -d "${full_dir}" ]]; then
while IFS= read -r -d $'\0' file; do while IFS= read -r -d '' file; do
FILES+=("$file") files+=("$file")
done < <(find "$full_dir" -type f \( -name "*.h" -o -name "*.cpp" -o -name "*.hpp" -o -name "*.c" \) -print0) done < <(find "${full_dir}" -type f \( \
-name "*.h" -o \
-name "*.cpp" -o \
-name "*.hpp" -o \
-name "*.c" \) -print0)
fi
done
fi
printf "%s\n" "${files[@]}"
}
# Main process
main() {
check_dependencies
local files
mapfile -t files < <(find_source_files)
if [[ ${#files[@]} -eq 0 ]]; then
${VERBOSE} && echo " Aucun fichier trouvé dans ${TARGET_DIRS[*]}"
return
fi
${VERBOSE} && echo "📁 Fichiers à vérifier : ${#files[@]}"
local format_args=("-style=file" "${CLANG_CONFIG}")
local errors=0
if [[ "${DRY_RUN}" == true ]]; then
format_args+=(--dry-run --Werror)
${VERBOSE} && echo "🔍 Mode vérification seulement"
else
format_args+=(-i)
${VERBOSE} && echo "🛠️ Application du formatage"
fi
for file in "${files[@]}"; do
${VERBOSE} && echo "🔎 Traitement : ${file}"
if ! "${CLANG_FORMAT_BIN}" "${format_args[@]}" "${file}"; then
((errors++))
${DRY_RUN} && echo "❌ Problème détecté : ${file}"
fi fi
done done
# Formatting command if [[ ${errors} -gt 0 ]]; then
FORMAT_CMD="$ROOT_DIR/.tools/clang-format -style=file:$CONFIG_FILE -i" echo -e "\n❌ ${errors} fichier(s) nécessitent un formatage"
$DRY_RUN && FORMAT_CMD+=" --dry-run --Werror"
# Execute formatting
ERRORS=0
for file in "${FILES[@]}"; do
if $VERBOSE; then
echo "Checking $file"
fi
if ! $FORMAT_CMD "$file"; then
((ERRORS++))
fi
done
if [ $ERRORS -gt 0 ]; then
echo "$ERRORS files need formatting!"
exit 1 exit 1
else else
echo "✅ All ${#FILES[@]} files formatted correctly!" echo -e "\n✅ Tous les fichiers sont correctement formatés !"
exit 0 exit 0
fi fi
}
# Execution
main

View file

@ -1,12 +1,27 @@
#!/bin/bash #!/bin/bash
HOOKS_DIR="../.git/hooks" HOOK_TYPE="pre-push" # Changement ici
PRE_PUSH_HOOK="$HOOKS_DIR/pre-push" GIT_HOOKS_DIR="$(git rev-parse --git-path hooks)"
TARGET_HOOK="$GIT_HOOKS_DIR/$HOOK_TYPE"
# Create symlink cat > "$TARGET_HOOK" <<EOL
ln -sf "$(dirname "$0")/format_code.sh" "$PRE_PUSH_HOOK" #!/bin/bash
# Set permissions set -eo pipefail
chmod +x "$PRE_PUSH_HOOK"
echo "Git hooks installed successfully!" ROOT_DIR=\$(git rev-parse --show-toplevel)
export PATH="\\\$PATH:\\\$ROOT_DIR/.tools"
# Lancer le check de formatage
"\\\$ROOT_DIR/.tools/format_code.sh" --dry-run --verbose --modified-only || exit 1
# Vérifier les modifications non poussées
git diff --cached --quiet || {
echo -e "\\n❌ Des modifications formatées ne sont pas dans le commit final !"
echo "Utilisez 'git add . && git commit --amend' pour corriger"
exit 1
}
EOL
chmod +x "$TARGET_HOOK"
echo "Hook $HOOK_TYPE installé avec succès !"