Last active
August 29, 2015 14:23
-
-
Save alvarezloaiciga/b28253ffedb52159c8af to your computer and use it in GitHub Desktop.
Rubocop pre-commit hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# This is a modified version of https://gist.github.com/mpeteuil/6147292 | |
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(' ') | |
unless changed_files.empty? | |
puts "\n\n FIXING STYLEGUIDES WITH RUBOCOP \n\n" | |
rubocop = system("rubocop -a --force-exclusion #{changed_files}") | |
if rubocop | |
system("git add #{changed_files}") | |
else | |
puts "\n\n\e[31mPlease fix the affected files, rubocop was not able to fix them.\e[0m\n\n" | |
exit 1 | |
end | |
end | |
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