Skip to content

Instantly share code, notes, and snippets.

@cben
Forked from durandom/pre-commit.rb
Last active August 10, 2017 01:10
Show Gist options
  • Save cben/b6f4a53fac485f75afb9150d03efb2f6 to your computer and use it in GitHub Desktop.
Save cben/b6f4a53fac485f75afb9150d03efb2f6 to your computer and use it in GitHub Desktop.
git pre-commit hook that runs rubocop on staged files
#!/usr/bin/env ruby
# make sure you have gem 'rubocop-git' in your Gemfile
require 'readline'
def input(prompt="", newline=false)
prompt += "\n" if newline
Readline.readline(prompt, true).squeeze(" ").strip
end
# http://stackoverflow.com/questions/3417896/how-do-i-prompt-the-user-from-within-a-commit-msg-hook#comment24746595_10015707
STDIN.reopen('/dev/tty')
system("cd #{File.expand_path(__dir__+'/../../')} ; bundle exec rubocop-git --cached")
exit 0 if $?.success?
exit 0 if input("Commit anyway? [y/N] ") == ?y
puts "\nConsider running \e[1mrubocop -a\e[22m on the offensive files"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment