From 2cef130191b4f538e0d2e18446c53ffaada65c4f Mon Sep 17 00:00:00 2001 From: David de la Iglesia Castro Date: Thu, 25 Aug 2022 10:17:06 +0200 Subject: [PATCH] repo: Handle no commits for `exp show` and `plots diff`. Make it consistent with all the other commands that use `brancher` (i.e. `dvc diff`, `metrics diff`, `params diff`) Closes #8163 --- dvc/commands/experiments/diff.py | 2 +- dvc/repo/experiments/show.py | 3 +++ dvc/repo/plots/diff.py | 2 ++ tests/func/experiments/test_diff.py | 11 +++++++++++ tests/func/experiments/test_show.py | 12 ++++++++++++ tests/func/plots/test_diff.py | 14 ++++++++++++++ 6 files changed, 43 insertions(+), 1 deletion(-) diff --git a/dvc/commands/experiments/diff.py b/dvc/commands/experiments/diff.py index 9b750dff3..4956d34d1 100644 --- a/dvc/commands/experiments/diff.py +++ b/dvc/commands/experiments/diff.py @@ -27,7 +27,7 @@ class CmdExperimentsDiff(CmdBase): if self.args.json: ui.write_json(diff) - else: + elif diff: from dvc.compare import show_diff precision = self.args.precision or DEFAULT_PRECISION diff --git a/dvc/repo/experiments/show.py b/dvc/repo/experiments/show.py index 2141a4dbd..c831a9375 100644 --- a/dvc/repo/experiments/show.py +++ b/dvc/repo/experiments/show.py @@ -142,6 +142,9 @@ def show( fetch_running: bool = True, ): + if repo.scm.no_commits: + return {} + if onerror is None: onerror = onerror_collect diff --git a/dvc/repo/plots/diff.py b/dvc/repo/plots/diff.py index 7ffd18e86..d09312afa 100644 --- a/dvc/repo/plots/diff.py +++ b/dvc/repo/plots/diff.py @@ -12,6 +12,8 @@ def _revisions(repo, revs, experiment): def diff(repo, *args, revs=None, experiment=False, **kwargs): + if repo.scm.no_commits: + return {} return repo.plots.show( *args, revs=_revisions(repo, revs, experiment), **kwargs ) diff --git a/tests/func/experiments/test_diff.py b/tests/func/experiments/test_diff.py index e6ea0ea8e..581b1aa46 100644 --- a/tests/func/experiments/test_diff.py +++ b/tests/func/experiments/test_diff.py @@ -1,6 +1,17 @@ from funcy import first +def test_diff_no_commits(tmp_dir): + from scmrepo.git import Git + + from dvc.repo import Repo + + git = Git.init(tmp_dir.fs_path) + assert git.no_commits + + assert Repo.init().experiments.diff() == {} + + def test_diff_empty(tmp_dir, scm, dvc, exp_stage): assert dvc.experiments.diff() == {"params": {}, "metrics": {}} diff --git a/tests/func/experiments/test_show.py b/tests/func/experiments/test_show.py index c973a0016..bf85f8c6b 100644 --- a/tests/func/experiments/test_show.py +++ b/tests/func/experiments/test_show.py @@ -35,6 +35,18 @@ def make_executor_info(**kwargs): return ExecutorInfo(**kwargs) +@pytest.mark.vscode +def test_show_no_commits(tmp_dir): + from scmrepo.git import Git + + from dvc.repo import Repo + + git = Git.init(tmp_dir.fs_path) + assert git.no_commits + + assert Repo.init().experiments.show() == {} + + @pytest.mark.vscode def test_show_simple(tmp_dir, scm, dvc, exp_stage): assert dvc.experiments.show()["workspace"] == { diff --git a/tests/func/plots/test_diff.py b/tests/func/plots/test_diff.py index 2ce8c0e3b..56cc332bc 100644 --- a/tests/func/plots/test_diff.py +++ b/tests/func/plots/test_diff.py @@ -1,3 +1,5 @@ +import pytest + from tests.utils.plots import get_plot @@ -46,3 +48,15 @@ def test_diff_dirty(tmp_dir, scm, dvc, run_copy_metrics): assert get_plot( diff_result, "workspace", "definitions", file="", endkey="data" ) == {"metric.json": props} + + +@pytest.mark.vscode +def test_no_commits(tmp_dir): + from scmrepo.git import Git + + from dvc.repo import Repo + + git = Git.init(tmp_dir.fs_path) + assert git.no_commits + + assert Repo.init().plots.diff() == {}