Skip to content

Commit 3de46dd

Browse files
authored
Merge pull request #2832 from skshetry/refactor-startswith-pathisin
Refactor `startswith` with `path_isin` util
2 parents f5c8d5d + 9ae7407 commit 3de46dd

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

dvc/repo/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from dvc.path_info import PathInfo
2121
from dvc.remote.base import RemoteActionNotImplemented
2222
from dvc.utils import relpath
23+
from dvc.utils.fs import path_isin
2324
from dvc.utils.compat import FileNotFoundError
2425
from dvc.utils.compat import fspath_py35
2526
from dvc.utils.compat import open as _open
@@ -166,7 +167,7 @@ def _ignore(self):
166167
+ updater.lock.files
167168
)
168169

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

172173
self.scm.ignore_list(flist)
@@ -193,7 +194,7 @@ def collect(self, target, with_deps=False, recursive=False, graph=None):
193194
ret = []
194195
for node in nodes:
195196
stage = attrs[node]
196-
if stage.path.startswith(target + os.sep):
197+
if path_isin(stage.path, target):
197198
ret.append(stage)
198199
return ret
199200

dvc/scm/git/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from dvc.utils import fix_env
1818
from dvc.utils import is_binary
1919
from dvc.utils import relpath
20+
from dvc.utils.fs import path_isin
2021
from dvc.utils.compat import cast_bytes_py2
2122
from dvc.utils.compat import open
2223
from dvc.utils.compat import str
@@ -134,7 +135,7 @@ def _get_gitignore(self, path):
134135

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

137-
if not gitignore.startswith(os.path.realpath(self.root_dir)):
138+
if not path_isin(gitignore, os.path.realpath(self.root_dir)):
138139
raise FileNotInRepoError(path)
139140

140141
return entry, gitignore

dvc/stage.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from dvc.utils import dict_md5
1919
from dvc.utils import fix_env
2020
from dvc.utils import relpath
21+
from dvc.utils.fs import path_isin
2122
from dvc.utils.collections import apply_diff
2223
from dvc.utils.fs import contains_symlink_up_to
2324
from dvc.utils.stage import dump_stage_file
@@ -390,8 +391,8 @@ def _check_stage_path(repo, path):
390391
if not os.path.isdir(real_path):
391392
raise StagePathNotDirectoryError(path)
392393

393-
proj_dir = os.path.realpath(repo.root_dir) + os.path.sep
394-
if not (real_path + os.path.sep).startswith(proj_dir):
394+
proj_dir = os.path.realpath(repo.root_dir)
395+
if real_path != proj_dir and not path_isin(real_path, proj_dir):
395396
raise StagePathOutsideError(path)
396397

397398
@property

tests/func/test_external_repo.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from dvc.external_repo import external_repo
66
from dvc.scm.git import Git
7+
from dvc.utils.fs import path_isin
78

89

910
def test_external_repo(erepo):
@@ -22,8 +23,6 @@ def test_external_repo(erepo):
2223

2324
# Check cache_dir is unset
2425
with external_repo(url) as repo:
25-
assert repo.cache.local.cache_dir.startswith(
26-
repo.root_dir + os.sep
27-
)
26+
assert path_isin(repo.cache.local.cache_dir, repo.root_dir)
2827

2928
assert mock.call_count == 1

0 commit comments

Comments
 (0)