elvis-check.sh 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/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-1'
  6. base="${1:-}"
  7. if [ "${base}" = "" ]; then
  8. echo "Usage $0 <git-compare-base-ref>"
  9. exit 1
  10. fi
  11. elvis_version="${2:-$ELVIS_VERSION}"
  12. echo "elvis -v: $elvis_version"
  13. echo "git diff base: $base"
  14. if [ ! -f ./elvis ] || [ "$(./elvis -v | grep -oE '[1-9]+\.[0-9]+\.[0-9]+\-emqx-[0-9]+')" != "$elvis_version" ]; then
  15. curl -fLO "https://github.com/emqx/elvis/releases/download/$elvis_version/elvis"
  16. chmod +x ./elvis
  17. fi
  18. git fetch origin "$base"
  19. git_diff() {
  20. git diff --name-only origin/"$base"...HEAD
  21. }
  22. bad_file_count=0
  23. for file in $(git_diff); do
  24. if [ ! -f "$file" ]; then
  25. # file is deleted, skip
  26. continue
  27. fi
  28. if [[ $file != *.erl ]]; then
  29. # not .erl file
  30. continue
  31. fi
  32. if ! ./elvis rock "$file" -c elvis.config; then
  33. bad_file_count=$(( bad_file_count + 1))
  34. fi
  35. done
  36. if [ $bad_file_count -gt 0 ]; then
  37. echo "elvis: $bad_file_count errors"
  38. exit 1
  39. fi