Skip to content

Commit eebdb25

Browse files
committed
Eliminate duplication of git.util.cwd logic
1 parent c7fad20 commit eebdb25

File tree

2 files changed

+3
-14
lines changed

2 files changed

+3
-14
lines changed

Diff for: git/util.py

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def wrapper(self: "Remote", *args: Any, **kwargs: Any) -> T:
150150

151151
@contextlib.contextmanager
152152
def cwd(new_dir: PathLike) -> Generator[PathLike, None, None]:
153+
"""Context manager to temporarily change directory. Not reentrant."""
153154
old_dir = os.getcwd()
154155
os.chdir(new_dir)
155156
try:

Diff for: test/test_git.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#
55
# This module is part of GitPython and is released under
66
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
7-
import contextlib
87
import os
98
import shutil
109
import subprocess
@@ -15,24 +14,13 @@
1514
from git import Git, refresh, GitCommandError, GitCommandNotFound, Repo, cmd
1615
from test.lib import TestBase, fixture_path
1716
from test.lib import with_rw_directory
18-
from git.util import finalize_process
17+
from git.util import cwd, finalize_process
1918

2019
import os.path as osp
2120

2221
from git.compat import is_win
2322

2423

25-
@contextlib.contextmanager
26-
def _chdir(new_dir):
27-
"""Context manager to temporarily change directory. Not reentrant."""
28-
old_dir = os.getcwd()
29-
os.chdir(new_dir)
30-
try:
31-
yield
32-
finally:
33-
os.chdir(old_dir)
34-
35-
3624
class TestGit(TestBase):
3725
@classmethod
3826
def setUpClass(cls):
@@ -102,7 +90,7 @@ def test_it_executes_git_not_from_cwd(self):
10290
print("#!/bin/sh", file=file)
10391
os.chmod(impostor_path, 0o755)
10492

105-
with _chdir(tmpdir):
93+
with cwd(tmpdir):
10694
self.assertRegex(self.git.execute(["git", "version"]), r"^git version\b")
10795

10896
@skipUnless(is_win, "The regression only affected Windows, and this test logic is OS-specific.")

0 commit comments

Comments
 (0)