Last active
September 26, 2017 13:45
-
-
Save MatiasFernandez/68b26ba09bd9b7b99e727f1e00e2c522 to your computer and use it in GitHub Desktop.
Git pre commit hook to run rubocop on modified files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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