Skip to content

Commit f4ed44a

Browse files
author
MarcoFalke
committed
Merge #11300: Tests: Add a lint check for trailing whitespace
1f379b1 Add tab char lint check and exclude imported dependencies (MeshCollider) dd36561 Add a lint check for trailing whitespace. (Evan Klitzke) Pull request description: This is a new attempt at #11005 Addressed nits, excluded imported dependencies, squashed the original commits, and added a test for tab characters in the *.cpp *.h *.md *.py *.sh files too as per @practicalswift suggestion Tree-SHA512: d2dfbedc8469026f39b0c63d9a71d8b8e2ed3815d69fecaabad10304d977d6345728c4c865ec7600ed539b1f7cabaa826b50312f4d2eef0a1583d4ff9024c36d
2 parents 7fcd61b + 1f379b1 commit f4ed44a

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
echo "Cannot run lint-whitespace.sh without commit range. To run locally, use:"
12+
echo "TRAVIS_COMMIT_RANGE='<commit range>' .lint-whitespace.sh"
13+
echo "For example:"
14+
echo "TRAVIS_COMMIT_RANGE='47ba2c3...ee50c9e' .lint-whitespace.sh"
15+
exit 1
16+
fi
17+
18+
showdiff() {
19+
if ! git diff -U0 "${TRAVIS_COMMIT_RANGE}" -- "." ":(exclude)src/leveldb/" ":(exclude)src/secp256k1/" ":(exclude)src/univalue/"; then
20+
echo "Failed to get a diff"
21+
exit 1
22+
fi
23+
}
24+
25+
showcodediff() {
26+
if ! git diff -U0 "${TRAVIS_COMMIT_RANGE}" -- *.cpp *.h *.md *.py *.sh ":(exclude)src/leveldb/" ":(exclude)src/secp256k1/" ":(exclude)src/univalue/"; then
27+
echo "Failed to get a diff"
28+
exit 1
29+
fi
30+
}
31+
32+
RET=0
33+
34+
# Check if trailing whitespace was found in the diff.
35+
if showdiff | grep -E -q '^\+.*\s+$'; then
36+
echo "This diff appears to have added new lines with trailing whitespace."
37+
echo "The following changes were suspected:"
38+
FILENAME=""
39+
SEEN=0
40+
while read -r line; do
41+
if [[ "$line" =~ ^diff ]]; then
42+
FILENAME="$line"
43+
SEEN=0
44+
elif [[ "$line" =~ ^@@ ]]; then
45+
LINENUMBER="$line"
46+
else
47+
if [ "$SEEN" -eq 0 ]; then
48+
# The first time a file is seen with trailing whitespace, we print the
49+
# filename (preceded by a newline).
50+
echo
51+
echo "$FILENAME"
52+
echo "$LINENUMBER"
53+
SEEN=1
54+
fi
55+
echo "$line"
56+
fi
57+
done < <(showdiff | grep -E '^(diff --git |@@|\+.*\s+$)')
58+
RET=1
59+
fi
60+
61+
# Check if tab characters were found in the diff.
62+
if showcodediff | grep -P -q '^\+.*\t'; then
63+
echo "This diff appears to have added new lines with tab characters instead of spaces."
64+
echo "The following changes were suspected:"
65+
FILENAME=""
66+
SEEN=0
67+
while read -r line; do
68+
if [[ "$line" =~ ^diff ]]; then
69+
FILENAME="$line"
70+
SEEN=0
71+
elif [[ "$line" =~ ^@@ ]]; then
72+
LINENUMBER="$line"
73+
else
74+
if [ "$SEEN" -eq 0 ]; then
75+
# The first time a file is seen with a tab character, we print the
76+
# filename (preceded by a newline).
77+
echo
78+
echo "$FILENAME"
79+
echo "$LINENUMBER"
80+
SEEN=1
81+
fi
82+
echo "$line"
83+
fi
84+
done < <(showcodediff | grep -P '^(diff --git |@@|\+.*\t)')
85+
RET=1
86+
fi
87+
88+
exit $RET

0 commit comments

Comments
 (0)