Update (working)
This commit is contained in:
parent
ba998dda99
commit
94db7760e2
2 changed files with 135 additions and 43 deletions
147
format_code.sh
147
format_code.sh
|
@ -1,54 +1,131 @@
|
|||
#!/bin/bash
|
||||
set -eo pipefail
|
||||
|
||||
# Configuration
|
||||
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")
|
||||
FILE_EXTS=("*.h" "*.cpp" "*.hpp" "*.c")
|
||||
|
||||
# Options
|
||||
DRY_RUN=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
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
--dry-run) DRY_RUN=true; shift ;;
|
||||
--verbose) VERBOSE=true; shift ;;
|
||||
--config) CONFIG_FILE="$2"; shift 2 ;;
|
||||
*) echo "Unknown option: $1"; exit 1 ;;
|
||||
--dry-run) DRY_RUN=true; shift ;;
|
||||
--verbose) VERBOSE=true; shift ;;
|
||||
--modified-only) CHECK_MODIFIED=true; shift ;;
|
||||
--help) show_help; exit 0 ;;
|
||||
*) echo "Unknown option: $1"; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Find all source files
|
||||
FILES=()
|
||||
|
||||
for dir in "${TARGET_DIRS[@]}"; do
|
||||
full_dir="$ROOT_DIR/$dir"
|
||||
if [ -d "$full_dir" ]; then
|
||||
while IFS= read -r -d $'\0' file; do
|
||||
FILES+=("$file")
|
||||
done < <(find "$full_dir" -type f \( -name "*.h" -o -name "*.cpp" -o -name "*.hpp" -o -name "*.c" \) -print0)
|
||||
# Verify dependencies
|
||||
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
|
||||
done
|
||||
|
||||
# Formatting command
|
||||
FORMAT_CMD="$ROOT_DIR/.tools/clang-format -style=file:$CONFIG_FILE -i"
|
||||
$DRY_RUN && FORMAT_CMD+=" --dry-run --Werror"
|
||||
|
||||
# Execute formatting
|
||||
ERRORS=0
|
||||
for file in "${FILES[@]}"; do
|
||||
if $VERBOSE; then
|
||||
echo "Checking $file"
|
||||
if [[ ! -f "${CLANG_CONFIG}" ]]; then
|
||||
echo "❌ Fichier de configuration .clang-format manquant"
|
||||
echo " Vérifiez le chemin: ${CLANG_CONFIG}"
|
||||
exit 2
|
||||
fi
|
||||
if ! $FORMAT_CMD "$file"; then
|
||||
((ERRORS++))
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
if [ $ERRORS -gt 0 ]; then
|
||||
echo "❌ $ERRORS files need formatting!"
|
||||
exit 1
|
||||
else
|
||||
echo "✅ All ${#FILES[@]} files formatted correctly!"
|
||||
exit 0
|
||||
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
|
||||
local full_dir="${ROOT_DIR}/${dir}"
|
||||
if [[ -d "${full_dir}" ]]; then
|
||||
while IFS= read -r -d '' file; do
|
||||
files+=("$file")
|
||||
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
|
||||
done
|
||||
|
||||
if [[ ${errors} -gt 0 ]]; then
|
||||
echo -e "\n❌ ${errors} fichier(s) nécessitent un formatage"
|
||||
exit 1
|
||||
else
|
||||
echo -e "\n✅ Tous les fichiers sont correctement formatés !"
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
|
||||
# Execution
|
||||
main
|
|
@ -1,12 +1,27 @@
|
|||
#!/bin/bash
|
||||
|
||||
HOOKS_DIR="../.git/hooks"
|
||||
PRE_PUSH_HOOK="$HOOKS_DIR/pre-push"
|
||||
HOOK_TYPE="pre-push" # Changement ici
|
||||
GIT_HOOKS_DIR="$(git rev-parse --git-path hooks)"
|
||||
TARGET_HOOK="$GIT_HOOKS_DIR/$HOOK_TYPE"
|
||||
|
||||
# Create symlink
|
||||
ln -sf "$(dirname "$0")/format_code.sh" "$PRE_PUSH_HOOK"
|
||||
cat > "$TARGET_HOOK" <<EOL
|
||||
#!/bin/bash
|
||||
|
||||
# Set permissions
|
||||
chmod +x "$PRE_PUSH_HOOK"
|
||||
set -eo pipefail
|
||||
|
||||
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 !"
|
Loading…
Reference in a new issue