1
0

pre-commit 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/bin/sh
  2. #
  3. # Pre-commit hook to format C++ files with clang-format
  4. # Excludes third-party library folders
  5. # Folders to exclude (third-party sources)
  6. EXCLUDE_DIRS="libs"
  7. # Get list of staged C++ files
  8. STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|h|c|cc|cxx|hpp)$')
  9. if [ -z "$STAGED_FILES" ]; then
  10. exit 0
  11. fi
  12. # Check if clang-format is available
  13. if ! command -v clang-format >/dev/null 2>&1; then
  14. echo "Warning: clang-format not found. Skipping formatting."
  15. echo "Install clang-format to enable automatic formatting."
  16. exit 0
  17. fi
  18. # Format each file that is not in excluded directories
  19. for FILE in $STAGED_FILES; do
  20. # Check if file is in excluded directory
  21. EXCLUDED=0
  22. for EXCLUDE_DIR in $EXCLUDE_DIRS; do
  23. case "$FILE" in
  24. $EXCLUDE_DIR/*)
  25. EXCLUDED=1
  26. break
  27. ;;
  28. esac
  29. done
  30. if [ $EXCLUDED -eq 0 ]; then
  31. echo "Formatting: $FILE"
  32. clang-format -i "$FILE"
  33. git add "$FILE"
  34. else
  35. echo "Skipping (third-party): $FILE"
  36. fi
  37. done
  38. exit 0