• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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="src/core/lib src/core/tsi src/core/ext src/cpp test/core test/cpp include src/compiler src/csharp src/ruby third_party/address_sorting src/objective-c"
20
21# file matching patterns to check
22GLOB="*.h *.c *.cc *.m *.mm"
23
24# clang format command
25CLANG_FORMAT=${CLANG_FORMAT:-clang-format-5.0}
26
27files=
28for dir in $DIRS
29do
30  for glob in $GLOB
31  do
32    files="$files `find ${CLANG_FORMAT_ROOT}/$dir -name $glob -and -not -name '*.generated.*' -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 end2end_nosec_tests.cc -and -not -name public_headers_must_be_c89.c -and -not -name grpc_shadow_boringssl.h`"
33  done
34done
35
36# The CHANGED_FILES variable is used to restrict the set of files to check.
37# Here we set files to the intersection of files and CHANGED_FILES
38if [ -n "$CHANGED_FILES" ]; then
39  files=$(comm -12 <(echo $files | tr ' ' '\n' | sort -u) <(echo $CHANGED_FILES | tr ' ' '\n' | sort -u))
40fi
41
42if [ "$TEST" == "" ]
43then
44  echo $files | xargs $CLANG_FORMAT -i
45else
46  ok=yes
47  for file in $files
48  do
49    tmp=`mktemp`
50    $CLANG_FORMAT $file > $tmp
51    diff -u $file $tmp || ok=no
52    rm $tmp
53  done
54  if [ $ok == no ]
55  then
56    false
57  fi
58fi
59