Skip to content

Instantly share code, notes, and snippets.

@KarlGl
Created August 4, 2015 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KarlGl/573ef6086cf265a629a9 to your computer and use it in GitHub Desktop.
Save KarlGl/573ef6086cf265a629a9 to your computer and use it in GitHub Desktop.
Rubocop Pre-Commit Hook
#!/usr/bin/env ruby
# Put this file in .git/hooks/pre-commit
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(' ')
system("bundle exec rubocop #{changed_files} -a") unless changed_files.empty?
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