diff --git a/dvc/command/base.py b/dvc/command/base.py index 67f9ac57b..ae41e9be5 100644 --- a/dvc/command/base.py +++ b/dvc/command/base.py @@ -29,13 +29,15 @@ def append_doc_link(help_message, path): class CmdBase(ABC): + UNINITIALIZED = False + def __init__(self, args): from dvc.repo import Repo from dvc.updater import Updater os.chdir(args.cd) - self.repo = Repo() + self.repo = Repo(uninitialized=self.UNINITIALIZED) self.config = self.repo.config self.args = args hardlink_lock = self.config["core"].get("hardlink_lock", False) diff --git a/dvc/command/metrics.py b/dvc/command/metrics.py index 52eb364e5..55e4fcb57 100644 --- a/dvc/command/metrics.py +++ b/dvc/command/metrics.py @@ -38,7 +38,11 @@ def show_metrics( raise BadMetricError(missing) -class CmdMetricsShow(CmdBase): +class CmdMetricsBase(CmdBase): + UNINITIALIZED = True + + +class CmdMetricsShow(CmdMetricsBase): def run(self): try: metrics = self.repo.metrics.show( @@ -104,7 +108,7 @@ def _show_diff(diff, markdown=False, no_path=False, old=False, precision=None): return table(header, rows, markdown) -class CmdMetricsDiff(CmdBase): +class CmdMetricsDiff(CmdMetricsBase): def run(self): try: diff = self.repo.metrics.diff( diff --git a/dvc/command/plots.py b/dvc/command/plots.py index 2fa779669..aa1493ff5 100644 --- a/dvc/command/plots.py +++ b/dvc/command/plots.py @@ -31,6 +31,8 @@ DIV_HTML = """
class CmdPlots(CmdBase): + UNINITILIZED = True + def _func(self, *args, **kwargs): raise NotImplementedError diff --git a/dvc/repo/__init__.py b/dvc/repo/__init__.py index 2822358f3..0bec31f21 100644 --- a/dvc/repo/__init__.py +++ b/dvc/repo/__init__.py @@ -80,7 +80,14 @@ class Repo: from dvc.repo.status import status from dvc.repo.update import update - def __init__(self, root_dir=None, scm=None, rev=None, subrepos=False): + def __init__( + self, + root_dir=None, + scm=None, + rev=None, + subrepos=False, + uninitialized=False, + ): from dvc.cache import Cache from dvc.data_cloud import DataCloud from dvc.lock import make_lock @@ -103,7 +110,12 @@ class Repo: ) self.state = StateNoop() else: - root_dir = self.find_root(root_dir) + try: + root_dir = self.find_root(root_dir) + except NotDvcRepoError: + if not uninitialized: + raise + root_dir = SCM(root_dir or os.curdir).root_dir self.root_dir = os.path.abspath(os.path.realpath(root_dir)) self.tree = LocalTree( self, diff --git a/tests/func/metrics/test_show.py b/tests/func/metrics/test_show.py index bc93f4aef..ae9a42bd4 100644 --- a/tests/func/metrics/test_show.py +++ b/tests/func/metrics/test_show.py @@ -112,19 +112,31 @@ def test_missing_cache(tmp_dir, dvc, run_copy_metrics): assert dvc.metrics.show() == {"": {"metrics.yaml": 1.1}} -def test_show_non_metric(tmp_dir, dvc): +@pytest.mark.parametrize("use_dvc", [True, False]) +def test_show_non_metric(tmp_dir, scm, use_dvc): tmp_dir.gen("metrics.yaml", "foo: 1.1") + if use_dvc: + dvc = Repo.init() + else: + dvc = Repo(uninitialized=True) + assert dvc.metrics.show(targets=["metrics.yaml"]) == { "": {"metrics.yaml": {"foo": 1.1}} } -def test_show_non_metric_branch(tmp_dir, scm, dvc): +@pytest.mark.parametrize("use_dvc", [True, False]) +def test_show_non_metric_branch(tmp_dir, scm, use_dvc): tmp_dir.scm_gen("metrics.yaml", "foo: 1.1", commit="init") with tmp_dir.branch("branch", new=True): tmp_dir.scm_gen("metrics.yaml", "foo: 2.2", commit="other") + if use_dvc: + dvc = Repo.init() + else: + dvc = Repo(uninitialized=True) + assert dvc.metrics.show(targets=["metrics.yaml"], revs=["branch"]) == { "workspace": {"metrics.yaml": {"foo": 1.1}}, "branch": {"metrics.yaml": {"foo": 2.2}}, diff --git a/tests/func/plots/test_plots.py b/tests/func/plots/test_plots.py index 27c5d07d3..778bb171d 100644 --- a/tests/func/plots/test_plots.py +++ b/tests/func/plots/test_plots.py @@ -8,6 +8,7 @@ from collections import OrderedDict import pytest from funcy import first +from dvc.repo import Repo from dvc.repo.plots.data import ( JSONPlotData, NoMetricInHistoryError, @@ -604,12 +605,19 @@ def test_multiple_plots(tmp_dir, scm, dvc, run_copy_metrics): assert len(dvc.plots.show().keys()) == 2 -def test_show_non_plot(tmp_dir, scm, dvc): +@pytest.mark.parametrize("use_dvc", [True, False]) +def test_show_non_plot(tmp_dir, scm, use_dvc): metric = [ {"first_val": 100, "val": 2}, {"first_val": 200, "val": 3}, ] _write_json(tmp_dir, metric, "metric.json") + + if use_dvc: + dvc = Repo.init() + else: + dvc = Repo(uninitialized=True) + plot_string = dvc.plots.show(targets=["metric.json"])["metric.json"] plot_content = json.loads(plot_string)