diff: hanle repo with no commits (#3784)

`git diff` doesn't throw errors, so we could do the same.

Fixes #3398
This commit is contained in:
Ruslan Kuprieiev
2020-05-14 02:14:37 +03:00
committed by GitHub
parent 833e82a7ea
commit 597ab98d0e
9 changed files with 63 additions and 2 deletions
+3
View File
@@ -52,6 +52,9 @@ def diff(self, a_rev="HEAD", b_rev=None):
if _exists(output)
}
if self.scm.no_commits:
return {}
working_tree = self.tree
a_tree = self.scm.get_tree(a_rev)
b_tree = self.scm.get_tree(b_rev) if b_rev else working_tree
+3
View File
@@ -14,6 +14,9 @@ def _get_metrics(repo, *args, rev=None, **kwargs):
def diff(repo, *args, a_rev=None, b_rev=None, **kwargs):
if repo.scm.no_commits:
return {}
with_unchanged = kwargs.pop("all", False)
old = _get_metrics(repo, *args, **kwargs, rev=(a_rev or "HEAD"))
+3
View File
@@ -13,6 +13,9 @@ def _get_params(repo, *args, rev=None, **kwargs):
def diff(repo, *args, a_rev=None, b_rev=None, **kwargs):
if repo.scm.no_commits:
return {}
with_unchanged = kwargs.pop("all", False)
old = _get_params(repo, *args, **kwargs, rev=(a_rev or "HEAD"))
+4
View File
@@ -439,3 +439,7 @@ class Git(Base):
self._verify_hook("post-checkout")
self._verify_hook("pre-commit")
self._verify_hook("pre-push")
@property
def no_commits(self):
return not self.list_all_commits()
+3 -2
View File
@@ -62,6 +62,7 @@ __all__ = [
"erepo_dir",
"git_dir",
"setup_remote",
"git_init",
]
@@ -91,7 +92,7 @@ class TmpDir(pathlib.Path):
str_path = os.fspath(self)
if scm:
_git_init(str_path)
git_init(str_path)
if dvc:
self.dvc = Repo.init(
str_path, no_scm=not scm and not hasattr(self, "scm")
@@ -238,7 +239,7 @@ def dvc(tmp_dir):
return tmp_dir.dvc
def _git_init(path):
def git_init(path):
from git import Repo
from git.exc import GitCommandNotFound
+11
View File
@@ -134,3 +134,14 @@ def test_metrics_diff_with_unchanged(tmp_dir, scm, dvc):
"xyz": {"old": 10, "new": 10, "diff": 0},
}
}
def test_no_commits(tmp_dir):
from dvc.repo import Repo
from dvc.scm.git import Git
from tests.dir_helpers import git_init
git_init(".")
assert Git().no_commits
assert Repo.init().metrics.diff() == {}
+11
View File
@@ -119,3 +119,14 @@ def test_pipeline_tracked_params(tmp_dir, scm, dvc, run_copy):
assert dvc.params.diff(a_rev="HEAD~2") == {
"params.yaml": {"foo": {"old": "bar", "new": "qux"}}
}
def test_no_commits(tmp_dir):
from dvc.repo import Repo
from dvc.scm.git import Git
from tests.dir_helpers import git_init
git_init(".")
assert Git().no_commits
assert Repo.init().params.diff() == {}
+11
View File
@@ -213,3 +213,14 @@ def test_diff_dirty(tmp_dir, scm, dvc):
def test_no_changes(tmp_dir, scm, dvc):
tmp_dir.dvc_gen("file", "first", commit="add a file")
assert dvc.diff() == {}
def test_no_commits(tmp_dir):
from dvc.repo import Repo
from dvc.scm.git import Git
from tests.dir_helpers import git_init
git_init(".")
assert Git().no_commits
assert Repo.init().diff() == {}
+14
View File
@@ -69,3 +69,17 @@ def test_is_tracked_unicode(tmp_dir, scm):
tmp_dir.gen("ṳṋṭṝḁḉḵḗḋ", "untracked")
assert scm.is_tracked("ṭṝḁḉḵḗḋ")
assert not scm.is_tracked("ṳṋṭṝḁḉḵḗḋ")
def test_no_commits(tmp_dir):
from tests.dir_helpers import git_init
from dvc.scm.git import Git
git_init(".")
assert Git().no_commits
tmp_dir.gen("foo", "foo")
Git().add(["foo"])
Git().commit("foo")
assert not Git().no_commits