Skip to content

Commit

Permalink
Added validation of path move in GitFilesystemEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
altvod committed Nov 22, 2023
1 parent 9b3fab9 commit 57e342f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions terrarium/dl_repmanager/dl_repmanager/fs_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)

import attr
from git import Repo as GitRepo


@attr.s(frozen=True)
Expand Down Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions terrarium/dl_repmanager/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 57e342f

Please sign in to comment.