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