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. elvis_version='1.1.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. git remote -v
  24. remote="$(git remote -v | grep -E "github\.com(:|/)$REPO((\.git)|(\s))" | grep fetch | awk '{print $1}')"
  25. git fetch "$remote" "$base"
  26. compare_base="$remote/$base"
  27. fi
  28. git_diff() {
  29. git diff --name-only --diff-filter=ACMRTUXB "$compare_base"...HEAD
  30. }
  31. bad_file_count=0
  32. for file in $(git_diff); do
  33. if [ ! -f "$file" ]; then
  34. # file is deleted, skip
  35. continue
  36. fi
  37. if [[ $file != *.erl ]]; then
  38. # not .erl file
  39. continue
  40. fi
  41. echo "$file ..."
  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