diff --git a/dvc/repo/__init__.py b/dvc/repo/__init__.py index 635f1085b..bd89fbd8e 100644 --- a/dvc/repo/__init__.py +++ b/dvc/repo/__init__.py @@ -126,6 +126,8 @@ class Repo: return root_dir, dvc_dir def _get_database_dir(self, db_name: str) -> Optional[str]: + from dvc.fs import localfs + # NOTE: by default, store SQLite-based remote indexes and state's # `links` and `md5s` caches in the repository itself to avoid any # possible state corruption in 'shared cache dir' scenario, but allow @@ -138,15 +140,21 @@ class Repo: import hashlib - root_dir_hash = hashlib.sha224(self.root_dir.encode("utf-8")).hexdigest() + if self.local_dvc_dir: + fs: "FileSystem" = localfs + local_root = fs.path.parent(self.local_dvc_dir) + else: + fs = self.fs + local_root = self.root_dir + root_dir_hash = hashlib.sha224(local_root.encode("utf-8")).hexdigest() - db_dir = self.fs.path.join( + db_dir = fs.path.join( base_db_dir, self.DVC_DIR, - f"{self.fs.path.name(self.root_dir)}-{root_dir_hash[0:7]}", + f"{fs.path.name(local_root)}-{root_dir_hash[0:7]}", ) - self.fs.makedirs(db_dir, exist_ok=True) + fs.makedirs(db_dir, exist_ok=True) return db_dir def __init__( # noqa: PLR0915 diff --git a/tests/unit/remote/test_remote.py b/tests/unit/remote/test_remote.py index 2820e1ba4..0847a779c 100644 --- a/tests/unit/remote/test_remote.py +++ b/tests/unit/remote/test_remote.py @@ -66,6 +66,8 @@ def test_remote_index_dir_config(make_tmp_dir, dvc): conf["remote"]["s3"] = {"url": "s3://bucket/name"} dvc.root_dir = "/usr/local/test_repo" + dvc.dvc_dir = "/usr/local/test_repo/.dvc" + dvc.__dict__.pop("local_dvc_dir") assert os.path.dirname( get_index(dvc.cloud.get_remote_odb(name="s3")).index_dir