|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Script to fix whitespace issues: trailing whitespace and DOS carriage returns. |
| 4 | +# |
| 5 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 6 | +# or more contributor license agreements. See the NOTICE file |
| 7 | +# distributed with this work for additional information |
| 8 | +# regarding copyright ownership. The ASF licenses this file |
| 9 | +# to you under the Apache License, Version 2.0 (the |
| 10 | +# "License"); you may not use this file except in compliance |
| 11 | +# with the License. You may obtain a copy of the License at |
| 12 | +# |
| 13 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +# |
| 15 | +# Unless required by applicable law or agreed to in writing, software |
| 16 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +# See the License for the specific language governing permissions and |
| 19 | +# limitations under the License. |
| 20 | + |
| 21 | +set -e |
| 22 | + |
| 23 | +# Get the repository root |
| 24 | +REPO_ROOT=$(cd $(dirname $0) && git rev-parse --show-toplevel) |
| 25 | + |
| 26 | +EXCLUDE_PATTERNS=( |
| 27 | + "lib/yamlcpp" |
| 28 | + "lib/Catch2" |
| 29 | + "lib/systemtap" |
| 30 | + ".gold:" |
| 31 | + ".test_input" |
| 32 | +) |
| 33 | + |
| 34 | +GREP_EXCLUDE_ARGS="" |
| 35 | +for pattern in "${EXCLUDE_PATTERNS[@]}"; do |
| 36 | + GREP_EXCLUDE_ARGS="${GREP_EXCLUDE_ARGS} | grep -F -v '${pattern}'" |
| 37 | +done |
| 38 | + |
| 39 | +echo "Fixing whitespace issues in ${REPO_ROOT}" |
| 40 | + |
| 41 | +# Should the file be excluded? |
| 42 | +should_exclude() { |
| 43 | + local file="$1" |
| 44 | + for pattern in "${EXCLUDE_PATTERNS[@]}"; do |
| 45 | + if echo "$file" | grep -F -q "$pattern"; then |
| 46 | + return 0 |
| 47 | + fi |
| 48 | + done |
| 49 | + return 1 |
| 50 | +} |
| 51 | + |
| 52 | +if [[ "$OSTYPE" == "darwin"* ]]; then |
| 53 | + SED_INPLACE=(sed -i '') |
| 54 | +else |
| 55 | + SED_INPLACE=(sed -i) |
| 56 | +fi |
| 57 | + |
| 58 | +FILES_MODIFIED=0 |
| 59 | + |
| 60 | +# Fix trailing whitespace. |
| 61 | +echo "Checking for trailing whitespace..." |
| 62 | +TRAILING_WS_FILES=$(git grep -IE ' +$' | eval "grep -F -v 'lib/yamlcpp' | grep -F -v 'lib/Catch2' | grep -F -v 'lib/systemtap' | grep -F -v '.gold:' | grep -F -v '.test_input'" | cut -d: -f1 | sort -u || true) |
| 63 | + |
| 64 | +if [ -n "$TRAILING_WS_FILES" ]; then |
| 65 | + while IFS= read -r file; do |
| 66 | + if [ -f "$file" ]; then |
| 67 | + echo " Fixing trailing whitespace in: $file" |
| 68 | + "${SED_INPLACE[@]}" 's/[[:space:]]*$//' "$file" |
| 69 | + FILES_MODIFIED=$((FILES_MODIFIED + 1)) |
| 70 | + fi |
| 71 | + done <<< "$TRAILING_WS_FILES" |
| 72 | +fi |
| 73 | + |
| 74 | +# Fix DOS carriage returns. |
| 75 | +echo "Checking for DOS carriage returns..." |
| 76 | +DOS_FILES=$(git grep -IE $'\r$' | eval "grep -F -v 'lib/yamlcpp' | grep -F -v 'lib/Catch2' | grep -F -v 'lib/systemtap' | grep -F -v '.test_input'" | cut -d: -f1 | sort -u || true) |
| 77 | + |
| 78 | +if [ -n "$DOS_FILES" ]; then |
| 79 | + while IFS= read -r file; do |
| 80 | + if [ -f "$file" ]; then |
| 81 | + echo " Removing DOS carriage returns from: $file" |
| 82 | + "${SED_INPLACE[@]}" $'s/\r$//' "$file" |
| 83 | + FILES_MODIFIED=$((FILES_MODIFIED + 1)) |
| 84 | + fi |
| 85 | + done <<< "$DOS_FILES" |
| 86 | +fi |
| 87 | + |
| 88 | +if [ $FILES_MODIFIED -eq 0 ]; then |
| 89 | + echo "Success! No whitespace issues found." |
| 90 | +else |
| 91 | + echo "Fixed whitespace issues in $FILES_MODIFIED file(s)." |
| 92 | +fi |
| 93 | + |
| 94 | +exit 0 |
| 95 | + |
0 commit comments