Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dvc/repo/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def _exists(output):
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
Expand Down
3 changes: 3 additions & 0 deletions dvc/repo/metrics/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
3 changes: 3 additions & 0 deletions dvc/repo/params/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
4 changes: 4 additions & 0 deletions dvc/scm/git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,7 @@ def _verify_dvc_hooks(self):
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()
5 changes: 3 additions & 2 deletions tests/dir_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"erepo_dir",
"git_dir",
"setup_remote",
"git_init",
]


Expand Down Expand Up @@ -96,7 +97,7 @@ def init(self, *, scm=False, dvc=False):
str_path = 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")
Expand Down Expand Up @@ -243,7 +244,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

Expand Down
11 changes: 11 additions & 0 deletions tests/func/metrics/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 11 additions & 0 deletions tests/func/params/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 11 additions & 0 deletions tests/func/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,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 changes: 14 additions & 0 deletions tests/unit/scm/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,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