Skip to content

Commit

Permalink
repo: brancher: reset parsed fields
Browse files Browse the repository at this point in the history
Related iterative#9754 and fixes `dvc fetch`.
  • Loading branch information
efiop committed Jul 25, 2023
1 parent 65ba9ba commit 6589e1d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
10 changes: 8 additions & 2 deletions dvc/repo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ def __init__( # noqa: PLR0915
scm: Optional[Union["Git", "NoSCM"]] = None,
):
from dvc.cachemgr import CacheManager
from dvc.config import Config
from dvc.data_cloud import DataCloud
from dvc.fs import GitFileSystem, LocalFileSystem, localfs
from dvc.lock import LockNoop, make_lock
Expand All @@ -163,6 +162,7 @@ def __init__( # noqa: PLR0915
self._fs_conf = {"repo_factory": repo_factory}
self._fs = fs or localfs
self._scm = scm
self._config = config
self._data_index = None

if rev and not fs:
Expand All @@ -182,7 +182,6 @@ def __init__( # noqa: PLR0915
scm=scm,
)

self.config: Config = Config(self.dvc_dir, fs=self.fs, config=config)
self._uninitialized = uninitialized

# used by DVCFileSystem to determine if it should traverse subrepos
Expand Down Expand Up @@ -234,6 +233,12 @@ def __init__( # noqa: PLR0915
def __str__(self):
return self.url or self.root_dir

@cached_property
def config(self):
from dvc.config import Config

return Config(self.dvc_dir, fs=self.fs, config=self._config)

@cached_property
def local_dvc_dir(self):
from dvc.fs import GitFileSystem, LocalFileSystem
Expand Down Expand Up @@ -629,6 +634,7 @@ def _reset(self):
self.__dict__.pop("dvcignore", None)
self.__dict__.pop("dvcfs", None)
self.__dict__.pop("datafs", None)
self.__dict__.pop("config", None)

def __enter__(self):
return self
Expand Down
8 changes: 8 additions & 0 deletions dvc/repo/brancher.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def brancher( # noqa: E302

saved_fs = self.fs
saved_root = self.root_dir
saved_dvc_dir = self.dvc_dir

scm = self.scm

Expand Down Expand Up @@ -101,6 +102,8 @@ def brancher( # noqa: E302
finally:
self.fs = saved_fs
self.root_dir = saved_root
self.dvc_dir = saved_dvc_dir
self._reset() # pylint: disable=protected-access


def _switch_fs(
Expand All @@ -125,6 +128,8 @@ def _switch_fs(

repo.fs = fs
repo.root_dir = root_dir
repo.dvc_dir = fs.path.join(root_dir, repo.DVC_DIR)
repo._reset() # pylint: disable=protected-access

if cwd_parts:
cwd = repo.fs.path.join("/", *cwd_parts)
Expand All @@ -149,9 +154,12 @@ def switch(repo: "Repo", rev: str) -> Iterator[str]:

saved_fs = repo.fs
saved_root = repo.root_dir
saved_dvc_dir = repo.dvc_dir
try:
_switch_fs(repo, rev, repo_root_parts, cwd_parts)
yield rev
finally:
repo.fs = saved_fs
repo.root_dir = saved_root
repo.dvc_dir = saved_dvc_dir
repo._reset() # pylint: disable=protected-access
3 changes: 2 additions & 1 deletion tests/func/test_data_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ def test_verify_hashes(tmp_dir, scm, dvc, mocker, tmp_path_factory, local_remote
# Removing cache will invalidate existing state entries
dvc.cache.local.clear()

dvc.config["remote"]["upstream"]["verify"] = True
with dvc.config.edit() as conf:
conf["remote"]["upstream"]["verify"] = True

dvc.pull()
assert hash_spy.call_count == 10
Expand Down
42 changes: 11 additions & 31 deletions tests/func/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,7 @@ def test_gc_cloud_positive(tmp_dir, dvc, tmp_path_factory, local_remote):
assert main(["gc", "-vf", flag]) == 0


def test_gc_cloud_remove_order(tmp_dir, scm, dvc, tmp_path_factory, mocker):
storage = os.fspath(tmp_path_factory.mktemp("test_remote_base"))
dvc.config["remote"]["local_remote"] = {"url": storage}
dvc.config["core"]["remote"] = "local_remote"

def test_gc_cloud_remove_order(tmp_dir, scm, dvc, mocker, local_remote):
(standalone, dir1, dir2) = tmp_dir.dvc_gen(
{
"file1": "standalone",
Expand Down Expand Up @@ -321,11 +317,7 @@ def test_date(tmp_dir, scm, dvc):
) # "modified, again"


def test_gc_not_in_remote(tmp_dir, scm, dvc, tmp_path_factory, mocker):
storage = os.fspath(tmp_path_factory.mktemp("test_remote_base"))
dvc.config["remote"]["local_remote"] = {"url": storage}
dvc.config["core"]["remote"] = "local_remote"

def test_gc_not_in_remote(tmp_dir, scm, dvc, mocker, local_remote):
(standalone, dir1, dir2) = tmp_dir.dvc_gen(
{
"file1": "standalone",
Expand Down Expand Up @@ -358,12 +350,9 @@ def test_gc_not_in_remote(tmp_dir, scm, dvc, tmp_path_factory, mocker):
)


def test_gc_not_in_remote_remote_arg(tmp_dir, scm, dvc, tmp_path_factory, mocker):
storage = os.fspath(tmp_path_factory.mktemp("test_remote_base"))
dvc.config["remote"]["local_remote"] = {"url": storage}
dvc.config["core"]["remote"] = "local_remote"
other_storage = os.fspath(tmp_path_factory.mktemp("test_remote_other"))
dvc.config["remote"]["other_remote"] = {"url": other_storage}
def test_gc_not_in_remote_remote_arg(tmp_dir, scm, dvc, mocker, make_remote):
make_remote("local_remote", typ="local")
make_remote("other_remote", typ="local", default=False)

tmp_dir.dvc_gen(
{
Expand All @@ -385,15 +374,9 @@ def test_gc_not_in_remote_remote_arg(tmp_dir, scm, dvc, tmp_path_factory, mocker
assert len(mocked_remove.mock_calls) == 3


def test_gc_not_in_remote_with_remote_field(
tmp_dir, scm, dvc, tmp_path_factory, mocker
):
storage = os.fspath(tmp_path_factory.mktemp("test_remote_base"))
dvc.config["remote"]["local_remote"] = {"url": storage}
dvc.config["core"]["remote"] = "local_remote"

other_storage = os.fspath(tmp_path_factory.mktemp("test_remote_other"))
dvc.config["remote"]["other_remote"] = {"url": other_storage}
def test_gc_not_in_remote_with_remote_field(tmp_dir, scm, dvc, mocker, make_remote):
make_remote("local_remote", typ="local")
make_remote("other_remote", typ="local", default=False)

text = textwrap.dedent(
"""\
Expand All @@ -420,12 +403,9 @@ def test_gc_not_in_remote_cloud(tmp_dir, scm, dvc):
dvc.gc(workspace=True, not_in_remote=True, cloud=True)


def test_gc_cloud_remote_field(tmp_dir, scm, dvc, tmp_path_factory, mocker):
storage = os.fspath(tmp_path_factory.mktemp("test_remote_base"))
dvc.config["remote"]["local_remote"] = {"url": storage}
dvc.config["core"]["remote"] = "local_remote"
other_storage = os.fspath(tmp_path_factory.mktemp("test_remote_other"))
dvc.config["remote"]["other_remote"] = {"url": other_storage}
def test_gc_cloud_remote_field(tmp_dir, scm, dvc, mocker, make_remote):
make_remote("local_remote", typ="local")
make_remote("other_remote", typ="local", default=False)

text = textwrap.dedent(
"""\
Expand Down

0 comments on commit 6589e1d

Please sign in to comment.