Skip to content

Commit dd36561

Browse files
eklitzkemeshcollider
authored andcommitted
Add a lint check for trailing whitespace.
This adds a new CHECK_DOC check that looks for newly introduced trailing whitespace. Existing trailing whitespace (of which there is plenty!) will not trigger an error. This is written in a generic way so that new lint-*.sh scripts can be added to contrib/devtools/, as I'd like to contribute additional lint checks in the future.
1 parent 7fcd61b commit dd36561

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ before_script:
4949
- if [ "$CHECK_DOC" = 1 -a "$TRAVIS_EVENT_TYPE" = "pull_request" ]; then contrib/devtools/commit-script-check.sh $TRAVIS_COMMIT_RANGE; fi
5050
- if [ "$CHECK_DOC" = 1 ]; then contrib/devtools/check-doc.py; fi
5151
- if [ "$CHECK_DOC" = 1 ]; then contrib/devtools/check-rpc-mappings.py .; fi
52+
- if [ "$CHECK_DOC" = 1 ]; then contrib/devtools/lint-all.sh; fi
5253
- unset CC; unset CXX
5354
- mkdir -p depends/SDKs depends/sdk-sources
5455
- if [ -n "$OSX_SDK" -a ! -f depends/sdk-sources/MacOSX${OSX_SDK}.sdk.tar.gz ]; then curl --location --fail $SDK_URL/MacOSX${OSX_SDK}.sdk.tar.gz -o depends/sdk-sources/MacOSX${OSX_SDK}.sdk.tar.gz; fi

contrib/devtools/lint-all.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2017 The Bitcoin Core developers
4+
# Distributed under the MIT software license, see the accompanying
5+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
6+
#
7+
# This script runs all contrib/devtools/lint-*.sh files, and fails if any exit
8+
# with a non-zero status code.
9+
10+
set -u
11+
12+
SCRIPTDIR=$(dirname "${BASH_SOURCE[0]}")
13+
LINTALL=$(basename "${BASH_SOURCE[0]}")
14+
15+
for f in "${SCRIPTDIR}"/lint-*.sh; do
16+
if [ "$(basename "$f")" != "$LINTALL" ]; then
17+
if ! "$f"; then
18+
echo "^---- failure generated from $f"
19+
exit 1
20+
fi
21+
fi
22+
done

contrib/devtools/lint-whitespace.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2017 The Bitcoin Core developers
4+
# Distributed under the MIT software license, see the accompanying
5+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
6+
#
7+
# Check for new lines in diff that introduce trailing whitespace.
8+
9+
# We can't run this check unless we know the commit range for the PR.
10+
if [ -z "${TRAVIS_COMMIT_RANGE}" ]; then
11+
exit 0
12+
fi
13+
14+
showdiff() {
15+
if ! git diff -U0 "${TRAVIS_COMMIT_RANGE}" --; then
16+
echo "Failed to get a diff"
17+
exit 1
18+
fi
19+
}
20+
21+
# Do a first pass, and if no trailing whitespace was found then exit early.
22+
if ! showdiff | grep -E -q '^\+.*\s+$'; then
23+
exit
24+
fi
25+
26+
echo "This diff appears to have added new lines with trailing whitespace."
27+
echo "The following changes were suspected:"
28+
29+
FILENAME=""
30+
SEEN=0
31+
32+
while read -r line; do
33+
if [[ "$line" =~ ^diff ]]; then
34+
FILENAME="$line"
35+
SEEN=0
36+
else
37+
if [ "$SEEN" -eq 0 ]; then
38+
# The first time a file is seen with trailing whitespace, we print the
39+
# filename (preceded by a newline).
40+
echo
41+
echo "$FILENAME"
42+
SEEN=1
43+
fi
44+
echo "$line"
45+
fi
46+
done < <(showdiff | grep -E '^(diff --git |\+.*\s+$)')
47+
exit 1

0 commit comments

Comments
 (0)