#!/bin/bash # -------------------------------------------------- Helper functions function echoe() { echo -e "$1" } function echoc() { echoe "\033[0;35m$1\033[0m" } # -------------------------------------------------- Usage USAGE="usage: sync [options][parameters] --stash stash the uncommited changes prior to checkout --pull pull from the checked out branch --make create the reference branch if not found, otherwise just checkout [branch] the reference branch to branch out from if --make is used. --onfail in case the given branch does not exist [branch] this branch will be used to check out to. --dryrun won't create the reference branch, but runs and shows the pass, fail. example: checkout to the feature branch and stay on staging if does not have it. sync leahi/features/LEAH-1884-UI-BN-AdministrativeScripts -onfail leahi/staging " function usage_print() { echoe "$USAGE" } # -------------------------------------------------- Error handling FAIL="[\033[0;31mFAIL\033[0m]" PASS="[\033[0;32m OK \033[0m]" ERR_REJECTED=1 ERR_NOPARAM=2 ERR_NOBRANCH=3 ERR_MISSING=4 ERR_NOTFOUND_CHECKOUT=5 ERR_NOTFOUND_FAILSAFE=6 ERR_NOTFOUND_REFERENCE=7 ERR_CHECHOUT=8 function err_print() { local err_message if (( $1 == $ERR_REJECTED ));then err_message="Cancelled by user." fi if (( $1 == $ERR_NOPARAM ));then err_message="No Parameter passed." fi if (( $1 == $ERR_NOBRANCH ));then err_message="The first argument has to be branch name to checkout to, and cannot start with '--'." fi if (( $1 == $ERR_MISSING ));then err_message="The expected parameter is missing for $2." fi if (( $1 == $ERR_NOTFOUND_CHECKOUT ));then err_message="The check out branch '$2' not found." fi if (( $1 == $ERR_NOTFOUND_FAILSAFE ));then err_message="The fail-safe branch '$2' not found." fi if (( $1 == $ERR_NOTFOUND_REFERENCE ));then err_message="The reference branch '$2' not found." fi if (( $1 == $ERR_CHECHOUT ));then err_message="The check out to branch '$2' failed." fi echoe "$FAIL #$1 - $err_message" } function err_exit() { err_print $1 exit $1 } function echopass() { echoe "$PASS $1" } function echofail() { echoe "$FAIL $1" } # -------------------------------------------------- Variables do_pull=0 do_make=0 ix_make=0 do_failsafe=0 ix_failsafe=0 do_dryrun=0 do_stash=0 STASH_MESSAGE_DEF="Sync repositories to branch" STASH_MESSAGE_USR="" # not implemented yet not_found=0 not_clear=0 REPOSITORIES=( \ "Projects/application" \ "Projects/common" \ "Projects/ui.scripts" \ "Projects/ui.config" \ "Projects/cloudsync" \ "Projects/drtserver" \ "Projects/dry-demo" \ "Projects/dialin" \ "Projects/simulator" \ ) BRANCH="staging" REFERENCE="$BRANCH" CHECKOUT="$BRANCH" FAILSAFE="" # -------------------------------------------------- Functions function getBranch() { echo $(git symbolic-ref --short HEAD) } function branch_print() { echoc `getBranch` } function hasBranch() { local branch="$1" local branch_list=`git branch -a` if echo "$branch_list" | grep -qw "$branch"; then echo true else echo false fi } # -------------------------------------------------- Command-Line arguments handler if (( ! ${#} )); then echoe "$USAGE" err_exit $ERR_NOPARAM $ANDEXIT fi index=1 for arg in "${@}"; do if (( do_make == 1 )) && (( index == ix_make + 1 )); then if [[ -z "$arg" ]]; then err_exit $ERR_MISSING "--make " fi REFERENCE="$arg" fi if (( do_failsafe == 1 )) && (( index == ix_failsafe + 1 )); then if [[ -z "$arg" ]]; then err_exit $ERR_MISSING "--onfail " fi FAILSAFE="$arg" fi if (( index == 1 )); then if [[ "$arg" != *"--"* ]]; then BRANCH="$1" else echo -e "$USAGE" err_exit $ERR_NOBRANCH $ANDEXIT fi fi if [[ "$arg" == *"--stash"* ]]; then do_stash=1; fi if [[ "$arg" == *"--pull"* ]]; then do_pull=1; fi if [[ "$arg" == *"--make"* ]]; then do_make=1; ix_make=$index; fi if [[ "$arg" == *"--onfail"* ]]; then do_failsafe=1; ix_failsafe=$index; fi if [[ "$arg" == *"--dryrun"* ]]; then do_dryrun=1; fi ((index++)) done # ---------------------------------------------------------------------------------------------------- # -------------------------------------------------- main -------------------------------------------- # ---------------------------------------------------------------------------------------------------- # -------------------------------------------------- confirmation - branch read -p "Checking out all the repositories to the '$BRANCH' branch:(y/n)" -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then err_exit $ERR_REJECTED fi for repo in ${REPOSITORIES[@]}; do echo echo " -------------------- $repo -------------------- " cd "$HOME/$repo" branch_print CHECKOUT="$BRANCH" if [[ "$(getBranch)" == "$BRANCH" ]]; then echopass "Already on the '$BRANCH'" continue fi if (( $do_stash ));then if [[ $STASH_MESSAGE_USR == "" ]]; then git stash push -m "$STASH_MESSAGE_DEF $BRANCH" else git stash push -m "$STASH_MESSAGE_USR" fi fi if (( $do_make )); then if ! `hasBranch "$REFERENCE"`; then err_print $ERR_NOTFOUND_REFERENCE "$REFERENCE" branch_print continue fi echopass "Checking out to the reference branch '$REFERENCE' to branch out." git checkout $REFERENCE if ! (( do_dryrun )); then git checkout -b $BRANCH fi echopass "The '$BRANCH' has been created" continue fi if ! `hasBranch "$BRANCH"`; then err_print $ERR_NOTFOUND_CHECKOUT "$BRANCH" if ! (( $do_failsafe )); then branch_print continue fi if ! `hasBranch "$FAILSAFE"`; then err_print $ERR_NOTFOUND_FAILSAFE "$FAILSAFE" branch_print continue fi if [[ "$(getBranch)" == "$FAILSAFE" ]]; then echopass "Already on the '$FAILSAFE'" continue fi CHECKOUT="$FAILSAFE" fi git checkout "$CHECKOUT" if ! (( $? )); then err_print $ERR_CHECHOUT $CHECKOUT else echopass "Checked out to the '$CHECKOUT'" fi branch_print if (( $do_pull )); then git pull; fi done