diff --git a/dvc/repo/metrics/diff.py b/dvc/repo/metrics/diff.py index 0f0a2550a..04ab6c945 100644 --- a/dvc/repo/metrics/diff.py +++ b/dvc/repo/metrics/diff.py @@ -12,6 +12,13 @@ def diff(repo, *args, a_rev=None, b_rev=None, **kwargs): b_rev = b_rev or "workspace" metrics = repo.metrics.show(*args, **kwargs, revs=[a_rev, b_rev]) + + # workspace may have been replaced by active branch + workspace_rev = (a_rev == "workspace") or (b_rev == "workspace") + if workspace_rev and "workspace" not in metrics: + active_branch = repo.scm.active_branch() + metrics["workspace"] = metrics[active_branch] + old = metrics.get(a_rev, {}).get("data", {}) new = metrics.get(b_rev, {}).get("data", {}) diff --git a/tests/func/metrics/test_diff.py b/tests/func/metrics/test_diff.py index 23adde515..1749b9476 100644 --- a/tests/func/metrics/test_diff.py +++ b/tests/func/metrics/test_diff.py @@ -235,3 +235,20 @@ def test_diff_top_level_metrics(tmp_dir, dvc, scm, dvcfile, metrics_file): "foo": {"diff": 2, "new": 5, "old": 3} } } + + +def test_metrics_diff_active_branch_unchanged( + tmp_dir, scm, dvc, run_copy_metrics +): + def _gen(val): + metrics = {"a": {"b": {"c": val, "d": 1, "e": str(val)}}} + (tmp_dir / "m_temp.yaml").dump(metrics) + run_copy_metrics( + "m_temp.yaml", "m.yaml", metrics=["m.yaml"], commit=str(val) + ) + + _gen(1) + _gen(2) + _gen(1) + + assert dvc.metrics.diff(a_rev=tmp_dir.scm.active_branch()) == {}