From c4ebfc0ed0fc1bc2784c02dbcd7b29330067429f Mon Sep 17 00:00:00 2001 From: AJ Heller Date: Wed, 24 Jan 2024 13:48:30 -0800 Subject: [PATCH] [sanity] Ignore .gitignored files in trailing newline check (#35655) See https://source.cloud.google.com/results/invocations/912189e8-824c-4f20-bef4-24416a2b666c Closes #35655 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/35655 from drfloob:check-fewer-things-for-newlines 3eb0a2bc3ad4b686e25a8709f806c965425391d2 PiperOrigin-RevId: 601223154 --- tools/distrib/check_trailing_newlines.sh | 28 ++++++++++-------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/tools/distrib/check_trailing_newlines.sh b/tools/distrib/check_trailing_newlines.sh index 21e194097a1..9d139d73876 100755 --- a/tools/distrib/check_trailing_newlines.sh +++ b/tools/distrib/check_trailing_newlines.sh @@ -17,25 +17,19 @@ cd $(dirname $0)/../.. function find_without_newline() { - find . -type f -not -path './third_party/*' -and \( \ - -name '*.c' \ - -or -name '*.cc' \ - -or -name '*.proto' \ - -or -name '*.rb' \ - -or -name '*.py' \ - -or -name '*.cs' \ - -or -name '*.sh' \) -print0 \ - | while IFS= read -r -d '' f; do - if [[ ! -z $f ]]; then - if [[ $(tail -c 1 "$f") != $NEWLINE ]]; then - echo "Error: file '$f' is missing a trailing newline character." - if $1; then # fix - sed -i -e '$a\' $f - echo 'Fixed!' + git ls-files | + sed -En -e '/^third_party/d' -e '/\.(c|cc|proto|rb|py|cs|sh)$/p' | + while IFS= read -r -d '' f; do + if [[ ! -z $f ]]; then + if [[ $(tail -c 1 "$f") != $NEWLINE ]]; then + echo "Error: file '$f' is missing a trailing newline character." + if $1; then # fix + sed -i -e '$a\' $f + echo 'Fixed!' + fi fi fi - fi - done + done } if [[ $# == 1 && $1 == '--fix' ]]; then