From 57e342f398a0b5bd7fbecb25f8f9a8a2c28387fd Mon Sep 17 00:00:00 2001 From: Grigory Statsenko Date: Fri, 10 Nov 2023 12:56:28 +0100 Subject: [PATCH] Added validation of path move in GitFilesystemEditor --- .../dl_repmanager/dl_repmanager/fs_editor.py | 17 +++++++++++++++++ terrarium/dl_repmanager/pyproject.toml | 1 + 2 files changed, 18 insertions(+) diff --git a/terrarium/dl_repmanager/dl_repmanager/fs_editor.py b/terrarium/dl_repmanager/dl_repmanager/fs_editor.py index 67407c206..3d530dbec 100644 --- a/terrarium/dl_repmanager/dl_repmanager/fs_editor.py +++ b/terrarium/dl_repmanager/dl_repmanager/fs_editor.py @@ -17,6 +17,7 @@ ) import attr +from git import Repo as GitRepo @attr.s(frozen=True) @@ -234,7 +235,23 @@ def _path_exists(self, path: Path) -> bool: class GitFilesystemEditor(DefaultFilesystemEditor): """An FS editor that buses git to move files and directories""" + def _validate_paths_in_same_repo(self, *paths: Path) -> None: + root_paths = {GitRepo(path, search_parent_directories=True).working_tree_dir for path in paths} + if len(root_paths): + raise RuntimeError( + "Cross-repository operations are not supported for GitFilesystemEditor. Use `--fs-editor default`" + ) + + def _find_existing_parent(self, path: Path) -> Path: + assert path.is_absolute() + while not path.exists(): + if path.parent == path: + break + path = path.parent + return path + def _move_path(self, old_path: Path, new_path: Path) -> None: + self._validate_paths_in_same_repo(old_path, self._find_existing_parent(new_path)) cwd = Path.cwd() rel_old_path = Path(os.path.relpath(old_path, cwd)) rel_new_path = Path(os.path.relpath(new_path, cwd)) diff --git a/terrarium/dl_repmanager/pyproject.toml b/terrarium/dl_repmanager/pyproject.toml index fbb137955..9d602b50d 100644 --- a/terrarium/dl_repmanager/pyproject.toml +++ b/terrarium/dl_repmanager/pyproject.toml @@ -11,6 +11,7 @@ readme = "README.md" [tool.poetry.dependencies] attrs = ">=22.2.0" frozendict = ">=2.3.8" +gitpython = ">=3.1.37" python = ">=3.10, <3.12" pyyaml = ">=6.0.1" tomlkit = "==0.11.8"