From be5ff1a6c4b227ba9325408dc8b74cbce8105a1e Mon Sep 17 00:00:00 2001 From: Allen Li Date: Wed, 31 Jul 2024 22:21:51 +0000 Subject: [PATCH] [git_auth] Move auto configure logic Bug: b/351071334 Change-Id: Ic6574f86208b22e406e36f8cd45a564ea74ec7f9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5742793 Reviewed-by: Yiwei Zhang Commit-Queue: Allen Li --- git_auth.py | 25 +++++++++++++++++++++++++ git_cl.py | 18 +----------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/git_auth.py b/git_auth.py index e17b66fd3..8ddb63483 100644 --- a/git_auth.py +++ b/git_auth.py @@ -223,6 +223,31 @@ class ConfigChanger(object): self._set_config_func(*args, **kwargs) +def AutoConfigure(cwd: str, cl: git_cl.Changelist) -> None: + """Configure Git authentication automatically. + + This tracks when the config that has already been applied and skips + doing anything if so. + + This may modify the global Git config and the local repo config as + needed. + """ + latestVer: int = ConfigChanger.VERSION + v: int = 0 + try: + v = int( + scm.GIT.GetConfig(cwd, 'depot-tools.gitauthautoconfigured') or '0') + except ValueError: + v = 0 + if v < latestVer: + logging.debug( + 'Automatically configuring Git repo authentication' + ' (current version: %r, latest: %r)', v, latestVer) + ConfigureRepo(cwd, cl) + scm.GIT.SetConfig(cwd, 'depot-tools.gitAuthAutoConfigured', + str(latestVer)) + + def Configure(cwd: str, cl: git_cl.Changelist) -> None: """Configure Git authentication. diff --git a/git_cl.py b/git_cl.py index 036797ab4..96e260707 100755 --- a/git_cl.py +++ b/git_cl.py @@ -2347,23 +2347,7 @@ class Changelist(object): return if newauth.Enabled(): - latestVer: int = git_auth.ConfigChanger.VERSION - v: int = 0 - try: - v = int( - scm.GIT.GetConfig(settings.GetRoot(), - 'depot-tools.gitauthautoconfigured', - default='0')) - except ValueError: - v = 0 - if v < latestVer: - logging.debug( - 'Automatically configuring Git repo authentication (current version: %r, latest: %r)', - v, latestVer) - git_auth.ConfigureRepo(os.getcwd(), Changelist()) - scm.GIT.SetConfig(settings.GetRoot(), - 'depot-tools.gitAuthAutoConfigured', - str(latestVer)) + git_auth.AutoConfigure(os.getcwd(), Changelist()) return # Lazy-loader to identify Gerrit and Git hosts.