lint-clang-format.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env bash
  2. set -e
  3. script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
  4. cd "${script_path}/.." || exit 1
  5. if [ "$#" -eq "1" ]; then
  6. mapfile -t files < <(
  7. git ls-files -- \
  8. '*.cpp' \
  9. '*.h' \
  10. ':!:Base' \
  11. ':!:Kernel/FileSystem/ext2_fs.h' \
  12. ':!:Userland/DevTools/HackStudio/LanguageServers/Cpp/Tests/*' \
  13. ':!:Userland/Libraries/LibCpp/Tests/parser/*' \
  14. ':!:Userland/Libraries/LibCpp/Tests/preprocessor/*'
  15. )
  16. else
  17. files=()
  18. for file in "${@:2}"; do
  19. if [[ "${file}" == *".cpp" || "${file}" == *".h" ]]; then
  20. files+=("${file}")
  21. fi
  22. done
  23. fi
  24. if (( ${#files[@]} )); then
  25. CLANG_FORMAT=false
  26. if command -v clang-format-11 >/dev/null 2>&1 ; then
  27. CLANG_FORMAT=clang-format-11
  28. elif command -v clang-format >/dev/null 2>&1 ; then
  29. CLANG_FORMAT=clang-format
  30. if ! "${CLANG_FORMAT}" --version | awk '{ if (substr($NF, 1, index($NF, ".") - 1) < 11) exit 1; }'; then
  31. echo "You are using '$("${CLANG_FORMAT}" --version)', which appears to not be clang-format 11 or later."
  32. echo "It is very likely that the resulting changes are not what you wanted."
  33. fi
  34. else
  35. echo "clang-format-11 is not available, but C or C++ files need linting! Either skip this script, or install clang-format-11."
  36. echo "(If you install a package 'clang-format', please make sure it's version 11 or later.)"
  37. exit 1
  38. fi
  39. if [ "$#" -gt "0" ] && [ "--overwrite-inplace" = "$1" ] ; then
  40. true # The only way to run this script.
  41. else
  42. # Note that this branch also covers --help, -h, -help, -?, etc.
  43. echo "USAGE: $0 --overwrite-inplace"
  44. echo "The argument is necessary to make you aware that this *will* overwrite your local files."
  45. exit 1
  46. fi
  47. echo "Using ${CLANG_FORMAT}"
  48. "${CLANG_FORMAT}" -style=file -i "${files[@]}"
  49. echo "Maybe some files have changed. Sorry, but clang-format doesn't indicate what happened."
  50. else
  51. echo "No .cpp or .h files to check."
  52. fi