Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork948
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
Merged
+53 −21
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
9 changes: 4 additions & 5 deletionsgit/cmd.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletiongit/util.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 | ||
13 changes: 13 additions & 0 deletionstest/fixtures/env_case.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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)) |
35 changes: 20 additions & 15 deletionstest/test_git.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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: | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.