elvis-check.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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-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. if [[ "$base" =~ [0-9a-f]{8,40} ]]; then
  19. # base is a commit sha1
  20. compare_base="$base"
  21. else
  22. remote="$(git remote -v | grep -E 'github\.com(.|/)emqx' | grep fetch | awk '{print $1}')"
  23. git fetch "$remote" "$base"
  24. compare_base="$remote/$base"
  25. fi
  26. git_diff() {
  27. git diff --name-only --diff-filter=ACMRTUXB "$compare_base"...HEAD
  28. }
  29. bad_file_count=0
  30. for file in $(git_diff); do
  31. if [ ! -f "$file" ]; then
  32. # file is deleted, skip
  33. continue
  34. fi
  35. if [[ $file != *.erl ]]; then
  36. # not .erl file
  37. continue
  38. fi
  39. if ! ./elvis rock "$file" -c elvis.config; then
  40. bad_file_count=$(( bad_file_count + 1))
  41. fi
  42. done
  43. if [ $bad_file_count -gt 0 ]; then
  44. echo "elvis: $bad_file_count errors"
  45. exit 1
  46. fi