diff --git a/git_auth.py b/git_auth.py index 23744181a..4643a5bdb 100644 --- a/git_auth.py +++ b/git_auth.py @@ -7,11 +7,16 @@ from __future__ import annotations import enum import functools -from typing import Callable +from typing import TYPE_CHECKING, Callable import urllib.parse +import gerrit_util +import newauth import scm +if TYPE_CHECKING: + import git_cl + class ConfigMode(enum.Enum): """Modes to pass to ConfigChanger""" @@ -68,14 +73,13 @@ class ConfigChanger(object): return parts._replace(path='/', query='', fragment='').geturl() @classmethod - def new_from_env(cls, cwd: str) -> 'ConfigChanger': + def new_from_env(cls, cwd: str, cl: git_cl.Changelist) -> 'ConfigChanger': """Create a ConfigChanger by inferring from env. The Gerrit host is inferred from the current repo/branch. The user, which is used to determine the mode, is inferred using git-config(1) in the given `cwd`. """ - cl = Changelist() # This is determined either from the branch or repo config. # # Example: chromium-review.googlesource.com diff --git a/git_cl.py b/git_cl.py index 2d699bfc7..3f36e7916 100755 --- a/git_cl.py +++ b/git_cl.py @@ -3671,14 +3671,16 @@ def ConfigureGitAuth() -> None: logging.debug('Configuring Git authentication...') logging.debug('Configuring global Git authentication...') + cl = Changelist() + # We want the user's global config. # We can probably assume the root directory doesn't have any local # Git configuration. - c = git_auth.ConfigChanger.new_from_env('/') + c = git_auth.ConfigChanger.new_from_env('/', cl) c.apply_global(os.path.expanduser('~')) cwd = os.getcwd() - c2 = git_auth.ConfigChanger.new_from_env(cwd) + c2 = git_auth.ConfigChanger.new_from_env(cwd, cl) if c2.mode == c.mode: logging.debug( 'Local user wants same mode %s as global; clearing local repo auth config', @@ -3696,14 +3698,15 @@ def ConfigureGitRepoAuth() -> None: """Configure the current Git repo authentication.""" logging.debug('Configuring current Git repo authentication...') cwd = os.getcwd() - c = git_auth.ConfigChanger.new_from_env(cwd) + c = git_auth.ConfigChanger.new_from_env(cwd, Changelist()) c.apply(cwd) def ClearGitRepoAuth() -> None: """Clear the current Git repo authentication.""" logging.debug('Clearing current Git repo authentication...') - c = git_auth.ConfigChanger.new_from_env(cwd) + cwd = os.getcwd() + c = git_auth.ConfigChanger.new_from_env(cwd, Changelist()) c.mode = git_auth.ConfigMode.NO_AUTH c.apply(cwd)