Skip to content

Instantly share code, notes, and snippets.

@devonzuegel
Created July 11, 2015 06:33
Show Gist options
  • Save devonzuegel/de9db337a9b42c07bf52 to your computer and use it in GitHub Desktop.
Save devonzuegel/de9db337a9b42c07bf52 to your computer and use it in GitHub Desktop.
pre-* git scripts
#!/usr/bin/env ruby
require 'pty'
require 'colorize'
require 'tempfile'
def system_stdout(cmd)
system('touch tempfile')
system("script -q /dev/null #{cmd} | tee tempfile")
output = File.readlines 'tempfile'
system('rm tempfile')
output.join("\n")
end
def fail_commit
puts
puts "\aCOMMIT FAILED!!".red
puts 'If you want to bypass this pre-commit hook, run the command again with the `-n` flag.'
puts
exit 1
end
def run_rubocop
puts "Running rubocop...\n".black
files = `$(git diff --cached --name-only --diff-filter=AMC | grep "\.rb$" | tr '\n' ' ')`
cmd = "bundle exec rubocop #{files} -R --auto-correct --format fuubar"
traceroute_output = system_stdout(cmd)
n_errors = traceroute_output.scan(/(\d+) offense detected/)[0].to_i
puts "#{n_errors} failed!".yellow
fail_commit unless n_errors.zero?
end
run_rubocop
puts
exit 0
# #!/usr/bin/env ruby
require 'colorize'
def system_stdout(cmd)
system('touch tempfile')
system("script -q /dev/null #{cmd} | tee tempfile")
output = File.readlines 'tempfile'
system('rm tempfile')
output.join("\n")
end
def fail_commit
puts
puts "\aCOMMIT FAILED!!".red
puts 'If you want to bypass this pre-push hook, run the command again with the `-n` flag.'
puts
exit 1
end
def run_test_suite
puts "Running full test suite...\n".black
rspec_output = system_stdout('rspec')
n_examples = rspec_output.match(/(\d+) examples/)[0].to_i rescue 0
n_failures = rspec_output.match(/(\d+) failure/)[0].to_i rescue 0
n_errors = (rspec_output.match(/(\d+) errors/)[0].to_i rescue 0) + n_failures
n_pending = rspec_output.match(/(\d+) pending/)[0].to_i rescue 0
fail_commit unless n_errors.zero?
end
def run_traceroute
puts "Running traceroute...\n".black
traceroute_output = system_stdout('rake traceroute')
n_errors = traceroute_output.scan(/\((\d+)\)/).map { |e| e[0].to_i rescue 0 }
n_errors = (n_errors | [0]).reduce(:+)
puts "#{n_errors} failed!".yellow
fail_commit unless n_errors.zero?
end
puts
run_test_suite
puts
run_traceroute
puts
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment