Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork938
Fix Windows environment variable upcasing bug#1650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -150,6 +150,7 @@ def wrapper(self: "Remote", *args: Any, **kwargs: Any) -> T: | ||
@contextlib.contextmanager | ||
def cwd(new_dir: PathLike) -> Generator[PathLike, None, None]: | ||
"""Context manager to temporarily change directory. Not reentrant.""" | ||
Byron marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
old_dir = os.getcwd() | ||
os.chdir(new_dir) | ||
try: | ||
@@ -158,6 +159,20 @@ def cwd(new_dir: PathLike) -> Generator[PathLike, None, None]: | ||
os.chdir(old_dir) | ||
@contextlib.contextmanager | ||
def patch_env(name: str, value: str) -> Generator[None, None, None]: | ||
"""Context manager to temporarily patch an environment variable.""" | ||
old_value = os.getenv(name) | ||
os.environ[name] = value | ||
try: | ||
yield | ||
finally: | ||
if old_value is None: | ||
del os.environ[name] | ||
else: | ||
os.environ[name] = old_value | ||
def rmtree(path: PathLike) -> None: | ||
"""Remove the given recursively. | ||
@@ -935,7 +950,7 @@ def _obtain_lock_or_raise(self) -> None: | ||
) | ||
try: | ||
with open(lock_file, mode="w"): | ||
Byron marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
pass | ||
except OSError as e: | ||
raise IOError(str(e)) from e | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import subprocess | ||
import sys | ||
import git | ||
_, working_dir, env_var_name = sys.argv | ||
# Importing git should be enough, but this really makes sure Git.execute is called. | ||
repo = git.Repo(working_dir) # Hold the reference. | ||
git.Git(repo.working_dir).execute(["git", "version"]) | ||
print(subprocess.check_output(["set", env_var_name], shell=True, text=True)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -4,35 +4,23 @@ | ||
# | ||
# This module is part of GitPython and is released under | ||
# the BSD License: http://www.opensource.org/licenses/bsd-license.php | ||
import os | ||
import shutil | ||
import subprocess | ||
import sys | ||
from tempfile import TemporaryDirectory, TemporaryFile | ||
from unittest import mock, skipUnless | ||
from git import Git, refresh, GitCommandError, GitCommandNotFound, Repo, cmd | ||
from test.lib import TestBase, fixture_path | ||
from test.lib import with_rw_directory | ||
from git.util importcwd,finalize_process | ||
import os.path as osp | ||
from git.compat import is_win | ||
class TestGit(TestBase): | ||
@classmethod | ||
def setUpClass(cls): | ||
@@ -102,9 +90,26 @@ def test_it_executes_git_not_from_cwd(self): | ||
print("#!/bin/sh", file=file) | ||
os.chmod(impostor_path, 0o755) | ||
withcwd(tmpdir): | ||
self.assertRegex(self.git.execute(["git", "version"]), r"^git version\b") | ||
@skipUnless(is_win, "The regression only affected Windows, and this test logic is OS-specific.") | ||
def test_it_avoids_upcasing_unrelated_environment_variable_names(self): | ||
old_name = "28f425ca_d5d8_4257_b013_8d63166c8158" | ||
Byron marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if old_name == old_name.upper(): | ||
raise RuntimeError("test bug or strange locale: old_name invariant under upcasing") | ||
Byron marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
os.putenv(old_name, "1") # It has to be done this lower-level way to set it lower-case. | ||
cmdline = [ | ||
sys.executable, | ||
fixture_path("env_case.py"), | ||
self.rorepo.working_dir, | ||
old_name, | ||
] | ||
pair_text = subprocess.check_output(cmdline, shell=False, text=True) | ||
new_name = pair_text.split("=")[0] | ||
self.assertEqual(new_name, old_name) | ||
def test_it_accepts_stdin(self): | ||
filename = fixture_path("cat_file_blob") | ||
with open(filename, "r") as fh: | ||