Skip to content

Instantly share code, notes, and snippets.

@tspacek
Last active May 1, 2020 05:53
Show Gist options
  • Save tspacek/ddf8c4d63b110ab3c402 to your computer and use it in GitHub Desktop.
Save tspacek/ddf8c4d63b110ab3c402 to your computer and use it in GitHub Desktop.
Pre-commit hook to run Rubocop
#!/usr/bin/env ruby
require 'english'
require 'rubocop'
require 'yaml'
ADDED_OR_MODIFIED = /A|AM|^M/.freeze
ignored_files = YAML.load_file('.rubocop.yml')['AllCops']['Exclude'] rescue []
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'
}.
select { |file_name|
not ignored_files.include? file_name
}.join(' ')
rubocop_command = "rubocop #{changed_files} -D -R"
unless changed_files.empty?
puts rubocop_command + "\n"
system(rubocop_command)
end
exit $CHILD_STATUS.to_s[-1].to_i
@tspacek
Copy link
Author

tspacek commented Aug 4, 2014

curl https://gist.githubusercontent.com/tspacek/ddf8c4d63b110ab3c402/raw/190a207d0327cb0d26aa79e833b8724194877fe0/Rubocop%20pre-commit > .git/hooks/pre-commit && chmod 755 .git/hooks/pre-commit;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment