Skip to content

Instantly share code, notes, and snippets.

@MatiasFernandez
Last active September 26, 2017 13:45
Show Gist options
  • Save MatiasFernandez/68b26ba09bd9b7b99e727f1e00e2c522 to your computer and use it in GitHub Desktop.
Save MatiasFernandez/68b26ba09bd9b7b99e727f1e00e2c522 to your computer and use it in GitHub Desktop.
Git pre commit hook to run rubocop on modified files
force_skip = ['master', 'develop'].include? `git rev-parse --abbrev-ref HEAD`.strip
diff_files = []
# Note: ACMRTUXB is all file types except delete
diff_files.concat(`git --no-pager diff --name-only --diff-filter='ACMRTUXB' origin/develop...`.split(/\n/))
diff_files.concat(`git --no-pager diff --name-only --diff-filter='ACMRTUXB' --cached`.split(/\n/))
if force_skip || diff_files.empty?
# so that rubocop doesnt run any tests
puts '-v'
else
puts diff_files.grep(/\.rb$/).join(' ')
end
#!/bin/sh
echo "Running rubocop..."
bundle exec rubocop --auto-correct --force-exclusion --fail-level autocorrect $(ruby .git/hooks/changed_ruby_files.rb)
RUBOCOP_CODE=$?
if [ $RUBOCOP_CODE -ne 0 ]; then
exit $RUBOCOP_CODE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment