From dbaad43b23bb4955414f76ed912ad4aaf10f0e9e Mon Sep 17 00:00:00 2001 From: Allen Li Date: Thu, 25 Jul 2024 21:31:10 +0000 Subject: [PATCH] [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 Commit-Queue: Allen Li --- git_cl.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/git_cl.py b/git_cl.py index 92dbe540b..be46068dd 100755 --- a/git_cl.py +++ b/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()