From 0aa2661f02e943e51694fbe8f7535ff65f3b3f4a Mon Sep 17 00:00:00 2001 From: Adam Wright Date: Sun, 10 Jan 2016 17:07:33 -0800 Subject: [PATCH] Pre-push hook that rejects branches with non-passing tests --- hooks/pre-push | 63 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/hooks/pre-push b/hooks/pre-push index b8da1e2..2482c86 100755 --- a/hooks/pre-push +++ b/hooks/pre-push @@ -1,12 +1,63 @@ #!/bin/sh -cargo test -CARGO_RESULT=$? -if [ "$CARGO_RESULT" != "0" ]; +# An example hook script to verify what is about to be pushed. Called by "git +# push" after it has checked the remote status, but before anything has been +# pushed. If this script exits with a non-zero status nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# +# +# This sample shows how to prevent push of commits where the log message starts +# with "WIP" (work in progress). + +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' # No Color + +BRANCH=$(git symbolic-ref HEAD 2>/dev/null) +BRANCH_RESULT=$? +BRANCH=${BRANCH#refs/heads/} +if [ "$BRANCH_RESULT" != "0" ]; then - echo - echo "FAIL (pre-push): Can't push unless all tests pass. code = $CARGO_RESULT" + echo You are not currently on a branch or other symbolic ref. Please check out such a ref to continue. Code: $SYMBOLIC_REF_RESULT exit 1 fi -exit 0 +#git stash -u &> /dev/null + +TEST_RESULT=0 +while read local_ref local_sha remote_ref remote_sha +do + git checkout $local_ref &>/dev/null + CURRENT_BRANCH=${local_ref#refs/heads/} + + cargo test &>/dev/null + CARGO_RESULT=$? + if [ "$CARGO_RESULT" != "0" ]; + then + echo ${RED}FAIL${NC} $CURRENT_BRANCH + TEST_RESULT=1 + else + echo ${GREEN}PASS${NC} $CURRENT_BRANCH + fi +done + +git checkout ${BRANCH} &>/dev/null +#git stash pop &>/dev/null + +if [ "$TEST_RESULT" == "1" ]; +then + exit 1 +fi + +echo Just testing so always exit 1 +exit 1