You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lint-whitespace.sh 2.6KB

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