Skip to content

Instantly share code, notes, and snippets.

@paulopatto
Forked from mpeteuil/rubocop_pre_commit_hook
Last active April 19, 2017 07:49
Show Gist options
  • Save paulopatto/06ac013d497ac7650af7e6ec772bd81c to your computer and use it in GitHub Desktop.
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.
#!/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
@paulopatto
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment