diff --git a/.git_pre_commit_hook.sh b/.git_pre_commit_hook.sh index 49b9117e..505b5f5e 100755 --- a/.git_pre_commit_hook.sh +++ b/.git_pre_commit_hook.sh @@ -18,4 +18,10 @@ then exit 1 fi +if [ $BRANCH_NAME == "public_beta" ] +then + echo -e $ABORT "Commiting to public_beta is prohibited." + exit 1 +fi + exit 0 diff --git a/script/git-publish.sh b/script/git-publish.sh index f93ca1e1..33cd0e27 100755 --- a/script/git-publish.sh +++ b/script/git-publish.sh @@ -6,6 +6,15 @@ INFO="\033[33mInfo:\033[00m" echo -e $INFO "Checking index" +#set branch to commit to +if [[ $# -gt 0 ]] +then + PUBLISH_TO_BRANCH=$1 +else + PUBLISH_TO_BRANCH="master" +fi +echo -e $INFO "branch to commit: $PUBLISH_TO_BRANCH" + # check clean index if [[ -n $(git diff HEAD) ]] then @@ -25,44 +34,44 @@ then exit 1 fi -# check if on master -if [ $BRANCH_NAME == "master" ] +# check if on $PUBLISH_TO_BRANCH +if [ $BRANCH_NAME == $PUBLISH_TO_BRANCH ] then - echo -e $ABORT "You are on master. Use this script only from a feature branch." + echo -e $ABORT "You are on $PUBLISH_TO_BRANCH. Use this script only from a feature branch." exit 1 fi -git checkout -q master +git checkout -q $PUBLISH_TO_BRANCH git remote update &>/dev/null -echo -e $INFO "Checking master" +echo -e $INFO "Checking $PUBLISH_TO_BRANCH" -# check if master is up to date -if [[ -n $(git diff origin/master) ]] +# check if $PUBLISH_TO_BRANCH is up to date +if [[ -n $(git diff origin/$PUBLISH_TO_BRANCH) ]] then - echo -e $ABORT "Your master is not up-to-date. Run 'git-sync-master.sh' to update master and rebase $BRANCH_NAME." + echo -e $ABORT "Your $PUBLISH_TO_BRANCH is not up-to-date. Run 'git-sync.sh' to update $PUBLISH_TO_BRANCH and rebase $BRANCH_NAME." git checkout -q $BRANCH_NAME exit 1 fi -MASTER_HEAD_COMMIT=$(git rev-parse HEAD) -BASE_COMMIT=$(git merge-base $BRANCH_NAME master) +PUBLIC_BRANCH_HEAD_COMMIT=$(git rev-parse HEAD) +BASE_COMMIT=$(git merge-base $BRANCH_NAME $PUBLISH_TO_BRANCH) git checkout -q $BRANCH_NAME BRANCH_HEAD_COMMIT=$(git rev-parse HEAD) # check if feature has commits -if [ $BRANCH_HEAD_COMMIT == $MASTER_HEAD_COMMIT ] +if [ $BRANCH_HEAD_COMMIT == $PUBLIC_BRANCH_HEAD_COMMIT ] then echo -e $ABORT "Your feature branch has no commits." exit 1 fi -# check if feature is not behind master -if [ $BASE_COMMIT != $MASTER_HEAD_COMMIT ] +# check if feature is not behind $PUBLIC_BRANCH_HEAD_COMMIT +if [ $BASE_COMMIT != $PUBLIC_BRANCH_HEAD_COMMIT ] then - echo -e $ABORT "Your feature branch is behind master. Use 'git rebase master' to update your feature branch." + echo -e $ABORT "Your feature branch is behind $PUBLISH_TO_BRANCH. Use 'git rebase $PUBLISH_TO_BRANCH' to update your feature branch." exit 1 fi @@ -87,19 +96,19 @@ then exit 1 fi -echo -e $INFO "Rebasing on master" -git checkout -q master +echo -e $INFO "Rebasing on $PUBLISH_TO_BRANCH" +git checkout -q $PUBLISH_TO_BRANCH git rebase -q $PUBLISH_BRANCH_NAME git branch -q -d $PUBLISH_BRANCH_NAME -echo -e $INFO "Uploading to origin master" -git push origin master +echo -e $INFO "Uploading to origin $PUBLISH_TO_BRANCH" +git push origin $PUBLISH_TO_BRANCH if [ $? != 0 ] then git reset --hard -q HEAD^ git checkout -q $BRANCH_NAME - echo -e $ABORT "Pushing to origin master has failed, there are new remote changes. Run 'git-sync-master.sh' to update master and rebase $BRANCH_NAME." + echo -e $ABORT "Pushing to origin $PUBLISH_TO_BRANCH has failed, there are new remote changes. Run 'git-sync.sh' to update $PUBLISH_TO_BRANCH and rebase $BRANCH_NAME." exit 1 fi diff --git a/script/git-sync-master.sh b/script/git-sync-master.sh deleted file mode 100755 index 9aaecd9a..00000000 --- a/script/git-sync-master.sh +++ /dev/null @@ -1,96 +0,0 @@ -#!/bin/bash - -ABORT="\033[31mAbort:\033[00m" -ALERT="\033[31mAlert:\033[00m" -SUCCESS="\033[32mSuccess:\033[00m" -INFO="\033[33mInfo:\033[00m" - -BRANCH_NAME=$(git symbolic-ref -q --short HEAD) - -# check if on branch -echo -e $INFO "Checking on branch" - -if [ -z $BRANCH_NAME ] -then - echo -e $ABORT "You are not on any branch." - exit 1 -fi - -# check clean index -echo -e $INFO "Checking index" - -if [[ -n $(git diff HEAD) ]] -then - echo -e $ABORT "You have uncommited changes." - exit 1 -fi - -# switch to master -if [ $BRANCH_NAME != 'master' ] -then - echo -e $INFO "Switching to master" - git checkout -q master - - if [ $? != 0 ] - then - echo -e $ABORT "Switching to master failed." - exit 1 - fi -fi - -# fetch remote changes -echo -e $INFO "Fetching changes" -git fetch origin master --tags - -if [ $? != 0 ] -then - echo -e $ABORT "Fetching from origin master failed" - git checkout -q "$BRANCH_NAME" - exit 1 -fi - -# rebase master to origin/master -if [[ -z $(git diff origin/master) ]] -then - echo -e $SUCCESS "Your master is up-to-date" -else - echo -e $INFO "Rebasing to origin/master" - git rebase origin/master - - if [ $? != 0 ] - then - echo -e $ALERT "Rebasing on master caused conflicts. This should never happen. Follow the instructions above to resolve them then use 'git push origin master' to update the remote." - exit 1 - fi - - echo -e $SUCCESS "Updated master" -fi - -if [ $BRANCH_NAME == 'master' ] -then - exit 0 -fi - -# switch to branch -echo -e $INFO "Switching back to $BRANCH_NAME" -git checkout -q "$BRANCH_NAME" - -if [ $? != 0 ] -then - echo -e $ABORT "Switching back to $BRANCH_NAME failed." - exit 1 -fi - -# rebase branch to master -echo -e $INFO "Rebasing $BRANCH_NAME to master" -git rebase master - -if [ $? != 0 ] -then - echo -e $ALERT "Rebasing $BRANCH_NAME to master caused conflicts. Follow the instructions above to resolve them." - exit 1 -else - echo -e $SUCCESS "$BRANCH_NAME is now up-to-date." -fi - -exit 0 diff --git a/script/git-sync.sh b/script/git-sync.sh new file mode 100755 index 00000000..7d28faab --- /dev/null +++ b/script/git-sync.sh @@ -0,0 +1,105 @@ +#!/bin/bash + +ABORT="\033[31mAbort:\033[00m" +ALERT="\033[31mAlert:\033[00m" +SUCCESS="\033[32mSuccess:\033[00m" +INFO="\033[33mInfo:\033[00m" + +BRANCH_NAME=$(git symbolic-ref -q --short HEAD) + +#set branch to sync with +if [[ $# -gt 0 ]] +then + SYNC_BRANCH=$1 +else + SYNC_BRANCH="master" +fi +echo -e $INFO "branch to commit: $SYNC_BRANCH" + +# check if on branch +echo -e $INFO "Checking on branch" + +if [ -z $BRANCH_NAME ] +then + echo -e $ABORT "You are not on any branch." + exit 1 +fi + +# check clean index +echo -e $INFO "Checking index" + +if [[ -n $(git diff HEAD) ]] +then + echo -e $ABORT "You have uncommited changes." + exit 1 +fi + +# switch to $SYNC_BRANCH +if [ $BRANCH_NAME != '$SYNC_BRANCH' ] +then + echo -e $INFO "Switching to $SYNC_BRANCH" + git checkout -q $SYNC_BRANCH + + if [ $? != 0 ] + then + echo -e $ABORT "Switching to $SYNC_BRANCH failed." + exit 1 + fi +fi + +# fetch remote changes +echo -e $INFO "Fetching changes" +git fetch origin $SYNC_BRANCH --tags + +if [ $? != 0 ] +then + echo -e $ABORT "Fetching from origin $SYNC_BRANCH failed" + git checkout -q "$BRANCH_NAME" + exit 1 +fi + +# rebase $SYNC_BRANCH to origin/$SYNC_BRANCH +if [[ -z $(git diff origin/$SYNC_BRANCH) ]] +then + echo -e $SUCCESS "Your $SYNC_BRANCH is up-to-date" +else + echo -e $INFO "Rebasing to origin/$SYNC_BRANCH" + git rebase origin/$SYNC_BRANCH + + if [ $? != 0 ] + then + echo -e $ALERT "Rebasing on $SYNC_BRANCH caused conflicts. This should never happen. Follow the instructions above to resolve them then use 'git push origin master' to update the remote." + exit 1 + fi + + echo -e $SUCCESS "Updated $SYNC_BRANCH" +fi + +if [ $BRANCH_NAME == '$SYNC_BRANCH' ] +then + exit 0 +fi + +# switch to branch +echo -e $INFO "Switching back to $BRANCH_NAME" +git checkout -q "$BRANCH_NAME" + +if [ $? != 0 ] +then + echo -e $ABORT "Switching back to $BRANCH_NAME failed." + exit 1 +fi + +# rebase branch to $SYNC_BRANCH +echo -e $INFO "Rebasing $BRANCH_NAME to $SYNC_BRANCH" +git rebase $SYNC_BRANCH + +if [ $? != 0 ] +then + echo -e $ALERT "Rebasing $BRANCH_NAME to $SYNC_BRANCH caused conflicts. Follow the instructions above to resolve them." + exit 1 +else + echo -e $SUCCESS "$BRANCH_NAME is now up-to-date." +fi + +exit 0