Arthur: test multi-branch with tf-sync-repos.yaml job

Signed-off-by: Arthur She <arthur.she@linaro.org>
Change-Id: I8ffb305b5b3216bc01baad3ad7ba896c4019467a
diff --git a/ci/tf-sync-repos.sh b/ci/tf-sync-repos.sh
index cd7eb72..bbf7d41 100755
--- a/ci/tf-sync-repos.sh
+++ b/ci/tf-sync-repos.sh
@@ -8,7 +8,8 @@
 
 # The Gerrit project and Github repo mapping
 # This script will mirror Gerrit_project to Github_repo
-# The format is [Gerrit_project]="Github_repo"
+# The format of the following key-value dictionary is
+# [Gerrit_project]="Github_repo"
 declare -A github_repos
 github_repos=(
 [TF-A/trusted-firmware-a]="TrustedFirmware-A/trusted-firmware-a"
@@ -32,7 +33,8 @@
 )
 
 # 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"
+# The format of the following key-value dictionary 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"
@@ -55,7 +57,8 @@
 [sandbox/arthur]="master:main branch-1:gh-branch-1"
 )
 
-[ -d "proj_src" ] && rm -rf proj_src
+_workdir=$(mktemp -d)
+trap '[ -d "${_workdir}" ] && rm -rf "${_workdir}"' EXIT
 
 gerrit_repo="https://review.trustedfirmware.org/${GERRIT_PROJECT}"
 gh_repo=${github_repos[${GERRIT_PROJECT}]}
@@ -65,42 +68,23 @@
     exit 1
 fi
 
-echo "Cloning repository ${gerrit_repo}"
-git clone ${gerrit_repo} proj_src
-
-cd proj_src
-
 echo "GitHub repo for \"${GERRIT_PROJECT}\" is \"https://github.com/${gh_repo}\""
 
 gh_remote="https://${AUTH_TOKEN}@github.com/${gh_repo}"
-git remote add github ${gh_remote}
 
-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/;;")
-
+git -C "${_workdir}" init > /dev/null 2>&1
 # 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}
+
+    # Only sync up the branch that triggered the job
+    [ "${gerrit_branch}" != "${GERRIT_REFNAME}" ] && continue
+
+    echo -e "\nFetching Gerrit ${GERRIT_PROJECT}:${gerrit_branch}\n"
+    git -C "${_workdir}" fetch --tags "${gerrit_repo}" "${gerrit_branch}"
+    echo -e "\nSyncing up to GitHub ${gh_repo}:${gh_branch}\n"
+    git -C "${_workdir}" push --tags "${gh_remote}" FETCH_HEAD:refs/heads/"${gh_branch}"
 done
-
-# Clean up workspace
-cd - > /dev/null
-rm -rf proj_src
-
-exit ${exit_code}
diff --git a/tf-sync-repos.yaml b/tf-sync-repos.yaml
index dea1af2..576be2c 100644
--- a/tf-sync-repos.yaml
+++ b/tf-sync-repos.yaml
@@ -32,11 +32,8 @@
           branches:
           - branch-compare-type: PLAIN
             branch-pattern: master
-        - project-compare-type: PLAIN
-          project-pattern: sandbox/arthur-2
-          branches:
           - branch-compare-type: PLAIN
-            branch-pattern: master
+            branch-pattern: branch-1
         silent-start: true
     wrappers:
     - credentials-binding: