b223f90ddd4d0020834dedcb9cba6118a2bd1cb3
[infra/cicd.git] / jjb / opendev / rebase-jobs.sh
1 #!/bin/bash
2 set -o errexit
3 set -o nounset
4 set -o pipefail
5
6 cd $WORKSPACE
7 # When the GERRIT_BRANCH is not set the rebase job wasn't triggered by merge
8 # but it's executed as part of timer. In this case we will make sure that all
9 # branches from remote are synced with Nordix Gerrit.
10 if [[ -z "${GERRIT_BRANCH:-}" ]] ; then
11     echo "-- Fetch all branches from OpenDev for project: ${PROJECT} --"
12     REMOTE_BRANCHES=$(git branch -r | grep -v 'HEAD -> ' | awk '/origin/ {gsub("origin/","");print}')
13     for branch in ${REMOTE_BRANCHES}  ; do
14         git branch -f --track ${branch} origin/${branch}
15     done
16 # The job was triggered by merge on OpenDev. We will make sure that branch set
17 # in GERRIT_BRANCH is in sync with Nordix Gerrit.
18 else
19     echo "-- Fetch branch: ${GERRIT_BRANCH} from OpenDev for project: ${PROJECT} --"
20     REMOTE_BRANCHES=${GERRIT_BRANCH}
21     git branch -f --track ${GERRIT_BRANCH} origin/${GERRIT_BRANCH}
22 fi
23 git fetch origin -v
24 echo "-- Adding new remote $NORDIX_REMOTE for sync --"
25 git remote add nordix $NORDIX_REMOTE
26 git remote -v
27 echo "-- Merging changes from OpenDev remote branches to local branch: --"
28 for branch in ${REMOTE_BRANCHES}  ; do
29     echo "------ Branch name: ${branch}"
30     git checkout ${branch} -q
31     git merge origin/${branch} --ff-only --stat
32 done
33 echo "-- Pushing changes to Nordix Gerrit --"
34 git push nordix --all -v
35 echo "--------------------------------------------------------------------"
36 # Make sure the Nordix is set from Jenkins Job configuration
37 # parameter: NORDIX_REMOTE in next job run
38 if git remote | grep nordix > /dev/null ; then
39     git remote rm nordix
40 fi