Fetch the partner ref. with fetch-and-push-partner tool

Extract the logic to fetch the partner ref. from merge-partner-on-aosp
to a new tool: fetch-and-push-partner that requires special credentials.

This patch makes sure the merge-partner-on-aosp tool now uses whatever
partner ref. exists in origin instead of retrieving a possibly updated
ref. before merging. This avoids confusion when pushing merge commits for
review: both local and remote repositories are starting from the same
branch states. Only the merge commits needs to be reviewed, not new patches
coming from upstream.

The fetch-and-push-partner tool fetches the partner security branch from
the related Google partner repository and pushes it to origin. The tool
should be run prior to running merge-partner-on-aosp.

Issue: ST-1156
Change-Id: Ic35250da68122c1c7e0b4a7a4c84b1f9de90c048
diff --git a/fetch-and-push-partner b/fetch-and-push-partner
new file mode 100755
index 0000000..9bdc78c
--- /dev/null
+++ b/fetch-and-push-partner
@@ -0,0 +1,54 @@
+#!/bin/bash
+
+# Copyright 2018 Fairphone B.V.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# Check https://partner-android.googlesource.com on how to set up access
+
+AOSP_PARTNER_REF=${AOSP_PARTNER_REF:-security-aosp-nyc-mr2-release}
+AOSP_PARTNER_BASE_URL=https://partner-android.googlesource.com
+
+url=${AOSP_PARTNER_BASE_URL}/${REPO_PROJECT}.git
+git ls-remote --exit-code ${url} ${AOSP_PARTNER_REF} > /dev/null 2>&1
+case $? in
+    0)
+        echo "INFO: Fetching AOSP-Partner ref. (${AOSP_PARTNER_REF})…"
+        git remote add aosp-partner ${url} 2> /dev/null || true
+        git fetch --quiet aosp-partner ${AOSP_PARTNER_REF}:${AOSP_PARTNER_REF}
+        if test $? -ne 0; then
+            echo "ERROR: Could not fetch ${AOSP_PARTNER_REF} from remote AOSP-Partner." >&2
+            exit 1
+        fi
+        aosp_partner_ref_commit=$(git rev-list -n1 ${AOSP_PARTNER_REF} 2> /dev/null)
+        echo "INFO: Pushing AOSP-Partner ref to origin…"
+        git push origin ${AOSP_PARTNER_REF}:refs/heads/${AOSP_PARTNER_REF}
+        if test $? -ne 0; then
+            echo "ERROR: Could not push ${AOSP_PARTNER_REF} to origin." >&2
+            exit 1
+        fi
+        ;;
+    2)
+        echo "WARNING: Skipping remote AOSP-Partner, ${AOSP_PARTNER_REF} does not exist on remote, ignoring project." >&2
+        exit 0
+        ;;
+    128)
+        echo "WARNING: Skipping remote AOSP-Partner, ${url} is not a valid remote, ignoring project." >&2
+        exit 0
+        ;;
+    *)
+        echo "ERROR: Skipping remote AOSP-Partner, could not list ${url}." >&2
+        exit 1
+        ;;
+esac