Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added validation of path move in GitFilesystemEditor #86

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading