Skip to content

Instantly share code, notes, and snippets.

@durandom
Created July 7, 2016 12:52
Show Gist options
  • Save durandom/1285dbe622a78de16599b19b5ec21f52 to your computer and use it in GitHub Desktop.
Save durandom/1285dbe622a78de16599b19b5ec21f52 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
@durandom
Copy link
Author

durandom commented Jul 7, 2016

install this into .git/hooks/pre-commit and chmod 755 .git/hooks/pre-commit

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