+#!/bin/bash
+set -o errexit
+set -o nounset
+set -o pipefail
+
+cd $WORKSPACE
+# Make sure the Nordix is set from Jenkins Job configuration
+# parameter: NORDIX_REMOTE and not from previous job execution
+if git remote | grep nordix > /dev/null ; then
+ git remote rm nordix
+fi
+# When the GERRIT_BRANCH is not set the rebase job wasn't triggered by merge
+# but it's executed as part of timer. In this case we will make sure that all
+# branches from remote are synced with Nordix Gerrit.
+if [[ -z "${GERRIT_BRANCH:-}" ]] ; then
+ echo "-- Fetch all branches from Acumos for project: ${PROJECT} --"
+ REMOTE_BRANCHES=$(git branch -r | grep -v 'HEAD -> ' | awk '/origin/ {gsub("origin/","");print}')
+ for branch in ${REMOTE_BRANCHES} ; do
+ git branch -f --track ${branch} origin/${branch}
+ done
+# The job was triggered by merge on Acumos. We will make sure that branch set
+# in GERRIT_BRANCH is in sync with Nordix Gerrit.
+else
+ echo "-- Fetch branch: ${GERRIT_BRANCH} from Acumos for project: ${PROJECT} --"
+ REMOTE_BRANCHES=${GERRIT_BRANCH}
+ git branch -f --track ${GERRIT_BRANCH} origin/${GERRIT_BRANCH}
+fi
+git fetch origin -v
+echo "-- Adding new remote $NORDIX_REMOTE for sync --"
+git remote add nordix $NORDIX_REMOTE
+git remote -v
+echo "-- Merging changes from Acumos remote branches to local branch: --"
+for branch in ${REMOTE_BRANCHES} ; do
+ echo "------ Branch name: ${branch}"
+ git checkout ${branch} -q
+ git merge origin/${branch} --ff-only --stat
+done
+echo "-- Pushing changes to Nordix Gerrit --"
+git push nordix --all -v
+echo "--------------------------------------------------------------------"