Skip to content

Instantly share code, notes, and snippets.

@mxm
Last active August 29, 2015 14:13
Show Gist options
  • Save mxm/4d1e26b901c66a682e4a to your computer and use it in GitHub Desktop.
Save mxm/4d1e26b901c66a682e4a to your computer and use it in GitHub Desktop.
pre-push hook for Flink commiters
#!/bin/bash
####
##
## Pre-push hook which protects a remote branch
## by running a verify command before pushing.
##
## Should be place in .git/hooks/pre-push
##
## protected_remote can be a substring
protected_remote="git.apache.org/flink.git"
protected_branch="master"
## command that verifies before pushing
verify="mvn clean package"
##
##
##
####
# parameters passed by git
remote_name=$1
remote_url=$2
current_branch_ref=$(git symbolic-ref HEAD)
protected_branch_ref="refs/heads/$protected_branch"
IFS=' '
while read local_ref local_sha remote_ref remote_sha
do
# trigger only if we are pushing to the remote and protected branch
if [[ $remote_url == *"$protected_remote"* ]] && \
[[ $remote_ref == $protected_branch_ref ]]; then
# check for pending changes which might not be commited
if [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]] ; then
echo "Please commit or stash your pending changes."
exit 1
fi
# to able to verify, we need to be on the branch to be pushed
# e.g. if you use git push other:master
if [[ $local_ref != $current_branch_ref ]] ; then
echo "Please switch to branch '$local_ref' to verify."
exit 1
fi
# verify
echo "Verifying push to $protected_branch via '$verify'."
$verify
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo "Commits could not be verified. Executed '$verify' and it returned $RESULT"
exit 1
fi
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment