• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright 2018 Google LLC
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
21FILES_TO_CHECK=$(git diff --name-only main | grep -E ".*\.(cpp|cc|c\+\+|cxx|c|h|hpp)$")
22
23if [ -z "${FILES_TO_CHECK}" ]; then
24  echo "No source code to check for formatting."
25  exit 0
26fi
27
28FORMAT_DIFF=$(git diff -U0 main -- ${FILES_TO_CHECK} | python ./tools/clang-format-diff.py -p1 -style=file)
29
30if [ -z "${FORMAT_DIFF}" ]; then
31  echo "All source code in PR properly formatted."
32  exit 0
33else
34  echo "Found formatting errors!"
35  echo "${FORMAT_DIFF}"
36  exit 1
37fi
38