[git_auth] Fix some bugs introduced during refactoring.

R=ayatane, dlf

Change-Id: I11371375b7f02e9ed5b67cde79ee05da6b0ba5c1
Bug: 351071334
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5740607
Commit-Queue: Allen Li <ayatane@chromium.org>
Reviewed-by: Dan Le Febvre <dlf@google.com>
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
changes/07/5740607/3
Robert Iannucci 10 months ago committed by LUCI CQ
parent 495e14b7b6
commit 4903a45ccc

@ -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

@ -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)

Loading…
Cancel
Save