diff --git a/dvc/repo/diff.py b/dvc/repo/diff.py index d18389e05c..5a83ed8008 100644 --- a/dvc/repo/diff.py +++ b/dvc/repo/diff.py @@ -1,8 +1,6 @@ import os -from dvc.exceptions import DvcException from dvc.repo import locked -from dvc.scm.git import Git from dvc.scm.tree import is_working_tree @@ -15,8 +13,6 @@ def diff(self, a_rev="HEAD", b_rev=None): the concept of `index`, but it keeps the same interface, thus, `dvc diff` would be the same as `dvc diff HEAD`. """ - if type(self.scm) is not Git: - raise DvcException("only supported for Git repositories") def _paths_checksums(): """ diff --git a/dvc/scm/__init__.py b/dvc/scm/__init__.py index dbf524cc76..9a0b0f4a04 100644 --- a/dvc/scm/__init__.py +++ b/dvc/scm/__init__.py @@ -1,13 +1,14 @@ """Manages source control systems (e.g. Git).""" -from dvc.scm.base import Base +from dvc.scm.base import Base, NoSCMError from dvc.scm.git import Git # Syntactic sugar to signal that this is an actual implementation for a DVC # project under no SCM control. class NoSCM(Base): - pass + def __getattr__(self, name): + raise NoSCMError def SCM( diff --git a/dvc/scm/base.py b/dvc/scm/base.py index 017263de12..e1bedc242a 100644 --- a/dvc/scm/base.py +++ b/dvc/scm/base.py @@ -24,6 +24,16 @@ class RevError(SCMError): pass +class NoSCMError(SCMError): + def __init__(self): + msg = ( + "Only supported for Git repositories. If you're " + "seeing this error in a Git repo, try updating the DVC " + "configuration with `dvc config core.no_scm false`." + ) + super().__init__(msg) + + class Base(object): """Base class for source control management driver implementations.""" diff --git a/tests/func/test_diff.py b/tests/func/test_diff.py index 4c3a660ddf..96776d6fe2 100644 --- a/tests/func/test_diff.py +++ b/tests/func/test_diff.py @@ -14,9 +14,11 @@ def digest(text): def test_no_scm(tmp_dir, dvc): + from dvc.scm.base import NoSCMError + tmp_dir.dvc_gen("file", "text") - with pytest.raises(DvcException, match=r"only supported for Git repos"): + with pytest.raises(NoSCMError): dvc.diff() diff --git a/tests/func/test_metrics.py b/tests/func/test_metrics.py index fab19c86d6..f50972fe23 100644 --- a/tests/func/test_metrics.py +++ b/tests/func/test_metrics.py @@ -12,6 +12,7 @@ from dvc.repo import Repo as DvcRepo from dvc.repo.metrics.show import NO_METRICS_FILE_AT_REFERENCE_WARNING from dvc.utils import relpath +from dvc.scm.base import NoSCMError from tests.basic_env import TestDvcGit @@ -1005,3 +1006,13 @@ def test_metrics_diff_deleted_metric(tmp_dir, scm, dvc): "a.b.e": {"old": "3", "new": None}, } } + + +def test_metrics_without_scm(tmp_dir, dvc): + metrics = {"acc": 0.97, "recall": 0.95} + metrics_name = "metrics.json" + tmp_dir.gen({metrics_name: json.dumps(metrics)}) + dvc.add(metrics_name) + dvc.metrics.add(metrics_name) + with pytest.raises(NoSCMError): + dvc.metrics.diff()