scm: map internal SCMError to DVC's exception type

This commit is contained in:
Saugat Pachhai (सौगात)
2021-11-23 19:21:51 +02:00
committed by Ruslan Kuprieiev
parent f76ef84c88
commit d95dd27773
5 changed files with 31 additions and 10 deletions
+2
View File
@@ -16,12 +16,14 @@ from dvc.exceptions import (
PathMissingError,
)
from dvc.repo import Repo
from dvc.scm import map_scm_exception
from dvc.utils import relpath
logger = logging.getLogger(__name__)
@contextmanager
@map_scm_exception()
def external_repo(
url, rev=None, for_write=False, cache_dir=None, cache_types=None, **kwargs
):
+4 -1
View File
@@ -286,7 +286,10 @@ class Repo:
assert self.scm
if isinstance(self.fs, LocalFileSystem):
return self.scm.get_rev()
from dvc.scm import map_scm_exception
with map_scm_exception():
return self.scm.get_rev()
return self.fs.rev
@cached_property
+3 -1
View File
@@ -42,7 +42,9 @@ def scm_locked(f):
# different sequences of git operations at once
@wraps(f)
def wrapper(exp, *args, **kwargs):
with exp.scm_lock:
from dvc.scm import map_scm_exception
with map_scm_exception(), exp.scm_lock:
return f(exp, *args, **kwargs)
return wrapper
+17 -6
View File
@@ -1,4 +1,6 @@
"""Manages source control systems (e.g. Git)."""
from contextlib import contextmanager
from typing import Iterator
from dvc.scm.base import Base, NoSCMError
from dvc.scm.git import Git
@@ -11,6 +13,20 @@ class NoSCM(Base):
raise NoSCMError
@contextmanager
def map_scm_exception(with_cause: bool = False) -> Iterator[None]:
from dvc.scm.base import SCMError
from dvc.scm.exceptions import SCMError as InternalSCMError
try:
yield
except InternalSCMError as exc:
into = SCMError(str(exc))
if with_cause:
raise into from exc
raise into
def SCM(
root_dir, search_parent_directories=True, no_scm=False
): # pylint: disable=invalid-name
@@ -26,17 +42,12 @@ def SCM(
Returns:
dvc.scm.base.Base: SCM instance.
"""
from dvc.scm.base import SCMError
from dvc.scm.exceptions import SCMError as InternalSCMError
try:
with map_scm_exception():
if no_scm:
return NoSCM(root_dir)
return Git(
root_dir, search_parent_directories=search_parent_directories
)
except InternalSCMError as exc:
raise SCMError(str(exc))
def clone(url: str, to_path: str, **kwargs):
+5 -2
View File
@@ -12,11 +12,14 @@ from funcy import cached_property, first
from pathspec.patterns import GitWildMatchPattern
from dvc.scm.base import Base
from dvc.scm.exceptions import FileNotInRepoError, RevError
from dvc.scm.exceptions import (
FileNotInRepoError,
GitHookAlreadyExists,
RevError,
)
from dvc.utils import relpath
from dvc.utils.fs import path_isin
from ..exceptions import GitHookAlreadyExists
from .backend.base import BaseGitBackend, NoGitBackendError
from .backend.dulwich import DulwichBackend
from .backend.gitpython import GitPythonBackend