Skip to content

Instantly share code, notes, and snippets.

@yoon
Created May 24, 2016 00:44
Show Gist options
  • Save yoon/c8a2080926c0d0638cd691bee437132d to your computer and use it in GitHub Desktop.
Save yoon/c8a2080926c0d0638cd691bee437132d to your computer and use it in GitHub Desktop.
rubocop pre-commit
#!/usr/bin/env ruby
# This is a modified version of https://gist.github.com/mpeteuil/6147292
require 'english'
require 'rubocop'
ADDED_OR_MODIFIED = /A|AM|^M/.freeze
changed_files = `git status --porcelain`.split(/\n/).
select { |file_name_with_status|
file_name_with_status =~ ADDED_OR_MODIFIED
}.
map { |file_name_with_status|
file_name_with_status.split(' ')[1]
}.
select { |file_name|
File.extname(file_name) == '.rb'
}.join(' ')
unless ENV["SKIP_RUBOCOP"] || changed_files.empty?
puts "\n\n FIXING STYLEGUIDES WITH RUBOCOP \n\n"
rubocop = system("rubocop -a --force-exclusion #{changed_files}")
if rubocop
system("git add #{changed_files}")
else
puts "\n\n\e[31mPlease fix the affected files, rubocop was not able to fix them.\e[0m\n\n"
exit 1
end
end
exit $CHILD_STATUS.to_s[-1].to_i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment