diff --git a/dvc/repo/metrics/show.py b/dvc/repo/metrics/show.py index 375d7a38b..01df0e86d 100644 --- a/dvc/repo/metrics/show.py +++ b/dvc/repo/metrics/show.py @@ -293,6 +293,15 @@ def show( if not res and not any(targets): raise NoMetricsError() + # Hide working tree metrics if they are the same as in the active branch + try: + active_branch = repo.scm.active_branch() + except TypeError: + pass # Detached head + else: + if res.get("working tree") == res.get(active_branch): + res.pop("working tree", None) + missing = set(targets) - found if missing: diff --git a/tests/func/test_metrics.py b/tests/func/test_metrics.py index dcd0a477c..ee5e89516 100644 --- a/tests/func/test_metrics.py +++ b/tests/func/test_metrics.py @@ -82,6 +82,25 @@ class TestMetricsBase(TestDvcGit): self.dvc.scm.checkout("master") +def test_show_dirty(tmp_dir, scm, dvc): + tmp_dir.gen("metric", "master") + dvc.run(metrics_no_cache=["metric"], overwrite=True) + tmp_dir.scm_add(["metric", "metric.dvc"], commit="add metric") + + tmp_dir.gen("metric", "dirty") + + assert dvc.metrics.show(["metric"]) == {"": {"metric": "dirty"}} + + assert dvc.metrics.show(["metric"], all_branches=True) == { + "working tree": {"metric": "dirty"}, + "master": {"metric": "master"}, + } + + assert dvc.metrics.show(["metric"], all_tags=True) == { + "working tree": {"metric": "dirty"} + } + + class TestMetrics(TestMetricsBase): def test_show(self): ret = self.dvc.metrics.show(["metric"], all_branches=True) @@ -707,7 +726,6 @@ class TestCachedMetrics(TestDvcGit): "master": {"metrics.json": ["master"]}, "one": {"metrics.json": ["one"]}, "two": {"metrics.json": ["two"]}, - "working tree": {"metrics.json": ["two"]}, }, ) @@ -721,7 +739,6 @@ class TestCachedMetrics(TestDvcGit): "master": {"metrics.json": ["master"]}, "one": {"metrics.json": ["one"]}, "two": {"metrics.json": ["two"]}, - "working tree": {"metrics.json": ["two"]}, }, )