From ab66bb9d5c278ec1645fea5bc3205c5e4b11d6b4 Mon Sep 17 00:00:00 2001 From: finswimmer Date: Fri, 28 Feb 2020 14:46:02 +0100 Subject: [PATCH] get_vcs starts searching git folder from tmp dir instead of project (#1946) (#1947) * fix (builder): take `self._original_path` if available to find `.git` folder * change (vcs): use `git rev-parse --show-toplevel` to find git root folder * fix (vcs): change back to original working dir after finding vcs * change (builder): introduce self._original_path to keep original path if(vcs): resolve directory for `get_vcs` --- poetry/masonry/builders/builder.py | 3 ++- poetry/vcs/__init__.py | 34 ++++++++++++++++++------------ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/poetry/masonry/builders/builder.py b/poetry/masonry/builders/builder.py index 40e8c771e81..f35c893866d 100644 --- a/poetry/masonry/builders/builder.py +++ b/poetry/masonry/builders/builder.py @@ -44,6 +44,7 @@ def __init__( self._io = io self._package = poetry.package self._path = poetry.file.parent + self._original_path = self._path packages = [] for p in self._package.packages: @@ -75,7 +76,7 @@ def build(self): @lru_cache(maxsize=None) def find_excluded_files(self): # type: () -> Set[str] # Checking VCS - vcs = get_vcs(self._path) + vcs = get_vcs(self._original_path) if not vcs: vcs_ignored_files = set() else: diff --git a/poetry/vcs/__init__.py b/poetry/vcs/__init__.py index d242ca6c285..c13e4fa05ed 100644 --- a/poetry/vcs/__init__.py +++ b/poetry/vcs/__init__.py @@ -1,22 +1,28 @@ +import os import subprocess -import warnings from poetry.utils._compat import Path +from poetry.utils._compat import decode from .git import Git def get_vcs(directory): # type: (Path) -> Git - directory = directory.resolve() - - for p in [directory] + list(directory.parents): - if (p / ".git").is_dir(): - try: - return Git(p) - except (subprocess.CalledProcessError, OSError): - # Either git could not be found or does not exist - warnings.warn( - "git executable could not be found", category=RuntimeWarning - ) - - return + working_dir = Path.cwd() + os.chdir(str(directory.resolve())) + + try: + git_dir = decode( + subprocess.check_output( + ["git", "rev-parse", "--show-toplevel"], stderr=subprocess.STDOUT + ) + ).strip() + + vcs = Git(Path(git_dir)) + + except subprocess.CalledProcessError: + vcs = None + finally: + os.chdir(str(working_dir)) + + return vcs