diff: handle exceptions

This commit is contained in:
Mr. Outis
2020-01-26 17:50:47 -06:00
parent bd1adf2c23
commit fe4eba5941
5 changed files with 20 additions and 5 deletions
+1 -1
View File
@@ -69,7 +69,7 @@ class CmdDiff(CmdBase):
print(self._format(diff))
except DvcException:
logger.exception("failed to get 'diff {}'")
logger.exception("failed to get diff")
return 1
return 0
+5
View File
@@ -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]):
+1 -1
View File
@@ -29,7 +29,7 @@ class CloneError(SCMError):
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)
)
+6 -1
View File
@@ -363,7 +363,12 @@ class Git(Base):
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()
+7 -2
View File
@@ -6,6 +6,7 @@ import pytest
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):