diff --git a/dvc/repo/metrics/show.py b/dvc/repo/metrics/show.py index a2a12babcf..445496ff5e 100644 --- a/dvc/repo/metrics/show.py +++ b/dvc/repo/metrics/show.py @@ -298,6 +298,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 c92ad9b81c..1d065e0cc1 100644 --- a/tests/func/test_metrics.py +++ b/tests/func/test_metrics.py @@ -83,6 +83,25 @@ def setUp(self): 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) @@ -708,7 +727,6 @@ def _test_metrics(self, func): "master": {"metrics.json": ["master"]}, "one": {"metrics.json": ["one"]}, "two": {"metrics.json": ["two"]}, - "working tree": {"metrics.json": ["two"]}, }, ) @@ -722,7 +740,6 @@ def _test_metrics(self, func): "master": {"metrics.json": ["master"]}, "one": {"metrics.json": ["one"]}, "two": {"metrics.json": ["two"]}, - "working tree": {"metrics.json": ["two"]}, }, )