rubyforgood/casa

View on GitHub
bin/git_hooks/lint

Summary

Maintainability
Test Coverage
#!/bin/sh
# Lints files on the current branch
# Usage:
#   lint <options>
#     --staged
#       lints if the files staged for commit contain lintable files
#     --unpushed
#       lints files changed by commits not yet pushed to origin
#     --all (default)
#       lints all files in the repo

repo_root="$(git rev-parse --show-toplevel)"
. "$repo_root/bin/git_hooks/logger"

log info "Attempting to lint"

current_branch="$(git branch --show-current)"

if [ $# -lt 1 ]; then
  diff_policy="--all"
else
  diff_policy=$1
fi

case $diff_policy in
  --all)
    changed_file_list="app/models/a.rb\nb.erb\nc.js"
    ;;

  --staged)
    changed_file_list=$(git diff --name-only --cached)
    ;;

  --unpushed)
    if [ -z "$(git ls-remote --heads origin ${current_branch})" ]; then
      changed_file_list=$(git diff --name-only HEAD~$(git cherry -v main ${current_branch} | wc -l) HEAD)
    else
      changed_file_list=$(git diff --name-only origin/${current_branch}..HEAD)
    fi
    ;;

  *)
    log error "unknown option $diff_policy"
    exit 1
    ;;
esac

erb_changed_count=$(echo "$changed_file_list" | grep -c "\.erb$")
factory_changed_count=$(echo "$changed_file_list" | grep -c -E "(app\/models|spec\/factories)\/.*\.rb$")
js_changed_count=$(echo "$changed_file_list" | grep -c "\.js$")
rb_changed_count=$(echo "$changed_file_list" | grep -c "\.rb$")

lint_time=$(date +%s)

if test $erb_changed_count -gt 0; then
  log info "Linting via erblint"

  cd $repo_root/app

  if ! [ -x "$(command -v bundle)" ]; then
    log error "Command bundle could not be found"
    exit 1
  else
    if ! bundle exec erblint --lint-all --autocorrect ; then
      log error "ERB Lint linting failed, could not fix 1 or more issues\n  See above output for more details"
      exit 1
    fi
  fi
fi

if test $factory_changed_count -gt 0; then
  log info "Linting via factory:lint"

  cd $repo_root/app

  if ! [ -x "$(command -v bundle)" ]; then
    log error "Command bundle could not be found"
    exit 1
  else
    if ! bundle exec rake factory_bot:lint; then
      log error "Factory bot linting failed, could not fix 1 or more issues\n  See above output for more details"
      exit 1
    fi
  fi
fi

if test $js_changed_count -gt 0; then
  log info "Linting javasript via standard"

  cd $repo_root/app

  if ! [ -x "$(command -v yarn)" ]; then
    log error "Command yarn could not be found"
    exit 1
  else
    if ! yarn lint:fix ; then
      log error "yarn linting failed, could not fix 1 or more issues\n  See above output for more details"
      exit 1
    fi
  fi
fi

if test $rb_changed_count -gt 0; then
  log info "Linting via standardrb"

  cd $repo_root

  if ! [ -x "$(command -v bundle)" ]; then
    log error "Command bundle could not be found"
    exit 1
  else
    if ! bundle exec standardrb --fix ; then
      log error "Standard linting failed, could not fix 1 or more issues\n  See above output for more details"
      exit 1
    fi
  fi
fi

cd $repo_root
for file in $(git diff --name-only); do
  last_modified_time=$(date -r $file +%s)
  if [ $last_modified_time -ge $lint_time ] ; then
    log warn "Some files were linted. Please preserve the changes before continuing.\n  Run git diff to view linter changes."
    exit 1
  fi
done

log success "No files were linted"