1#!/bin/bash
2# Copyright 2015 gRPC authors.
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
16set -e
17
18# directories to run against
19DIRS="examples/cpp examples/android/binder src/core/lib src/core/tsi src/core/ext src/cpp test/core test/cpp include src/compiler src/ruby src/objective-c tools/distrib/python"
20
21# file matching patterns to check
22GLOB="*.h *.c *.cc *.m *.mm"
23
24# clang format command
25CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
26
27# number of CPUs available
28CPU_COUNT=`nproc`
29
30files=
31for dir in $DIRS
32do
33  for glob in $GLOB
34  do
35    files="$files `find ${CLANG_FORMAT_ROOT}/$dir -name $glob -and -not -name '*.generated.*' -and -not -name '*.upb.h' -and -not -name '*.upb.c' -and -not -name '*.upbdefs.h' -and -not -name '*.upbdefs.c' -and -not -name '*.pb.h' -and -not -name '*.pb.c' -and -not -name '*.pb.cc' -and -not -name '*.pbobjc.h' -and -not -name '*.pbobjc.m' -and -not -name '*.pbrpc.h' -and -not -name '*.pbrpc.m' -and -not -name end2end_tests.cc -and -not -name grpc_shadow_boringssl.h -and -not -name grpc_tls_credentials_options.h -and -not -name grpc_tls_credentials_options_comparator_test.cc`"
36  done
37done
38
39# The CHANGED_FILES variable is used to restrict the set of files to check.
40# Here we set files to the intersection of files and CHANGED_FILES
41if [ -n "$CHANGED_FILES" ]; then
42  files=$(comm -12 <(echo $files | tr ' ' '\n' | sort -u) <(echo $CHANGED_FILES | tr ' ' '\n' | sort -u))
43fi
44
45if [ "$TEST" == "" ]
46then
47  echo $files | xargs -P $CPU_COUNT -n 1 $CLANG_FORMAT -i
48else
49  ok=yes
50  for file in $files
51  do
52    tmp=`mktemp`
53    $CLANG_FORMAT $file > $tmp
54    diff -u $file $tmp || ok=no
55    rm $tmp
56  done
57  if [ $ok == no ]
58  then
59    false
60  fi
61fi
62