diff --git a/dvc/command/diff.py b/dvc/command/diff.py index 9e43709b4e..e68c269acf 100644 --- a/dvc/command/diff.py +++ b/dvc/command/diff.py @@ -69,7 +69,7 @@ def run(self): print(self._format(diff)) except DvcException: - logger.exception("failed to get 'diff {}'") + logger.exception("failed to get diff") return 1 return 0 diff --git a/dvc/repo/diff.py b/dvc/repo/diff.py index a1ec334c03..89676f24b1 100644 --- a/dvc/repo/diff.py +++ b/dvc/repo/diff.py @@ -1,7 +1,9 @@ import collections import os +from dvc.exceptions import DvcException from dvc.repo import locked +from dvc.scm.git import Git Diffable = collections.namedtuple("Diffable", "filename, checksum") @@ -54,6 +56,9 @@ def diff(self, a_ref="HEAD", b_ref=None, *, target=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") + outs = {} for branch in self.brancher(revs=[a_ref, b_ref]): diff --git a/dvc/scm/base.py b/dvc/scm/base.py index 412731ca89..e35c7b7d7d 100644 --- a/dvc/scm/base.py +++ b/dvc/scm/base.py @@ -29,7 +29,7 @@ def __init__(self, url, path): class RevError(SCMError): def __init__(self, url, rev): super().__init__( - "Failed to access revision '{}' for repo '{}'".format(rev, url) + "failed to access revision '{}' for repo '{}'".format(rev, url) ) diff --git a/dvc/scm/git/__init__.py b/dvc/scm/git/__init__.py index 2d334a6493..8efb2dc5de 100644 --- a/dvc/scm/git/__init__.py +++ b/dvc/scm/git/__init__.py @@ -363,7 +363,12 @@ def get_rev(self): return self.repo.git.rev_parse("HEAD") def resolve_rev(self, rev): - return self.repo.git.rev_parse(rev) + from git.exc import GitCommandError + + try: + return self.repo.git.rev_parse(rev) + except GitCommandError as exc: + raise RevError(url=self.root_dir, rev=rev) from exc def close(self): self.repo.close() diff --git a/tests/func/test_diff.py b/tests/func/test_diff.py index f7bb34d086..cd58870003 100644 --- a/tests/func/test_diff.py +++ b/tests/func/test_diff.py @@ -6,6 +6,7 @@ import colorama from dvc.main import main +from dvc.exceptions import DvcException def digest(text): @@ -15,7 +16,8 @@ def digest(text): def test_no_scm(tmp_dir, dvc): tmp_dir.dvc_gen("file", "text") - pytest.skip("TODO: define behavior, should it fail?") + with pytest.raises(DvcException, match=r"only supported for Git repos"): + dvc.diff() def test_added(tmp_dir, scm, dvc): @@ -86,7 +88,10 @@ def test_refs(tmp_dir, scm, dvc): ], } - pytest.skip('TODO: test dvc.diff("missing")') + with pytest.raises( + DvcException, match=r"failed to access revision 'missing'" + ): + dvc.diff("missing") def test_target(tmp_dir, scm, dvc):