Skip to content

Commit 72d01c5

Browse files
committed
cmake format target: add whitespace correction
Our format target does formatting of C++ and Python files via clang-format and yapf. Annoyingly, it hasn't done some extra whitespace checks that CI does. So a PR can be made that passes `format` locally but then CI will reject it for trailing whitespace, or DOS line endinds. This adds a new tools/whitespace-format.sh script that automatically fixes trailing whitespace and DOS carriage returns, matching the checks performed by CI. This script is then integrated into the `format` cmake target so it gets run for that. I verified that the script works on both Linux and Mac.
1 parent 1fc5b47 commit 72d01c5

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,18 @@ add_custom_target(
806806
VERBATIM
807807
)
808808

809+
add_custom_target(
810+
whitespace-format
811+
${CMAKE_SOURCE_DIR}/tools/whitespace-format.sh
812+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
813+
COMMENT "fixing whitespace issues (trailing whitespace, DOS line endings)"
814+
VERBATIM
815+
)
816+
809817
# Add a format target that runs all the formatters.
810818
add_custom_target(
811819
format
812-
DEPENDS clang-format yapf cmake-format
820+
DEPENDS clang-format yapf cmake-format whitespace-format
813821
COMMENT "formatting all files"
814822
)
815823

tools/whitespace-format.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)