Skip to content

Instantly share code, notes, and snippets.

@meesterdude meesterdude/gitpreflight.rb Secret
Created Aug 6, 2012

Embed
What would you like to do?
commit preflight script to check for stupid errors and run specs before commiting
#!/usr/bin/ruby
# ©2012 Russell Jennings. Released under MIT license.
class String
def colorize(text, color_code) "\e[#{color_code}m#{text}\e[0m" end
def red() colorize(self, 31) end
def green() colorize(self, 32) end
def yellow() colorize(self, 33) end
def pink() colorize(self, 35) end
end
puts "#### Git PreFlight ####".pink
puts "#### ©2012 Russell Jennings ####".pink
sleep 1
puts "checking staged files for errors"
sleep 1
gitdiff =%x[git diff --staged]
if gitdiff.empty?
puts "nothing staged for commit. exiting...".yellow
exit 0
end
@gitlines = gitdiff.split("\n")
@errors = Array.new
i = 0
for line in @gitlines
case
when line.match(/binding.pry/)
@gitlines[i] = line.yellow
@errors.push "found pry entry"
when line.match(/console.log/)
@gitlines[i] = line.yellow
@errors.push "found console.log entry"
when line.match(/^\+{1}.*#+/)
@gitlines[i] = line.yellow
@errors.push "found possible commented out code"
when line.match(/^\+{1}/)
@gitlines[i] = line.green
when line.match(/^-{1}/)
@gitlines[i] = line.red
end
i = i + 1
end
if @errors.size > (0)
@gitlines.each {|line| puts line}
puts "### WARNING ###".red
puts "possibly found the following #{@errors.size} errors:".yellow
@errors.each {|err| puts err.yellow }
puts "Continue committing with #{@errors.size} possible errors? (y)/n".pink
exit 0 if STDIN.gets.chop.downcase =~ /n/
else
puts "no errors found"
puts "want to see the commit diff? y/(n)".pink
@gitlines.each {|line| puts line} if STDIN.gets.chop.downcase =~ /y/
end
puts "Should I run the specs before committing? y/(n)".pink
if STDIN.gets.chop.downcase =~ /y/
puts "Run [A]ll, [C]ontrollers, or [M]odels?".pink
what2run = STDIN.gets.chop.downcase
case what2run
when 'a'
puts "running all specs..."
runcommand = %x[bundle exec rspec spec]
exit_code = $?.exitstatus
when 'c'
puts "running controller specs..."
runcommand = %x[bundle exec rspec spec/controllers]
exit_code = $?.exitstatus
when 'm'
puts "running model specs..."
runcommand = %x[bundle exec rspec spec/models]
exit_code = $?.exitstatus
else
puts "invalid option supplied"
exit 1
end
case exit_code
when 1
puts runcommand
puts "Specs failed!".red
puts "Continue with commit? y/(n)".pink
exit 0 unless STDIN.gets.chop.downcase =~ /y/
when 0
puts "all specs passed"
end
end
commit = %x[git commit -m "#{ARGV[0]}"]
puts commit
puts "### Commit complete ###".pink
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.