Last active
May 1, 2020 05:53
-
-
Save tspacek/ddf8c4d63b110ab3c402 to your computer and use it in GitHub Desktop.
Pre-commit hook to run Rubocop
This file contains hidden or 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 | |
require 'english' | |
require 'rubocop' | |
require 'yaml' | |
ADDED_OR_MODIFIED = /A|AM|^M/.freeze | |
ignored_files = YAML.load_file('.rubocop.yml')['AllCops']['Exclude'] rescue [] | |
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' | |
}. | |
select { |file_name| | |
not ignored_files.include? file_name | |
}.join(' ') | |
rubocop_command = "rubocop #{changed_files} -D -R" | |
unless changed_files.empty? | |
puts rubocop_command + "\n" | |
system(rubocop_command) | |
end | |
exit $CHILD_STATUS.to_s[-1].to_i |
Author
tspacek
commented
Aug 4, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment