Skip to content

Instantly share code, notes, and snippets.

@thejonanshow
Created September 21, 2016 00:38
Show Gist options
  • Save thejonanshow/8d27dd1164baf2a904ed490150b04234 to your computer and use it in GitHub Desktop.
Save thejonanshow/8d27dd1164baf2a904ed490150b04234 to your computer and use it in GitHub Desktop.
Git pre-push hook to check that specs have been run.
#!/bin/sh
# Pre-push check that specs have been run against HEAD.
# ignore projects that don't have a spec directory and spec_helper
if [[ -e "spec" && -e "spec/spec_helper.rb" ]]; then
if grep -Fxq "# Write .spec_sha for pre-push hook" spec/spec_helper.rb; then
local_sha="`git log -n 1 --pretty=format:%H`"
# if the .spec_sha file doesn't exist or the sha doesn't match run rake
if ! [[ -e ".spec_sha" && "$local_sha" == `cat .spec_sha` ]]; then
echo "Specs have not been run since the last commit, running specs with 'rake'."
rake
exit $?
fi
else
# prepend a line to spec_helper.rb that writes the .spec_sha file
echo "Adding code to spec_helper to write the .spec_sha file."
echo "File.write('.spec_sha', %x[git log -n 1 --pretty=format:%H])" | cat - spec/spec_helper.rb > /tmp/out && mv /tmp/out spec/spec_helper.rb
echo "# Write .spec_sha for pre-push hook" | cat - spec/spec_helper.rb > /tmp/out && mv /tmp/out spec/spec_helper.rb
exit 1
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment