···11+#!/bin/bash
22+# Copyright (c) 2017 Google Inc.
33+44+# Licensed under the Apache License, Version 2.0 (the "License");
55+# you may not use this file except in compliance with the License.
66+# You may obtain a copy of the License at
77+#
88+# http://www.apache.org/licenses/LICENSE-2.0
99+#
1010+# Unless required by applicable law or agreed to in writing, software
1111+# distributed under the License is distributed on an "AS IS" BASIS,
1212+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313+# See the License for the specific language governing permissions and
1414+# limitations under the License.
1515+#
1616+# Script to determine if source code in Pull Request is properly formatted.
1717+# Exits with non 0 exit code if formatting is needed.
1818+#
1919+# This script assumes to be invoked at the project root directory.
2020+2121+BASE_BRANCH=${1:-master}
2222+2323+FILES_TO_CHECK=$(git diff --name-only ${BASE_BRANCH} | grep -E ".*\.(cpp|cc|c\+\+|cxx|c|h|hpp)$")
2424+2525+if [ -z "${FILES_TO_CHECK}" ]; then
2626+ echo "No source code to check for formatting."
2727+ exit 0
2828+fi
2929+3030+FORMAT_DIFF=$(git diff -U0 ${BASE_BRANCH} -- ${FILES_TO_CHECK} | python3 ./sdk/tools/clang-format-diff.py -binary ${CLFORMAT_BINARY} -p1 -style=file)
3131+3232+if [ -z "${FORMAT_DIFF}" ]; then
3333+ echo "All source code in PR properly formatted."
3434+ exit 0
3535+else
3636+ echo "Found formatting errors!"
3737+ echo "${FORMAT_DIFF}"
3838+ exit 1
3939+fi