From dbebea0aa25545cd6dec98862c1a7488314dfbd2 Mon Sep 17 00:00:00 2001 From: Allen Li Date: Sat, 20 Jul 2024 00:22:21 +0000 Subject: [PATCH] [git_cl] Add apply_global Bug: b/351071334 Change-Id: I235eb26cd46a82251b3a757edd37961bce067522 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5719714 Reviewed-by: Yiwei Zhang Reviewed-by: Robbie Iannucci Commit-Queue: Allen Li --- git_cl.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/git_cl.py b/git_cl.py index 31d5b70e92..bf8772a24a 100755 --- a/git_cl.py +++ b/git_cl.py @@ -3767,6 +3767,14 @@ class GitAuthConfigChanger(object): self._apply_sso(cwd) self._apply_gitcookies(cwd) + def apply_global(self, cwd: str) -> None: + """Apply config changes to the global (user) Git config. + + This uses the instance's mode as a default for the Gerrit host. + """ + self._apply_global_cred_helper(cwd) + self._apply_global_sso(cwd) + def _apply_cred_helper(self, cwd: str) -> None: """Apply config changes relating to credential helper.""" cred_key: str = f'credential.{self._base_url}.helper' @@ -3810,6 +3818,50 @@ class GitAuthConfigChanger(object): else: raise TypeError(f'Invalid mode {self.mode!r}') + def _apply_global_cred_helper(self, cwd: str) -> None: + """Apply config changes relating to credential helper.""" + cred_key: str = f'credential.{self._base_url}.helper' + if self.mode == GitConfigMode.NEW_AUTH: + self._set_config(cwd, cred_key, '', scope='global', modify_all=True) + self._set_config(cwd, cred_key, 'luci', scope='global', append=True) + elif self.mode == GitConfigMode.NEW_AUTH_SSO: + # Avoid editing the user's config in case they manually + # configured something. + pass + elif self.mode == GitConfigMode.OLD_AUTH: + # Avoid editing the user's config in case they manually + # configured something. + pass + else: + raise TypeError(f'Invalid mode {self.mode!r}') + + def _apply_global_sso(self, cwd: str) -> None: + """Apply config changes relating to SSO.""" + sso_key: str = f'url.sso://{self._shortname}/.insteadOf' + if self.mode == GitConfigMode.NEW_AUTH: + # Do not unset protocol.sso.allow because it may be used by other hosts. + self._set_config(cwd, + sso_key, + None, + scope='global', + modify_all=True) + elif self.mode == GitConfigMode.NEW_AUTH_SSO: + self._set_config(cwd, + 'protocol.sso.allow', + 'always', + scope='global') + self._set_config(cwd, + sso_key, + self._base_url, + scope='global', + modify_all=True) + elif self.mode == GitConfigMode.OLD_AUTH: + # Avoid editing the user's config in case they manually + # configured something. + pass + else: + raise TypeError(f'Invalid mode {self.mode!r}') + def _set_config(self, *args, **kwargs) -> None: self._set_config_func(*args, **kwargs)