Skip to content

Instantly share code, notes, and snippets.

@ryanoglesby08
Created February 26, 2015 01:13
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 ryanoglesby08/a24d602bf5d5a58f444f to your computer and use it in GitHub Desktop.
Save ryanoglesby08/a24d602bf5d5a58f444f to your computer and use it in GitHub Desktop.
A rake task to run rubocop on changed ruby files in a git repository. Meant to be run as part of a pre-commit task.
desc 'Run Rubocop on uncommitted changed files'
task :git_rubocop do
RUBY_FILENAME_PATTERNS = %w(Gemfile Rakefile .rb .rake)
def ruby_file?(filename)
RUBY_FILENAME_PATTERNS.each do |pattern|
return true if filename.include? pattern
end
false
end
def deleted?(git_file)
git_file.split(' ').first.include? 'D'
end
def filename_from(git_file)
git_file.split(' ').last
end
git_files = `git status -uno --porcelain`
filenames = git_files.split("\n")
.reject { |git_file| deleted?(git_file) }
.map { |git_file| filename_from(git_file) }
.select { |filename| ruby_file?(filename) }
.join(' ')
sh "bundle exec rubocop #{filenames}" unless filenames.empty?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment