Skip to content

Instantly share code, notes, and snippets.

@danielpowell4
Created February 2, 2017 19:16
Show Gist options
  • Save danielpowell4/fb80b5fff300a6968f75348d805143a7 to your computer and use it in GitHub Desktop.
Save danielpowell4/fb80b5fff300a6968f75348d805143a7 to your computer and use it in GitHub Desktop.
Rubocop precommit git hook for ruby apps that need cleanup as new features are coming along
# Place in your project here: .git/hooks/pre-commit
# Runs all cops on newly added files
# Runs standard cops on files that have been modified
#!/usr/bin/env ruby
require 'rubocop'
def get_files(selector)
`git status --porcelain`.split(/\n/).
select { |file_name_with_status|
file_name_with_status =~ selector
}.
map { |file_name_with_status|
file_name_with_status.split(' ')[1]
}.
select { |file_name|
File.extname(file_name) == '.rb'
}.join(' ')
end
ADDED = /A/.freeze
MODIFIED = /AM|^M/.freeze
changed_files = get_files(MODIFIED)
added_files = get_files(ADDED)
system("rubocop #{changed_files}") unless changed_files.empty?
system("rubocop -c .rubocop_full.yml #{added_files}") unless added_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