Переглянути джерело

[gclient_scm] Add new auth stack setup to gclient

Bug: b/356935829
Change-Id: Ie917f142c3fddba398a5351d685a7b131b298a76
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5762791
Commit-Queue: Allen Li <ayatane@chromium.org>
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Allen Li 1 рік тому
батько
коміт
a603aded9a
2 змінених файлів з 26 додано та 0 видалено
  1. 6 0
      gclient_scm.py
  2. 20 0
      git_auth.py

+ 6 - 0
gclient_scm.py

@@ -21,8 +21,10 @@ import traceback
 
 
 import gclient_utils
 import gclient_utils
 import gerrit_util
 import gerrit_util
+import git_auth
 import git_cache
 import git_cache
 import git_common
 import git_common
+import newauth
 import scm
 import scm
 import subprocess2
 import subprocess2
 
 
@@ -1321,6 +1323,10 @@ class GitWrapper(SCMWrapper):
         parent_dir = os.path.dirname(self.checkout_path)
         parent_dir = os.path.dirname(self.checkout_path)
         gclient_utils.safe_makedirs(parent_dir)
         gclient_utils.safe_makedirs(parent_dir)
 
 
+        # Set up Git authentication configuration that is needed to clone/fetch the repo.
+        if newauth.Enabled():
+            git_auth.ConfigureGlobal('/', url)
+
         if hasattr(options, 'no_history') and options.no_history:
         if hasattr(options, 'no_history') and options.no_history:
             self._Run(['init', self.checkout_path], options, cwd=self._root_dir)
             self._Run(['init', self.checkout_path], options, cwd=self._root_dir)
             self._Run(['remote', 'add', 'origin', url], options)
             self._Run(['remote', 'add', 'origin', url], options)

+ 20 - 0
git_auth.py

@@ -108,6 +108,20 @@ class ConfigChanger(object):
             remote_url=remote_url,
             remote_url=remote_url,
         )
         )
 
 
+    @classmethod
+    def new_for_remote(cls, cwd: str, remote_url: str) -> ConfigChanger:
+        """Create a ConfigChanger for the given Gerrit host.
+
+        The user, which is used to determine the mode, is inferred using
+        git-config(1) in the given `cwd`.
+        """
+        c = cls(
+            mode=ConfigMode.NEW_AUTH,
+            remote_url=remote_url,
+        )
+        c.mode = cls._infer_mode(cwd, c._shortname + '-review.googlesource.com')
+        return c
+
     @staticmethod
     @staticmethod
     def _infer_mode(cwd: str, gerrit_host: str) -> ConfigMode:
     def _infer_mode(cwd: str, gerrit_host: str) -> ConfigMode:
         """Infer default mode to use."""
         """Infer default mode to use."""
@@ -280,6 +294,12 @@ def Configure(cwd: str, cl: git_cl.Changelist) -> None:
     c2.apply(cwd)
     c2.apply(cwd)
 
 
 
 
+def ConfigureGlobal(cwd: str, remote_url: str) -> None:
+    """Configure global/user Git authentication."""
+    logging.debug('Configuring global Git authentication for %s', remote_url)
+    ConfigChanger.new_for_remote(cwd, remote_url).apply_global(cwd)
+
+
 def ClearRepoConfig(cwd: str, cl: git_cl.Changelist) -> None:
 def ClearRepoConfig(cwd: str, cl: git_cl.Changelist) -> None:
     """Clear the current Git repo authentication."""
     """Clear the current Git repo authentication."""
     logging.debug('Clearing current Git repo authentication...')
     logging.debug('Clearing current Git repo authentication...')