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 17cd $(dirname $0)/../.. 18 19function find_without_newline() { 20 git ls-files | 21 sed -En -e '/^third_party/d' -e '/\.(c|cc|proto|rb|py|cs|sh)$/p' | 22 while IFS= read -r -d '' f; do 23 if [[ ! -z $f ]]; then 24 if [[ $(tail -c 1 "$f") != $NEWLINE ]]; then 25 echo "Error: file '$f' is missing a trailing newline character." 26 if $1; then # fix 27 sed -i -e '$a\' $f 28 echo 'Fixed!' 29 fi 30 fi 31 fi 32 done 33} 34 35if [[ $# == 1 && $1 == '--fix' ]]; then 36 FIX=true 37else 38 FIX=false 39fi 40 41ERRORS=$(find_without_newline $FIX) 42if [[ "$ERRORS" != '' ]]; then 43 echo "$ERRORS" 44 if ! $FIX; then 45 exit 1 46 fi 47fi 48