cronet import: use folder.origin instead of git.origin

git.origin uses a bare repo which is not supported by gclient. There may
be ways to get this to work including the git-origin-checkout-hook, but
those are a) really slow and b) much more complex than just using a
folder.origin.

This change also adds support to the import script to configure the
origin folder (i.e. clone the repo and run gclient) before the copybara
script is invoked.

Test: run import script
Change-Id: I6289108271960c87583a059a553b0d0e7db8af68
diff --git a/Cronet/tools/import/copy.bara.sky b/Cronet/tools/import/copy.bara.sky
index 104de68..59e59e3 100644
--- a/Cronet/tools/import/copy.bara.sky
+++ b/Cronet/tools/import/copy.bara.sky
@@ -19,6 +19,7 @@
 
     # Exclude existing *OWNERS files
     "**/*OWNERS",
+    "**/.git/**",
 ]
 
 cronet_origin_files = glob(
@@ -94,11 +95,8 @@
 core.workflow(
     name = "import_cronet",
     authoring = authoring.overwrite("Cronet Mainline Eng <[email protected]>"),
-    origin = git.origin(
-        url = "https://chromium.googlesource.com/chromium/src.git",
-        # Source ref is set by the invoking script.
-        ref = "overwritten-by-script",
-    ),
+    # Origin folder is specified via source_ref argument, see import_cronet.sh
+    origin = folder.origin(),
     origin_files = cronet_origin_files,
     destination = git.destination(
         # The destination URL is set by the invoking script.
diff --git a/Cronet/tools/import/git_checkout_hook.sh b/Cronet/tools/import/git_checkout_hook.sh
deleted file mode 100755
index 48b737c..0000000
--- a/Cronet/tools/import/git_checkout_hook.sh
+++ /dev/null
@@ -1,39 +0,0 @@
-#!/bin/bash
-
-# Copyright 2023 Google Inc. All rights reserved.
-#
-# 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.
-
-# Script to invoke copybara locally to import Cronet into Android.
-# Inputs:
-#  Environment:
-#   REV: The git revision to sync.
-set -e
-
-# HACK: Copybara does a bare checkout which gclient does not support.
-git init
-git remote add origin https://chromium.googlesource.com/chromium/src.git
-git fetch origin --depth=1 "${REV}"
-git reset --hard FETCH_HEAD
-
-# For some reason, gclient still likes to reference the repository name (src)
-# despite name being './'.
-ln -s . src
-gclient sync \
-    --no-history \
-    --nohooks \
-    --shallow \
-    --spec="solutions=[{'name':'./','managed':False,'url':'https://chromium.googlesource.com/chromium/src.git'}];target_os=['android']" \
-    --rev="${REV}"
-
-
diff --git a/Cronet/tools/import/import_cronet.sh b/Cronet/tools/import/import_cronet.sh
index b71297c..d0c8deb 100755
--- a/Cronet/tools/import/import_cronet.sh
+++ b/Cronet/tools/import/import_cronet.sh
@@ -33,6 +33,8 @@
     exit 1
 }
 
+COPYBARA_FOLDER_ORIGIN="/tmp/copybara-origin"
+
 #######################################
 # Create upstream-import branch in external/cronet.
 # Globals:
@@ -49,31 +51,59 @@
 }
 
 #######################################
+# Setup folder.origin for copybara inside /tmp
+# Globals:
+#   COPYBARA_FOLDER_ORIGIN
+# Arguments:
+#   new_rev, string
+#######################################
+setup_folder_origin() {
+    local _new_rev=$1
+    mkdir -p "${COPYBARA_FOLDER_ORIGIN}"
+    cd "${COPYBARA_FOLDER_ORIGIN}"
+
+    # For this to work _new_rev must be a branch or a tag.
+    git clone --depth=1 --branch "${_new_rev}" https://chromium.googlesource.com/chromium/src.git
+
+    cat <<EOF >.gclient
+solutions = [
+  {
+    "name": "src",
+    "url": "https://chromium.googlesource.com/chromium/src.git",
+    "managed": False,
+    "custom_deps": {},
+    "custom_vars": {},
+  },
+]
+target_os = ["android"]
+EOF
+    cd src
+    # Set appropriate gclient flags to speed up syncing.
+    gclient sync \
+        --no-history
+        --shallow
+}
+
+#######################################
 # Runs the copybara import of Chromium
 # Globals:
 #   ANDROID_BUILD_TOP
+#   COPYBARA_FOLDER_ORIGIN
 # Arguments:
-#   new_rev, string
 #   last_rev, string or empty
 #   force, string or empty
 #######################################
 do_run_copybara() {
-    local _new_rev=$1
-    local _last_rev=$2
-    local _force=$3
+    local _last_rev=$1
+    local _force=$2
 
     local -a flags
     flags+=(--git-destination-url="file://${ANDROID_BUILD_TOP}/external/cronet")
-    flags+=(--repo-timeout 3h)
+    flags+=(--repo-timeout 3m)
 
-    # git_checkout_hook.sh reruns git clone and subsequently invokes gclient,
-    # so this can take a while.
-    flags+=(--commands-timeout 3h)
-
-    # Export _new_rev for use in git_checkout_hook.sh.
-    # Arguments are not supported for --git-origin-checkout-hook.
-    export REV="${_new_rev}"
-    flags+=(--git-origin-checkout-hook="${PWD}/git_checkout_hook.sh")
+    # buildtools/third_party/libc++ contains an invalid symlink
+    flags+=(--folder-origin-ignore-invalid-symlinks)
+    flags+=(--git-no-verify)
 
     if [ ! -z "${_force}" ]; then
         flags+=(--force)
@@ -86,7 +116,7 @@
     /google/bin/releases/copybara/public/copybara/copybara \
         "${flags[@]}" \
         "${ANDROID_BUILD_TOP}/packages/modules/Connectivity/Cronet/tools/import/copy.bara.sky" \
-        import_cronet "${_new_rev}"
+        import_cronet "${COPYBARA_FOLDER_ORIGIN}/src"
 }
 
 while getopts $OPTSTRING opt; do
@@ -105,5 +135,6 @@
 fi
 
 setup_upstream_import_branch
-do_run_copybara "${new_rev}" "${last_rev}" "${force}"
+setup_folder_origin "${new_rev}"
+do_run_copybara "${last_rev}" "${force}"