Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Safely pust to git master once CI passes:
# git-tpush: Safely push to git master once CI passes
# Copyright (C) 2016+ James Shubin, AGPLv3+
# Written by James Shubin <>
# Put in your $PATH such as ~/bin/ as "git-tpush" and make it executable
ORIGIN='origin' # remote remote, most call this 'origin', some say 'upstream'
BRANCH='master' # remote branch, most everyone calls this master
POLL='5s' # how often do we sleep between polling the ci?
TPUSH='tpush' # branch prefix to use, eg: $TPUSH/test-N, iow: tpush/test-0
GIT=`which git`
if [ -z $GIT ]; then
echo "The \`git' utility can't be found." # lol
echo "Are you sure you're ready for this?"
exit 1
HUB=`which hub`
if [ -z $HUB ]; then
echo "The \`hub' utility can't be found."
exit 1
TPUT=`which tput`
HEAD=`git describe --contains --all HEAD` # current branch
upstream_sha1=`git show -s --format='format:%H' $UPSTREAM`
head_sha1=`git show -s --format='format:%H' $HEAD`
# TODO: compare the individual sequences of commits, to check if they overlap
if [ "$upstream_sha1" = "$head_sha1" ]; then
echo "Your branch is up-to-date with '$UPSTREAM'."
exit 0
# find an unused local and remote branch
while branch="$TPUSH/test-$count" && ((git branch --list "$branch" | grep -qF "$branch") || (git ls-remote --exit-code $ORIGIN "$branch" >/dev/null)); do
count=`expr $count + 1`
echo "tpush branch is: $branch"
git checkout -b "$branch"
git push $ORIGIN "$branch"
git checkout $HEAD # return to previous branch, or: git checkout -
echo "- CI is starting..."
i=1; spinner='\|/-'
sleep 5s # let it startup
# wait for ci status
while status="`hub ci-status $head_sha1`" ; [ "$status" = "pending" ]; do
([ ! -z $TPUT ] && [ "$once" = "1" ] && $TPUT cuu 1 && $TPUT el) ; once="1"
echo "${spinner:i++%${#spinner}:1} Waiting for CI..."
sleep $POLL
git branch -d $branch && git push $ORIGIN ":${branch}" # cleanup
# check result
if [ "$status" = "success" ]; then
git push $ORIGIN $BRANCH # w00t!
echo 'Done!'
exit 0
# we failed!
# TODO: add some red text here
echo 'Upstream CI failed with:'
echo "`hub ci-status -v $head_sha1`"
echo "Fix branch '$HEAD' with:"
echo 'git commit --amend'
echo 'or:'
echo 'git reset --soft HEAD^'
echo 'Happy hacking!'
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.