From 41169f9774d51325b9244b6c271ab69cd883e7a9 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Wed, 5 Aug 2020 13:52:01 +0300 Subject: [PATCH] config: use wtree for non-repo configs For example, when `dvc import`ing something, dvc won't see stuff declared in system and global configs, because GitTree doesn't see stuff that is outside of the git repo. Discord context: https://discordapp.com/channels/485586884165107732/563406153334128681/740543492417126521 --- dvc/config.py | 15 +++++++++++---- tests/unit/test_config.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/dvc/config.py b/dvc/config.py index 26181e4d67..6aa5908e59 100644 --- a/dvc/config.py +++ b/dvc/config.py @@ -324,11 +324,17 @@ def load(self, validate=True): if not self["cache"].get("dir") and self.dvc_dir: self["cache"]["dir"] = os.path.join(self.dvc_dir, "cache") + def _get_tree(self, level): + # NOTE: this might be a GitTree, which doesn't see things outside of + # the repo. + return self.tree if level == "repo" else self.wtree + def _load_config(self, level): filename = self.files[level] + tree = self._get_tree(level) - if self.tree.exists(filename, use_dvcignore=False): - with self.tree.open(filename) as fobj: + if tree.exists(filename, use_dvcignore=False): + with tree.open(filename) as fobj: conf_obj = configobj.ConfigObj(fobj) else: conf_obj = configobj.ConfigObj() @@ -336,13 +342,14 @@ def _load_config(self, level): def _save_config(self, level, conf_dict): filename = self.files[level] + tree = self._get_tree(level) logger.debug(f"Writing '{filename}'.") - self.tree.makedirs(os.path.dirname(filename)) + tree.makedirs(os.path.dirname(filename)) config = configobj.ConfigObj(_pack_remotes(conf_dict)) - with self.tree.open(filename, "wb") as fobj: + with tree.open(filename, "wb") as fobj: config.write(fobj) config.filename = filename diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index e5d970c275..8ddba35468 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -3,6 +3,7 @@ import pytest from dvc.config import Config +from dvc.tree.local import LocalTree @pytest.mark.parametrize( @@ -16,3 +17,19 @@ ) def test_to_relpath(path, expected): assert Config._to_relpath(os.path.join(".", "config"), path) == expected + + +def test_get_tree(tmp_dir, scm): + tmp_dir.scm_gen("foo", "foo", commit="add foo") + + tree = scm.get_tree("master") + config = Config(tree=tree) + + assert config.tree == tree + assert config.wtree != tree + assert isinstance(config.wtree, LocalTree) + + assert config._get_tree("repo") == tree + assert config._get_tree("local") == config.wtree + assert config._get_tree("global") == config.wtree + assert config._get_tree("system") == config.wtree