Debian Codegraph @

tools/distrib/check_trailing_newlines.sh

raw
1 #!/bin/bash
2 # Copyright 2016 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
16 # change to root directory
17 cd $(dirname $0)/../..
18
19 function find_without_newline() {
20 find . -type f -not -path './third_party/*' -and \( \
21 -name '*.c' \
22 -or -name '*.cc' \
23 -or -name '*.proto' \
24 -or -name '*.rb' \
25 -or -name '*.py' \
26 -or -name '*.cs' \
27 -or -name '*.sh' \) -print0 \
28 | while IFS= read -r -d '' f; do
29 if [[ ! -z $f ]]; then
30 if [[ $(tail -c 1 "$f") != $NEWLINE ]]; then
31 echo "Error: file '$f' is missing a trailing newline character."
32 if $1; then # fix
33 sed -i -e '$a\' $f
34 echo 'Fixed!'
35 fi
36 fi
37 fi
38 done
39 }
40
41 if [[ $# == 1 && $1 == '--fix' ]]; then
42 FIX=true
43 else
44 FIX=false
45 fi
46
47 ERRORS=$(find_without_newline $FIX)
48 if [[ "$ERRORS" != '' ]]; then
49 echo "$ERRORS"
50 if ! $FIX; then
51 exit 1
52 fi
53 fi