Skip to content

Commit

Permalink
Merge pull request #2832 from skshetry/refactor-startswith-pathisin
Browse files Browse the repository at this point in the history
Refactor `startswith` with `path_isin` util
  • Loading branch information
efiop authored Nov 22, 2019
2 parents f5c8d5d + 9ae7407 commit 3de46dd
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
5 changes: 3 additions & 2 deletions dvc/repo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from dvc.path_info import PathInfo
from dvc.remote.base import RemoteActionNotImplemented
from dvc.utils import relpath
from dvc.utils.fs import path_isin
from dvc.utils.compat import FileNotFoundError
from dvc.utils.compat import fspath_py35
from dvc.utils.compat import open as _open
Expand Down Expand Up @@ -166,7 +167,7 @@ def _ignore(self):
+ updater.lock.files
)

if self.cache.local.cache_dir.startswith(self.root_dir + os.sep):
if path_isin(self.cache.local.cache_dir, self.root_dir):
flist += [self.cache.local.cache_dir]

self.scm.ignore_list(flist)
Expand All @@ -193,7 +194,7 @@ def collect(self, target, with_deps=False, recursive=False, graph=None):
ret = []
for node in nodes:
stage = attrs[node]
if stage.path.startswith(target + os.sep):
if path_isin(stage.path, target):
ret.append(stage)
return ret

Expand Down
3 changes: 2 additions & 1 deletion dvc/scm/git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from dvc.utils import fix_env
from dvc.utils import is_binary
from dvc.utils import relpath
from dvc.utils.fs import path_isin
from dvc.utils.compat import cast_bytes_py2
from dvc.utils.compat import open
from dvc.utils.compat import str
Expand Down Expand Up @@ -134,7 +135,7 @@ def _get_gitignore(self, path):

gitignore = os.path.join(ignore_file_dir, self.GITIGNORE)

if not gitignore.startswith(os.path.realpath(self.root_dir)):
if not path_isin(gitignore, os.path.realpath(self.root_dir)):
raise FileNotInRepoError(path)

return entry, gitignore
Expand Down
5 changes: 3 additions & 2 deletions dvc/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from dvc.utils import dict_md5
from dvc.utils import fix_env
from dvc.utils import relpath
from dvc.utils.fs import path_isin
from dvc.utils.collections import apply_diff
from dvc.utils.fs import contains_symlink_up_to
from dvc.utils.stage import dump_stage_file
Expand Down Expand Up @@ -390,8 +391,8 @@ def _check_stage_path(repo, path):
if not os.path.isdir(real_path):
raise StagePathNotDirectoryError(path)

proj_dir = os.path.realpath(repo.root_dir) + os.path.sep
if not (real_path + os.path.sep).startswith(proj_dir):
proj_dir = os.path.realpath(repo.root_dir)
if real_path != proj_dir and not path_isin(real_path, proj_dir):
raise StagePathOutsideError(path)

@property
Expand Down
5 changes: 2 additions & 3 deletions tests/func/test_external_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from dvc.external_repo import external_repo
from dvc.scm.git import Git
from dvc.utils.fs import path_isin


def test_external_repo(erepo):
Expand All @@ -22,8 +23,6 @@ def test_external_repo(erepo):

# Check cache_dir is unset
with external_repo(url) as repo:
assert repo.cache.local.cache_dir.startswith(
repo.root_dir + os.sep
)
assert path_isin(repo.cache.local.cache_dir, repo.root_dir)

assert mock.call_count == 1

0 comments on commit 3de46dd

Please sign in to comment.