From 11bf826a107d024c8c5d05bdbc30edd6a012015e Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 10 Nov 2026 16:16:59 -0500 Subject: [PATCH] git_command: move _GetBasicEnv to module level RepoSourceVersion() and _build_env() are module-level functions that called GitCommand._GetBasicEnv(), while GitCommand itself never called it directly. When tests mock git_command.GitCommand before RepoSourceVersion() has cached its result (such as running test_upload_for_review_real_event_log in isolation), calling GitCommand._GetBasicEnv() returned a MagicMock instead of an env dict, causing subprocess.run to fail with ValueError. Change-Id: Ib8b866b012602c549ce2d59105526d847b0d7d51 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/656383 Reviewed-by: Gavin Mak Commit-Queue: Mike Frysinger Tested-by: Mike Frysinger --- git_command.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/git_command.py b/git_command.py index 149803570..63fd3c7a6 100644 --- a/git_command.py +++ b/git_command.py @@ -87,7 +87,7 @@ def RepoSourceVersion(): # We avoid GitCommand so we don't run into circular deps -- GitCommand needs # to initialize version info we provide. if ver is None: - env = GitCommand._GetBasicEnv() + env = _get_basic_env() proj = os.path.dirname(os.path.abspath(__file__)) env[GIT_DIR] = os.path.join(proj, ".git") @@ -220,6 +220,25 @@ def git_require(min_version, fail=False, msg=""): return False +def _get_basic_env() -> None: + """Return a basic env for running git under. + + This is guaranteed to be side-effect free. + """ + env = os.environ.copy() + for key in ( + repo_trace.REPO_TRACE, + GIT_DIR, + "GIT_ALTERNATE_OBJECT_DIRECTORIES", + "GIT_OBJECT_DIRECTORY", + "GIT_WORK_TREE", + "GIT_GRAFT_FILE", + "GIT_INDEX_FILE", + ): + env.pop(key, None) + return env + + def _build_env( _kwargs_only=(), bare: Optional[bool] = False, @@ -232,7 +251,7 @@ def _build_env( assert _kwargs_only == (), "_build_env only accepts keyword arguments." - env = GitCommand._GetBasicEnv() + env = _get_basic_env() if disable_editor: env["GIT_EDITOR"] = ":" @@ -542,25 +561,6 @@ class GitCommand: return buffer - @staticmethod - def _GetBasicEnv(): - """Return a basic env for running git under. - - This is guaranteed to be side-effect free. - """ - env = os.environ.copy() - for key in ( - repo_trace.REPO_TRACE, - GIT_DIR, - "GIT_ALTERNATE_OBJECT_DIRECTORIES", - "GIT_OBJECT_DIRECTORY", - "GIT_WORK_TREE", - "GIT_GRAFT_FILE", - "GIT_INDEX_FILE", - ): - env.pop(key, None) - return env - def VerifyCommand(self) -> None: if self.rc == 0: return None