mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
dvc: refactor startswith usage with path_isin util
This commit is contained in:
@@ -20,6 +20,7 @@ from dvc.ignore import DvcIgnoreFilter
|
||||
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
|
||||
@@ -166,7 +167,7 @@ class Repo(object):
|
||||
+ 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)
|
||||
@@ -193,7 +194,7 @@ class Repo(object):
|
||||
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
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ from dvc.scm.git.tree import GitTree
|
||||
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
|
||||
@@ -134,7 +135,7 @@ class Git(Base):
|
||||
|
||||
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
|
||||
|
||||
+4
-2
@@ -18,6 +18,7 @@ from dvc.exceptions import DvcException
|
||||
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
|
||||
@@ -390,8 +391,9 @@ class Stage(object):
|
||||
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
|
||||
|
||||
@@ -4,6 +4,7 @@ from mock import patch
|
||||
|
||||
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):
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user