check_code_format.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. BASE_BRANCH=${1:-master}
  20. FILES_TO_CHECK=$(git diff --name-only ${BASE_BRANCH} | grep -E ".*\.(cpp|cc|c\+\+|cxx|c|h|hpp)$")
  21. if [ -z "${FILES_TO_CHECK}" ]; then
  22. echo "No source code to check for formatting."
  23. exit 0
  24. fi
  25. FORMAT_DIFF=$(git diff -U0 ${BASE_BRANCH} -- ${FILES_TO_CHECK} | python ./utils/clang-format-diff.py -p1 -style=file)
  26. if [ -z "${FORMAT_DIFF}" ]; then
  27. echo "All source code in PR properly formatted."
  28. exit 0
  29. else
  30. echo "Found formatting errors!"
  31. echo "${FORMAT_DIFF}"
  32. exit 1
  33. fi