Skip to content

Commit

Permalink
get_vcs: don't change the current working directory
Browse files Browse the repository at this point in the history
It's not needed to change global state here, just change the
working directory of the child processes instead.

Since os.chdir() raised for non-existing paths, keep
that behaviour by passing strict to resolve().
  • Loading branch information
lazka committed Feb 26, 2024
1 parent 510e5ec commit 869605c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/poetry/core/vcs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from __future__ import annotations

import os
import subprocess

from pathlib import Path
from typing import TYPE_CHECKING

from poetry.core.vcs.git import Git


def get_vcs(directory: Path) -> Git | None:
working_dir = Path.cwd()
os.chdir(str(directory.resolve()))
if TYPE_CHECKING:
from pathlib import Path


def get_vcs(directory: Path) -> Git | None:
directory = directory.resolve(strict=True)
vcs: Git | None

try:
Expand All @@ -21,6 +22,7 @@ def get_vcs(directory: Path) -> Git | None:
[executable(), "check-ignore", "."],
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
cwd=directory,
).returncode

if check_ignore == 0:
Expand All @@ -31,13 +33,12 @@ def get_vcs(directory: Path) -> Git | None:
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
cwd=directory,
).strip()

vcs = Git((directory.resolve() / rel_path_to_git_dir).resolve())
vcs = Git((directory / rel_path_to_git_dir).resolve())

except (subprocess.CalledProcessError, OSError, RuntimeError):
vcs = None
finally:
os.chdir(str(working_dir))

return vcs

0 comments on commit 869605c

Please sign in to comment.