elvis-check.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env bash
  2. ## This script checks style of changed files.
  3. ## Expect argument 1 to be the git compare base.
  4. set -euo pipefail
  5. elvis_version='1.0.0-emqx-2'
  6. base="${1:-}"
  7. repo="${2:-emqx/emqx}"
  8. REPO="${GITHUB_REPOSITORY:-${repo}}"
  9. if [ "${base}" = "" ]; then
  10. echo "Usage $0 <git-compare-base-ref>"
  11. exit 1
  12. fi
  13. echo "elvis -v: $elvis_version"
  14. echo "git diff base: $base"
  15. if [ ! -f ./elvis ] || [ "$(./elvis -v | grep -oE '[1-9]+\.[0-9]+\.[0-9]+\-emqx-[0-9]+')" != "$elvis_version" ]; then
  16. curl --silent --show-error -fLO "https://github.com/emqx/elvis/releases/download/$elvis_version/elvis"
  17. chmod +x ./elvis
  18. fi
  19. if [[ "$base" =~ [0-9a-f]{8,40} ]]; then
  20. # base is a commit sha1
  21. compare_base="$base"
  22. else
  23. remote="$(git remote -v | grep -E "github\.com(:|/)$REPO((\.git)|(\s))" | grep fetch | awk '{print $1}')"
  24. git fetch "$remote" "$base"
  25. compare_base="$remote/$base"
  26. fi
  27. git_diff() {
  28. git diff --name-only --diff-filter=ACMRTUXB "$compare_base"...HEAD
  29. }
  30. bad_file_count=0
  31. for file in $(git_diff); do
  32. if [ ! -f "$file" ]; then
  33. # file is deleted, skip
  34. continue
  35. fi
  36. if [[ $file != *.erl ]]; then
  37. # not .erl file
  38. continue
  39. fi
  40. if ! ./elvis rock "$file" -c elvis.config; then
  41. bad_file_count=$(( bad_file_count + 1))
  42. fi
  43. done
  44. if [ $bad_file_count -gt 0 ]; then
  45. echo "elvis: $bad_file_count errors"
  46. exit 1
  47. fi
  48. ### now check new-line at EOF for changed files
  49. nl_at_eof() {
  50. local file="$1"
  51. if ! [ -f "$file" ]; then
  52. return
  53. fi
  54. case "$file" in
  55. *.png|*rebar3)
  56. return
  57. ;;
  58. esac
  59. local lastbyte
  60. lastbyte="$(tail -c 1 "$file" 2>&1)"
  61. if [ "$lastbyte" != '' ]; then
  62. echo "$file"
  63. return 1
  64. fi
  65. }
  66. for file in $(git_diff); do
  67. if ! nl_at_eof "$file"; then
  68. bad_file_count=$(( bad_file_count + 1 ))
  69. fi
  70. done
  71. exit $bad_file_count