| LOCAL_BRANCH=$1 |
| BUILD_FROM_TAG=$2 |
| REMOTE_BRANCH=$3 |
| CHECKOUT_SUCCESSFUL=0 |
| |
| buildFromTag(){ |
| local tag=$1 |
| local localbranch=$1 |
| local remotebranch=$2 |
| git fetch --tags |
| git show-ref --tags --verify --quiet refs/tags/$tag |
| if [ $? -eq 0 ]; then |
| echo "Build from tag: Checkout tag ${tag}." |
| git checkout $tag |
| else |
| echo "Build from tag: Checkout local branch ${localbranch} or remote branch ${remotebranch}." |
| checkoutLocalOrRemoteBranch $localbranch $remotebranch |
| fi |
| } |
| |
| checkoutLocalBranch(){ |
| local localbranch=$1 |
| local remotebranch=$2 |
| |
| CHECKOUT_SUCCESSFUL=0 |
| if [ ! -z "$(git branch | grep $localbranch)" ]; then |
| echo "Checkout local branch ${localbranch}." |
| git checkout $localbranch |
| git pull origin $remotebranch |
| CHECKOUT_SUCCESSFUL=1 |
| else |
| echo "Could not checkout local branch ${localbranch}." |
| fi |
| } |
| |
| checkoutRemoteBranch(){ |
| local localbranch=$1 |
| local remotebranch=$2 |
| local gitremote=`git remote` |
| |
| CHECKOUT_SUCCESSFUL=0 |
| if [ ! -z "$(git branch -r | grep ${gitremote}/$remotebranch)" ]; then |
| echo "Checkout remote branch ${gitremote}/${remotebranch}." |
| git checkout -b $localbranch ${gitremote}/$remotebranch |
| git pull origin $remotebranch |
| CHECKOUT_SUCCESSFUL=1 |
| else |
| echo "Could not checkout remote branch ${gitremote}/${remotebranch}." |
| fi |
| } |
| |
| checkoutLocalOrRemoteBranch(){ |
| local localbranch=$1 |
| local remotebranch=$2 |
| |
| echo "Trying to checkout local branch ${localbranch} or remote branch ${remotebranch}." |
| |
| CHECKOUT_SUCCESSFUL=0 |
| checkoutLocalBranch $localbranch $remotebranch |
| |
| if [ $CHECKOUT_SUCCESSFUL -eq 0 ]; then |
| checkoutRemoteBranch $localbranch $remotebranch |
| fi |
| } |
| |
| git rm --cached -r . > /dev/null |
| git reset --hard > /dev/null |
| |
| if echo $BUILD_FROM_TAG | grep -q -w "true"; then |
| buildFromTag $LOCAL_BRANCH $REMOTE_BRANCH |
| else |
| checkoutLocalOrRemoteBranch $LOCAL_BRANCH $REMOTE_BRANCH |
| fi |