Skip to content

Instantly share code, notes, and snippets.

@mpeteuil
Created August 3, 2013 17:44
Show Gist options
  • Star 56 You must be signed in to star a gist
  • Fork 28 You must be signed in to fork a gist
  • Save mpeteuil/6147292 to your computer and use it in GitHub Desktop.
Save mpeteuil/6147292 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
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(' ')
system("rubocop #{changed_files}") unless changed_files.empty?
exit $CHILD_STATUS.to_s[-1].to_i
@pachacamac
Copy link

Needed to change require "english" to require "English"

@drmartell
Copy link

I'm using this to run against staged files only: https://gist.github.com/drmartell/a45a0d277921943aab91c6b593956de4

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