--- a/src/poetry/core/vcs/__init__.py +++ b/src/poetry/core/vcs/__init__.py @@ -9,8 +9,7 @@ def get_vcs(directory: Path) -> Git | None: - working_dir = Path.cwd() - os.chdir(str(directory.resolve())) + cwd = str(directory.resolve()) vcs: Git | None @@ -27,17 +26,21 @@ vcs = None else: git_dir = subprocess.check_output( - [executable(), "rev-parse", "--show-toplevel"], + [executable(), "rev-parse", "--show-prefix"], stderr=subprocess.STDOUT, text=True, + cwd=cwd, encoding="utf-8", ).strip() + git_dir = git_dir.strip()[:-1] # remove the trailing os.sep + if not git_dir: + git_dir = cwd + else: + git_dir = cwd[:-len(git_dir)] vcs = Git(Path(git_dir)) except (subprocess.CalledProcessError, OSError, RuntimeError): vcs = None - finally: - os.chdir(str(working_dir)) return vcs