From 853d304ce2c18fb59a80f4eeb086d020d476a014 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Thu, 15 Apr 2021 23:16:01 +0300 Subject: [PATCH] repo: handle rev without scm (#5822) This part of logic used to be only used by external repo for cloned repos, but since we now handle local repos without cloning, we don't always pass scm instance. This PR automatically creates scm instance for such cases. Fixes #5590 --- dvc/repo/__init__.py | 4 ++++ tests/func/test_api.py | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/dvc/repo/__init__.py b/dvc/repo/__init__.py index 863979c69..61ed75150 100644 --- a/dvc/repo/__init__.py +++ b/dvc/repo/__init__.py @@ -141,6 +141,7 @@ class Repo: from dvc.repo.params import Params from dvc.repo.plots import Plots from dvc.repo.stage import StageLoad + from dvc.scm import SCM from dvc.stage.cache import StageCache from dvc.state import State, StateNoop @@ -149,6 +150,9 @@ class Repo: "repo_factory": repo_factory, } + if rev and not scm: + scm = SCM(root_dir or os.curdir) + self.root_dir, self.dvc_dir, self.tmp_dir = self._get_repo_dirs( root_dir=root_dir, scm=scm, rev=rev, uninitialized=uninitialized ) diff --git a/tests/func/test_api.py b/tests/func/test_api.py index 6050e1a39..79bc900ff 100644 --- a/tests/func/test_api.py +++ b/tests/func/test_api.py @@ -182,6 +182,15 @@ def test_open_not_cached(dvc): api.read(metric_file) +def test_open_rev(tmp_dir, scm, dvc): + tmp_dir.scm_gen("foo", "foo", commit="foo") + + (tmp_dir / "foo").write_text("bar") + + with api.open("foo", rev="master") as fobj: + assert fobj.read() == "foo" + + @pytest.mark.parametrize("as_external", [True, False]) @pytest.mark.parametrize("remote", [pytest.lazy_fixture("ssh")], indirect=True) @pytest.mark.parametrize(