Skip to content

Instantly share code, notes, and snippets.

@drkibitz
Last active December 20, 2015 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drkibitz/6043416 to your computer and use it in GitHub Desktop.
Save drkibitz/6043416 to your computer and use it in GitHub Desktop.
jenkins conditional to check if push is allowed
# Rebase any commits on top of anything new from remote
git pull --rebase $UPSTREAM_GIT_REMOTE $UPSTREAM_GIT_BRANCH
# get the tracking-branch name
tracking_branch=$(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD))
# creates global variables $1 and $2 based on left vs. right tracking
# inspired by @adam_spiers
set -- $(git rev-list --left-right --count $tracking_branch...HEAD)
behind=$1
ahead=$2
if [ $behind -gt 0 ] || [ $ahead -lt 1 ]; then
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment