Skip to content

Instantly share code, notes, and snippets.

@david-hodgetts
Created August 12, 2011 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save david-hodgetts/1141992 to your computer and use it in GitHub Desktop.
Save david-hodgetts/1141992 to your computer and use it in GitHub Desktop.
pre-commit git hook, runs rspec iff branch is master
#!/usr/bin/env ruby
# pre-commit git hook
# will run rspec if and only if current branch is master
# script adapted from http://book.git-scm.com/5_git_hooks.html
def run_tests
html_path = "/tmp/git_hook_spec_results.html"
`rspec -f p -f h -o #{html_path} spec` # run the spec. send progress to screen. save html results to html_path
# find out how many errors were found
html = open(html_path).read
examples = html.match(/(\d+) examples?/)[0].to_i rescue 0
failures = html.match(/(\d+) failures?/)[0].to_i rescue 0
pending = html.match(/(\d+) pending/)[0].to_i rescue 0
if failures.zero?
puts "0 failures! #{examples} run, #{pending} pending"
else
puts "\aDID NOT COMMIT YOUR FILES!"
puts "View spec results at #{File.expand_path(html_path)}"
puts
puts "#{failures} failures! #{examples} run, #{pending} pending"
exit 1
end
end
#get current_branch
list_of_branches = `git branch`.split("\n")
current_branch = list_of_branches.find {|string| string.match /^\* /}.sub("* ", "")
if current_branch == 'master'
puts "we are on branch master, running tests----"
run_tests
puts "---------------------------------end tests"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment