|
8 | 8 |
|
9 | 9 | # We can't run this check unless we know the commit range for the PR.
|
10 | 10 | if [ -z "${TRAVIS_COMMIT_RANGE}" ]; then
|
11 |
| - exit 0 |
| 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 |
12 | 16 | fi
|
13 | 17 |
|
14 | 18 | showdiff() {
|
15 |
| - if ! git diff -U0 "${TRAVIS_COMMIT_RANGE}" --; then |
| 19 | + if ! git diff -U0 "${TRAVIS_COMMIT_RANGE}" -- "." ":(exclude)src/leveldb/" ":(exclude)src/secp256k1/" ":(exclude)src/univalue/"; then |
16 | 20 | echo "Failed to get a diff"
|
17 | 21 | exit 1
|
18 | 22 | fi
|
19 | 23 | }
|
20 | 24 |
|
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 | +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 | +} |
25 | 31 |
|
26 |
| -echo "This diff appears to have added new lines with trailing whitespace." |
27 |
| -echo "The following changes were suspected:" |
| 32 | +RET=0 |
28 | 33 |
|
29 |
| -FILENAME="" |
30 |
| -SEEN=0 |
| 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 |
31 | 60 |
|
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 |
| 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" |
43 | 83 | fi
|
44 |
| - echo "$line" |
45 |
| - fi |
46 |
| -done < <(showdiff | grep -E '^(diff --git |\+.*\s+$)') |
47 |
| -exit 1 |
| 84 | + done < <(showcodediff | grep -P '^(diff --git |@@|\+.*\t)') |
| 85 | + RET=1 |
| 86 | +fi |
| 87 | + |
| 88 | +exit $RET |
0 commit comments