Skip to content

Commit

Permalink
diff: handle exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr. Outis committed Jan 26, 2020
1 parent bd1adf2 commit fe4eba5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dvc/command/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions dvc/repo/diff.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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]):
Expand Down
2 changes: 1 addition & 1 deletion dvc/scm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)


Expand Down
7 changes: 6 additions & 1 deletion dvc/scm/git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 7 additions & 2 deletions tests/func/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import colorama

from dvc.main import main
from dvc.exceptions import DvcException


def digest(text):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit fe4eba5

Please sign in to comment.