Skip to content
Open
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
3 changes: 3 additions & 0 deletions newsfragments/263.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Allow repeated ``merge_tree(symlinks=True)`` calls to replace existing links,
files, and directories at a copied link's destination without following the
destination link.
4 changes: 4 additions & 0 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,10 @@ def ignored(item: Self) -> bool:
dest = dst_path / source.name
if symlinks and source.islink():
target = source.readlink()
if not dest.islink() and dest.is_dir():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we create the replacement link in the destination directory before removing the old entry? I reproduced this on macOS with an ACL that denies add_file while allowing deletion. When the source link collides with an existing file, this branch removes the file and then target.symlink(dest) raises PermissionError. The base revision raises FileExistsError and leaves the existing file intact. An injected symlink-creation failure also confirmed the same loss for existing directories and links. Staging the new link under a temporary sibling name would preserve the old entry when link creation is denied.

dest.rmtree()
else:
dest.remove_p()
target.symlink(dest)
elif source.is_dir():
source.merge_tree(
Expand Down
35 changes: 35 additions & 0 deletions tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,41 @@ def test_with_nonexisting_dst_args(self):
assert set(self.subdir_b.iterdir()) == expected
self.check_link()

@pytest.mark.parametrize('existing', ['link', 'dangling', 'file', 'directory'])
def test_replace_existing_link_destination(self, existing):
dest = self.subdir_b / self.test_link.name
old_target = self.test_dir / 'old-target'
old_target.mkdir()
sentinel = old_target / 'keep.txt'
sentinel.write_text('keep', encoding='utf-8')
if existing == 'link':
dest.symlink_to(old_target, target_is_directory=True)
elif existing == 'dangling':
(self.test_dir / 'missing').symlink(dest)
elif existing == 'file':
dest.write_text('old', encoding='utf-8')
else:
dest.mkdir()
(dest / 'old.txt').write_text('old', encoding='utf-8')

self.subdir_a.merge_tree(self.subdir_b, symlinks=True)

assert dest.islink()
assert dest.readlink() == self.test_file
assert sentinel.read_text(encoding='utf-8') == 'keep'

def test_repeated_merge_with_changed_link_target(self):
self.subdir_a.merge_tree(self.subdir_b, symlinks=True)
self.subdir_a.merge_tree(self.subdir_b, symlinks=True)
self.test_link.remove()
Path('relative-missing-target').symlink(self.test_link)

self.subdir_a.merge_tree(self.subdir_b, symlinks=True)

dest = self.subdir_b / self.test_link.name
assert dest.islink()
assert dest.readlink() == 'relative-missing-target'

def test_with_existing_dst(self):
self.subdir_b.rmtree()
self.subdir_a.copytree(self.subdir_b, True)
Expand Down