diff --git a/.gitignore b/.gitignore index 40b7eda..127d860 100755 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /nbproject/ +/.idea/ 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}` diff --git a/buildenv.sh b/buildenv.sh deleted file mode 100755 index c1ec2d0..0000000 --- a/buildenv.sh +++ /dev/null @@ -1,77 +0,0 @@ -#!/bin/bash -# Change build number on environmnet -# Syntax: $ buildenv {env} {branch} {build number} -# {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' -ENV=$1 -ENVREGEX="^w{3}_.*_[a-zA-Z]{2,4}$" -BRANCH=$2 -BRANCHREGEX="^(dev|test|staging)$" -BNR=$3 -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 - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Incorrect arguments specified${CNORMAL}\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}" - exit 0 -fi - -if ! [[ $ENV =~ $ENVREGEX ]]; then - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Bad environment${CNORMAL}\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 "{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 "{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 "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}" - exit 1; -fi - -STARTDIR=${PWD} -if ! [ -d "$TMPDIR" ]; then - git clone $REPO $TMPDIR -fi -cd $TMPDIR -git pull origin master -FILE=environment_definition/${BRANCH}1/environment_definition.def -sed -i "s/^${ENV}.*/${ENV} = ${BNR}/g" $FILE -git add $FILE -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 -echo -e "\n${CERROR} ${DIFFR} ${CNORMAL}" -echo -e "${CSUCCESS} ${DIFFG} ${CNORMAL}" diff --git a/config/global.sh b/config/global.sh new file mode 100644 index 0000000..8f2bfa9 --- /dev/null +++ b/config/global.sh @@ -0,0 +1,17 @@ +#!/bin/bash +#Global configuration +declare -A gConf +gConf[dirStart]=${PWD} +gConf[dirInstall]=~/.bashscripts +gConf[dirSymlinks]=/usr/local/bin +gConf[dirTmp]=/.bashscripts/tmp +gConf[colN]='\e[00m' +gConf[colH]='\e[01;36m' +gConf[colE]='\033[31m' +gConf[colS]='\e[32m' + +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/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/gcobranch.sh b/gcobranch.sh deleted file mode 100755 index 5c70c51..0000000 --- a/gcobranch.sh +++ /dev/null @@ -1,73 +0,0 @@ -#!/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<__.|_|-|_|'; -ERRCOUNT=0 - -if [ $# -lt 2 ] - then - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Incorrect arguments specified${CNORMAL}" - echo "Usage: gcobranch [branch] [dir] [dir]..." - exit 0 -fi - -branch=$1 -directories="${@:2}"; - -for dir in $directories -do - if [ -d "$dir" ]; then - cd $dir>/dev/null; - echo -e "Scanning ${PWD}"; - cd ->/dev/null - - for d in `find $dir -name .git -type d`; do - cd $d/.. > /dev/null - echo -e "\n${CHIGHLIGHT}Repo: `pwd`$CNORMAL" - CURRENT=`git rev-parse --abbrev-ref HEAD`; - echo -e "Current branch$CNORMAL: ${CURRENT}" - TXTCHECKOUT="No need to checkout" - - if [[ "${CURRENT}" != "${branch}" ]]; then - TXTCHECKOUT=$((git checkout $branch) 2>&1); - if [[ "${TXTCHECKOUT}" =~ 'did not match any file' ]]; then - echo "Creating new branch $branch"; - TXTCHECKOUT=$((git checkout -b $branch origin/$branch) 2>&1); - fi - else - lcommit=`git log -n 1 --pretty=format:"%H, %cn : %s"`; - echo -e "Already on ${branch}" - echo -e "Last commit: ${lcommit}" - fi - - 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" - 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}" - fi - - cd - > /dev/null - done - else - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Directory $dir does not exist ${CNORMAL}" - fi -done - -if [[ "${ERRCOUNT}" == "0" ]] ; then - finalcol=${CSUCCESS} -else - finalcol=${CERROR} -fi - -echo -e "\n${finalcol}Done with ${ERRCOUNT} errors${CNORMAL}" - - diff --git a/git-branch-restore.sh b/git-branch-restore.sh deleted file mode 100755 index 96e545a..0000000 --- a/git-branch-restore.sh +++ /dev/null @@ -1,71 +0,0 @@ -#!/bin/bash -# Restore mising test and development branches in repos founded in dirs -# Syntax: $ <>.sh {dir1} [{dir2} {dir3}... - -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 "Usage: gcobranch [branch] [dir] [dir]..." - exit 0 -fi - -directories="${@:1}"; -echo $directories - -for dir in $directories do - if [ -d "$dir" ]; then - cd $dir>/dev/null; - echo -e "Scanning ${PWD}"; - cd ->/dev/null - - for d in `find $dir -name .git -type d`; do - cd $d/.. > /dev/null - echo -e "\n${CHIGHLIGHT}Repo: `pwd`$CNORMAL" - CURRENT=`git rev-parse --abbrev-ref HEAD`; - git checkout master - git fetch --all - - TESTBR=`git branch | grep "test" | tr -d '* '` - DEVBR=`git branch | grep "development" | tr -d '* '` - - RTESTBR=`git branch -r | grep -w 'origin/test' | tr -d " origin/"` - RDEVBR=`git branch -r | grep -w 'origin/development' | tr -d " origin/"` - - echo -e "===Current branch$CNORMAL: ${CURRENT}" - echo -e "===Test: (${TESTBR}) on remote (${RTESTBR})" - echo -e "===Dev : (${DEVBR}) on remote (${RDEVBR})" - - if [[ "${RTESTBR}" != "" ]]; then - if [[ "${TESTBR}" != 'test' ]]; then - echo -e "Creating local Test" - git checkout -b test origin/test - fi - fi - - if [[ "${RDEVBR}" != "" ]]; then - if [[ "${DEVBR}" != 'development' ]]; then - echo -e "Creating local Development" - git checkout -b development origin/development - fi - fi - - cd - > /dev/null - done - else - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Directory $dir does not exist ${CNORMAL}" - fi -done - -if [[ "${ERRCOUNT}" == "0" ]] ; then - finalcol=${CSUCCESS} -else - finalcol=${CERROR} -fi - -echo -e "\n${finalcol}Done with ${ERRCOUNT} errors${CNORMAL}" diff --git a/git-check-changes.sh b/git-check-changes.sh deleted file mode 100755 index 246140a..0000000 --- a/git-check-changes.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/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<__.|_|-|_|'; -ERRCOUNT=0 -CHANGPENDING='' -# ERRCOUNT=$[ERRCOUNT + 1] -if [ $# -lt 1 ] - then - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Incorrect arguments specified${CNORMAL}" - echo "Usage: git-check-status [dir] [dir]..." - exit 0 -fi - -directories="${@:1}"; - -for dir in $directories -do - if [ -d "$dir" ]; then - cd $dir>/dev/null; - echo -e "Scanning ${PWD}"; - cd ->/dev/null - - for d in `find $dir -name .git -type d`; do - cd $d/.. > /dev/null - CHANGE=`git status --porcelain` - - if [ -n "$CHANGE" ]; then - PWD=`pwd ` - echo -e "\n${CHIGHLIGHT}${PWD}" - echo -e "\n${CERROR}${CHANGE}" - - fi - cd - > /dev/null - done - else - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Directory $dir does not exist ${CNORMAL}" - fi -done diff --git a/gitmerge.sh b/gitmerge.sh deleted file mode 100755 index f49c1d8..0000000 --- a/gitmerge.sh +++ /dev/null @@ -1,77 +0,0 @@ -#!/bin/bash -# 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<__.|_|-|_|' -ERRCOUNT=0 -ALLOWEDARGS=1 -OPTP=false -PRBRANCHES=(test master) - -while getopts ":p" opt; do - case $opt in - p) - OPTP=true - ALLOWEDARGS=$[ALLOWEDARGS + 1] - ;; - \?) - echo "Invalid option: -$OPTARG" >&2 - ;; - esac -done - -if [ $# -gt $ALLOWEDARGS ]; then - echo -e "${CERROR}${ERRMASCOT}ERROR: Received $# arguments, only ${ALLOWEDARGS} allowed! Syntax: gitmerge [options] [branch] ${CNORMAL}" - exit 1 -fi - -currentBranch="$(b=$(git symbolic-ref -q HEAD); { [ -n "$b" ] && echo ${b##refs/heads/}; } || echo HEAD)" -mergeBranch="development" -if [[ $1 && "$OPTP" = false ]]; then - mergeBranch=$1 -elif [[ $1 && "$OPTP" = true && $2 ]]; then - mergeBranch=$2 -fi -echo -e "Merging ${CHIGHLIGHT}${currentBranch}${CNORMAL} with ${CHIGHLIGHT}${mergeBranch}${CNORMAL}" - -CHECKOUT=$((git checkout $mergeBranch) 2>&1) -if [[ "${CHECKOUT}" =~ "error: pathspec" ]]; then - echo -e "No such branch ${mergeBranch}. Creating..." - $((git checkout -b $mergeBranch origin/$mergeBranch) 2>&1) - CURRENT=$((git rev-parse --abbrev-ref HEAD) 2>&1) - if [[ "${CURRENT}" != "${mergeBranch}" ]]; then - echo -e "Error: could not switch to ${mergeBranch}. ABORTING" - exit 1 - fi -fi -git pull origin $mergeBranch -git merge --no-ff $currentBranch -git push origin $mergeBranch -git checkout $currentBranch - -if [ "$OPTP" = true ] ; then - currentOrigin=`git config --get remote.origin.url`; - if [[ "${currentOrigin}" =~ "x-oauth-basic" ]]; then - cleanedOrigin=(`echo $currentOrigin | tr '@' "\n"`) - newOrigin="https://${cleanedOrigin[1]}" - if [[ "${newOrigin}" =~ "https://github.com/".*".git" ]] ; then - currentOrigin=$newOrigin - fi - elif [[ "${currentOrigin}" =~ "git@github.com:" ]]; then - cleanedOrigin=(`echo $currentOrigin | tr ':' "\n"`) - newOrigin="https://github.com/${cleanedOrigin[1]}" - if [[ "${newOrigin}" =~ "https://github.com/".*".git" ]] ; then - currentOrigin=$newOrigin - fi - fi - for prBranch in ${PRBRANCHES[@]}; do - echo -e "${CHIGHLIGHT}PR ${prBranch}:${CNORMAL} ${currentOrigin/%.git//compare/${prBranch}...${currentBranch}}" - done -fi - -exit 0 diff --git a/gitpress.sh b/gitpress.sh deleted file mode 100755 index 5e0065a..0000000 --- a/gitpress.sh +++ /dev/null @@ -1,56 +0,0 @@ -#!/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<__.|_|-|_|' -ALLOWEDARGS=2 -OPTF=false -OPTP=false -UNSAFEBRANCHES=(development test master) - -while getopts ":fp" opt; do - case $opt in - f) - OPTF=true - ALLOWEDARGS=$[ALLOWEDARGS + 1] - ;; - p) - OPTP=true - ALLOWEDARGS=$[ALLOWEDARGS + 1] - ;; - \?) - echo "Invalid option: -$OPTARG" >&2 - ;; - esac -done - -if [ $# -gt $ALLOWEDARGS ]; then - echo -e "${CERROR}${ERRMASCOT}ERROR: Received $# arguments, only ${ALLOWEDARGS} allowed! Syntax: gitpress [options]${CNORMAL}" - exit 1 -fi - -CURRENTBRANCH="$(b=$(git symbolic-ref -q HEAD); { [ -n "$b" ] && echo ${b##refs/heads/}; } || echo HEAD)" -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}" - exit 1 - fi - done -fi - -git reset $(git commit-tree HEAD^{tree} -m "Compress") - -SAFEPUSH="git push origin +${CURRENTBRANCH}" -if [[ "$OPTP" = true ]]; then - $(${SAFEPUSH}) -else - echo -e "Use command '${SAFEPUSH}' to overwrite remote ${CURRENTBRANCH} branch" -fi - -exit 0 diff --git a/guorigin.sh b/guorigin.sh deleted file mode 100755 index 50af870..0000000 --- a/guorigin.sh +++ /dev/null @@ -1,83 +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}...] - -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 "Usage: guorigin {dir1} [{dir2} {dir3}...]" - exit 0 -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 - cd $d/.. > /dev/null - echo -e "\n${CHIGHLIGHT}Updating `pwd`${CNORMAL}" - CURRENT=`git config --get remote.origin.url`; - 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 - `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}" - else - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${CERROR}ERROR: could not change origin${CNORMAL}" - fi - else - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${CERROR}ERROR: ${NEW} is not a valid repository${CNORMAL}" - 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`; - if [[ "${NEW}" =~ ${NEWCHANGED} ]] ; then - echo -e "${CSUCCESS}Origin changed to ${NEWCHANGED}${CNORMAL}" - else - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${CERROR}ERROR: could not change origin${CNORMAL}" - fi - else - ERRCOUNT=$[ERRCOUNT + 1] - echo -e "${CERROR}ERROR: ${NEW} is not a valid repository${CNORMAL}" - fi - else - echo -e "Origin OK" - fi - cd - > /dev/null - done - else - echo -e "\n${CERROR}${ERRMASCOT}\nERROR: Directory $dir does not exist ${CNORMAL}" - fi -done - -if [[ "${ERRCOUNT}" == "0" ]] ; then - finalcol=${CSUCCESS} -else - finalcol=${CERROR} -fi - -echo -e "\n${finalcol}Completed with ${ERRCOUNT} errors${CNORMAL}" diff --git a/install.sh b/install.sh index a624486..ce2a9e2 100755 --- a/install.sh +++ b/install.sh @@ -1,30 +1,28 @@ #!/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 -SYMLINKDIR=/usr/local/bin -cd $INSTDIR +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 [ ! -f ${SYMLINKDIR}/${SCRIPT} ]; then + if [ ! -h ${gConf[dirSymlinks]}/$SCRIPT ]; then echo "Installing $SCRIPT" - sudo ln -s $INSTDIR/${SCRIPT}.sh ${SYMLINKDIR}/${SCRIPT} + sudo ln -s ${gConf[dirInstall]}/scripts/$SCRIPT.sh ${gConf[dirSymlinks]}/$SCRIPT else - SYMLINK=$(readlink -f ${SYMLINKDIR}/${SCRIPT}) - if [[ $SYMLINK != $INSTDIR/${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/${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/lib/commons.sh b/lib/commons.sh new file mode 100644 index 0000000..305b3e5 --- /dev/null +++ b/lib/commons.sh @@ -0,0 +1,159 @@ +#!/bin/bash +# Container of all common functions +source ~/.bashscripts/config/global.sh +source ~/.bashscripts/config/user.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[colS]}$1${gConf[colN]}" + fi +} + +function messageHighlight { + if [ -z "$1" ]; then + echo "messageHightlight param 1 error" + else + echo -e "${gConf[colH]}$1${gConf[colN]}" + fi +} + +function messageError { + erroCount=$[erroCount + 1] + if [ -z "$1" ]; then + echo "messageError param 1 error" + else + if [ "$2" == false ]; then + local em='' + local er='\n' + else + local em=$(getRandomErrorMascot) + local er='ERROR: ' + fi + echo -e "${gConf[colE]}${em}${er}$1${gConf[colN]}" + fi +} + +function messageExit { + message "\n" + if [[ "${erroCount}" == "0" ]] ; then + messageSuccess "Completed without errors" + exit 0 + else + messageError "Completed with ${erroCount} errors" false + exit 1 + fi +} + +function exitIfErrors { + if ! [[ $erroCount =~ 0 ]]; then + messageExit + fi +} + +#pass an array of all orginal args except 0 ! +function removeOptionsFromArguments { + argumentsWithoutOptions=() + for arg in "$@" + do + if [[ ${arg:0:1} != "-" ]]; then + argumentsWithoutOptions+=($arg) + fi + done +} + +function gitGetCurrentOrigin { + git config --get remote.origin.url +} + +function gitSetCurrentOrigin { + git remote set-url origin $1 +} + +function gitGetCurrentBranch { + #$(b=$(git symbolic-ref -q HEAD); { [ -n "$b" ] && echo ${b##refs/heads/}; } || echo HEAD) + git rev-parse --abbrev-ref HEAD 2>&1 +} + +function findAllGitDrectories { + find $1 -name .git -type d +} + +function gitFetchAll { + 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 + 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 + local currentOrigin=$newOrigin + fi + fi + + echo $currentOrigin +} + +function gitDiffDeleted { + git diff HEAD^ HEAD --color=always|perl -wlne 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/' +} + +function gitDiffAdded { + git diff HEAD^ HEAD --color=always|perl -wlne 'print $1 if /^\e\[31m-(.*)\e\[m$/' +} + +function gitCheckoutBranchWithOrigin { + local branchToCheckout=$1 + checkout=$((git checkout $branchToCheckout) 2>&1) + if [[ "${checkout}" =~ "error: pathspec" ]]; then + message "No such branch ${branchToCheckout}. Creating..." + $((git checkout -b $branchToCheckout origin/$branchToCheckout) &> /dev/null) + currentBranch=$(gitGetCurrentBranch) + if [[ "${currentBranch}" != "${branchToCheckout}" ]]; then + $((git fetch --all) &> /dev/null) + $((git fetch origin $mergeBranch:branchToCheckout) &> /dev/null) + currentBranch=$(gitGetCurrentBranch) + if [[ "${currentBranch}" != "${branchToCheckout}" ]]; then + 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 { + local lc=$((git log -n 1 --pretty=format:"%H, %cn : %s") 2>&1) + echo $lc +} + +function getRandomErrorMascot { + printf $'%s' "${errorMascots[$(($(rand "${#errorMascots[*]}")+1))]}" +} + +function rand { + printf $(( $1 * RANDOM / 32767 )) +} diff --git a/scripts/buildenv.sh b/scripts/buildenv.sh new file mode 100755 index 0000000..18b988c --- /dev/null +++ b/scripts/buildenv.sh @@ -0,0 +1,66 @@ +#!/bin/bash +# Change build number on environmnet +# Syntax: $ buildenv {env} {branch} {build number} +# { env} syntax: www_domain_extension +# {branch} syntax: dev | test | staging +# {build number} syntax: integer 1-5 digits long +source ~/.bashscripts/lib/commons.sh + +erroCount=0 +minArgsCount=3 +env=$1 +envRegex="^w{3}_.*_[a-zA-Z]{2,4}$" +branch=$2 +branchRegex="^(dev|test|staging)$" +buildNo=$3 +buildNoRegex="[0-9]{1,5}" + +if [ $# -lt $minArgsCount ]; then + messageError "Incorrect arguments specified" + message "Syntax: buildenv {env} {branch} {build number}" + message "{env} syntax: www_domain_extension" + message "{branch} syntax: dev | test | staging" + messageExit +fi + +if ! [[ $env =~ $envRegex ]]; then + messageError "Bad environment" + message "{env} syntax: www_domain_extension" +fi + +if ! [[ $branch =~ $branchRegex ]]; then + messageError "Bad branch" + message "{branch} syntax: dev | test | staging" +fi + +if ! [[ $buildNo =~ $buildNoRegex ]]; then + messageError "Bad build number" + message "{build number} syntax: integer 1-5 digits long" +fi + +if [[ uConf[enrichedRepository] =~ "https/link/to/encriched.git" ]]; then + messageError "Bad repository name" + message "Please change line 4 'https/link/to/encriched.git' in file ~/.bashscripts/config/user.sh to valid link to enriched github repository" +fi + +exitIfErrors + +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} = ${buildNo}/g" $FILE +git add $FILE +git commit -m "${env} to ${buildNo}" +git push origin master +cd gConf[dirStart] +message "\n${gConf[colE]} ${gitDiffDeleted} ${gConf[colN]}" +message "${gConf[colS]} ${gitDiffAdded} ${gConf[colN]}" + +messageExit diff --git a/scripts/gcobranch.sh b/scripts/gcobranch.sh new file mode 100755 index 0000000..dc080bc --- /dev/null +++ b/scripts/gcobranch.sh @@ -0,0 +1,52 @@ +#!/bin/bash +# Checks out and pulls specified {branch} in all git repos found in each of specified {dir} +# Syntax: $ gcobranch {branch} {dir1} [{dir2} {dir3}...] +source ~/.bashscripts/lib/commons.sh + +erroCount=0 +minArgsCount=2 + +if [ $# -lt $minArgsCount ]; then + messageError "Received $# arguments, at least ${minArgsCount} required!" + message "Syntax: gcobranch {branch} {dir1} [{dir2} {dir3}...]" + messageExit +fi + +checkoutBranch=$1 +directories="${@:2}"; + +for dir in $directories +do + if [ -d "$dir" ]; then + cd $dir>/dev/null; + message "\nScanning ${PWD}" + cd ->/dev/null + for d in `findAllGitDrectories $dir`; do + cd $d/.. > /dev/null + pwd=$(pwd) + messageHighlight "\nRepo: ${pwd}" + currentBranch=$(gitGetCurrentBranch) + message "Current branch: ${currentBranch}" + if [[ "${currentBranch}" != "${checkoutBranch}" ]]; then + gitCheckoutBranchWithOrigin $checkoutBranch + checkedout=" checked out and" + else + message "Already on ${checkoutBranch}" + checkedout="" + fi + + currentBranch=$(gitGetCurrentBranch) + if [[ "${currentBranch}" == "${checkoutBranch}" ]]; then + pull=$((git pull origin $checkoutBranch) 2>&1) + lastCommit=$(gitLastCommit) + message "Last commit: ${lastCommit}" + messageSuccess "${checkoutBranch}${checkedout} pulled" + fi + cd - > /dev/null + done + else + messageError "Directory ${dir} does not exist" + fi +done + +messageExit diff --git a/scripts/gitmerge.sh b/scripts/gitmerge.sh new file mode 100755 index 0000000..4b389d2 --- /dev/null +++ b/scripts/gitmerge.sh @@ -0,0 +1,51 @@ +#!/bin/bash +# Merges current branch with {branch}, pushes changes to origin +# If no {branch} is specified, "development" is used +# Syntax: $ gitmerge [options] [branch] +source ~/.bashscripts/lib/commons.sh + +erroCount=0 +optP=false +prBranches=(test master) + +while getopts ":p" opt; do + case $opt in + p) + optP=true + ;; + \?) + messageError "Invalid option: -$OPTARG" >&2 + messageExit + ;; + esac +done + +currentBranch="$(gitGetCurrentBranch)" +mergeBranch="development" +if [[ $1 && "$optP" = false ]]; then + mergeBranch=$1 +elif [[ $1 && "$optP" = true && $2 ]]; then + mergeBranch=$2 +fi +message "Merging ${gConf[colH]}${currentBranch}${gConf[colN]} with ${gConf[colH]}${mergeBranch}${gConf[colN]}" + +gitCheckoutBranchWithOrigin $mergeBranch +currentBranch=$(gitGetCurrentBranch) +if [[ "${currentBranch}" != "${mergeBranch}" ]]; then + messageExit +fi + +git pull origin $mergeBranch +git merge --no-ff $currentBranch +git push origin $mergeBranch +git checkout $currentBranch + +if [ "$optP" = true ] ; then + origin=$(gitOriginAnyToHttps) + message "\n" + for prBranch in ${prBranches[@]}; do + message "${gConf[colH]}PR ${prBranch}:${gConf[colN]} ${origin/%.git//compare/${prBranch}...${currentBranch}}" + done +fi + +messageExit diff --git a/scripts/gitpress.sh b/scripts/gitpress.sh new file mode 100755 index 0000000..1b4be0d --- /dev/null +++ b/scripts/gitpress.sh @@ -0,0 +1,56 @@ +#!/bin/bash +# compresses all commits in current local branch into single commit +# Syntax: $ gitpress [options] +source ~/.bashscripts/lib/commons.sh + +erroCount=0 +maxArgsCount=0 +optF=false +optP=false +unsafeBranches=(development test master) + +while getopts ":fp" opt; do + case $opt in + f) + optF=true + maxArgsCount=$[maxArgsCount + 1] + ;; + p) + optP=true + maxArgsCount=$[maxArgsCount + 1] + ;; + \?) + messageError "Invalid option: -$OPTARG" >&2 + messageExit + ;; + esac +done + +if [ $# -gt $maxArgsCount ]; then + messageError "Received $# arguments, only ${allowedArgsCount} allowed!" + message "Syntax: gitpress [options]" + exit 1 +fi + +currentBranch="$(gitGetCurrentBranch)" +if [[ "$optF" = false ]]; then + for unsafeBranche in ${unsafeBranches[@]} + do + if [[ ${unsafeBranche} == ${currentBranch} ]]; then + messageError "Use -f option to compress, branch name '${unsafeBranche}' is too generic" + exit 1 + fi + done +fi + +git reset $(git commit-tree HEAD^{tree} -m "Compress") + +safePush="git push origin +${currentBranch}" +if [[ "$optP" = true ]]; then + $(${safePush}) +else + message "Use command '${safePush}' to overwrite remote ${currentBranch} branch" +fi + +messageExit + diff --git a/scripts/gitscan.sh b/scripts/gitscan.sh new file mode 100755 index 0000000..07b1ae0 --- /dev/null +++ b/scripts/gitscan.sh @@ -0,0 +1,104 @@ +#!/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 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 +optC=false +optM=false + +while getopts "ocm" opt; do + case $opt in + o) + optO=true + ;; + c) + optC=true + ;; + m) + optM=true + ;; + \?) + messageError "Invalid option: -$OPTARG" >&2 + messageExit + ;; + esac +done + +removeOptionsFromArguments "$@" + +if [ ${#argumentsWithoutOptions[@]} -lt $minArgsCount ]; then + messageError "Incorrect arguments specified" + message "Syntax: gitscan [options] {dir1} [{dir2} {dir3}...]" + messageExit +fi + +for dir in "${argumentsWithoutOptions[@]}" +do + if [ -d "$dir" ]; then + cd $dir>/dev/null; + cd ->/dev/null + for d in `findAllGitDrectories $dir`; do + cd $d/.. > /dev/null + pwd=$(pwd) + messageHighlight "\nScanning ${PWD}" + if [[ "$optO" = true ]]; then + updateOriginUrl + fi + if [[ "$optC" = true ]]; then + createBranches "development" "test" + fi + if [[ "$optM" = true ]]; then + listAllModifiedFiles $pwd + fi + cd - > /dev/null + done + else + messageError "Directory ${dir} does not exist" + fi +done + +messageExit