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(