-
-
Save paulopatto/06ac013d497ac7650af7e6ec772bd81c to your computer and use it in GitHub Desktop.
Ruby style guide git pre-commit hook using Rubocop as the style guide checker. Only runs on staged ruby files that have been added and/or modified.
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 | |
# http://ruby-doc.org/stdlib-2.0.0/libdoc/English/rdoc/English.html | |
require 'English' | |
require 'rubocop' | |
ADDED_OR_MODIFIED = /^\s*(A|AM|M)\s*/ | |
GIT_COMMAND = "git status --porcelain".freeze | |
modified_files = `#{GIT_COMMAND}`.split(/\n/) | |
changed_files = modified_files | |
.select { |file| file =~ ADDED_OR_MODIFIED } # Check monitored files | |
.map { |file| file.gsub(ADDED_OR_MODIFIED, '') } # collect modified files names | |
.select { |file| File.extname(file) == '.rb' } # Check to ruby files (.rb) | |
.join(' ') # joins files in string | |
system("rubocop --color #{changed_files}") 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
See https://gist.github.com/mpeteuil/6147292#gistcomment-1733607