From 2ce5d943dc083a5217068a72cd4f0ecdac24ccc8 Mon Sep 17 00:00:00 2001 From: Gundars Date: Sun, 22 Feb 2015 00:29:13 +0200 Subject: [PATCH 01/14] refactoting init --- config/global.sh | 4 ++++ install.sh | 7 ++++--- lib/commons.sh | 3 +++ buildenv.sh => scripts/buildenv.sh | 0 gcobranch.sh => scripts/gcobranch.sh | 0 gitmerge.sh => scripts/gitmerge.sh | 0 gitpress.sh => scripts/gitpress.sh | 0 guorigin.sh => scripts/guorigin.sh | 0 8 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 config/global.sh create mode 100644 lib/commons.sh rename buildenv.sh => scripts/buildenv.sh (100%) rename gcobranch.sh => scripts/gcobranch.sh (100%) rename gitmerge.sh => scripts/gitmerge.sh (100%) rename gitpress.sh => scripts/gitpress.sh (100%) rename guorigin.sh => scripts/guorigin.sh (100%) diff --git a/config/global.sh b/config/global.sh new file mode 100644 index 0000000..569b411 --- /dev/null +++ b/config/global.sh @@ -0,0 +1,4 @@ +#!/bin/bash +#Global configuration +declare -A CONF +globalConf[errorMascot]=' __\n / _)\n .-^^^-/ /\n __/ /\n<__.|_|-|_|'; diff --git a/install.sh b/install.sh index 8ed55b2..f13a28c 100755 --- a/install.sh +++ b/install.sh @@ -1,6 +1,7 @@ #!/bin/bash # Install other scripts from this directory to system # Usage: git clone https://github.com/Gundars/bashscripts.git ~/.bashscripts && sudo bash install.sh +source ~/.bashscripts/lib/commons.sh STARTDIR=${PWD} INSTDIR=~/.bashscripts @@ -12,13 +13,13 @@ for SCRIPT in ${SCRIPTS[@]} do if [ ! -f ${SYMLINKDIR}/${SCRIPT} ]; then echo "Installing $SCRIPT" - sudo ln -s $INSTDIR/${SCRIPT}.sh ${SYMLINKDIR}/${SCRIPT} + sudo ln -s $INSTDIR/scripts/${SCRIPT}.sh ${SYMLINKDIR}/${SCRIPT} else SYMLINK=$(readlink -f ${SYMLINKDIR}/${SCRIPT}) - if [[ $SYMLINK != $INSTDIR/${SCRIPT}.sh ]]; then + if [[ $SYMLINK != $INSTDIR/scripts/${SCRIPT}.sh ]]; then echo "Relinking existing $SCRIPT" sudo rm ${SYMLINKDIR}/${SCRIPT} - sudo ln -s $INSTDIR/${SCRIPT}.sh ${SYMLINKDIR}/${SCRIPT} + sudo ln -s $INSTDIR/scripts/${SCRIPT}.sh ${SYMLINKDIR}/${SCRIPT} else echo "$SCRIPT already installed" fi diff --git a/lib/commons.sh b/lib/commons.sh new file mode 100644 index 0000000..df7e26e --- /dev/null +++ b/lib/commons.sh @@ -0,0 +1,3 @@ +#!/bin/bash +# Container of all common functions +source ~/.bashscripts/config/global.sh diff --git a/buildenv.sh b/scripts/buildenv.sh similarity index 100% rename from buildenv.sh rename to scripts/buildenv.sh diff --git a/gcobranch.sh b/scripts/gcobranch.sh similarity index 100% rename from gcobranch.sh rename to scripts/gcobranch.sh diff --git a/gitmerge.sh b/scripts/gitmerge.sh similarity index 100% rename from gitmerge.sh rename to scripts/gitmerge.sh diff --git a/gitpress.sh b/scripts/gitpress.sh similarity index 100% rename from gitpress.sh rename to scripts/gitpress.sh diff --git a/guorigin.sh b/scripts/guorigin.sh similarity index 100% rename from guorigin.sh rename to scripts/guorigin.sh From c66ceabf061cf36bb365803ba634b60b8edf080f Mon Sep 17 00:00:00 2001 From: Gundars Date: Sun, 22 Feb 2015 01:14:09 +0200 Subject: [PATCH 02/14] global config --- config/global.sh | 13 +++++++++++-- install.sh | 23 ++++++++++------------- scripts/buildenv.sh | 24 ++++++++++-------------- scripts/gcobranch.sh | 27 +++++++++++---------------- scripts/gitmerge.sh | 13 ++++--------- scripts/gitpress.sh | 11 +++-------- scripts/guorigin.sh | 31 +++++++++++++------------------ 7 files changed, 62 insertions(+), 80 deletions(-) diff --git a/config/global.sh b/config/global.sh index 569b411..6778654 100644 --- a/config/global.sh +++ b/config/global.sh @@ -1,4 +1,13 @@ #!/bin/bash #Global configuration -declare -A CONF -globalConf[errorMascot]=' __\n / _)\n .-^^^-/ /\n __/ /\n<__.|_|-|_|'; +declare -A gConf +gConf[dirStart]=${PWD} +gConf[dirInstall]=~/.bashscripts +gConf[dirSymlinks]=/usr/local/bin +gConf[colorNormal]='\e[00m' +gConf[colorHighlight]='\e[01;36m' +gConf[colorError]='\033[31m' +gConf[colorSuccess]='\e[32m' +gConf[errorMascot]=' __\n / _)\n .-^^^-/ /\n __/ /\n<__.|_|-|_| ' + + diff --git a/install.sh b/install.sh index f13a28c..86ba79a 100755 --- a/install.sh +++ b/install.sh @@ -3,29 +3,26 @@ # Usage: git clone https://github.com/Gundars/bashscripts.git ~/.bashscripts && sudo bash install.sh source ~/.bashscripts/lib/commons.sh -STARTDIR=${PWD} -INSTDIR=~/.bashscripts -SYMLINKDIR=/usr/local/bin -cd $INSTDIR +cd ${gConf[dirInstall]} git pull origin master SCRIPTS=(gcobranch gitmerge guorigin buildenv gitpress) for SCRIPT in ${SCRIPTS[@]} do - if [ ! -f ${SYMLINKDIR}/${SCRIPT} ]; then + if [ ! -h ${gConf[dirSymlinks]}/$SCRIPT ]; then echo "Installing $SCRIPT" - sudo ln -s $INSTDIR/scripts/${SCRIPT}.sh ${SYMLINKDIR}/${SCRIPT} + sudo ln -s ${gConf[dirInstall]}/scripts/$SCRIPT.sh ${gConf[dirSymlinks]}/$SCRIPT else - SYMLINK=$(readlink -f ${SYMLINKDIR}/${SCRIPT}) - if [[ $SYMLINK != $INSTDIR/scripts/${SCRIPT}.sh ]]; then + SYMLINK=$(readlink -f ${gConf[dirSymlinks]}/$SCRIPT) + if [[ $SYMLINK != ${gConf[dirInstall]}/scripts/$SCRIPT.sh ]]; then echo "Relinking existing $SCRIPT" - sudo rm ${SYMLINKDIR}/${SCRIPT} - sudo ln -s $INSTDIR/scripts/${SCRIPT}.sh ${SYMLINKDIR}/${SCRIPT} + sudo rm ${gConf[dirSymlinks]}/$SCRIPT + sudo ln -s ${gConf[dirInstall]}/scripts/$SCRIPT.sh ${gConf[dirSymlinks]}/$SCRIPT else echo "$SCRIPT already installed" fi fi done -if [ ! -f ${SYMLINKDIR}/updatebashscripts ]; then - sudo ln -s $INSTDIR/install.sh ${SYMLINKDIR}/updatebashscripts +if [ ! -h ${gConf[dirSymlinks]}/updatebashscripts ]; then + sudo ln -s ${gConf[dirInstall]}/install.sh ${gConf[dirSymlinks]}/updatebashscripts fi -cd $STARTDIR +cd ${gConf[dirStart]} diff --git a/scripts/buildenv.sh b/scripts/buildenv.sh index c1ec2d0..2331122 100755 --- a/scripts/buildenv.sh +++ b/scripts/buildenv.sh @@ -4,11 +4,7 @@ # {env} syntax: www_domain_extension # {branch} syntax: dev | test | staging # {build number} syntax: integer 1-5 digits long - -CNORMAL='\e[00m' -CHIGHLIGHT="\e[01;36m" -CERROR='\033[31m' -CSUCCESS='\e[32m' +source ~/.bashscripts/lib/commons.sh ENV=$1 ENVREGEX="^w{3}_.*_[a-zA-Z]{2,4}$" BRANCH=$2 @@ -22,40 +18,40 @@ REPO="https/link/to/encriched.git" if [ $# -lt 3 ]; then ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Incorrect arguments specified${CNORMAL}\n" + echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Incorrect arguments specified${gConf[colorNormal]}\n" echo "Usage: buildenv {env} {branch} {build number}" echo "{env} syntax: www_domain_extension" echo "{branch} syntax: dev | test | staging" - echo -e "\n${CERROR}Exited with ${ERRCOUNT} errors${CNORMAL}" + echo -e "\n${gConf[colorError]}Exited with ${ERRCOUNT} errors${gConf[colorNormal]}" exit 0 fi if ! [[ $ENV =~ $ENVREGEX ]]; then ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Bad environment${CNORMAL}\n" + echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Bad environment${gConf[colorNormal]}\n" echo "{env} syntax: www_domain_extension" fi if ! [[ $BRANCH =~ $BRANCHREGEX ]]; then ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Bad branch${CNORMAL}\n" + echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Bad branch${gConf[colorNormal]}\n" echo "{branch} syntax: dev | test | staging" fi if ! [[ $BNR =~ $BNRREGEX ]]; then ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Bad build number${CNORMAL}\n" + echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Bad build number${gConf[colorNormal]}\n" echo "{build number} syntax: integer 1-5 digits long" fi if [[ $REPO =~ "https/link/to/encriched.git" ]]; then ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Bad repository name${CNORMAL}\n" + echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Bad repository name${gConf[colorNormal]}\n" echo "Please change line 21 'https/link/to/encriched.git' in file ~/.bashscripts/buildenv.sh to valid link to repository" fi if ! [[ $ERRCOUNT =~ 0 ]]; then - echo -e "\n${CERROR}Exited with ${ERRCOUNT} errors${CNORMAL}" + echo -e "\n${gConf[colorError]}Exited with ${ERRCOUNT} errors${gConf[colorNormal]}" exit 1; fi @@ -73,5 +69,5 @@ DIFFG=`git diff HEAD^ HEAD --color=always|perl -wlne 'print $1 if /^\e\[32m\+\e\ DIFFR=`git diff HEAD^ HEAD --color=always|perl -wlne 'print $1 if /^\e\[31m-(.*)\e\[m$/'` git push origin master cd $STARTDIR -echo -e "\n${CERROR} ${DIFFR} ${CNORMAL}" -echo -e "${CSUCCESS} ${DIFFG} ${CNORMAL}" +echo -e "\n${gConf[colorError]} ${DIFFR} ${gConf[colorNormal]}" +echo -e "${gConf[colorSuccess]} ${DIFFG} ${gConf[colorNormal]}" diff --git a/scripts/gcobranch.sh b/scripts/gcobranch.sh index 5c70c51..41a7a25 100755 --- a/scripts/gcobranch.sh +++ b/scripts/gcobranch.sh @@ -1,17 +1,12 @@ #!/bin/bash # Checks out and pulls specified {branch} in all git repos found in each of specified {dir} # Syntax: $ gcobranch.sh {branch} {dir1} [{dir2} {dir3}...] - -CNORMAL='\e[00m' -CHIGHLIGHT="\e[01;36m" -CERROR='\033[31m' -CSUCCESS='\e[32m' -ERRMASCOT=' __\n / _)\n .-^^^-/ /\n __/ /\n<__.|_|-|_|'; +source ~/.bashscripts/lib/commons.sh ERRCOUNT=0 if [ $# -lt 2 ] then - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Incorrect arguments specified${CNORMAL}" + echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Incorrect arguments specified${gConf[colorNormal]}" echo "Usage: gcobranch [branch] [dir] [dir]..." exit 0 fi @@ -28,9 +23,9 @@ do for d in `find $dir -name .git -type d`; do cd $d/.. > /dev/null - echo -e "\n${CHIGHLIGHT}Repo: `pwd`$CNORMAL" + echo -e "\n${gConf[colorHighlight]}Repo: `pwd`$gConf[colorNormal]" CURRENT=`git rev-parse --abbrev-ref HEAD`; - echo -e "Current branch$CNORMAL: ${CURRENT}" + echo -e "Current branch$gConf[colorNormal]: ${CURRENT}" TXTCHECKOUT="No need to checkout" if [[ "${CURRENT}" != "${branch}" ]]; then @@ -48,26 +43,26 @@ do TXTPULL=$((git pull origin $branch) 2>&1); NEWCHANGED=`git rev-parse --abbrev-ref HEAD`; if [[ "${branch}" == ${NEWCHANGED} ]] ; then - echo -e "${CSUCCESS}${NEWCHANGED} checked out & pulled" + echo -e "${gConf[colorSuccess]}${NEWCHANGED} checked out & pulled" else ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: could not check out ${branch}" - echo -e "${CHIGHLIGHT}Checkout output:${CNORMAL}\n ${TXTCHECKOUT} \n${CHIGHLIGHT}Pull output:${CNORMAL} ${TXTPULL}" + echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: could not check out ${branch}" + echo -e "${gConf[colorHighlight]}Checkout output:${gConf[colorNormal]}\n ${TXTCHECKOUT} \n${gConf[colorHighlight]}Pull output:${gConf[colorNormal]} ${TXTPULL}" fi cd - > /dev/null done else - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Directory $dir does not exist ${CNORMAL}" + echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Directory $dir does not exist ${gConf[colorNormal]}" fi done if [[ "${ERRCOUNT}" == "0" ]] ; then - finalcol=${CSUCCESS} + finalcol=${gConf[colorSuccess]} else - finalcol=${CERROR} + finalcol=${gConf[colorError]} fi -echo -e "\n${finalcol}Done with ${ERRCOUNT} errors${CNORMAL}" +echo -e "\n${finalcol}Done with ${ERRCOUNT} errors${gConf[colorNormal]}" diff --git a/scripts/gitmerge.sh b/scripts/gitmerge.sh index 7671594..46c08e6 100755 --- a/scripts/gitmerge.sh +++ b/scripts/gitmerge.sh @@ -2,12 +2,7 @@ # Merges current branch with {branch}, pushes changes to origin # If no {branch} is specified, "development" is used # Syntax: $ gitmerge [options] [branch] - -CNORMAL='\e[00m' -CHIGHLIGHT="\e[01;36m" -CERROR='\033[31m' -CSUCCESS='\e[32m' -ERRMASCOT=' __\n / _)\n .-^^^-/ /\n __/ /\n<__.|_|-|_|' +source ~/.bashscripts/lib/commons.sh ERRCOUNT=0 ALLOWEDARGS=1 OPTP=false @@ -26,7 +21,7 @@ while getopts ":p" opt; do done if [ $# -gt $ALLOWEDARGS ]; then - echo -e "${CERROR}${ERRMASCOT}ERROR: Received $# arguments, only ${ALLOWEDARGS} allowed! Syntax: gitmerge [options] [branch] ${CNORMAL}" + echo -e "${gConf[colorError]}${ERRMASCOT}ERROR: Received $# arguments, only ${ALLOWEDARGS} allowed! Syntax: gitmerge [options] [branch] ${gConf[colorNormal]}" exit 1 fi @@ -37,7 +32,7 @@ if [[ $1 && "$OPTP" = false ]]; then elif [[ $1 && "$OPTP" = true && $2 ]]; then mergeBranch=$2 fi -echo -e "Merging ${CHIGHLIGHT}${currentBranch}${CNORMAL} with ${CHIGHLIGHT}${mergeBranch}${CNORMAL}" +echo -e "Merging ${gConf[colorHighlight]}${currentBranch}${gConf[colorNormal]} with ${gConf[colorHighlight]}${mergeBranch}${gConf[colorNormal]}" CHECKOUT=$((git checkout $mergeBranch) 2>&1) if [[ "${CHECKOUT}" =~ "error: pathspec" ]]; then @@ -70,7 +65,7 @@ if [ "$OPTP" = true ] ; then fi fi for prBranch in ${PRBRANCHES[@]}; do - echo -e "${CHIGHLIGHT}PR ${prBranch}:${CNORMAL} ${currentOrigin/%.git//compare/${prBranch}...${currentBranch}}" + echo -e "${gConf[colorHighlight]}PR ${prBranch}:${gConf[colorNormal]} ${currentOrigin/%.git//compare/${prBranch}...${currentBranch}}" done fi diff --git a/scripts/gitpress.sh b/scripts/gitpress.sh index 5e0065a..1f07561 100755 --- a/scripts/gitpress.sh +++ b/scripts/gitpress.sh @@ -1,12 +1,7 @@ #!/bin/bash # compresses all commits in current local branch into single commit # Syntax: $ gitpress [options] - -CNORMAL='\e[00m' -CHIGHLIGHT="\e[01;36m" -CERROR='\033[31m' -CSUCCESS='\e[32m' -ERRMASCOT=' __\n / _)\n .-^^^-/ /\n __/ /\n<__.|_|-|_|' +source ~/.bashscripts/lib/commons.sh ALLOWEDARGS=2 OPTF=false OPTP=false @@ -29,7 +24,7 @@ while getopts ":fp" opt; do done if [ $# -gt $ALLOWEDARGS ]; then - echo -e "${CERROR}${ERRMASCOT}ERROR: Received $# arguments, only ${ALLOWEDARGS} allowed! Syntax: gitpress [options]${CNORMAL}" + echo -e "${gConf[colorError]}${ERRMASCOT}ERROR: Received $# arguments, only ${ALLOWEDARGS} allowed! Syntax: gitpress [options]${gConf[colorNormal]}" exit 1 fi @@ -38,7 +33,7 @@ if [[ "$OPTF" = false ]]; then for UNSAFEBRANCH in ${UNSAFEBRANCHES[@]} do if [[ ${UNSAFEBRANCH} == ${CURRENTBRANCH} ]]; then - echo -e "${CERROR}${ERRMASCOT}ERROR: Use -f option to compress, branch name '${UNSAFEBRANCH}' is too generic${CNORMAL}" + echo -e "${gConf[colorError]}${ERRMASCOT}ERROR: Use -f option to compress, branch name '${UNSAFEBRANCH}' is too generic${gConf[colorNormal]}" exit 1 fi done diff --git a/scripts/guorigin.sh b/scripts/guorigin.sh index 50af870..5ea7664 100755 --- a/scripts/guorigin.sh +++ b/scripts/guorigin.sh @@ -1,17 +1,12 @@ #!/bin/bash # Update git origins from o-auth and SSH to native https in all git repos found in each of specified directories {dir} # Syntax: $ guorigin.sh {dir1} [{dir2} {dir3}...] +source ~/.bashscripts/lib/commons.sh -CNORMAL='\e[00m' -CHIGHLIGHT="\e[01;36m" -CERROR='\033[31m' -CSUCCESS='\e[32m' -ERRMASCOT=' __\n / _)\n .-^^^-/ /\n __/ /\n<__.|_|-|_|'; ERRCOUNT=0 - if [ $# -lt 1 ] then - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Incorrect arguments specified${CNORMAL}" + echo -e "\n${gConf[colorError]}${gConf[errorMascot]}ERROR: Incorrect arguments specified${gConf[colorNormal]}" echo "Usage: guorigin {dir1} [{dir2} {dir3}...]" exit 0 fi @@ -27,7 +22,7 @@ do for d in `find $dir -name .git -type d`; do cd $d/.. > /dev/null - echo -e "\n${CHIGHLIGHT}Updating `pwd`${CNORMAL}" + echo -e "\n${gConf[colorHighlight]}Updating `pwd`${gConf[colorNormal]}" CURRENT=`git config --get remote.origin.url`; echo -e "Current Origin: ${CURRENT}" FILTERBY="x-oauth-basic" @@ -39,14 +34,14 @@ do `git remote set-url origin "${NEW}"` NEWCHANGED=`git config --get remote.origin.url`; if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then - echo -e "${CSUCCESS}Origin changed to ${NEWCHANGED}${CNORMAL}" + echo -e "${gConf[colorSuccess]}Origin changed to ${NEWCHANGED}${gConf[colorNormal]}" else ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${CERROR}ERROR: could not change origin${CNORMAL}" + echo -e "${gConf[colorError]}ERROR: could not change origin${gConf[colorNormal]}" fi else ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${CERROR}ERROR: ${NEW} is not a valid repository${CNORMAL}" + echo -e "${gConf[colorError]}ERROR: ${NEW} is not a valid repository${gConf[colorNormal]}" fi elif [[ "${CURRENT}" =~ "${FILTERBYSSH}" ]]; then STR_ARRAY=(`echo $CURRENT | tr ':' "\n"`) @@ -55,14 +50,14 @@ do `git remote set-url origin "${NEW}"` NEWCHANGED=`git config --get remote.origin.url`; if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then - echo -e "${CSUCCESS}Origin changed to ${NEWCHANGED}${CNORMAL}" + echo -e "${gConf[colorSuccess]}Origin changed to ${NEWCHANGED}${gConf[colorNormal]}" else ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${CERROR}ERROR: could not change origin${CNORMAL}" + echo -e "${gConf[colorError]}ERROR: could not change origin${gConf[colorNormal]}" fi else ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${CERROR}ERROR: ${NEW} is not a valid repository${CNORMAL}" + echo -e "${gConf[colorError]}ERROR: ${NEW} is not a valid repository${gConf[colorNormal]}" fi else echo -e "Origin OK" @@ -70,14 +65,14 @@ do cd - > /dev/null done else - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Directory $dir does not exist ${CNORMAL}" + echo -e "\n${gConf[colorError]}${gConf[errorMascot]}\nERROR: Directory $dir does not exist ${gConf[colorNormal]}" fi done if [[ "${ERRCOUNT}" == "0" ]] ; then - finalcol=${CSUCCESS} + finalcol=${gConf[colorSuccess]} else - finalcol=${CERROR} + finalcol=${gConf[colorError]} fi -echo -e "\n${finalcol}Completed with ${ERRCOUNT} errors${CNORMAL}" +echo -e "\n${finalcol}Completed with ${ERRCOUNT} errors${gConf[colorNormal]}" From d5efbb51b7e03d6e6ad9edc02bd11cbba908e0d3 Mon Sep 17 00:00:00 2001 From: Gundars Date: Sun, 22 Feb 2015 13:55:47 +0200 Subject: [PATCH 03/14] updated functions --- lib/commons.sh | 50 +++++++++++++++++++++++++++++++++++++++++++ scripts/guorigin.sh | 52 +++++++++++++++++---------------------------- 2 files changed, 70 insertions(+), 32 deletions(-) diff --git a/lib/commons.sh b/lib/commons.sh index df7e26e..ba2107b 100644 --- a/lib/commons.sh +++ b/lib/commons.sh @@ -1,3 +1,53 @@ #!/bin/bash # Container of all common functions source ~/.bashscripts/config/global.sh + +function message { + if [ -z "$1" ]; then + echo "message param 1 error" + else + echo -e "$1" + fi +} +function messageSuccess { + if [ -z "$1" ]; then + echo "messageSuccess param 1 error" + else + echo -e "${gConf[colorSuccess]}$1${gConf[colorNormal]}" + fi +} +function messageHighlight { + if [ -z "$1" ]; then + echo "messageHightlight param 1 error" + else + echo -e "${gConf[colorHighlight]}$1${gConf[colorNormal]}" + fi +} +function messageError { + ERRCOUNT=$[ERRCOUNT + 1] + if [ -z "$1" ]; then + echo "messageError param 1 error" + else + echo -e "${gConf[colorError]}${gConf[errorMascot]} ERROR: $1${gConf[colorNormal]}" + fi +} +function messageExit { + if [[ "${ERRCOUNT}" == "0" ]] ; then + messageSuccess "Completed without errors" + else + messageError "Completed with ${ERRCOUNT} errors" + fi +} +function gitGetCurrentOrigin { + git config --get remote.origin.url +} +function gitSetCurrentOrigin { + if [ -z "$1" ]; then + echo "gitSetCurrentOrigin param 1 error" + else + git remote set-url origin $1 + fi +} +function findAllGitDrectories { + find $1 -name .git -type d +} diff --git a/scripts/guorigin.sh b/scripts/guorigin.sh index 5ea7664..04cccc0 100755 --- a/scripts/guorigin.sh +++ b/scripts/guorigin.sh @@ -6,24 +6,21 @@ source ~/.bashscripts/lib/commons.sh ERRCOUNT=0 if [ $# -lt 1 ] then - echo -e "\n${gConf[colorError]}${gConf[errorMascot]}ERROR: Incorrect arguments specified${gConf[colorNormal]}" - echo "Usage: guorigin {dir1} [{dir2} {dir3}...]" - exit 0 + messageError "Incorrect arguments specified" + message "Usage: guorigin {dir1} [{dir2} {dir3}...]" + exit 1 fi directories="${@:1}"; - for dir in $directories do if [ -d "$dir" ]; then cd $dir>/dev/null; - echo -e "\nScanning ${PWD}"; cd ->/dev/null - - for d in `find $dir -name .git -type d`; do + for d in `findAllGitDrectories $dir`; do cd $d/.. > /dev/null - echo -e "\n${gConf[colorHighlight]}Updating `pwd`${gConf[colorNormal]}" - CURRENT=`git config --get remote.origin.url`; + messageHighlight "\nUpdating ${PWD}" + CURRENT=$(gitGetCurrentOrigin) echo -e "Current Origin: ${CURRENT}" FILTERBY="x-oauth-basic" FILTERBYSSH="git@github.com:" @@ -31,48 +28,39 @@ do STR_ARRAY=(`echo $CURRENT | tr '@' "\n"`) NEW="https://${STR_ARRAY[1]}" if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then - `git remote set-url origin "${NEW}"` - NEWCHANGED=`git config --get remote.origin.url`; + gitSetCurrentOrigin $NEW + NEWCHANGED=$(gitGetCurrentOrigin) if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then - echo -e "${gConf[colorSuccess]}Origin changed to ${NEWCHANGED}${gConf[colorNormal]}" + messageSuccess "Origin changed to ${NEWCHANGED}" else - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${gConf[colorError]}ERROR: could not change origin${gConf[colorNormal]}" + messageError "unable to change origin" fi else - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${gConf[colorError]}ERROR: ${NEW} is not a valid repository${gConf[colorNormal]}" + messageError "${NEW} is not a valid repository" fi elif [[ "${CURRENT}" =~ "${FILTERBYSSH}" ]]; then STR_ARRAY=(`echo $CURRENT | tr ':' "\n"`) NEW="https://github.com/${STR_ARRAY[1]}" if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then - `git remote set-url origin "${NEW}"` - NEWCHANGED=`git config --get remote.origin.url`; + gitSetCurrentOrigin $NEW + NEWCHANGED=$(gitGetCurrentOrigin) if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then - echo -e "${gConf[colorSuccess]}Origin changed to ${NEWCHANGED}${gConf[colorNormal]}" + messageSuccess "Origin changed to ${NEWCHANGED}" else - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${gConf[colorError]}ERROR: could not change origin${gConf[colorNormal]}" + messageError "unable to change origin" fi else - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${gConf[colorError]}ERROR: ${NEW} is not a valid repository${gConf[colorNormal]}" + messageError "${NEW} is not a valid repository" fi else - echo -e "Origin OK" + message "Origin OK" fi cd - > /dev/null done else - echo -e "\n${gConf[colorError]}${gConf[errorMascot]}\nERROR: Directory $dir does not exist ${gConf[colorNormal]}" + messageError "Directory ${dir} does not exist" fi done -if [[ "${ERRCOUNT}" == "0" ]] ; then - finalcol=${gConf[colorSuccess]} -else - finalcol=${gConf[colorError]} -fi - -echo -e "\n${finalcol}Completed with ${ERRCOUNT} errors${gConf[colorNormal]}" +messageExit +exit 0 From 3917879ec361aec0e08f630b4d05a8f5169fb5da Mon Sep 17 00:00:00 2001 From: Gundars Date: Sun, 22 Feb 2015 17:38:33 +0200 Subject: [PATCH 04/14] common function impl --- config/global.sh | 1 + lib/commons.sh | 24 ++++++++++++++++++++++-- scripts/buildenv.sh | 40 ++++++++++++++++++---------------------- scripts/gcobranch.sh | 3 ++- scripts/gitmerge.sh | 16 +++++++++------- scripts/gitpress.sh | 14 +++++++++----- scripts/guorigin.sh | 2 +- 7 files changed, 62 insertions(+), 38 deletions(-) diff --git a/config/global.sh b/config/global.sh index 6778654..ac3b87b 100644 --- a/config/global.sh +++ b/config/global.sh @@ -4,6 +4,7 @@ declare -A gConf gConf[dirStart]=${PWD} gConf[dirInstall]=~/.bashscripts gConf[dirSymlinks]=/usr/local/bin +gConf[dirTmp]=/.bashscripts/tmp gConf[colorNormal]='\e[00m' gConf[colorHighlight]='\e[01;36m' gConf[colorError]='\033[31m' diff --git a/lib/commons.sh b/lib/commons.sh index ba2107b..4336644 100644 --- a/lib/commons.sh +++ b/lib/commons.sh @@ -9,6 +9,7 @@ function message { echo -e "$1" fi } + function messageSuccess { if [ -z "$1" ]; then echo "messageSuccess param 1 error" @@ -16,6 +17,7 @@ function messageSuccess { echo -e "${gConf[colorSuccess]}$1${gConf[colorNormal]}" fi } + function messageHighlight { if [ -z "$1" ]; then echo "messageHightlight param 1 error" @@ -23,24 +25,37 @@ function messageHighlight { echo -e "${gConf[colorHighlight]}$1${gConf[colorNormal]}" fi } + function messageError { ERRCOUNT=$[ERRCOUNT + 1] if [ -z "$1" ]; then echo "messageError param 1 error" else - echo -e "${gConf[colorError]}${gConf[errorMascot]} ERROR: $1${gConf[colorNormal]}" + if [ "$2" == false ]; then + local em='' + local er='\n' + else + local em=${gConf[errorMascot]} + local er='ERROR: ' + fi + echo -e "${gConf[colorError]}${em}${er}$1${gConf[colorNormal]}" fi } + function messageExit { if [[ "${ERRCOUNT}" == "0" ]] ; then messageSuccess "Completed without errors" + exit 0 else - messageError "Completed with ${ERRCOUNT} errors" + messageError "Completed with ${ERRCOUNT} errors" false + exit 1 fi } + function gitGetCurrentOrigin { git config --get remote.origin.url } + function gitSetCurrentOrigin { if [ -z "$1" ]; then echo "gitSetCurrentOrigin param 1 error" @@ -48,6 +63,11 @@ function gitSetCurrentOrigin { git remote set-url origin $1 fi } + +function gitGetCurrentBranch { + $(b=$(git symbolic-ref -q HEAD); { [ -n "$b" ] && echo ${b##refs/heads/}; } || echo HEAD) +} + function findAllGitDrectories { find $1 -name .git -type d } diff --git a/scripts/buildenv.sh b/scripts/buildenv.sh index 2331122..a9df2ac 100755 --- a/scripts/buildenv.sh +++ b/scripts/buildenv.sh @@ -5,6 +5,8 @@ # {branch} syntax: dev | test | staging # {build number} syntax: integer 1-5 digits long source ~/.bashscripts/lib/commons.sh + +ERRCOUNT=0 ENV=$1 ENVREGEX="^w{3}_.*_[a-zA-Z]{2,4}$" BRANCH=$2 @@ -17,49 +19,41 @@ ERRCOUNT=0 REPO="https/link/to/encriched.git" if [ $# -lt 3 ]; then - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Incorrect arguments specified${gConf[colorNormal]}\n" - echo "Usage: buildenv {env} {branch} {build number}" + messageError "Incorrect arguments specified" + echo "Syntax: buildenv {env} {branch} {build number}" echo "{env} syntax: www_domain_extension" echo "{branch} syntax: dev | test | staging" - echo -e "\n${gConf[colorError]}Exited with ${ERRCOUNT} errors${gConf[colorNormal]}" - exit 0 + messageExit fi if ! [[ $ENV =~ $ENVREGEX ]]; then - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Bad environment${gConf[colorNormal]}\n" - echo "{env} syntax: www_domain_extension" + messageError "Bad environment" + message "{env} syntax: www_domain_extension" fi if ! [[ $BRANCH =~ $BRANCHREGEX ]]; then - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Bad branch${gConf[colorNormal]}\n" - echo "{branch} syntax: dev | test | staging" + messageError "Bad branch" + message "{branch} syntax: dev | test | staging" fi if ! [[ $BNR =~ $BNRREGEX ]]; then - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Bad build number${gConf[colorNormal]}\n" - echo "{build number} syntax: integer 1-5 digits long" + messageError "Bad build number$" + message "{build number} syntax: integer 1-5 digits long" fi if [[ $REPO =~ "https/link/to/encriched.git" ]]; then - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Bad repository name${gConf[colorNormal]}\n" - echo "Please change line 21 'https/link/to/encriched.git' in file ~/.bashscripts/buildenv.sh to valid link to repository" + messageError "Bad repository name" + message "Please change line 21 'https/link/to/encriched.git' in file ~/.bashscripts/buildenv.sh to valid link to repository" fi if ! [[ $ERRCOUNT =~ 0 ]]; then - echo -e "\n${gConf[colorError]}Exited with ${ERRCOUNT} errors${gConf[colorNormal]}" - exit 1; + messageExit fi -STARTDIR=${PWD} if ! [ -d "$TMPDIR" ]; then git clone $REPO $TMPDIR fi -cd $TMPDIR +cd gConf[dirTmp] git pull origin master FILE=environment_definition/${BRANCH}1/environment_definition.def sed -i "s/^${ENV}.*/${ENV} = ${BNR}/g" $FILE @@ -68,6 +62,8 @@ git commit -m "${ENV} to ${BNR}" DIFFG=`git diff HEAD^ HEAD --color=always|perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/'` DIFFR=`git diff HEAD^ HEAD --color=always|perl -wlne 'print $1 if /^\e\[31m-(.*)\e\[m$/'` git push origin master -cd $STARTDIR +cd gConf[dirStart] echo -e "\n${gConf[colorError]} ${DIFFR} ${gConf[colorNormal]}" echo -e "${gConf[colorSuccess]} ${DIFFG} ${gConf[colorNormal]}" + +messageExit diff --git a/scripts/gcobranch.sh b/scripts/gcobranch.sh index 41a7a25..2fa8b90 100755 --- a/scripts/gcobranch.sh +++ b/scripts/gcobranch.sh @@ -2,12 +2,13 @@ # Checks out and pulls specified {branch} in all git repos found in each of specified {dir} # Syntax: $ gcobranch.sh {branch} {dir1} [{dir2} {dir3}...] source ~/.bashscripts/lib/commons.sh + ERRCOUNT=0 if [ $# -lt 2 ] then echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Incorrect arguments specified${gConf[colorNormal]}" - echo "Usage: gcobranch [branch] [dir] [dir]..." + echo "Syntax: gcobranch [branch] [dir] [dir]..." exit 0 fi diff --git a/scripts/gitmerge.sh b/scripts/gitmerge.sh index 46c08e6..b952972 100755 --- a/scripts/gitmerge.sh +++ b/scripts/gitmerge.sh @@ -3,6 +3,7 @@ # If no {branch} is specified, "development" is used # Syntax: $ gitmerge [options] [branch] source ~/.bashscripts/lib/commons.sh + ERRCOUNT=0 ALLOWEDARGS=1 OPTP=false @@ -21,26 +22,27 @@ while getopts ":p" opt; do done if [ $# -gt $ALLOWEDARGS ]; then - echo -e "${gConf[colorError]}${ERRMASCOT}ERROR: Received $# arguments, only ${ALLOWEDARGS} allowed! Syntax: gitmerge [options] [branch] ${gConf[colorNormal]}" + messageError "Received $# arguments, only ${ALLOWEDARGS} allowed!" + message "Syntax: gitmerge [options] [branch]" exit 1 fi -currentBranch="$(b=$(git symbolic-ref -q HEAD); { [ -n "$b" ] && echo ${b##refs/heads/}; } || echo HEAD)" +currentBranch="$(gitGetCurrentBranch)" mergeBranch="development" if [[ $1 && "$OPTP" = false ]]; then mergeBranch=$1 elif [[ $1 && "$OPTP" = true && $2 ]]; then mergeBranch=$2 fi -echo -e "Merging ${gConf[colorHighlight]}${currentBranch}${gConf[colorNormal]} with ${gConf[colorHighlight]}${mergeBranch}${gConf[colorNormal]}" +message "Merging ${gConf[colorHighlight]}${currentBranch}${gConf[colorNormal]} with ${gConf[colorHighlight]}${mergeBranch}${gConf[colorNormal]}" CHECKOUT=$((git checkout $mergeBranch) 2>&1) if [[ "${CHECKOUT}" =~ "error: pathspec" ]]; then - echo -e "No such branch ${mergeBranch}. Creating..." + message "No such branch ${mergeBranch}. Creating..." git checkout -b $mergeBranch origin/$mergeBranch CURRENT=$((git rev-parse --abbrev-ref HEAD) 2>&1) if [[ "${CURRENT}" != "${mergeBranch}" ]]; then - echo -e "Error: could not switch to ${mergeBranch}. ABORTING" + messageError "could not switch to ${mergeBranch}" exit 1 fi fi @@ -50,7 +52,7 @@ git push origin $mergeBranch git checkout $currentBranch if [ "$OPTP" = true ] ; then - currentOrigin=`git config --get remote.origin.url`; + currentOrigin=$(gitGetCurrentOrigin) if [[ "${currentOrigin}" =~ "x-oauth-basic" ]]; then cleanedOrigin=(`echo $currentOrigin | tr '@' "\n"`) newOrigin="https://${cleanedOrigin[1]}" @@ -69,4 +71,4 @@ if [ "$OPTP" = true ] ; then done fi -exit 0 +messageExit diff --git a/scripts/gitpress.sh b/scripts/gitpress.sh index 1f07561..f8af5f7 100755 --- a/scripts/gitpress.sh +++ b/scripts/gitpress.sh @@ -2,6 +2,8 @@ # compresses all commits in current local branch into single commit # Syntax: $ gitpress [options] source ~/.bashscripts/lib/commons.sh + +ERRCOUNT=0 ALLOWEDARGS=2 OPTF=false OPTP=false @@ -24,16 +26,17 @@ while getopts ":fp" opt; do done if [ $# -gt $ALLOWEDARGS ]; then - echo -e "${gConf[colorError]}${ERRMASCOT}ERROR: Received $# arguments, only ${ALLOWEDARGS} allowed! Syntax: gitpress [options]${gConf[colorNormal]}" + messageError "Received $# arguments, only ${ALLOWEDARGS} allowed!" + message "Syntax: gitpress [options]" exit 1 fi -CURRENTBRANCH="$(b=$(git symbolic-ref -q HEAD); { [ -n "$b" ] && echo ${b##refs/heads/}; } || echo HEAD)" +CURRENTBRANCH="$(gitGetCurrentBranch)" if [[ "$OPTF" = false ]]; then for UNSAFEBRANCH in ${UNSAFEBRANCHES[@]} do if [[ ${UNSAFEBRANCH} == ${CURRENTBRANCH} ]]; then - echo -e "${gConf[colorError]}${ERRMASCOT}ERROR: Use -f option to compress, branch name '${UNSAFEBRANCH}' is too generic${gConf[colorNormal]}" + messageError "Use -f option to compress, branch name '${UNSAFEBRANCH}' is too generic" exit 1 fi done @@ -45,7 +48,8 @@ SAFEPUSH="git push origin +${CURRENTBRANCH}" if [[ "$OPTP" = true ]]; then $(${SAFEPUSH}) else - echo -e "Use command '${SAFEPUSH}' to overwrite remote ${CURRENTBRANCH} branch" + message "Use command '${SAFEPUSH}' to overwrite remote ${CURRENTBRANCH} branch" fi -exit 0 +messageExit + diff --git a/scripts/guorigin.sh b/scripts/guorigin.sh index 04cccc0..73d3e0c 100755 --- a/scripts/guorigin.sh +++ b/scripts/guorigin.sh @@ -63,4 +63,4 @@ do done messageExit -exit 0 + From 52110c42806d9eafbe763a8d74b50e3ba33256d9 Mon Sep 17 00:00:00 2001 From: Gundars Date: Sun, 1 Mar 2015 16:27:15 +0200 Subject: [PATCH 05/14] user config --- .gitignore | 1 + config/global.sh | 1 - config/user.sh | 4 ++++ lib/commons.sh | 1 + scripts/buildenv.sh | 27 ++++++++++++++------------- scripts/gcobranch.sh | 3 ++- scripts/gitpress.sh | 2 +- 7 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 config/user.sh diff --git a/.gitignore b/.gitignore index 40b7eda..127d860 100755 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /nbproject/ +/.idea/ diff --git a/config/global.sh b/config/global.sh index ac3b87b..d9506a0 100644 --- a/config/global.sh +++ b/config/global.sh @@ -11,4 +11,3 @@ gConf[colorError]='\033[31m' gConf[colorSuccess]='\e[32m' gConf[errorMascot]=' __\n / _)\n .-^^^-/ /\n __/ /\n<__.|_|-|_| ' - diff --git a/config/user.sh b/config/user.sh new file mode 100644 index 0000000..07ce786 --- /dev/null +++ b/config/user.sh @@ -0,0 +1,4 @@ +#!/bin/bash +#User configuration +declare -A uConf +uConf[enrichedRepository]=https/link/to/encriched.git \ No newline at end of file diff --git a/lib/commons.sh b/lib/commons.sh index 4336644..d546438 100644 --- a/lib/commons.sh +++ b/lib/commons.sh @@ -1,6 +1,7 @@ #!/bin/bash # Container of all common functions source ~/.bashscripts/config/global.sh +source ~/.bashscripts/config/user.sh function message { if [ -z "$1" ]; then diff --git a/scripts/buildenv.sh b/scripts/buildenv.sh index a9df2ac..5c3fadc 100755 --- a/scripts/buildenv.sh +++ b/scripts/buildenv.sh @@ -1,24 +1,21 @@ #!/bin/bash # Change build number on environmnet # Syntax: $ buildenv {env} {branch} {build number} -# {env} syntax: www_domain_extension +# { env} syntax: www_domain_extension # {branch} syntax: dev | test | staging # {build number} syntax: integer 1-5 digits long source ~/.bashscripts/lib/commons.sh ERRCOUNT=0 +ALLOWEDARGS=3 ENV=$1 -ENVREGEX="^w{3}_.*_[a-zA-Z]{2,4}$" BRANCH=$2 -BRANCHREGEX="^(dev|test|staging)$" BNR=$3 +ENVREGEX="^w{3}_.*_[a-zA-Z]{2,4}$" +BRANCHREGEX="^(dev|test|staging)$" BNRREGEX="[0-9]{1,5}" -TMPDIR=~/.bashscripts/tmp -ERRMASCOT=' __\n / _)\n .-^^^-/ /\n __/ /\n<__.|_|-|_|'; -ERRCOUNT=0 -REPO="https/link/to/encriched.git" -if [ $# -lt 3 ]; then +if [ $# -lt $ALLOWEDARGS ]; then messageError "Incorrect arguments specified" echo "Syntax: buildenv {env} {branch} {build number}" echo "{env} syntax: www_domain_extension" @@ -37,25 +34,29 @@ if ! [[ $BRANCH =~ $BRANCHREGEX ]]; then fi if ! [[ $BNR =~ $BNRREGEX ]]; then - messageError "Bad build number$" + messageError "Bad build number" message "{build number} syntax: integer 1-5 digits long" fi -if [[ $REPO =~ "https/link/to/encriched.git" ]]; then +if [[ uConf[enrichedRepository] =~ "https/link/to/encriched.git" ]]; then messageError "Bad repository name" - message "Please change line 21 'https/link/to/encriched.git' in file ~/.bashscripts/buildenv.sh to valid link to repository" + message "Please change line 4 'https/link/to/encriched.git' in file ~/.bashscripts/config/user.sh to valid link to enriched github repository" fi if ! [[ $ERRCOUNT =~ 0 ]]; then messageExit fi -if ! [ -d "$TMPDIR" ]; then - git clone $REPO $TMPDIR +if ! [ -d gConf[dirTmp] ]; then + git clone uConf[enrichedRepository] gConf[dirTmp] fi cd gConf[dirTmp] git pull origin master FILE=environment_definition/${BRANCH}1/environment_definition.def +if ! [ -f $FILE ]; then + messageError "File ${FILE} does not exist" + messageExit +fi sed -i "s/^${ENV}.*/${ENV} = ${BNR}/g" $FILE git add $FILE git commit -m "${ENV} to ${BNR}" diff --git a/scripts/gcobranch.sh b/scripts/gcobranch.sh index 2fa8b90..7c63031 100755 --- a/scripts/gcobranch.sh +++ b/scripts/gcobranch.sh @@ -4,8 +4,9 @@ source ~/.bashscripts/lib/commons.sh ERRCOUNT=0 +ALLOWEDARGS=2 -if [ $# -lt 2 ] +if [ $# -lt $ALLOWEDARGS ] then echo -e "\n${gConf[colorError]}${ERRMASCOT}\nERROR: Incorrect arguments specified${gConf[colorNormal]}" echo "Syntax: gcobranch [branch] [dir] [dir]..." diff --git a/scripts/gitpress.sh b/scripts/gitpress.sh index f8af5f7..4c40984 100755 --- a/scripts/gitpress.sh +++ b/scripts/gitpress.sh @@ -4,7 +4,7 @@ source ~/.bashscripts/lib/commons.sh ERRCOUNT=0 -ALLOWEDARGS=2 +ALLOWEDARGS=0 OPTF=false OPTP=false UNSAFEBRANCHES=(development test master) From 24d259ad5aca9afaa4752aaf34a9fa7ee05e1064 Mon Sep 17 00:00:00 2001 From: Gundars Date: Sun, 8 Mar 2015 15:04:37 +0000 Subject: [PATCH 06/14] gitscan --- scripts/gcobranch.sh | 2 +- scripts/gitscan.sh | 111 +++++++++++++++++++++++++++++++++++++++++++ scripts/guorigin.sh | 66 ------------------------- 3 files changed, 112 insertions(+), 67 deletions(-) mode change 100755 => 100644 scripts/gcobranch.sh create mode 100644 scripts/gitscan.sh delete mode 100755 scripts/guorigin.sh diff --git a/scripts/gcobranch.sh b/scripts/gcobranch.sh old mode 100755 new mode 100644 index 7c63031..6d97ce5 --- a/scripts/gcobranch.sh +++ b/scripts/gcobranch.sh @@ -1,6 +1,6 @@ #!/bin/bash # Checks out and pulls specified {branch} in all git repos found in each of specified {dir} -# Syntax: $ gcobranch.sh {branch} {dir1} [{dir2} {dir3}...] +# Syntax: $ gcobranch {branch} {dir1} [{dir2} {dir3}...] source ~/.bashscripts/lib/commons.sh ERRCOUNT=0 diff --git a/scripts/gitscan.sh b/scripts/gitscan.sh new file mode 100644 index 0000000..0138b20 --- /dev/null +++ b/scripts/gitscan.sh @@ -0,0 +1,111 @@ +#!/bin/bash +# Scan all directories {dir} for git repositories and perform actions based on options +# Syntax: $ gitscan [options] {dir1} [{dir2} {dir3}...] +# Options: +# -o update remote origin links from oauth/ssh to https:// +# -m list all modified files +# -c create branches in sync with origin +source ~/.bashscripts/lib/commons.sh + +ERRCOUNT=0 +ALLOWEDARGS=0 +OPTO=false +OPTC=false +OPTM=false + +while getopts "ocm" opt; do + case $opt in + o) + OPTO=true + ALLOWEDARGS=$[ALLOWEDARGS + 1] + ;; + c) + OPTC=true + ALLOWEDARGS=$[ALLOWEDARGS + 1] + ;; + m) + OPTM=true + ALLOWEDARGS=$[ALLOWEDARGS + 1] + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + ;; + esac +done + + +if [ $# -lt $ALLOWEDARGS ] + then + messageError "Incorrect arguments specified" + message "Usage: gitscan [options] {dir1} [{dir2} {dir3}...]" + exit 1 +fi + +directories="${@:1}"; +for dir in $directories +do + if [ -d "$dir" ]; then + cd $dir>/dev/null; + cd ->/dev/null + for d in `findAllGitDrectories $dir`; do + cd $d/.. > /dev/null + messageHighlight "\nUpdating ${PWD}" + + # OPT -o + if [[ "$OPTO" = false ]]; then + CURRENT=$(gitGetCurrentOrigin) + echo -e "Current Origin: ${CURRENT}" + FILTERBY="x-oauth-basic" + FILTERBYSSH="git@github.com:" + if [[ "${CURRENT}" =~ "${FILTERBY}" ]]; then + STR_ARRAY=(`echo $CURRENT | tr '@' "\n"`) + NEW="https://${STR_ARRAY[1]}" + if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then + gitSetCurrentOrigin $NEW + NEWCHANGED=$(gitGetCurrentOrigin) + if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then + messageSuccess "Origin changed to ${NEWCHANGED}" + else + messageError "unable to change origin" + fi + else + messageError "${NEW} is not a valid repository" + fi + elif [[ "${CURRENT}" =~ "${FILTERBYSSH}" ]]; then + STR_ARRAY=(`echo $CURRENT | tr ':' "\n"`) + NEW="https://github.com/${STR_ARRAY[1]}" + if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then + gitSetCurrentOrigin $NEW + NEWCHANGED=$(gitGetCurrentOrigin) + if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then + messageSuccess "Origin changed to ${NEWCHANGED}" + else + messageError "unable to change origin" + fi + else + messageError "${NEW} is not a valid repository" + fi + else + message "Origin OK" + fi + fi + + # OPT -c + if [[ "$OPTC" = false ]]; then + + fi + + # OPT -m + if [[ "$OPTM" = false ]]; then + + fi + + cd - > /dev/null + done + else + messageError "Directory ${dir} does not exist" + fi +done + +messageExit + diff --git a/scripts/guorigin.sh b/scripts/guorigin.sh deleted file mode 100755 index 73d3e0c..0000000 --- a/scripts/guorigin.sh +++ /dev/null @@ -1,66 +0,0 @@ -#!/bin/bash -# Update git origins from o-auth and SSH to native https in all git repos found in each of specified directories {dir} -# Syntax: $ guorigin.sh {dir1} [{dir2} {dir3}...] -source ~/.bashscripts/lib/commons.sh - -ERRCOUNT=0 -if [ $# -lt 1 ] - then - messageError "Incorrect arguments specified" - message "Usage: guorigin {dir1} [{dir2} {dir3}...]" - exit 1 -fi - -directories="${@:1}"; -for dir in $directories -do - if [ -d "$dir" ]; then - cd $dir>/dev/null; - cd ->/dev/null - for d in `findAllGitDrectories $dir`; do - cd $d/.. > /dev/null - messageHighlight "\nUpdating ${PWD}" - CURRENT=$(gitGetCurrentOrigin) - echo -e "Current Origin: ${CURRENT}" - FILTERBY="x-oauth-basic" - FILTERBYSSH="git@github.com:" - if [[ "${CURRENT}" =~ "${FILTERBY}" ]]; then - STR_ARRAY=(`echo $CURRENT | tr '@' "\n"`) - NEW="https://${STR_ARRAY[1]}" - if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then - gitSetCurrentOrigin $NEW - NEWCHANGED=$(gitGetCurrentOrigin) - if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then - messageSuccess "Origin changed to ${NEWCHANGED}" - else - messageError "unable to change origin" - fi - else - messageError "${NEW} is not a valid repository" - fi - elif [[ "${CURRENT}" =~ "${FILTERBYSSH}" ]]; then - STR_ARRAY=(`echo $CURRENT | tr ':' "\n"`) - NEW="https://github.com/${STR_ARRAY[1]}" - if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then - gitSetCurrentOrigin $NEW - NEWCHANGED=$(gitGetCurrentOrigin) - if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then - messageSuccess "Origin changed to ${NEWCHANGED}" - else - messageError "unable to change origin" - fi - else - messageError "${NEW} is not a valid repository" - fi - else - message "Origin OK" - fi - cd - > /dev/null - done - else - messageError "Directory ${dir} does not exist" - fi -done - -messageExit - From 989c51a5268c24c5a5c24e4ccad687a52d052b94 Mon Sep 17 00:00:00 2001 From: Gundars Date: Tue, 10 Mar 2015 22:55:29 +0200 Subject: [PATCH 07/14] gitscan -o -c -m --- install.sh | 2 +- lib/commons.sh | 4 ++ scripts/gcobranch.sh | 0 scripts/gitscan.sh | 142 ++++++++++++++++++++++++++----------------- 4 files changed, 91 insertions(+), 57 deletions(-) mode change 100644 => 100755 scripts/gcobranch.sh mode change 100644 => 100755 scripts/gitscan.sh diff --git a/install.sh b/install.sh index 86ba79a..ce2a9e2 100755 --- a/install.sh +++ b/install.sh @@ -5,7 +5,7 @@ source ~/.bashscripts/lib/commons.sh cd ${gConf[dirInstall]} git pull origin master -SCRIPTS=(gcobranch gitmerge guorigin buildenv gitpress) +SCRIPTS=(gcobranch gitmerge gitscan buildenv gitpress) for SCRIPT in ${SCRIPTS[@]} do if [ ! -h ${gConf[dirSymlinks]}/$SCRIPT ]; then diff --git a/lib/commons.sh b/lib/commons.sh index d546438..7328ff2 100644 --- a/lib/commons.sh +++ b/lib/commons.sh @@ -72,3 +72,7 @@ function gitGetCurrentBranch { function findAllGitDrectories { find $1 -name .git -type d } + +function gitFetchAll { + git fetch --all +} \ No newline at end of file diff --git a/scripts/gcobranch.sh b/scripts/gcobranch.sh old mode 100644 new mode 100755 diff --git a/scripts/gitscan.sh b/scripts/gitscan.sh old mode 100644 new mode 100755 index 0138b20..a0f80fe --- a/scripts/gitscan.sh +++ b/scripts/gitscan.sh @@ -4,7 +4,7 @@ # Options: # -o update remote origin links from oauth/ssh to https:// # -m list all modified files -# -c create branches in sync with origin +# -c create test and development branches in sync with origin source ~/.bashscripts/lib/commons.sh ERRCOUNT=0 @@ -28,12 +28,12 @@ while getopts "ocm" opt; do ALLOWEDARGS=$[ALLOWEDARGS + 1] ;; \?) - echo "Invalid option: -$OPTARG" >&2 - ;; + messageError "Invalid option: -$OPTARG" >&2 + messageExit + ;; esac done - if [ $# -lt $ALLOWEDARGS ] then messageError "Incorrect arguments specified" @@ -41,65 +41,96 @@ if [ $# -lt $ALLOWEDARGS ] exit 1 fi -directories="${@:1}"; -for dir in $directories +function updateOriginLinks { + CURRENT=$(gitGetCurrentOrigin) + echo -e "Current Origin: ${CURRENT}" + FILTERBY="x-oauth-basic" + FILTERBYSSH="git@github.com:" + if [[ "${CURRENT}" =~ "${FILTERBY}" ]]; then + STR_ARRAY=(`echo $CURRENT | tr '@' "\n"`) + NEW="https://${STR_ARRAY[1]}" + if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then + gitSetCurrentOrigin $NEW + NEWCHANGED=$(gitGetCurrentOrigin) + if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then + messageSuccess "Origin changed to ${NEWCHANGED}" + else + messageError "unable to change origin" + fi + else + messageError "${NEW} is not a valid repository" + fi + elif [[ "${CURRENT}" =~ "${FILTERBYSSH}" ]]; then + STR_ARRAY=(`echo $CURRENT | tr ':' "\n"`) + NEW="https://github.com/${STR_ARRAY[1]}" + if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then + gitSetCurrentOrigin $NEW + NEWCHANGED=$(gitGetCurrentOrigin) + if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then + messageSuccess "Origin changed to ${NEWCHANGED}" + else + messageError "unable to change origin" + fi + else + messageError "${NEW} is not a valid repository" + fi + else + message "Origin OK" + fi +} + +function listAllModifiedFiles { + MODIFIED=`git status --porcelain` + if [ -n "$MODIFIED" ]; then + messageHighlight "\nModified files found in ${1}" + message $MODIFIED + fi +} + +function createBranches { + CURRENT=$(gitGetCurrentOrigin) + git checkout master + gitFetchAll + branches=${@:1}; + for branch in $branches + do + BRANCHLOCAL=$(git branch | grep "${branch}" | tr -d '* ') + BRANCHORIGIN=$(git branch -r | grep -w 'origin/${branch}' | tr -d " origin/") + if [[ "${BRANCHLOCAL}" != "" && "${BRANCHORIGIN}" != '${branch}' ]]; then + message "Creating branch ${branch} origin/${branch}" + $(git checkout -b ${branch} origin/${branch}) + fi + done +} + +# Remove options from args +ARGSORIGINAL=${@:1} +ARGSNEW=() +for ARG in ${ARGSORIGINAL[@]} +do + if [[ ${ARG:0:1} != "-" ]]; then + ARGSNEW+=($ARG) + fi +done + +for dir in $ARGSNEW do if [ -d "$dir" ]; then cd $dir>/dev/null; cd ->/dev/null for d in `findAllGitDrectories $dir`; do cd $d/.. > /dev/null - messageHighlight "\nUpdating ${PWD}" - - # OPT -o - if [[ "$OPTO" = false ]]; then - CURRENT=$(gitGetCurrentOrigin) - echo -e "Current Origin: ${CURRENT}" - FILTERBY="x-oauth-basic" - FILTERBYSSH="git@github.com:" - if [[ "${CURRENT}" =~ "${FILTERBY}" ]]; then - STR_ARRAY=(`echo $CURRENT | tr '@' "\n"`) - NEW="https://${STR_ARRAY[1]}" - if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then - gitSetCurrentOrigin $NEW - NEWCHANGED=$(gitGetCurrentOrigin) - if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then - messageSuccess "Origin changed to ${NEWCHANGED}" - else - messageError "unable to change origin" - fi - else - messageError "${NEW} is not a valid repository" - fi - elif [[ "${CURRENT}" =~ "${FILTERBYSSH}" ]]; then - STR_ARRAY=(`echo $CURRENT | tr ':' "\n"`) - NEW="https://github.com/${STR_ARRAY[1]}" - if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then - gitSetCurrentOrigin $NEW - NEWCHANGED=$(gitGetCurrentOrigin) - if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then - messageSuccess "Origin changed to ${NEWCHANGED}" - else - messageError "unable to change origin" - fi - else - messageError "${NEW} is not a valid repository" - fi - else - message "Origin OK" - fi + PWD=$(pwd) + messageHighlight "\nScanning ${PWD}" + if [[ "$OPTO" = true ]]; then + updateOriginLinks fi - - # OPT -c - if [[ "$OPTC" = false ]]; then - + if [[ "$OPTC" = true ]]; then + createBranches 'development' 'test' fi - - # OPT -m - if [[ "$OPTM" = false ]]; then - + if [[ "$OPTM" = true ]]; then + listAllModifiedFiles $PWD fi - cd - > /dev/null done else @@ -107,5 +138,4 @@ do fi done -messageExit - +messageExit \ No newline at end of file From 63cde20a3c856ec0625950da505d1dff99f63b6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gundars=20M=C4=93ness?= Date: Thu, 12 Mar 2015 18:29:33 +0200 Subject: [PATCH 08/14] Update gitmerge.sh --- scripts/gitmerge.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/gitmerge.sh b/scripts/gitmerge.sh index b952972..5e37790 100755 --- a/scripts/gitmerge.sh +++ b/scripts/gitmerge.sh @@ -39,7 +39,7 @@ message "Merging ${gConf[colorHighlight]}${currentBranch}${gConf[colorNormal]} w CHECKOUT=$((git checkout $mergeBranch) 2>&1) if [[ "${CHECKOUT}" =~ "error: pathspec" ]]; then message "No such branch ${mergeBranch}. Creating..." - git checkout -b $mergeBranch origin/$mergeBranch + $((git checkout -b $mergeBranch origin/$mergeBranch) 2>&1) CURRENT=$((git rev-parse --abbrev-ref HEAD) 2>&1) if [[ "${CURRENT}" != "${mergeBranch}" ]]; then messageError "could not switch to ${mergeBranch}" From fd5a07fec83d5fd094f2562afc2e793dbc4ffd4f Mon Sep 17 00:00:00 2001 From: Gundars Date: Sun, 15 Mar 2015 02:08:41 +0200 Subject: [PATCH 09/14] v1.2 dev & error handling --- config/global.sh | 6 ++- lib/commons.sh | 54 ++++++++++++--------- scripts/gcobranch.sh | 6 ++- scripts/gitscan.sh | 111 ++++++++++++++++--------------------------- 4 files changed, 82 insertions(+), 95 deletions(-) diff --git a/config/global.sh b/config/global.sh index 5f2aea3..8f2bfa9 100644 --- a/config/global.sh +++ b/config/global.sh @@ -9,5 +9,9 @@ gConf[colN]='\e[00m' gConf[colH]='\e[01;36m' gConf[colE]='\033[31m' gConf[colS]='\e[32m' -gConf[errorMascot]=' __\n / _)\n .-^^^-/ /\n __/ /\n<__.|_|-|_| ' +declare -A errorMascots +errorMascots[1]=' __\n / _)\n .-^^^-/ /\n __/ /\n<__.|_|-|_|\n' +errorMascots[2]='''\^~~~~\ ) ( /~~~~^/\n ) *** \ {**} / *** (\n ) *** \_ ^^ _/ *** (\n ) **** vv **** (\n )_**** ****_(\n )*** m m ***( \n''' +errorMascots[3]=''' _____\n / \ \n| () () |\n \ ^ /\n |||||\n |||||\n''' +errorMascots[4]=''' ,\n _)\_\n //}\n (_;\ \n>>>===> \`==\n /__/\n ``\n''' diff --git a/lib/commons.sh b/lib/commons.sh index 66687ad..1ac3265 100644 --- a/lib/commons.sh +++ b/lib/commons.sh @@ -36,7 +36,7 @@ function messageError { local em='' local er='\n' else - local em=${gConf[errorMascot]} + local em=$(getRandomErrorMascot) local er='ERROR: ' fi echo -e "${gConf[colE]}${em}${er}$1${gConf[colN]}" @@ -44,7 +44,8 @@ function messageError { } function messageExit { - if [[ "${erroCount}" == "0" ]] ; then + message "\n" + if [[ "${erroCount}" == "0" ]] ; then messageSuccess "Completed without errors" exit 0 else @@ -59,27 +60,23 @@ function exitIfErrors { fi } -function removeOptionsFromArguments ( - arguments=$1 +#pass an array of all orginal args except 0 ! +function removeOptionsFromArguments { argumentsWithoutOptions=() - for arg in ${arguments[@]} + for arg in "$@" do if [[ ${arg:0:1} != "-" ]]; then argumentsWithoutOptions+=($arg) fi done -) +} function gitGetCurrentOrigin { git config --get remote.origin.url } function gitSetCurrentOrigin { - if [ -z "$1" ]; then - echo "gitSetCurrentOrigin param 1 error" - else - git remote set-url origin $1 - fi + git remote set-url origin $1 } function gitGetCurrentBranch { @@ -92,25 +89,29 @@ function findAllGitDrectories { } function gitFetchAll { - git fetch --all + local a=$((git fetch --all) 2>&1) } +function gitCheckout { + local a=$((git checkout $1) 2>&1) +} function gitOriginAnyToHttps { local currentOrigin=$(gitGetCurrentOrigin) if [[ "${currentOrigin}" =~ "x-oauth-basic" ]]; then local cleanedOrigin=(`echo $currentOrigin | tr '@' "\n"`) local newOrigin="https://${cleanedOrigin[1]}" if [[ "${newOrigin}" =~ "https://github.com/".*".git" ]] ; then - currentOrigin=$newOrigin + local currentOrigin=$newOrigin fi elif [[ "${currentOrigin}" =~ "git@github.com:" ]]; then local cleanedOrigin=(`echo $currentOrigin | tr ':' "\n"`) local newOrigin="https://github.com/${cleanedOrigin[1]}" if [[ "${newOrigin}" =~ "https://github.com/".*".git" ]] ; then - currentOrigin=$newOrigin + local currentOrigin=$newOrigin fi fi - echo currentOrigin + + echo $currentOrigin } function gitDiffDeleted { @@ -122,18 +123,18 @@ function gitDiffAdded { } function gitCheckoutBranchWithOrigin { - mergeBranch=$1 - checkout=$((git checkout $mergeBranch) 2>&1) + local branchToCheckout=$1 + checkout=$((git checkout $branchToCheckout) 2>&1) if [[ "${checkout}" =~ "error: pathspec" ]]; then - message "No such branch ${mergeBranch}. Creating..." - $((git checkout -b $mergeBranch origin/$mergeBranch) &> /dev/null) + message "No such branch ${branchToCheckout}. Creating..." + $((git checkout -b $branchToCheckout origin/$branchToCheckout) &> /dev/null) currentBranch=$(gitGetCurrentBranch) - if [[ "${currentBranch}" != "${mergeBranch}" ]]; then + if [[ "${currentBranch}" != "${branchToCheckout}" ]]; then $((git fetch --all) &> /dev/null) - $((git fetch origin $mergeBranch:$mergeBranch) &> /dev/null) + $((git fetch origin $mergeBranch:branchToCheckout) &> /dev/null) currentBranch=$(gitGetCurrentBranch) - if [[ "${currentBranch}" != "${mergeBranch}" ]]; then - messageError "Branch ${mergeBranch} does not exist in origin" + if [[ "${currentBranch}" != "${branchToCheckout}" ]]; then + messageError "Branch ${branchToCheckout} does not exist in origin" fi fi fi @@ -144,3 +145,10 @@ function gitLastCommit { echo $lc } +function getRandomErrorMascot { + printf $'%s' "${errorMascots[$(($(rand "${#errorMascots[*]}")+1))]}" +} + +function rand { + printf $(( $1 * RANDOM / 32767 )) +} \ No newline at end of file diff --git a/scripts/gcobranch.sh b/scripts/gcobranch.sh index 00c0be2..3b41c53 100755 --- a/scripts/gcobranch.sh +++ b/scripts/gcobranch.sh @@ -29,8 +29,10 @@ do message "Current branch: ${currentBranch}" if [[ "${currentBranch}" != "${checkoutBranch}" ]]; then gitCheckoutBranchWithOrigin $checkoutBranch + checkedout=" checked out and" else - message "Already on ${branch}" + message "Already on ${checkoutBranch}" + checkedout="" fi currentBranch=$(gitGetCurrentBranch) @@ -38,7 +40,7 @@ do pull=$((git pull origin $checkoutBranch) 2>&1) lastCommit=$(gitLastCommit) message "Last commit: ${lastCommit}" - messageSuccess "${checkoutBranch} checked out and pulled" + messageSuccess "${checkoutBranch}${checkedout} pulled" fi cd - > /dev/null done diff --git a/scripts/gitscan.sh b/scripts/gitscan.sh index 5c159bf..07b1ae0 100755 --- a/scripts/gitscan.sh +++ b/scripts/gitscan.sh @@ -7,6 +7,43 @@ # -c create test and development branches in sync with origin source ~/.bashscripts/lib/commons.sh +function updateOriginUrl { + local currentOrigin=$(gitGetCurrentOrigin) + local httpsOrigin=$(gitOriginAnyToHttps) + message "Current origins URL: ${currentOrigin}" + if [[ "${currentOrigin}" != "${httpsOrigin}" ]]; then + gitSetCurrentOrigin $httpsOrigin + local newOrigin=$(gitGetCurrentOrigin) + if [[ "${newOrigin}" = "${httpsOrigin}" ]]; then + messageSuccess "Origins url changed to ${newOrigin}" + else + messageError "Unable to change origins url" + fi + else + message "URL OK" + fi +} + +function listAllModifiedFiles { + modified=$(git status --porcelain) + if [ -n "$modified" ]; then + messageSuccess "\nModified files found in ${1}" + git status --porcelain + fi +} + +function createBranches { + currentBranch=$(gitGetCurrentOrigin) + gitCheckout master + gitFetchAll + for branch in "$@" + do + message "Creating branch ${branch} origin/${branch}" + gitCheckoutBranchWithOrigin $branch + done + gitCheckout $currentBranch +} + erroCount=0 minArgsCount=1 optO=false @@ -31,7 +68,7 @@ while getopts "ocm" opt; do esac done -removeOptionsFromArguments ${@:1} +removeOptionsFromArguments "$@" if [ ${#argumentsWithoutOptions[@]} -lt $minArgsCount ]; then messageError "Incorrect arguments specified" @@ -39,7 +76,7 @@ if [ ${#argumentsWithoutOptions[@]} -lt $minArgsCount ]; then messageExit fi -for dir in $argumentsWithoutOptions +for dir in "${argumentsWithoutOptions[@]}" do if [ -d "$dir" ]; then cd $dir>/dev/null; @@ -49,13 +86,13 @@ do pwd=$(pwd) messageHighlight "\nScanning ${PWD}" if [[ "$optO" = true ]]; then - updateOriginLinks + updateOriginUrl fi if [[ "$optC" = true ]]; then - createBranches 'development' 'test' + createBranches "development" "test" fi if [[ "$optM" = true ]]; then - listAllModifiedFiles $PWD + listAllModifiedFiles $pwd fi cd - > /dev/null done @@ -65,67 +102,3 @@ do done messageExit - -#=============================================================== - -function updateOriginLinks { - CURRENT=$(gitGetCurrentOrigin) - echo -e "Current Origin: ${CURRENT}" - FILTERBY="x-oauth-basic" - FILTERBYSSH="git@github.com:" - if [[ "${CURRENT}" =~ "${FILTERBY}" ]]; then - STR_ARRAY=(`echo $CURRENT | tr '@' "\n"`) - NEW="https://${STR_ARRAY[1]}" - if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then - gitSetCurrentOrigin $NEW - NEWCHANGED=$(gitGetCurrentOrigin) - if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then - messageSuccess "Origin changed to ${NEWCHANGED}" - else - messageError "unable to change origin" - fi - else - messageError "${NEW} is not a valid repository" - fi - elif [[ "${CURRENT}" =~ "${FILTERBYSSH}" ]]; then - STR_ARRAY=(`echo $CURRENT | tr ':' "\n"`) - NEW="https://github.com/${STR_ARRAY[1]}" - if [[ "${NEW}" =~ "https://github.com/".*".git" ]] ; then - gitSetCurrentOrigin $NEW - NEWCHANGED=$(gitGetCurrentOrigin) - if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then - messageSuccess "Origin changed to ${NEWCHANGED}" - else - messageError "unable to change origin" - fi - else - messageError "${NEW} is not a valid repository" - fi - else - message "Origin OK" - fi -} - -function listAllModifiedFiles { - MODIFIED=`git status --porcelain` - if [ -n "$MODIFIED" ]; then - messageHighlight "\nModified files found in ${1}" - message $MODIFIED - fi -} - -function createBranches { - CURRENT=$(gitGetCurrentOrigin) - git checkout master - gitFetchAll - branches=${@:1}; - for branch in $branches - do - BRANCHLOCAL=$(git branch | grep "${branch}" | tr -d '* ') - BRANCHORIGIN=$(git branch -r | grep -w 'origin/${branch}' | tr -d " origin/") - if [[ "${BRANCHLOCAL}" != "" && "${BRANCHORIGIN}" != '${branch}' ]]; then - message "Creating branch ${branch} origin/${branch}" - $(git checkout -b ${branch} origin/${branch}) - fi - done -} From 326ac5ae34d3cdcbd6371e87864463edf1dfd398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gundars=20M=C4=93ness?= Date: Sun, 15 Mar 2015 02:15:54 +0200 Subject: [PATCH 10/14] Update README.md --- README.md | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3cd5a1c..d069e9a 100755 --- a/README.md +++ b/README.md @@ -26,10 +26,22 @@ Syntax: `$ gitmerge [options] [branch]` Options: - `-p` generate links to test/master pull requests -###guorigin -Git update origin - updates git remote origins format from o-auth and SSH to native https in all git repos found in each of specified directories {dir} +###gitscan +Scan all directories {dir} for git repositories and perform actions based on options -Syntax: `$ guorigin {dir1} [{dir2} {dir3}...]` +Syntax: `$ gitscan [options] {dir1} [{dir2} {dir3}...]` + +Options: +- `-o` update remote origin URLs format from oauth/ssh to https +- `-m` list all modified files +- `-c` create test and development branches in sync with origin + +###buildenv +Changes {build number} for a {branch} on site environmnet {env} + +**Warning:** Before using script, change line 21 - swap dummy string with https link to enriched github repository + +Syntax: `$ buildenv {env} {branch} {build number}` ###gitpress Git compress - compresses all commits in current local branch into single commit @@ -42,9 +54,3 @@ Options: **Warning:** This will disable history, allign with master to fix pull requests -###buildenv -Changes {build number} for a {branch} on site environmnet {env} - -**Warning:** Before using script, change line 21 - swap dummy string with https link to enriched github repository - -Syntax: `$ buildenv {env} {branch} {build number}` From 1d384c1310752781cfbd8b0a7f7c27196dcf6233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gundars=20M=C4=93ness?= Date: Sun, 15 Mar 2015 02:32:47 +0200 Subject: [PATCH 11/14] Update install.sh --- install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.sh b/install.sh index c9ea99c..ce2a9e2 100755 --- a/install.sh +++ b/install.sh @@ -5,7 +5,7 @@ source ~/.bashscripts/lib/commons.sh cd ${gConf[dirInstall]} git pull origin master -SCRIPTS=(gcobranch gitmerge guorigin buildenv gitpress git-branch-restore git-check-changes) +SCRIPTS=(gcobranch gitmerge gitscan buildenv gitpress) for SCRIPT in ${SCRIPTS[@]} do if [ ! -h ${gConf[dirSymlinks]}/$SCRIPT ]; then From da13762e7ac072eaf9e90ee3467122a7d0af8cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gundars=20M=C4=93ness?= Date: Sun, 15 Mar 2015 02:54:50 +0200 Subject: [PATCH 12/14] Update gcobranch.sh --- scripts/gcobranch.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/gcobranch.sh b/scripts/gcobranch.sh index 3b41c53..04695e9 100755 --- a/scripts/gcobranch.sh +++ b/scripts/gcobranch.sh @@ -7,7 +7,7 @@ erroCount=0 minArgsCount=2 if [ $# -lt $minArgsCount ]; then - messageError "Received $# arguments, only ${allowedArgsCount} allowed!" + messageError "Received $# arguments, only ${minArgsCount} allowed!" message "Syntax: gcobranch [branch] [dir] [dir]..." messageExit fi @@ -49,4 +49,4 @@ do fi done -messageExit \ No newline at end of file +messageExit From 83f59a2c0e47fab3b69217535ad98a8f3858a9de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gundars=20M=C4=93ness?= Date: Sun, 15 Mar 2015 02:58:00 +0200 Subject: [PATCH 13/14] Update commons.sh --- lib/commons.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/commons.sh b/lib/commons.sh index 1ac3265..305b3e5 100644 --- a/lib/commons.sh +++ b/lib/commons.sh @@ -137,7 +137,12 @@ function gitCheckoutBranchWithOrigin { messageError "Branch ${branchToCheckout} does not exist in origin" fi fi + else + if [[ "${branchToCheckout}" != $(gitGetCurrentBranch) ]]; then + messageError "Failed to check out ${branchToCheckout}" + fi fi + } function gitLastCommit { @@ -151,4 +156,4 @@ function getRandomErrorMascot { function rand { printf $(( $1 * RANDOM / 32767 )) -} \ No newline at end of file +} From bc83402f7cf8662d62837db8a56552214e0f0604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gundars=20M=C4=93ness?= Date: Sun, 15 Mar 2015 02:59:14 +0200 Subject: [PATCH 14/14] Update gcobranch.sh --- scripts/gcobranch.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/gcobranch.sh b/scripts/gcobranch.sh index 04695e9..dc080bc 100755 --- a/scripts/gcobranch.sh +++ b/scripts/gcobranch.sh @@ -7,8 +7,8 @@ erroCount=0 minArgsCount=2 if [ $# -lt $minArgsCount ]; then - messageError "Received $# arguments, only ${minArgsCount} allowed!" - message "Syntax: gcobranch [branch] [dir] [dir]..." + messageError "Received $# arguments, at least ${minArgsCount} required!" + message "Syntax: gcobranch {branch} {dir1} [{dir2} {dir3}...]" messageExit fi