Skip to content

Instantly share code, notes, and snippets.

@judoole
Forked from torbjornvatn/ship.sh
Created September 1, 2011 12:48
Show Gist options
  • Save judoole/1186096 to your computer and use it in GitHub Desktop.
Save judoole/1186096 to your computer and use it in GitHub Desktop.
Ship script inspired by this blog: http://reinh.com/blog/2008/08/27/hack-and-and-ship.html
_git_branches()
{
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
CURRENT=`git branch | grep '^*' | awk '{print $2}'`
local versions=$(git branch | grep -v $CURRENT)
COMPREPLY=($(compgen -W "${versions}" $cur))
}
complete -F _git_branches ship
# SHIP
#!/bin/bash
#
# Do a check if you have uncommited changes
#
GIT_NOT_COMMITED=`git status | grep 'nothing to commit' | awk '{print $2}'`
if [ -z "$GIT_NOT_COMMITED" ]; then
echo "Current branch has uncommited changes"
exit
fi
#
# Oy! Checking if the branch to ship to really exists
#
TO_SHIP_TO=$1
CURRENT=`git branch | grep '\*' | awk '{print $2}'`
if [ -z $TO_SHIP_TO ]; then
echo "First argument has to be one of these branches:"
git branch | grep -v $CURRENT
exit 1
fi
#
# Checkout to the ship branch
#
STATUS=`git checkout $TO_SHIP_TO | awk '{print $1}'`
if [[ "${STATUS}" =~ error ]]; then
echo "Something went terribly wrong! ${STATUS}"
git checkout ${CURRENT}
exit 1
fi
#
# Pull the latest from origin into ship-branch
#
echo "Pulling ${TO_SHIP_TO} from origin"
STATUS=`git pull | awk '{print $1}'`
if [[ "${STATUS}" =~ error ]]; then
echo "Something went terribly wrong! ${STATUS}"
git checkout ${CURRENT}
exit 1
fi
#
# Merge into whatever youre shipping to wit --no-ff
#
echo "Merging ${CURRENT} into ${TO_SHIP_TO}"
STATUS=`git merge --no-ff ${CURRENT} | awk '{print $1}'`
if [[ "${STATUS}" =~ error ]]; then
echo "Something went terribly wrong with the merge! ${STATUS}"
exit 1
fi
#
# Pushing
#
GIT_NOT_COMMITED=`git status | grep 'nothing to commit' | awk '{print $2}'`
if [ -z "$GIT_NOT_COMMITED" ]; then
echo "Current branch has uncommited changes"
exit
else
echo "Pushing to origin ${TO_SHIP_TO}"
git push && \
git checkout ${CURRENT}
fi
@judoole
Copy link
Author

judoole commented Nov 17, 2011

Codecompletion for usage in your .bashrc og .profile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment