Explorar o código

[git_cl] Add global+local Git auth configuration to creds check

Adds new Git auth configuration logic which sets up a global config
which applies to all repos for a Gerrit host, as well as a local
config if the current repo has a different user set up (e.g., someone
with multiple emails used across different repos for the same Gerrit
host).

Bug: b/351071334
Change-Id: I3a73a50b7ecc80ba22442de808b7f20a488ffc6f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5723271
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
Commit-Queue: Allen Li <ayatane@chromium.org>
Allen Li hai 1 ano
pai
achega
dbaad43b23
Modificáronse 1 ficheiros con 31 adicións e 1 borrados
  1. 31 1
      git_cl.py

+ 31 - 1
git_cl.py

@@ -3661,6 +3661,36 @@ def DownloadGerritHook(force):
                          'chmod +x .git/hooks/commit-msg' % src)
 
 
+def ConfigureGitAuth() -> None:
+    """Configure Git authentication.
+
+    This may modify the global Git config and the local repo config as
+    needed.
+    """
+    logging.debug('Configuring Git authentication...')
+
+    logging.debug('Configuring global Git authentication...')
+    # We want the user's global config.
+    # We can probably assume the root directory doesn't have any local
+    # Git configuration.
+    c = GitAuthConfigChanger.new_from_env('/')
+    c.apply_global(os.path.expanduser('~'))
+
+    cwd = os.getcwd()
+    c2 = GitAuthConfigChanger.new_from_env(cwd)
+    if c2.mode == c.mode:
+        logging.debug(
+            'Local user wants same mode %s as global; clearing local repo auth config',
+            c2.mode)
+        c2.mode = GitAuthMode.NO_AUTH
+        c2.apply(cwd)
+        return
+    logging.debug('Local user wants mode %s while global user wants mode %s',
+                  c2.mode, c.mode)
+    logging.debug('Configuring current Git repo authentication...')
+    c2.apply(cwd)
+
+
 def ConfigureGitRepoAuth() -> None:
     """Configure the current Git repo authentication."""
     logging.debug('Configuring current Git repo authentication...')
@@ -4100,7 +4130,7 @@ def CMDcreds_check(parser, args):
     _, _ = parser.parse_args(args)
 
     if newauth.Enabled():
-        ConfigureGitRepoAuth()
+        ConfigureGitAuth()
         return 0
     if newauth.ExplicitlyDisabled():
         ClearGitRepoAuth()