Skip to content

Commit 27eec49

Browse files
committed
Refactor with OutOfWorkSpaceError
1 parent 76481bd commit 27eec49

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

dvc/ignore.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pathspec.util import normalize_file
99
from pygtrie import StringTrie
1010

11+
from dvc.exceptions import DvcException
1112
from dvc.path_info import PathInfo
1213
from dvc.pathspec_math import PatternInfo, merge_patterns
1314
from dvc.system import System
@@ -16,6 +17,10 @@
1617
logger = logging.getLogger(__name__)
1718

1819

20+
class OutOfWorkingSpaceError(DvcException):
21+
"""Thrown when unable to acquire the lock for DVC repo."""
22+
23+
1924
class DvcIgnore:
2025
DVCIGNORE_FILE = ".dvcignore"
2126

@@ -85,7 +90,9 @@ def _get_normalize_path(self, dirname, basename):
8590
# NOTE: `os.path.join` is ~x5.5 slower
8691
path = f"{rel}{os.sep}{basename}"
8792
else:
88-
return False
93+
raise OutOfWorkingSpaceError(
94+
f"`{dirname}` is out side of `{self.dirname}`"
95+
)
8996

9097
if not System.is_unix():
9198
path = normalize_file(path)
@@ -116,16 +123,13 @@ def _ignore_details(self, path, is_dir):
116123
result = []
117124
for ignore, pattern in zip(self.regex_pattern_list, self.pattern_list):
118125
regex = re.compile(ignore[0])
119-
# skip system pattern
120-
if not pattern.file_info:
121-
continue
122-
if is_dir:
123-
path_dir = f"{path}/"
124-
if regex.match(path) or regex.match(path_dir):
125-
result.append(pattern.file_info)
126-
else:
127-
if regex.match(path):
128-
result.append(pattern.file_info)
126+
if regex.match(path) or (is_dir and regex.match(f"{path}/")):
127+
if not pattern.file_info:
128+
raise OutOfWorkingSpaceError(
129+
f"`{path}` is not in work space."
130+
)
131+
result.append(pattern.file_info)
132+
129133
return result
130134

131135
def __hash__(self):
@@ -233,10 +237,10 @@ def _update_sub_repo(self, root, dirs):
233237
self.ignores_trie_tree[root] = new_pattern
234238

235239
def __call__(self, root, dirs, files):
236-
ignore_pattern = self._get_trie_pattern(root)
237-
if ignore_pattern:
240+
try:
241+
ignore_pattern = self._get_trie_pattern(root)
238242
return ignore_pattern(root, dirs, files)
239-
else:
243+
except OutOfWorkingSpaceError:
240244
return dirs, files
241245

242246
def _get_trie_pattern(self, dirname):
@@ -246,8 +250,9 @@ def _get_trie_pattern(self, dirname):
246250

247251
prefix = self.ignores_trie_tree.longest_prefix(dirname).key
248252
if not prefix:
249-
# outside of the repo
250-
return None
253+
raise OutOfWorkingSpaceError(
254+
f"`{dirname}` is out side of `{self.root_dir}`"
255+
)
251256

252257
dirs = list(
253258
takewhile(
@@ -264,14 +269,13 @@ def _get_trie_pattern(self, dirname):
264269
return self.ignores_trie_tree.get(dirname)
265270

266271
def _is_ignored(self, path, is_dir=False):
267-
if self._outside_repo(path):
268-
return True
269-
dirname, basename = os.path.split(os.path.normpath(path))
270-
ignore_pattern = self._get_trie_pattern(dirname)
271-
if ignore_pattern:
272+
try:
273+
self._outside_repo(path)
274+
dirname, basename = os.path.split(os.path.normpath(path))
275+
ignore_pattern = self._get_trie_pattern(dirname)
272276
return ignore_pattern.matches(dirname, basename, is_dir)
273-
else:
274-
return False
277+
except OutOfWorkingSpaceError:
278+
return True
275279

276280
def is_ignored_dir(self, path):
277281
path = os.path.abspath(path)
@@ -294,28 +298,29 @@ def _outside_repo(self, path):
294298
[os.path.abspath(path), self.root_dir]
295299
)
296300
):
297-
return True
298-
return False
301+
raise OutOfWorkingSpaceError(f"{path} is out of {self.root_dir}")
299302

300303
def check_ignore(self, targets):
301304
check_results = []
302305
for target in targets:
303306
full_target = os.path.abspath(target)
304-
if not self._outside_repo(full_target):
307+
try:
308+
self._outside_repo(full_target)
305309
dirname, basename = os.path.split(
306310
os.path.normpath(full_target)
307311
)
308312
pattern = self._get_trie_pattern(dirname)
309-
if pattern:
310-
matches = pattern.match_details(
311-
dirname, basename, os.path.isdir(full_target)
312-
)
313+
matches = pattern.match_details(
314+
dirname, basename, os.path.isdir(full_target)
315+
)
313316

314-
if matches:
315-
check_results.append(
316-
CheckIgnoreResult(target, True, matches)
317-
)
318-
continue
317+
if matches:
318+
check_results.append(
319+
CheckIgnoreResult(target, True, matches)
320+
)
321+
continue
322+
except OutOfWorkingSpaceError:
323+
pass
319324
check_results.append(CheckIgnoreResult(target, False, ["::"]))
320325

321326
return check_results

tests/func/test_check_ignore.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ def test_check_ignore_dir(tmp_dir, dvc, path, ret):
7070

7171

7272
def test_check_ignore_default_dir(tmp_dir, dvc):
73-
tmp_dir.gen(DvcIgnore.DVCIGNORE_FILE, "dir/")
7473
assert main(["check-ignore", "-q", ".dvc"]) == 1
7574

7675

0 commit comments

Comments
 (0)