Arthur: tf-sync-repos

Support mirroring multiple branches

Signed-off-by: Arthur She <arthur.she@linaro.org>
Change-Id: I155790affd86b1401fb5e4474570f483e9985e56
diff --git a/ci/tf-sync-repos.sh b/ci/tf-sync-repos.sh
index ce3c134..cd7eb72 100755
--- a/ci/tf-sync-repos.sh
+++ b/ci/tf-sync-repos.sh
@@ -6,8 +6,11 @@
 #
 set -e
 
-declare -A github_repo
-github_repo=(
+# The Gerrit project and Github repo mapping
+# This script will mirror Gerrit_project to Github_repo
+# The format is [Gerrit_project]="Github_repo"
+declare -A github_repos
+github_repos=(
 [TF-A/trusted-firmware-a]="TrustedFirmware-A/trusted-firmware-a"
 [TF-A/tf-a-tests]="TrustedFirmware-A/tf-a-tests"
 [ci/tf-a-ci-scripts]="TrustedFirmware-A/tf-a-ci-scripts"
@@ -28,34 +31,76 @@
 [sandbox/arthur]="arthur-she/sandbox"
 )
 
-if [ -z "${github_repo[${GERRIT_PROJECT}]}" ]; then
+# Mirror branches, mirror Gerrit_Branch to Github_Branch
+# The format is [Gerrit project]="Gerrit_Branch_1:Github_Branch_1 Gerrit_Branch_2:Github_Branch_2"
+declare -A mirror_branches
+mirror_branches=(
+[TF-A/trusted-firmware-a]="master:main"
+[TF-A/tf-a-tests]="master:main"
+[ci/tf-a-ci-scripts]="master:main"
+[TF-M/trusted-firmware-m]="main:main"
+[TF-M/tf-m-tests]="main:main"
+[TF-M/tf-m-tools]="main:main"
+[TF-M/tf-m-extras]="main:main"
+[hafnium/hafnium]="master:main"
+[hafnium/driver/linux]="master:main"
+[hafnium/prebuilts]="master:main"
+[hafnium/project/reference]="master:main"
+[hafnium/third_party/dtc]="master:main"
+[hafnium/third_party/googletest]="master:main"
+[hafnium/third_party/linux]="master:main"
+[ci/hafnium-job-configs]="master:main"
+[ci/hafnium-ci-scripts]="master:main"
+[TS/trusted-services]="main:main integration:integration"
+[sandbox/arthur]="master:main branch-1:gh-branch-1"
+)
+
+[ -d "proj_src" ] && rm -rf proj_src
+
+gerrit_repo="https://review.trustedfirmware.org/${GERRIT_PROJECT}"
+gh_repo=${github_repos[${GERRIT_PROJECT}]}
+
+if [ -z "${gh_repo}" ]; then
     echo "Can not find GitHub repo for \"${GERRIT_PROJECT}\" Gerrit project!"
     exit 1
 fi
 
-cd ${WORKSPACE}
-
-gerrit_repo="https://review.trustedfirmware.org/${GERRIT_PROJECT}"
 echo "Cloning repository ${gerrit_repo}"
 git clone ${gerrit_repo} proj_src
 
 cd proj_src
 
-# Get the Gerrit project default branch, since some use master, and some use main
-def_branch=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
+echo "GitHub repo for \"${GERRIT_PROJECT}\" is \"https://github.com/${gh_repo}\""
 
-echo "GitHub repo: ${github_repo[${GERRIT_PROJECT}]}"
-
-gh_remote="https://${AUTH_TOKEN}@github.com/${github_repo[${GERRIT_PROJECT}]}"
+gh_remote="https://${AUTH_TOKEN}@github.com/${gh_repo}"
 git remote add github ${gh_remote}
 
-# Get the Github repo default branch
-git fetch github
-gh_default_branch=$(git remote show github| sed -n '/HEAD branch/s/.*: //p')
+gerrit_def_branch=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
+gerrit_branches=$(git branch -a|grep "remotes/origin"|grep -v HEAD|sed "s;.*/origin/;;")
 
-echo "Sync ${GERRIT_PROJECT}:${def_branch} to Github:${gh_default_branch}"
-git push --tags github ${def_branch}:${gh_default_branch}
+# Do the mirroring
+exit_code=0
+mb="${mirror_branches[${GERRIT_PROJECT}]}"
+for branch in ${mb}
+do
+    gerrit_branch=$(echo ${branch} | awk -F: '{print $1}')
+    gh_branch=$(echo ${branch} | awk -F: '{print $2}')
+    if ! $(echo ${gerrit_branches}|grep -q "${gerrit_branch}"); then
+        echo "Can not find \"${gerrit_branch}\" branch on the Gerrit project \"${GERRIT_PROJECT}\""
+        exit_code=1
+        break
+    fi
+    echo "Mirror Gerrit project \"${GERRIT_PROJECT}:${gerrit_branch}\" to Github \"${gh_repo}:${gh_branch}\""
+    if [ "${gerrit_branch}" = "${gerrit_def_branch}" ]; then
+        git checkout ${gerrit_branch} > /dev/null 2>&1
+    else
+        git checkout -b ${gerrit_branch} origin/${gerrit_branch} > /dev/null 2>&1
+    fi
+    git push --tags github ${gerrit_branch}:${gh_branch}
+done
 
 # Clean up workspace
 cd - > /dev/null
 rm -rf proj_src
+
+exit ${exit_code}