check_code_format.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/bin/bash
  2. # Copyright (c) 2017 Google Inc.
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # Script to determine if source code in Pull Request is properly formatted.
  16. # Exits with non 0 exit code if formatting is needed.
  17. #
  18. # This script assumes to be invoked at the project root directory.
  19. FILES_TO_CHECK=$(git diff --name-only master | grep -E ".*\.(cpp|cc|c\+\+|cxx|c|h|hpp)$")
  20. if [ -z "${FILES_TO_CHECK}" ]; then
  21. echo "No source code to check for formatting."
  22. exit 0
  23. fi
  24. FORMAT_DIFF=$(git diff -U0 master -- ${FILES_TO_CHECK} | python ./utils/clang-format-diff.py -p1 -style=file)
  25. if [ -z "${FORMAT_DIFF}" ]; then
  26. echo "All source code in PR properly formatted."
  27. exit 0
  28. else
  29. echo "Found formatting errors!"
  30. echo "${FORMAT_DIFF}"
  31. exit 1
  32. fi