Skip to content

Instantly share code, notes, and snippets.

@davidlibrera
Last active March 29, 2018 18:02
Show Gist options
  • Select an option

  • Save davidlibrera/2e43424ed11f3672e62526dc96d40feb to your computer and use it in GitHub Desktop.

Select an option

Save davidlibrera/2e43424ed11f3672e62526dc96d40feb 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 'rubocop'
changed_files = `git diff --name-only --cached`.split(/\n/).
select { |file_name|
file_name.end_with?(".rb") ||
file_name.match?(/^Gemfile/)
}.join(' ')
`git stash --keep-index`
system("bundle exec rubocop -c .rubocop.yml '#{changed_files}'") unless changed_files.empty?
exit_status = $CHILD_STATUS.exitstatus
`git stash pop`
exit exit_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment