Skip to content

Instantly share code, notes, and snippets.

@annacruz
Created December 30, 2019 13:25
Show Gist options
  • Save annacruz/397c6829c3e91a55300fde01bdfb063d to your computer and use it in GitHub Desktop.
Save annacruz/397c6829c3e91a55300fde01bdfb063d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# put this in the file .git/hooks/pre-commit
require "shellwords"
puts `git status --porcelain`
puts "Running Rubocop from #{__FILE__}"
changed = `git status --porcelain`.
split("\n").
map { |l| l.split(" ", 2) }.
select { |status, _| ["A", "AM", "M"].include?(status) }.
map { |_, file| file.delete('"') }
exit if changed.empty?
parallel = ((File.read(".rubocop.yml").include?("UseCache: false") rescue false) ? "" : " --parallel")
result = `bundle exec rubocop#{parallel} --force-exclusion #{changed.shelljoin}`
puts result unless $?.success?
exit $?.exitstatus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment