elvis-check.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. set -x
  6. elvis_version='1.0.0-emqx-2'
  7. base="${1:-}"
  8. repo="${2:-emqx/emqx}"
  9. REPO="${GITHUB_REPOSITORY:-${repo}}"
  10. if [ "${base}" = "" ]; then
  11. echo "Usage $0 <git-compare-base-ref>"
  12. exit 1
  13. fi
  14. echo "elvis -v: $elvis_version"
  15. echo "git diff base: $base"
  16. if [ ! -f ./elvis ] || [ "$(./elvis -v | grep -oE '[1-9]+\.[0-9]+\.[0-9]+\-emqx-[0-9]+')" != "$elvis_version" ]; then
  17. curl --silent --show-error -fLO "https://github.com/emqx/elvis/releases/download/$elvis_version/elvis"
  18. chmod +x ./elvis
  19. fi
  20. if [[ "$base" =~ [0-9a-f]{8,40} ]]; then
  21. # base is a commit sha1
  22. compare_base="$base"
  23. else
  24. git remote -v
  25. remote="$(git remote -v | grep -E "github\.com(:|/)$REPO((\.git)|(\s))" | grep fetch | awk '{print $1}')"
  26. git fetch "$remote" "$base"
  27. compare_base="$remote/$base"
  28. fi
  29. git_diff() {
  30. git diff --name-only --diff-filter=ACMRTUXB "$compare_base"...HEAD
  31. }
  32. bad_file_count=0
  33. for file in $(git_diff); do
  34. if [ ! -f "$file" ]; then
  35. # file is deleted, skip
  36. continue
  37. fi
  38. if [[ $file != *.erl ]]; then
  39. # not .erl file
  40. continue
  41. fi
  42. if ! ./elvis rock "$file" -c elvis.config; then
  43. bad_file_count=$(( bad_file_count + 1))
  44. fi
  45. done
  46. if [ $bad_file_count -gt 0 ]; then
  47. echo "elvis: $bad_file_count errors"
  48. exit 1
  49. fi