clang-format-check 658 B

123456789101112131415161718192021222324252627
  1. #!/bin/bash
  2. CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
  3. CLANG_FORMAT_VERSION=${CLANG_FORMAT_VERSION:-}
  4. if ! type $CLANG_FORMAT >/dev/null || \
  5. ! $CLANG_FORMAT --version | grep -q "version ${CLANG_FORMAT_VERSION}"; then
  6. # If running tests, mark this test as skipped.
  7. exit 77
  8. fi
  9. errors=0
  10. paths=$(git ls-files | grep '\.[ch]$')
  11. for path in $paths; do
  12. in=$(cat $path)
  13. out=$($CLANG_FORMAT $path)
  14. if [ "$in" != "$out" ]; then
  15. diff -u -L $path -L "$path.formatted" $path - <<<$out
  16. errors=1
  17. fi
  18. done
  19. if [ $errors -ne 0 ]; then
  20. echo "Formatting errors detected, run ./scripts/clang-format to fix!"
  21. exit 1
  22. fi