check_trailing_newlines.sh 1.6 KB

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